C#开发实例 鼠标篇

鼠标的操作控制:

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

1.1获取鼠标信息

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

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

C#开发实例 鼠标篇

主要程序代码如下:

         [DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]

         public static extern int GetDoubleClickTime();

         private void Frm_Main_Load(object sender, EventArgs e)
{ label1.Text = GetDoubleClickTime() + "毫秒";
}

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

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

C#开发实例 鼠标篇

         [DllImport("user32.dll", EntryPoint = "GetCaretBlinkTime")]

         public static extern int GetCaretBlinkTime();

         private void Frm_Main_Load(object sender, EventArgs e)
{ label1.Text = GetCaretBlinkTime() + "毫秒";
}

③实例003 获取鼠标键数

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

C#开发实例 鼠标篇

         public const int SM = ;

         [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]

         public static extern int GetSystemMetrics(int intcount);

         private void Frm_Main_Load(object sender, EventArgs e)
{
int intCon = GetSystemMetrics(SM);
label1.Text = "鼠标键数" + intCon + "个";
}

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

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

C#开发实例 鼠标篇

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

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

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

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

1.2鼠标基本设置

①鼠标指针形状

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

       this.label1.Cursor = Cursors.Hand;

C#开发实例 鼠标篇

②鼠标的图形样式

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

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

③左右键互换功能

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

         [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]

         public static extern int SwapMouseButton(int bSwap);

         public void DefultRightButton()
{
SwapMouseButton();
}
public void DefultLeftButton()
{
SwapMouseButton();
} private void button1_Click(object sender, EventArgs e)
{
this.DefultLeftButton();
}
private void button2_Click(object sender, EventArgs e)
{
this.DefultRightButton();
}

C#开发实例 鼠标篇

④固定鼠标控制区域

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

         private void button1_Click(object sender, EventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
private void button2_Click(object sender, EventArgs e)
{
Screen[] screens = Screen.AllScreens;
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Clip = screens[].Bounds;
}

C#开发实例 鼠标篇

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

①隐藏鼠标按钮

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

         [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
public static extern bool ShowCursor(bool bShow); private void button1_Click(object sender, EventArgs e)
{
ShowCursor(false);//隐藏鼠标
}
private void button2_Click(object sender, EventArgs e)
{
ShowCursor(true);//显示鼠标
}

C#开发实例 鼠标篇

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

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

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

③利用鼠标绘图

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

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

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

pt1,pt2为2个点

         private bool G_OnMouseDown;
private Graphics g;
private Pen pen = new Pen(Color.Blue, );
private Point CPoint,lastPoint; [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
public static extern bool ShowCursor(bool bShow);
private void Form1_MouseMove(object sender, MouseEventArgs e)
{ if (lastPoint.Equals(Point.Empty))
{
lastPoint = new Point(e.X, e.Y);
}
if (G_OnMouseDown)
{
Graphics g = this.CreateGraphics();
CPoint = new Point(e.X, e.Y);
g.DrawLine(pen, CPoint, lastPoint);
g.Dispose();
}
lastPoint = new Point(e.X, e.Y);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
G_OnMouseDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
G_OnMouseDown = false;
// this.Refresh();
}

C#开发实例 鼠标篇

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

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

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

C#开发实例 鼠标篇

上一篇:maven多项目配置实践


下一篇:LIST OF BEST OPEN SOURCE BLOCKCHAIN PLATFORMS