读书人

C#鼠标拖动有关问题

发布时间: 2012-04-16 16:20:04 作者: rapoo

C#鼠标拖动问题
各位,C#中如何按住鼠标左键拖动一个按钮随其在winform中运动,释放鼠标左键后它就停留在当前窗体不在运动
下面是我的程序
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{


Point p = new Point(e.X, e.Y);
button1.Location = p;
button1.Text = e.X.ToString() + e.Y.ToString();

}
}
请各位指教,小弟初学者,打扰各位了

[解决办法]
随便写了一个,仅供参考,只考虑了向右向下,不过建议你还是发消息吧,网上有例子

C# code
        bool down = false;        private void button10_MouseMove(object sender, MouseEventArgs e)        {            if (!down) return;            Point p = button10.PointToScreen(button10.Location);            Point p1 = button10.PointToScreen(e.Location);            button10.Left = button10.Left + (p1.X - p.X);            button10.Top = button10.Top + (p1.Y - p.Y);            this.Update();        }        private void button10_MouseDown(object sender, MouseEventArgs e)        {            down = true;        }        private void button10_MouseUp(object sender, MouseEventArgs e)        {            down = false;        }
[解决办法]
C# code
 // C#鼠标拖动任意控件Point p = new Point(0, 0);        //利用Windows的API函数:SendMessage 和 ReleaseCapture         const uint WM_SYSCOMMAND = 0x0112;        const uint SC_MOVE = 0xF010;        const uint HTCAPTION = 0x0002;        [DllImport("user32.dll", EntryPoint = "SendMessageA")]        private static extern int SendMessage(IntPtr hwnd, uint wMsg, uint wParam, uint lParam);        [DllImport("user32.dll")]        private static extern int ReleaseCapture();public void MouseDown(object sender, MouseEventArgs e)        {                Button bt = sender as Button;                ReleaseCapture();                SendMessage((sender as Control).Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);}public void bt_MouseMove(object sender, MouseEventArgs e)        {                        Button bt = sender as Button;            if (e.Button == MouseButtons.Left)            {                bt.Location = new Point(bt.Left + e.X - p.X, bt.Top + e.Y - p.Y);            }        }
[解决办法]

创建一个panel,两个button(button的移动类似)
C# code
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace 实现panel的拖动和缩放{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        Point p = new Point(0, 0);//定义一个点        //并为控件 添加 MouseDown 事件        // C#鼠标拖动任意控件        //利用Windows的API函数:SendMessage 和 ReleaseCapture         const uint WM_SYSCOMMAND = 0x0112;        const uint SC_MOVE = 0xF010;        const uint HTCAPTION = 0x0002;        [DllImport("user32.dll", EntryPoint = "SendMessageA")]        private static extern int SendMessage(IntPtr hwnd, uint wMsg, uint wParam, uint lParam);        [DllImport("user32.dll")]        private static extern int ReleaseCapture();        int xx = 0;        private void addpanel_Click(object sender, EventArgs e)        {//创建panel            xx++;            CreateControls.CreatePanel(backpanel, xx.ToString(), panel_MouseMove, panel_MouseDown, panel_Paint);        }        public void panel_Paint(object sender, PaintEventArgs e)        {            Panel panel = sender as Panel;            ControlPaint.DrawBorder(e.Graphics,                                        panel.ClientRectangle,                                        Color.Red,                                        1,                                        ButtonBorderStyle.Solid,                                        Color.Red,                                        1,                                        ButtonBorderStyle.Solid,                                        Color.Red,                                        1,                                        ButtonBorderStyle.Solid,                                        Color.Red,                                        1,                                        ButtonBorderStyle.Solid);        }        public void panel_MouseMove(object sender, MouseEventArgs e)        {//实现panel的缩放和拖动            Panel panel = sender as Panel;//取得当前panel            if (e.Button == System.Windows.Forms.MouseButtons.Left)            {//如果点击的为鼠标左键                if (ModifierKeys == Keys.Control)                {//如果按住ctrl键,则为缩放panel                    panel.Width += e.X - p.X;//取出panel缩放后的实际宽度                    panel.Height += e.Y - p.Y;//取出panel缩放后的实际高度                    p = new Point(e.X, e.Y);//给点重新赋值                    panel.Refresh();//刷新放置绘制出现延迟                }                else                {//否则为拖动panel                    panel.Location = new Point(panel.Left + e.X - p.X, panel.Top + e.Y - p.Y);//拖动panel后重新定位位置                }            }        }        public void panel_MouseDown(object sender, MouseEventArgs e)        {            Panel panel = sender as Panel;            panelname = panel.Name;            if ((int)Control.ModifierKeys == (int)Keys.Control)            {//如果按住ctrl键,则更新p点                p = new Point(e.X, e.Y);            }            else            {//否则执行移动,使用api函数                ReleaseCapture();                SendMessage((sender as Control).Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);            }        }        public string panelname;        private void delpanel_Click(object sender, EventArgs e)        {            if (panelname == "" || panelname == null)            { MessageBox.Show("请选择一个panel"); }            else            {                Panel panel = backpanel.Controls[panelname] as Panel;                backpanel.Controls.Remove(panel);                panelname = "";            }        }    }} 

读书人网 >C#

热点推荐