这里将介绍C#鼠标位置相关获取程序代码,包括从图片的划分,左右边框的确定,鼠标移动的控制,鼠标手势的控制等等。对于标题栏双击***化的功能,也是重要的实现。
- // Mouse Position Operation
- //Code Technology Document
- //NITI Collection
- //图片的划分
- //Bottom_Left
- //Bottom_Middle
- //Bottom_Right
- //
- //Middle_Left左边框
- //Middle_Right右边框
- //标题栏
- //Top_Left
- //Top_Middle
- //Top_Right
- //系统按钮
- //SysButton_Min
- //SysButton_Max
- //SysButton_Close
- //SysButton_Restore
- namespace mouse
- {
- //以下是上述画皮肤方法的具体实现部分,举一个画左边框的代码示例,
- private void DrawMiddle_Left(Graphics g)
- {
- Brush brush = new TextureBrush(Middle_Left, new Rectangle(0, 0,Middle_Left.Width, Middle_Left.Height));
- g.FillRectangle(brush, 0, TITLE_WIDTH, Middle_Left.Width,Height - Bottom_Middle.Height - TITLE_WIDTH);
- }
C#鼠标位置之鼠标移动 ,以及反应代码
- //定义了一个抽象的基类MouseAction,用来表示所有的鼠标事件,它有一个抽象方法Action:
- public abstract class MouseAction
- {
- public abstract void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form);
- }
- //向右拉伸窗口事件的代码响应
- // MouseSizeLeft:拉伸左边框
- // MouseSizeBottom:拉伸下边框
- // MouseSizeTop:拉伸上边框
- // MouseSizeTopLeft:拉伸左上角
- // MouseSizeTopRight:拉伸右上角
- // MouseSizeBottomLeft:拉伸左下角
- // MouseSizeBottomRight:拉伸右下角
- // MouseDrag:鼠标拖动
- public class MouseSizeRight : MouseAction
- {
- private int lx;
- public MouseSizeRight(int LocationX)
- {
- lx = LocationX;
- }
- public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
- {
- form.Width = ScreenX - lx;
- form.Invalidate();
- }
- }
- // 鼠标拖动同样也很简单,不过却稍不同于窗口的缩放拉伸,这里举出它的实现代码:
- public class MouseDrag : MouseAction
- {
- private int x, y;
- public MouseDrag(int hitX, int hitY)
- {
- x = hitX;
- y = hitY;
- }
- public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
- {
- form.Location = new Point(ScreenX - x, ScreenY - y); [Page]
- }
- }
- //接下来我们开始编写发出事件的代码,先定义几个变量:
- private int LEFT = 5, RIGHT = 5, BOTTOM = 5, TOP = 5, TITLE_WIDTH = 45;//边框和标题栏的大小
- private int x = 0, y = 0;//保存鼠标的临时坐标
- private MouseAction mouse;//鼠标的事件响应对象
- 然后在Form的MouseDown事件中记录下鼠标的当前坐标:
- x = e.X;
- y = e.Y;
- 附:e为System.Windows.Forms.MouseEventArgs
- //////////
- ///根据鼠标的坐标定义出事件响应对象:
- ///
- //鼠标点击左上边框
- if((e.X <= LEFT + 10 && e.Y <= TOP) || (e.Y <= TOP + 10 && e.X <= LEFT))
- {
- mouse = new MouseSizeTopLeft(Location.X, Location.Y, Width, Height);
- return;
- }
- //鼠标点击系统关闭按纽
- if(e.X > Width - 20 && e.Y > 6 && e.X < Width - 20 + SysButton_Min.Width && e.Y < 6 + SysButton_Min.Height)
- {
- Close();
- return;
- }
- //大部分的事件响应实际上是在MouseMove事件中完成的:
- private void Form_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
- {
- this.Parent.Cursor = CheckCursorType(e.X, e.Y);//改变鼠标的指针形状
- if(mouse != null)
- {
- mouse.Action(Control.MousePosition.X, Control.MousePosition.Y, this);//执行时间响应
- //注意坐标是Control.MousePosition这个静态变量给出的,它的值为鼠标在桌面上的全局坐标
- }
- }
***,C#鼠标位置MouseUp事件中将mouse变量释放掉:
- private void Form_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
- {
- mouse = null;
- }
- 加上标题栏的双击***化或者还原的事件:
- private void Form_DoubleClick(object sender, System.EventArgs e)
- {
- if(y > TOP && y < TITLE_WIDTH)
- {
- if(WindowState == FormWindowState.Normal)
- {
- WindowState = FormWindowState.Maximized;
- SysButton = SysButton_Restore; [Page]
- Invalidate();
- }
- else if(WindowState == FormWindowState.Maximized)
- {
- WindowState = FormWindowState.Normal;
- SysButton = SysButton_Max;
- Invalidate();
- }
- }
- }
- //防止窗体被缩小成一个点,***给窗口的MinimumSize赋上一个适当的值,例如200,200
- }
【编辑推荐】