我的鼠标移动控件的方法

    xiaoxiao2022-07-12  144

    public Form1() { InitializeComponent();

    } public bool Moved = false; public bool CanMove = false;//编辑设定未触发就会锁定状态 private void button1_MouseDown(object sender, MouseEventArgs e) { if (Moved&&CanMove) { Control bt = sender as Control; bt.Top = e.Y+bt.Location.Y; bt.Left = e.X+bt.Location.X; } } private void button1_MouseDown_1(object sender, MouseEventArgs e) { Moved = true; } private void button1_MouseUp(object sender, MouseEventArgs e) { Moved = false; CanMove = false; } private void button2_Click(object sender, EventArgs e) { CanMove = true; }

    点击 编辑控件后才能调整,鼠标弹起后就可以结束编辑。

    另一种方法

    [DllImportAttribute(“user32.dll”)] private extern static bool ReleaseCapture(); [DllImportAttribute(“user32.dll”)] private extern static int SendMessage(IntPtr handle, int m, int p, int h); private void button1_MouseDown(object sender, MouseEventArgs e) { Control bt = sender as Control; if (e.Button == MouseButtons.Left) { this.Cursor = Cursors.SizeAll; ReleaseCapture(); SendMessage(bt.Handle, 0xA1, 0x2, 0); this.Cursor = Cursors.Default; } }

    最新回复(0)