对鼠标操作Label的方法
1 #region 2 //定义一个枚举类型,描述光标状态 3 private enum EnumMousePointPosition 4 { 5 #region 6 MouseSizeNone = 0, //'无 7 MouseSizeRight = 1, //'拉伸右边框 8 MouseSizeLeft = 2, //'拉伸左边框 9 MouseSizeBottom = 3, //'拉伸下边框 10 MouseSizeTop = 4, //'拉伸上边框 11 MouseSizeTopLeft = 5, //'拉伸左上角 12 MouseSizeTopRight = 6, //'拉伸右上角 13 MouseSizeBottomLeft = 7, //'拉伸左下角 14 MouseSizeBottomRight = 8, //'拉伸右下角 15 MouseDrag = 9 // '鼠标拖动 16 #endregion 17 } 18 //定义自己的MyMouseDown事件 19 private void MyMouseDown(object sender, MouseEventArgs e) 20 { 21 p.X = e.X; 22 p.Y = e.Y; 23 p1.X = e.X; 24 p1.Y = e.Y; 25 } 26 //定义自己的MyMouseLeave事件 27 private void MyMouseLeave(object sender, EventArgs e) 28 { 29 m_MousePointPosition = EnumMousePointPosition.MouseSizeNone; 30 this.Cursor = Cursors.Arrow; 31 } 32 //设计一个函数,确定光标在不同位置的样式 33 private EnumMousePointPosition MousePointPosition(Size size, MouseEventArgs e) 34 { 35 if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height)) 36 { 37 if (e.X < Band) 38 { 39 if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTopLeft; } 40 else 41 { 42 if (e.Y > -1 * Band + size.Height) 43 { return EnumMousePointPosition.MouseSizeBottomLeft; } 44 else 45 { return EnumMousePointPosition.MouseSizeLeft; } 46 } 47 } 48 else 49 { 50 if (e.X > -1 * Band + size.Width) 51 { 52 if (e.Y < Band) 53 { return EnumMousePointPosition.MouseSizeTopRight; } 54 else 55 { 56 if (e.Y > -1 * Band + size.Height) 57 { return EnumMousePointPosition.MouseSizeBottomRight; } 58 else 59 { return EnumMousePointPosition.MouseSizeRight; } 60 } 61 } 62 else 63 { 64 if (e.Y < Band) 65 { return EnumMousePointPosition.MouseSizeTop; } 66 else 67 { 68 if (e.Y > -1 * Band + size.Height) 69 { return EnumMousePointPosition.MouseSizeBottom; } 70 else 71 { return EnumMousePointPosition.MouseDrag; } 72 } 73 } 74 } 75 } 76 else 77 { return EnumMousePointPosition.MouseSizeNone; } 78 } 79 80 //定义自己的MyMouseMove事件,并使用上面定义的函数 81 private void MyMouseMove(object sender, MouseEventArgs e) 82 { 83 Control lCtrl = (sender as Control); 84 if (e.Button == MouseButtons.Left) 85 { 86 switch (m_MousePointPosition) 87 { 88 //鼠标拖动 89 case EnumMousePointPosition.MouseDrag: 90 lCtrl.Left = lCtrl.Left + e.X - p.X; 91 lCtrl.Top = lCtrl.Top + e.Y - p.Y; 92 break; 93 //拉伸下边框 94 case EnumMousePointPosition.MouseSizeBottom: 95 lCtrl.Height = lCtrl.Height + e.Y - p1.Y; 96 p1.X = e.X; 97 p1.Y = e.Y; //'记录光标拖动的当前点 98 break; 99 //拉伸左下角 100 case EnumMousePointPosition.MouseSizeBottomRight: 101 lCtrl.Width = lCtrl.Width + e.X - p1.X; 102 lCtrl.Height = lCtrl.Height + e.Y - p1.Y; 103 p1.X = e.X; 104 p1.Y = e.Y; //'记录光标拖动的当前点 105 break; 106 //拉伸又边框 107 case EnumMousePointPosition.MouseSizeRight: 108 lCtrl.Width = lCtrl.Width + e.X - p1.X; 109 // lCtrl.Height = lCtrl.Height + e.Y - p1.Y; 110 p1.X = e.X; 111 p1.Y = e.Y; //'记录光标拖动的当前点 112 break; 113 //拉伸上边框 114 case EnumMousePointPosition.MouseSizeTop: 115 lCtrl.Top = lCtrl.Top + (e.Y - p.Y); 116 lCtrl.Height = lCtrl.Height - (e.Y - p.Y); 117 break; 118 //拉伸左边框 119 case EnumMousePointPosition.MouseSizeLeft: 120 lCtrl.Left = lCtrl.Left + e.X - p.X; 121 lCtrl.Width = lCtrl.Width - (e.X - p.X); 122 break; 123 //拉伸左下角 124 case EnumMousePointPosition.MouseSizeBottomLeft: 125 lCtrl.Left = lCtrl.Left + e.X - p.X; 126 lCtrl.Width = lCtrl.Width - (e.X - p.X); 127 lCtrl.Height = lCtrl.Height + e.Y - p1.Y; 128 p1.X = e.X; 129 p1.Y = e.Y; //'记录光标拖动的当前点 130 break; 131 //拉伸右上角 132 case EnumMousePointPosition.MouseSizeTopRight: 133 lCtrl.Top = lCtrl.Top + (e.Y - p.Y); 134 lCtrl.Width = lCtrl.Width + (e.X - p1.X); 135 lCtrl.Height = lCtrl.Height - (e.Y - p.Y); 136 p1.X = e.X; 137 p1.Y = e.Y; //'记录光标拖动的当前点 138 break; 139 //拉伸左上角 140 case EnumMousePointPosition.MouseSizeTopLeft: 141 lCtrl.Left = lCtrl.Left + e.X - p.X; 142 lCtrl.Top = lCtrl.Top + (e.Y - p.Y); 143 lCtrl.Width = lCtrl.Width - (e.X - p.X); 144 lCtrl.Height = lCtrl.Height - (e.Y - p.Y); 145 break; 146 147 default: 148 break; 149 } 150 if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth; 151 if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight; 152 } 153 154 else 155 { 156 m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态 157 switch (m_MousePointPosition) //'改变光标 158 { 159 case EnumMousePointPosition.MouseSizeNone: 160 this.Cursor = Cursors.Arrow; //'箭头 161 break; 162 163 case EnumMousePointPosition.MouseDrag: 164 this.Cursor = Cursors.SizeAll; //'四方向 165 break; 166 167 case EnumMousePointPosition.MouseSizeBottom: 168 this.Cursor = Cursors.SizeNS; //'南北 169 break; 170 171 case EnumMousePointPosition.MouseSizeTop: 172 this.Cursor = Cursors.SizeNS; //'南北 173 break; 174 175 case EnumMousePointPosition.MouseSizeLeft: 176 this.Cursor = Cursors.SizeWE; //'东西 177 break; 178 179 case EnumMousePointPosition.MouseSizeRight: 180 this.Cursor = Cursors.SizeWE; //'东西 181 break; 182 183 case EnumMousePointPosition.MouseSizeBottomLeft: 184 this.Cursor = Cursors.SizeNESW; //'东北到南西 185 break; 186 187 case EnumMousePointPosition.MouseSizeBottomRight: 188 this.Cursor = Cursors.SizeNWSE; //'东南到西北 189 break; 190 191 case EnumMousePointPosition.MouseSizeTopLeft: 192 this.Cursor = Cursors.SizeNWSE; //'东南到西北 193 break; 194 195 case EnumMousePointPosition.MouseSizeTopRight: 196 this.Cursor = Cursors.SizeNESW; //'东北到南西 197 break; 198 199 default: 200 break; 201 202 } 203 } 204 } 205 #endregion 定义鼠标事件 206 //将界面panel1上的所有控件都绑定MyMouseDown、MyMouseLeave、MyMouseMove事件,记得在Form初始化或者Form_Load时先执行它 207 private void initProperty() 208 { 209 for (int i = 0; i < this.panel1.Controls.Count; i++) 210 { 211 this.panel1.Controls[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown); 212 this.panel1.Controls[i].MouseLeave += new System.EventHandler(MyMouseLeave); 213 this.panel1.Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove); 214 } 215 }