移动控件

      在练习中,感觉项目中如用上移动控件的方法会更好,于是自己写了一个:只是功能有限,需再完善。

  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Text;
  7using System.Windows.Forms;
  8
  9namespace scws_Repl
 10{
 11    public partial class Form1 : Form
 12    {
 13        public Form1()
 14        {
 15            InitializeComponent();
 16        }

 17
 18        设置成可拖动窗体 与mousemove事件冲突
 38
 39
 40        private System.Drawing.Point mousePoint;
 41        private System.Windows.Forms.Button mybutton = null;
 42
 43        private void button1_Click(object sender, EventArgs e)
 44        {
 45            mybutton = new Button();           
 46            this.Controls.Add(mybutton);
 47            mybutton.Text = "newbutton";
 48            mybutton.MouseDown += new MouseEventHandler(mybutton_MouseDown);
 49            mybutton.MouseMove += new MouseEventHandler(mybutton_MouseMove);
 50            mybutton.MouseUp += new MouseEventHandler(mybutton_MouseUp);
 51        }

 52        
 53       
 54        private void Form1_MouseMove(object sender, MouseEventArgs e)
 55        {
 56            this.mousePoint.X = e.X;
 57            this.mousePoint.Y = e.Y;
 58            this.linkLabel1.Text = "相对窗口 X:" + mousePoint.X.ToString();
 59            this.linkLabel2.Text = "相对窗口 Y:" + mousePoint.Y.ToString();
 60            this.Text = "相对屏幕 X:" + Cursor.Position.X.ToString() + "          Y:" + Cursor.Position.Y.ToString();
 61        }

 62
 63        /// <summary>
 64        /// 记录鼠标位置
 65        /// </summary>

 66        private Point mousePos; 
 67        private int diffx, diffy;
 68        /// <summary>
 69        /// 记录是否可以移动 
 70        /// </summary>

 71        private bool beginmove = false;
 72
 73        private void mybutton_MouseMove(object sender, MouseEventArgs e)
 74        {
 75            if (beginmove)
 76            {
 77                Button b1 = (Button)sender;
 78                //
 79                diffx = mousePos.X - e.X;
 80                diffy = mousePos.Y - e.Y;
 81                b1.Location = new Point(b1.Location.X - diffx, b1.Location.Y - diffy);
 82               
 83            }

 84        }

 85
 86        private void mybutton_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
 87        {
 88            beginmove = true;//开始移动     
 89            mousePos = new Point(e.X, e.Y);
 90        }

 91
 92        private void mybutton_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
 93        {
 94            if (e.Button == MouseButtons.Left)
 95            {
 96                beginmove = false;
 97                Button b1 = (Button)sender;
 98                diffx = b1.Location.X - mousePos.X + e.X;
 99                diffy = b1.Location.Y - mousePos.Y + e.Y;
100                this.linkLabel3.Text = b1.Text;
101                b1.BackColor = Color.Red;
102                this.linkLabel4.Text = b1.BackColor.ToString();
103                this.linkLabel5.Text = diffx.ToString();
104                this.linkLabel6.Text = diffy.ToString();
105            }

106        }

107    }
       
108}

关于移动和拖动及缩放大小的代码如下:

  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Text;
  7using System.Windows.Forms;
  8
  9namespace scws_Repl
 10{
 11    public partial class Form2 : Form
 12    {
 13        public Form2()
 14        {
 15            InitializeComponent();
 16        }

 17
 18        private enum EnumMousePointPosition
 19        {
 20            MouseSizeNone = 0,       //'无
 21            MouseSizeRight = 1,      //'拉伸右边框
 22            MouseSizeLeft = 2,       //'拉伸左边框
 23            MouseSizeBottom = 3,     //'拉伸下边框
 24            MouseSizeTop = 4,        //'拉伸上边框
 25            MouseSizeTopLeft = 5,    //'拉伸左上角
 26            MouseSizeTopRight = 6,   //'拉伸右上角
 27            MouseSizeBottomLeft = 7//'拉伸左下角
 28            MouseSizeBottomRight = 8,//'拉伸右下角
 29            MouseDrag = 9            // '鼠标拖动
 30        }

 31
 32        //定义几个变量
 33        const int Band = 5;
 34        const int MinWidth = 10;
 35        const int MinHeight = 10;
 36        private EnumMousePointPosition m_MousePointPosition;
 37        private Point p, p1;
 38
 39        //定义自己的MyMouseDown事件
 40        private void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
 41        {
 42            p.X = e.X;
 43            p.Y = e.Y; p1.X = e.X;
 44            p1.Y = e.Y;
 45        }

 46
 47        //定义自己的MyMouseLeave事件
 48        private void MyMouseLeave(object sender, System.EventArgs e)
 49        {
 50            m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
 51            this.Cursor = Cursors.Arrow;
 52        }

 53
 54
 55        //设计一个函数,确定光标在控件不同位置的样式
 56        private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e)
 57        {
 58            if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))
 59            {
 60                if (e.X < Band)
 61                {
 62                    if (e.Y < Band) return EnumMousePointPosition.MouseSizeTopLeft; }
 63                    else
 64                    {
 65                        if (e.Y > -1 * Band + size.Height)
 66                        return EnumMousePointPosition.MouseSizeBottomLeft; }
 67                        else
 68                        return EnumMousePointPosition.MouseSizeLeft; }
 69                    }

 70                }

 71                else
 72                {
 73                    if (e.X > -1 * Band + size.Width)
 74                    {
 75                        if (e.Y < Band)
 76                        return EnumMousePointPosition.MouseSizeTopRight; }
 77                        else
 78                        {
 79                            if (e.Y > -1 * Band + size.Height)
 80                            return EnumMousePointPosition.MouseSizeBottomRight; }
 81                            else
 82                            return EnumMousePointPosition.MouseSizeRight; }
 83                        }

 84                    }

 85                    else
 86                    {
 87                        if (e.Y < Band)
 88                        return EnumMousePointPosition.MouseSizeTop; }
 89                        else
 90                        {
 91                            if (e.Y > -1 * Band + size.Height)
 92                            return EnumMousePointPosition.MouseSizeBottom; }
 93                            else
 94                            return EnumMousePointPosition.MouseDrag; }
 95                        }

 96                    }

 97                }

 98            }

 99            else
