1、获取鼠标窗体坐标屏幕坐标
protected override void OnMouseMove(MouseEventArgs e)
{
Point clientMouse=e.Location;//获取鼠标窗体客户区坐标
Point screenMouse=PointToScreen(clientMouse);//获取鼠标屏幕坐标
this.Text=string.Format("鼠标窗体坐标:({0},{1} 鼠标屏幕坐标:({2},{3}))",
clientMouse.X,clientMouse.Y,screenMouse.X,screenMouse.Y);
}
2、利用鼠标画图
bool isMouseDown = false;
Point pointMouse = new Point();
protected override void OnMouseDown(MouseEventArgs e)
{
pointMouse = e.Location;
isMouseDown = true;
}
protected override void OnMouseUp(MouseEventArgs e)
{
isMouseDown = false;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (isMouseDown)
{
using (Graphics G = this.CreateGraphics())
{
using (Pen pen = new Pen(Color.Black, 3f))
{
G.DrawLine(pen, pointMouse, e.Location);
pointMouse = e.Location;
}
}
}
}