WinFrom 获取鼠标点击时的坐标?

zhaozhijun0207 2008-11-29 11:15:53
[C#]
1.鼠标在窗体上点击,获取在窗体上坐标?

2.鼠标点击一个GridView的单元格,获取这个单元格的坐标?
...全文
1363 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
net5i 2008-11-29
  • 打赏
  • 举报
回复
单元格有一个:Cell.ContentBounds可以获取单元格内容的Rectangle
net5i 2008-11-29
  • 打赏
  • 举报
回复
单元格有一个:Cell.ContentBounds可以获取单元格内容的Rectangle
zhaozhijun0207 2008-11-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Fibona 的回复:]
在WinForm程序中可以用system.Windows.Forms.Form.MousePosition属性来获取鼠标,然后将得到的点的坐标减去窗体的位置(Location属性)就是鼠标的相对位置了。
[/Quote]


能否详细也?
wuyq11 2008-11-29
  • 打赏
  • 举报
回复
system.Windows.Forms.Form.MousePosition属性来获取鼠标,
MousePosition.Y
mousePosition.Y;
通过GridView1.Row[i].Cells[i].Style[ "Width "]单元格的宽度和高度实现
yhy0611 2008-11-29
  • 打赏
  • 举报
回复

破网速,真恶!
yhy0611 2008-11-29
  • 打赏
  • 举报
回复
MousePosition.X
MousePosition.Y
长沙三毛 2008-11-29
  • 打赏
  • 举报
回复
        private void Form1_MouseClick(object sender, MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
}

控件的也是捕获MouseClick事件
Fibona 2008-11-29
  • 打赏
  • 举报
回复
在WinForm程序中可以用system.Windows.Forms.Form.MousePosition属性来获取鼠标,然后将得到的点的坐标减去窗体的位置(Location属性)就是鼠标的相对位置了。
net5i 2008-11-29
  • 打赏
  • 举报
回复
GetCellBounds函数已经返回了单元格相对于DataGridView控件左上角的坐标

要继续获取相对于WinForm左上角坐标可以这样:

Rectangle bounds = this.GetCellBounds(cell);
Control parentControl = this.dataGridView1;
while(parentControl != null)
{
bounds.X += parentControl.Left;
bounds.Y += parentControl.Top;
parentControl = parentControl.Parent;
}

为保持GetCellBounds函数的独立性,以及通用性,建议搂主不要把这段代码也放到GetCellBounds函数中
LoveInterMilan 2008-11-29
  • 打赏
  • 举报
回复
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
MessageBox.Show("坐标是:("+x.ToString()+y.ToString()+")");
}

net5i 2008-11-29
  • 打赏
  • 举报
回复
把测试代码也贴出来吧:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(this.GetCellBounds(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).ToString());
}
zhaozhijun0207 2008-11-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 net5i 的回复:]

没有直接获取单元格坐标的方法,需要循环计算得来
[/Quote]


哦,这样啊.


能不能获取鼠标相对于当前点击窗体的坐标?
System.Windows.Forms.Form.MousePosition.Y 这个获取的是[以屏幕坐标表示]
我想获取的是已当前窗体坐标表示

怎么写?
net5i 2008-11-29
  • 打赏
  • 举报
回复
刚才自己测试了一下可以正确获取的
kinghongchen 2008-11-29
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zhaozhijun0207 的回复:]
引用 5 楼 wuyq11 的回复:
system.Windows.Forms.Form.MousePosition属性来获取鼠标,
MousePosition.Y
mousePosition.Y;
通过GridView1.Row[i].Cells[i].Style[ "Width "]单元格的宽度和高度实现



GridView1.Row[i].Cells[i].Style[ "Width "]
无法将带 [] 的索引应用于“System.Windows.Forms.DataGridViewCellStyle”类型的表达式
[/Quote]

5楼写的应该是GridView1.Rows[i].Cells[i].Style[ "Width "] 吧
net5i 2008-11-29
  • 打赏
  • 举报
回复
帮搂主写了一个通用函数,搂主可以测试一下:
/// <summary>
/// 获取单元格相对于DataGridView控件左上角的坐标
/// </summary>
/// <param name="cell"></param>
/// <returns></returns>
private Rectangle GetCellBounds(DataGridViewCell cell)
{
if (cell == null || cell.DataGridView == null)
return Rectangle.Empty;
Rectangle bounds = Rectangle.Empty;

bounds.Width = cell.DataGridView.Columns[cell.ColumnIndex].Width;
bounds.Height = cell.DataGridView.Rows[cell.RowIndex].Height;

if (cell.DataGridView.RowHeadersVisible)
bounds.X += cell.DataGridView.RowHeadersWidth;
DataGridViewRow row = cell.DataGridView.Rows[cell.RowIndex];
for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
{
if (cellIndex < cell.ColumnIndex)
bounds.X += cell.DataGridView.Columns[cellIndex].Width;
}

if (cell.DataGridView.ColumnHeadersVisible)
bounds.Y += cell.DataGridView.ColumnHeadersHeight;
for (int rowIndex = 0; rowIndex < cell.DataGridView.Rows.Count; rowIndex++)
{
if (rowIndex < cell.RowIndex)
bounds.Y += row.Height;
}
return bounds;
}
net5i 2008-11-29
  • 打赏
  • 举报
回复

没有直接获取单元格坐标的方法,需要循环计算得来
net5i 2008-11-29
  • 打赏
  • 举报
回复
搂主要注意:

单元格的高度和宽度,并不是由单元格决定的,而是由行和列决定的,
所以应该这样获取:

DataGridViewRow row = ...;
= row.Height;
DataGridViewColumn column = ...;
= column.Width;

zhaozhijun0207 2008-11-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 net5i 的回复:]
单元格有一个:Cell.ContentBounds可以获取单元格内容的Rectangle
[/Quote]


单元格的内容,我可以获取.我现在需要获取坐标
zhaozhijun0207 2008-11-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyq11 的回复:]
system.Windows.Forms.Form.MousePosition属性来获取鼠标,
MousePosition.Y
mousePosition.Y;
通过GridView1.Row[i].Cells[i].Style[ "Width "]单元格的宽度和高度实现
[/Quote]


GridView1.Row[i].Cells[i].Style[ "Width "]
无法将带 [] 的索引应用于“System.Windows.Forms.DataGridViewCellStyle”类型的表达式

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