zoukankan      html  css  js  c++  java
  • C#开发实例 鼠标篇

    鼠标的操作控制:

      鼠标是计算机的一个重要组成部分,有很多默认的设置,如双击时间间隔,闪烁频率,移动速度等,本篇使用C#获取这些基本的信息。

    1.1获取鼠标信息

    ①实例001 获取鼠标双击时间间隔

    主要用到的API函数为GetDoubleClickTime。函数主要用来判断连续2次鼠标单击之间会被处理成双击的时间间隔。

    主要程序代码如下:

    1         [DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
    2 
    3         public static extern int GetDoubleClickTime();
    4 
    5         private void Frm_Main_Load(object sender, EventArgs e)
    6         {
    7 
    8             label1.Text = GetDoubleClickTime() + "毫秒";
    9         }

    ②实例002 获取光标闪烁频率

    主要用到的API函数为GetCaretBlinkTime,获取光标闪烁的时间,返回值为int类型,毫秒为单位。

    1         [DllImport("user32.dll", EntryPoint = "GetCaretBlinkTime")]
    2 
    3         public static extern int GetCaretBlinkTime();
    4 
    5         private void Frm_Main_Load(object sender, EventArgs e)
    6         {
    7 
    8             label1.Text = GetCaretBlinkTime() + "毫秒";
    9         }

    ③实例003 获取鼠标键数

    鼠标种类多种多样,除了左右键,还有滚轮,有的带功能键,主要用到的API函数为GetSystemMetrics,intcount指定欲获取的信息。

     1         public const int SM = 43;
     2 
     3         [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
     4 
     5         public static extern int GetSystemMetrics(int intcount);
     6 
     7         private void Frm_Main_Load(object sender, EventArgs e)
     8         {
     9             int intCon = GetSystemMetrics(SM);
    10             label1.Text = "鼠标键数" + intCon + "";
    11         }

    ④实例004 显示鼠标等待光圈

    主要用到了Form类的Cursor属性,Cursor属性主要用来获取或设置当鼠标指正位于窗体上时显示的光标。

    1         private void Frm_Main_Load(object sender, EventArgs e)
    2         {
    3             this.Cursor = Cursors.WaitCursor;
    4         }

    ⑤获取鼠标在窗体上的位置

    获取鼠标位置在开发过程中很重要,通过单击鼠标获取鼠标的当前位置。主要用到MouseEventArgs类的X和Y属性,MouseEventArgs类为MouseUp、MouseDown、MouseMove事件提供数据。

    1         private void Frm_MouseDown(object sender, MouseEventArgs e)
    2         {
    3             this.label1.Text = e.X.ToString;
    4             this.label2.Text = e.Y.ToString;
    5         }

    1.2鼠标基本设置

    ①鼠标指针形状

    鼠标的指针经常会有多种样式,如“等待”,打开链接的“小手”。本实例是将鼠标放到label上显示小手。

    1       this.label1.Cursor = Cursors.Hand;

    ②鼠标的图形样式

    用户使用鼠标时,为了美观,自定义鼠标图片。

    1         private void button1_Click(object sender, EventArgs e)
    2         {
    3             this.Cursor = new Cursor("pen_il.cur");   //改变鼠标图片
    4         }

    ③左右键互换功能

    在控制面板中,用户可以对鼠标做相应的设置,在这里左右键互换用到的API函数为SwapMouseButton。在SwapMouseButton()中,返回为真交换左右键,否则恢复正常。

     1         [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
     2 
     3         public static extern int SwapMouseButton(int bSwap);
     4 
     5         public void DefultRightButton()
     6         {
     7             SwapMouseButton(1);
     8         }
     9         public void DefultLeftButton()
    10         {
    11             SwapMouseButton(0);
    12         }
    13 
    14         private void button1_Click(object sender, EventArgs e)
    15         {
    16             this.DefultLeftButton();
    17         }
    18         private void button2_Click(object sender, EventArgs e)
    19         {
    20             this.DefultRightButton();
    21         }

    ④固定鼠标控制区域

    在实际应用中,有时候要限制鼠标的移动区域,让其只能在某一区域移动。

     1         private void button1_Click(object sender, EventArgs e)
     2         {
     3             this.Cursor = new Cursor(Cursor.Current.Handle);
     4             Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
     5             Cursor.Clip = new Rectangle(this.Location, this.Size);
     6         }
     7         private void button2_Click(object sender, EventArgs e)
     8         {
     9             Screen[] screens = Screen.AllScreens;
    10             this.Cursor = new Cursor(Cursor.Current.Handle);
    11             Cursor.Clip = screens[0].Bounds;
    12         }

    1.3鼠标操作在实际中的应用

    ①隐藏鼠标按钮

    在实际应用中,有时候会将鼠标隐藏,通过使用键盘来操作。主要用到的API函数是ShowCursor。ShowCursor(bool bShow);bshow为true显示指针,如果为false隐藏指针。

     1         [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
     2         public static extern bool ShowCursor(bool bShow);
     3 
     4         private void button1_Click(object sender, EventArgs e)
     5         {
     6             ShowCursor(false);//隐藏鼠标
     7         }
     8         private void button2_Click(object sender, EventArgs e)
     9         {
    10             ShowCursor(true);//显示鼠标
    11         }

    ②通过双击窗体模拟按键tab操作

    通常我们在一个文本框中输入完成时,会按tab切换到下一个文本框,能否通过双击窗体实现。主要用到的是SendKey类中的Send方法向活动发送按键操作。

    1         private void Frm_DoubleClick(object sender, EventArgs e)
    2         {
    3             SendKeys.Send("{Tab}");
    4         }

    ③利用鼠标绘图

    通过鼠标绘制图形,主要用到Graphics类中的DrawLine方法和MouseEventArgs类的X,Y属性,DrawLine方法主要用来绘制直线,该方法可被重载,这里用到的重载形式为

    public void DrawLine(Pen pen,Point pt1,Point pt2)

    pen:为Pen对象,确定线条的颜色、宽度、样式

    pt1,pt2为2个点

     1         private bool G_OnMouseDown;
     2         private Graphics g;
     3         private Pen pen = new Pen(Color.Blue, 5);
     4         private Point CPoint,lastPoint;
     5 
     6         [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
     7         public static extern bool ShowCursor(bool bShow);
     8         private void Form1_MouseMove(object sender, MouseEventArgs e)
     9         {
    10 
    11             if (lastPoint.Equals(Point.Empty))
    12             {
    13                 lastPoint = new Point(e.X, e.Y);
    14             }
    15             if (G_OnMouseDown)
    16             {
    17                 Graphics g = this.CreateGraphics();
    18                 CPoint = new Point(e.X, e.Y);
    19                 g.DrawLine(pen, CPoint, lastPoint);
    20                 g.Dispose();
    21             }
    22             lastPoint = new Point(e.X, e.Y);
    23         }
    24         private void Form1_MouseDown(object sender, MouseEventArgs e)
    25         {
    26             G_OnMouseDown = true;
    27         }
    28         private void Form1_MouseUp(object sender, MouseEventArgs e)
    29         {
    30             G_OnMouseDown = false;
    31          //   this.Refresh();
    32         }

    ④使窗体始终在其他窗口之上

    有时候播放视频的时候,播放器会始终在桌面最顶端,主要用到的是TopMost,以及任务栏不显示ShowInTaskbar设置为true。

    1         private void Frm_Main_Load(object sender, EventArgs e)
    2         {
    3             this.ShowInTaskbar = false;//在任务栏不显示
    4             CanPenetrate();
    5             this.TopMost = true;//始终在顶层
    6         }

     

  • 相关阅读:
    jdk环境变量配置(默认安装在c盘下)
    Less使用笔记
    Bootstrap4元素显示和隐藏
    npm常见命令及参数用法
    详解:cssrem插件 -- VS Code px转rem神器
    关于position:fixed的注意点
    解决:无法push到远程仓储
    解决:'git' 不是内部或外部命令,也不是可运行的程序
    小程序3-地图定位2
    转-前端开发流程
  • 原文地址:https://www.cnblogs.com/SeekHit/p/5125713.html
Copyright © 2011-2022 走看看