C# 鼠标移动Winform窗体内或者panel容器内的控件 显示虚线/实现虚线框来确定位置

C# 鼠标移动WinForm窗体或者panel容器内的控件 移动虚线/实现虚线框来确定位置

1.用到的方法介绍

今天,根据领导指示指导移动容器内的控件,生成虚线框,使用  

ControlPaint.DrawReversibleFrame
 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.程序源代码工程文件下载

源代码工程文件下载

 

posted @ 2018-12-13 10:28  JiYF  阅读(2464)  评论(0编辑  收藏  举报