C# 鼠標移動Winfor窗體內或者panel容器內的控件 顯示虛線/實現虛線框來確定位置
阿新 • • 發佈:2018-12-13
nbsp 框架 移動 oca 效果 font pub ret 背景色
C# 鼠標移動窗體或者panel容器內的控件 移動虛線/實現虛線框來確定位置
1.用到的方法介紹
今天,根據領導指示指導移動容器內的控件,生成虛線框,使用
1 // 2 // 摘要: 3 // 在屏幕上的指定邊界內,按指定背景色繪制處於指定狀態的可逆框架。 4 // 5 // 參數: 6 // rectangle: 7 // 代表要繪制矩形的尺寸的 System.Drawing.Rectangle(采用屏幕坐標)。 8 // 9 // backColor: 10 // 框架的背景的 System.Drawing.Color。 11 // 12 // style: 13 // System.Windows.Forms.FrameStyle 值之一,它指定框架的樣式。14 public static void DrawReversibleFrame(Rectangle rectangle, Color backColor, FrameStyle style);
2.程序運行效果
3.代碼實現
1 public Form1() 2 { 3 InitializeComponent(); 4 } 5 6 private Point downPoint; 7 private Rectangle downRectangle;8 private Rectangle lastRectangle; 9 10 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 11 { 12 if (e.Button != MouseButtons.Left) return; 13 14 downPoint = e.Location; 15 downRectangle =new Rectangle(0, 0, ((Control)sender).Width, pictureBox1.Height);16 downRectangle.Offset(((Control)sender).PointToScreen(new Point(0, 0))); 17 ControlPaint.DrawReversibleFrame(downRectangle, Color.White, FrameStyle.Thick); 18 19 lastRectangle = downRectangle; 20 } 21 private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 22 { 23 if (e.Button != MouseButtons.Left) return; 24 25 ControlPaint.DrawReversibleFrame(lastRectangle, Color.White, FrameStyle.Thick); 26 27 Rectangle rectangle = downRectangle; 28 rectangle.Offset(e.X - downPoint.X, e.Y - downPoint.Y); 29 ControlPaint.DrawReversibleFrame(rectangle, Color.White, FrameStyle.Thick); 30 31 lastRectangle = rectangle; 32 } 33 private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 34 { 35 if (e.Button != MouseButtons.Left) return; 36 37 ControlPaint.DrawReversibleFrame(lastRectangle, Color.White, FrameStyle.Thick); 38 39 pictureBox1.Location = new Point( 40 ((Control)sender).Location.X + e.X - downPoint.X, 41 ((Control)sender).Location.Y + e.Y - downPoint.Y); 42 }
4.程序源代碼工程文件下載
源代碼工程文件下載
C# 鼠標移動Winfor窗體內或者panel容器內的控件 顯示虛線/實現虛線框來確定位置