首页 新闻 会员 周边

datagridview中DataGridViewButtonColumn列的按钮获取位置问题

0
[已解决问题] 解决于 2014-01-09 10:17

datagridview中DataGridViewButtonColumn列的按钮

我想在点按钮后 弹出一个无边框的窗口.这个窗口位置 正好在 按钮的后面 .要对齐.

我现在实现不了对齐效果,请高手指点12.

simadi的主页 simadi | 初学一级 | 园豆:134
提问于:2014-01-06 14:40
< >
分享
最佳答案
0

用PointToScreen。

private void Button_Click(object sender, RoutedEventArgs e)
        {           
            Button button = sender as Button;
            Point p = button.PointToScreen(new Point(button.ActualWidth,0));
            Window w = new Window();
            w.WindowStyle= WindowStyle.None;
            w.AllowsTransparency = true;
            w.Background = new SolidColorBrush(Colors.White);
            w.Height = 30;
            w.Width = 100;
            w.Left = p.X;
            w.Top = p.Y;
            w.Show();
        }

亲测可行。可惜我不能上传图片看效果。

奖励园豆:5
德年 | 小虾三级 |园豆:810 | 2014-01-06 17:04

DataGridViewButtonCell  好像没 PointToScreen 这个属性?

simadi | 园豆:134 (初学一级) | 2014-01-06 17:12

@simadi: 

 Point p=new Point();
            p.X = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).X;
            p.Y=dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,false).Y;
            p.X = p.X + Column1.Width; //Column1是目标DataGridViewButtonCloumn
            
            Point screenPoint = dataGridView1.PointToScreen(p);
            Form form = new Form();
            form.FormBorderStyle = FormBorderStyle.None;
            form.BackColor = Color.White;
            form.Show();
            form.Location = screenPoint;

原先的是WPF的。。。这个才是Winform

德年 | 园豆:810 (小虾三级) | 2014-01-06 18:01

如果还是对不齐的话,可能是 你的无边框的窗体的问题,你测试下把无边框窗体的Location=new Point(0,0); 看下窗体是否在屏幕左上角。

德年 | 园豆:810 (小虾三级) | 2014-01-06 18:22

@德年: 搞定了!谢谢啊!!

simadi | 园豆:134 (初学一级) | 2014-01-09 10:16
其他回答(1)
0

Rectangle rect = this.DataGridView.GetCellDisplayRectangle(e.ColumnIndex.e.RowIndex,false);

Launcher | 园豆:45045 (高人七级) | 2014-01-06 15:36

我就是用的 var cellRectangle = dgv.GetCellDisplayRectangle(e.ColumnIndex + 1, e.RowIndex, true);,然后新窗口的 Location =  =new Point(cellRectangle.Location.X,cellRectangle.Location.Y); 这样是不行的,你试试

支持(0) 反对(0) simadi | 园豆:134 (初学一级) | 2014-01-06 15:57

我取的是按钮单元格右侧的单元格的位置.

支持(0) 反对(0) simadi | 园豆:134 (初学一级) | 2014-01-06 15:58

@simadi: 那为什么你不一开始就把你的代码贴出来,然后问为什么位置不正确呢?

你有递增过 ColumnIndex 和 RowIndex 后观察过 (x,y) 的变化规律吗?

有想过 GetCellDisplayRectangle 返回的矩形框是以 DataGridView 控件,还是窗体客户区或者屏幕左上角为原点的呢?说实话,我也不知道,这得写代码测试,你自己测试下,总结下,就能找到正确位置了。

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2014-01-06 16:14

@Launcher: 试了,就是不行 才来问的 谢谢回答!

支持(0) 反对(0) simadi | 园豆:134 (初学一级) | 2014-01-06 16:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册