100            return EnumMousePointPosition.MouseSizeNone; }
101        }

102
103        //定义自己的MyMouseMove事件,在这个事件里,会使用上面设计的函数
104        private void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
105        {
106            Control lCtrl = (sender as Control);
107
108            if (e.Button == MouseButtons.Left)
109            {
110                switch (m_MousePointPosition)
111                {
112                    case EnumMousePointPosition.MouseDrag:
113                        lCtrl.Left = lCtrl.Left + e.X - p.X;
114                        lCtrl.Top = lCtrl.Top + e.Y - p.Y;
115                        break;
116                    case EnumMousePointPosition.MouseSizeBottom:
117                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
118                        p1.X = e.X;
119                        p1.Y = e.Y; //'记录光标拖动的当前点
120                        break;
121                    case EnumMousePointPosition.MouseSizeBottomRight:
122                        lCtrl.Width = lCtrl.Width + e.X - p1.X;
123                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
124                        p1.X = e.X;
125                        p1.Y = e.Y; //'记录光标拖动的当前点
126                        break;
127                    case EnumMousePointPosition.MouseSizeRight:
128                        lCtrl.Width = lCtrl.Width + e.X - p1.X;
129                        //      lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
130                        p1.X = e.X;
131                        p1.Y = e.Y; //'记录光标拖动的当前点
132                        break;
133                    case EnumMousePointPosition.MouseSizeTop:
134                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
135                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
136                        break;
137                    case EnumMousePointPosition.MouseSizeLeft:
138                        lCtrl.Left = lCtrl.Left + e.X - p.X;
139                        lCtrl.Width = lCtrl.Width - (e.X - p.X);
140                        break;
141                    case EnumMousePointPosition.MouseSizeBottomLeft:
142                        lCtrl.Left = lCtrl.Left + e.X - p.X;
143                        lCtrl.Width = lCtrl.Width - (e.X - p.X);
144                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
145                        p1.X = e.X;
146                        p1.Y = e.Y; //'记录光标拖动的当前点
147                        break;
148                    case EnumMousePointPosition.MouseSizeTopRight:
149                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
150                        lCtrl.Width = lCtrl.Width + (e.X - p1.X);
151                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
152                        p1.X = e.X;
153                        p1.Y = e.Y; //'记录光标拖动的当前点
154                        break;
155                    case EnumMousePointPosition.MouseSizeTopLeft:
156                        lCtrl.Left = lCtrl.Left + e.X - p.X;
157                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
158                        lCtrl.Width = lCtrl.Width - (e.X - p.X);
159                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
160                        break;
161                    default:
162                        break;
163                }

164                if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;
165                if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;
166
167            }

168            else
169            {
170                m_MousePointPosition = MousePointPosition(lCtrl.Size, e);  //'判断光标的位置状态
171                switch (m_MousePointPosition)  //'改变光标
172                {
173                    case EnumMousePointPosition.MouseSizeNone:
174                        this.Cursor = Cursors.Arrow;       //'箭头
175                        break;
176                    case EnumMousePointPosition.MouseDrag:
177                        this.Cursor = Cursors.SizeAll;     //'四方向
178                        break;
179                    case EnumMousePointPosition.MouseSizeBottom:
180                        this.Cursor = Cursors.SizeNS;      //'南北
181                        break;
182                    case EnumMousePointPosition.MouseSizeTop:
183                        this.Cursor = Cursors.SizeNS;      //'南北
184                        break;
185                    case EnumMousePointPosition.MouseSizeLeft:
186                        this.Cursor = Cursors.SizeWE;      //'东西
187                        break;
188                    case EnumMousePointPosition.MouseSizeRight:
189                        this.Cursor = Cursors.SizeWE;      //'东西
190                        break;
191                    case EnumMousePointPosition.MouseSizeBottomLeft:
192                        this.Cursor = Cursors.SizeNESW;    //'东北到南西
193                        break;
194                    case EnumMousePointPosition.MouseSizeBottomRight:
195                        this.Cursor = Cursors.SizeNWSE;    //'东南到西北
196                        break;
197                    case EnumMousePointPosition.MouseSizeTopLeft:
198                        this.Cursor = Cursors.SizeNWSE;    //'东南到西北
199                        break;
200                    case EnumMousePointPosition.MouseSizeTopRight:
201                        this.Cursor = Cursors.SizeNESW;    //'东北到南西
202                        break;
203                    default:
204                        break;
205                }

206            }

207        }

208
209
210        //制作一个初始化过程,将界面panel1上的所有控件都绑定MyMouseDown、MyMouseLeave、MyMouseMove事件,记得在Form初始化或者Form_Load时先执行它。
211        private void initProperty()
212        {
213            for (int i = 0; i < this.panel1.Controls.Count; i++)
214            {
215                this.panel1.Controls[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
216                this.panel1.Controls[i].MouseLeave += new System.EventHandler(MyMouseLeave);
217                this.panel1.Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
218            }

219        }

220
221        private void Form2_Load(object sender, EventArgs e)
222        {
223            initProperty();
224        }

225    }

226}
posted @ 2008-05-26 20:47  Simmy.卧龙先生  阅读(371)  评论(0编辑  收藏  举报