欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

win32获取鼠标位置和移动窗体

程序员文章站 2022-03-31 09:38:08
...

win32 获取鼠标位置
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
    public int X;
    public int Y;
    public POINT(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }
}
移动窗体
    private void g1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                //this.DragMove();
                MouseCursor_Point = System.Windows.Forms.Cursor.Position;
            }
        }
        System.Drawing.Point MouseCursor_Point;
        private void g1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                System.Drawing.Point MouseCursor_Point_Aux;
                MouseCursor_Point_Aux = System.Windows.Forms.Cursor.Position;
                if (MouseCursor_Point_Aux.X >= MouseCursor_Point.X)
                    this.Left += MouseCursor_Point_Aux.X - MouseCursor_Point.X;
                else
                    this.Left -= MouseCursor_Point.X - MouseCursor_Point_Aux.X;
                if (MouseCursor_Point_Aux.Y >= MouseCursor_Point.Y)
                    this.Top += MouseCursor_Point_Aux.Y - MouseCursor_Point.Y;
                else
                    this.Top -= MouseCursor_Point.Y - MouseCursor_Point_Aux.Y;
                MouseCursor_Point = MouseCursor_Point_Aux;
            }
        }
获取鼠标在窗体内的位置
e.MouseDevice.GetPosition(curtainCanvas);