C#程序设计_鼠标和键盘

一、获取鼠标的窗体/屏幕坐标

protected override void OnMouseMove(MouseEventArgs e)
{
    Point clientMouse = e.Location; //相对窗体坐标
    Point screenMouse = PointToScreen(clientMouse); //相对屏幕坐标
    this.Text = string.Format("鼠标窗体坐标:({0},{1}) 鼠标屏幕坐标:({2},{3})",
    clientMouse.X, clientMouse.Y, screenMouse.X, screenMouse.Y);
}

该实例重写了鼠标移动事件函数OnMouseMove,利用参数e获取窗体坐标,利用PointToScreen方法获取屏幕坐标。

二、鼠标长时间按下事件

鼠标持续按下2秒,弹窗

public partial class Form1 : Form
{
    Timer timer = new Timer(); //创建实例
    int timeout = 0; //超时时间
    MouseEventArgs LongDown; //鼠标长时间按下事件参数
    public Form1()//窗体构造函数
    {
        InitializeComponent();
        timer.Interval = 1000; //定时器间隔,单位毫秒
        timer.Tick += new EventHandler(timer_Tick); //注册timer_Tick事件
    }
    void timer_Tick(object sender, EventArgs e)//每个间隔执行一次
    {
        if (++timeout == 2) OnMouseLongDown(this.LongDown); 
        //鼠标按下2秒,调用长时间按下事件
    }
    protected override void OnMouseDown(MouseEventArgs e)
    {
        this.LongDown = e; //鼠标按下事件参数
        this.Cursor = Cursors.Hand; //鼠标按下为手型
        timeout = 0;  //超时时间清0
        timer.Start(); //计时器启动
    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
        this.Cursor = Cursors.Arrow;//鼠标松开为箭头
        timer.Stop(); //计时器停止
    }
    protected virtual void OnMouseLongDown(MouseEventArgs e)
    {
        MessageBox.Show("鼠标被长时间按下!");
    }
}

鼠标按下,启动定时器;

鼠标抬起,停止定时器;

定时器计时后,每秒timeout加1,达到设定时间后触发函数OnMouseLongDown,弹窗。

三、根据区域设置鼠标类型

四个区域,对应四种鼠标图标

protected override void OnMouseMove(MouseEventArgs e)
{
      if (e.X < ClientSize.Width / 2)
          if (e.Y < ClientSize.Height / 2)
              this.Cursor = Cursors.Hand; //左上,鼠标为手形图标
          else
              this.Cursor = Cursors.Help; //左下,鼠标为帮助图标
      else
          if (e.Y < ClientSize.Height / 2)
              this.Cursor = Cursors.WaitCursor; //右上,鼠标为等待图标
          else
              this.Cursor = Cursors.Cross; //右下,鼠标为十字型图标
}

该实例通过修改窗体的Cursor对象实现鼠标样式的改变。

四、创建鼠标图标

protected override void OnLoad(EventArgs e)
{
    Bitmap icon = new Bitmap(100,100);//鼠标图标的大小
    Graphics G = Graphics.FromImage(icon); //创建新的绘图
    G.SmoothingMode = SmoothingMode.AntiAlias; //消除锯齿
    G.Clear(Color.Transparent); //图标背景设为透明
    G.FillPie(new SolidBrush(Color.FromArgb(255, Color.Red)) //绘制扇形作为鼠标指针
    , new Rectangle(0, 0, icon.Width, icon.Height), 30f, 30f);
    G.Dispose(); //释放绘图表面
    this.Cursor = new Cursor(icon.GetHicon()); //设置新的鼠标对象
    icon.Dispose();
}

该实例利用绘制图标自定义鼠标的图标:

1、创建了一个100x100位图对象;

2、在位图中绘制一个扇形形状作为鼠标指针;

3、通过GetHicon方法获取图标句柄,并创建Cursor对象。

五、限制鼠标活动范围

抬起,活动范围全屏;按下,活动范围窗体

protected override void OnMouseDown(MouseEventArgs e)//鼠标按下
{
    this.Text = "鼠标按下,活动范围限制为窗体";
    Cursor.Clip = new Rectangle(this.Location, this.Size);
}
protected override void OnMouseUp(MouseEventArgs e)//鼠标抬起
{
    this.Text = "鼠标抬起,活动范围无限制";
    Rectangle clip = new Rectangle();
    foreach (var item in Screen.AllScreens) //遍历所有屏幕
        clip = Rectangle.Union(clip, item.Bounds);//包含所有屏幕的最小矩形
    Cursor.Clip = clip;
}

该实例通过调用Cursor类的Clip属性(Rectangle类型)来设置鼠标的活动区域。

六、隐藏和显示鼠标指针

按下:隐藏;抬起:显示

protected override void OnMouseDown(MouseEventArgs e)
{
    this.Text = "鼠标按下:隐藏";
    Cursor.Hide(); //隐藏鼠标
}
protected override void OnMouseUp(MouseEventArgs e)
{
    this.Text = "鼠标抬起:显示";
    Cursor.Show(); //显示鼠标
}

该实例通过调用Cursor类的Hide和Show方法来实现鼠标的隐藏与显示。

七、利用鼠标进行书写

bool isMouseDown = false; //声明Bool变量:鼠标是否按下
Point pointMouse = new Point();//鼠标上一时刻的坐标
protected override void OnMouseDown(MouseEventArgs e)
{
    pointMouse = e.Location; //鼠标上一时刻坐标
    isMouseDown = true; //鼠标已经按下
}
protected override void OnMouseMove(MouseEventArgs e)
{
    if (isMouseDown)
    {
        using (Graphics G = this.CreateGraphics()) //获取窗体绘制表面
        {
            G.SmoothingMode = SmoothingMode.AntiAlias; //消除锯齿
            using (Pen pen = new Pen(Color.Red, 3f)) //创建宽度为3像素的黑色画笔
            {
                G.DrawLine(pen, pointMouse, e.Location); //绘制连接两个坐标的线段
                pointMouse = e.Location; //重新设置鼠标坐标
            }
        }
    }
}
protected override void OnMouseUp(MouseEventArgs e)
{
    isMouseDown = false; //鼠标未按下
}
private void button1_Click(object sender, EventArgs e)
{
    this.Invalidate();//清屏
}

该实例通过利用DrawLine方法绘制连续的线段实现书写。

鼠标按下,把实时坐标赋值给pointMouse;

鼠标按下并移动,绘制当前点与pointMouse之间的线段;

鼠标抬起,停止绘制。

八、按下组合键实现窗体全屏

Ctrl+Enter组合键实现全屏缩放

protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.KeyData == (Keys.Control | Keys.Enter)) //判断键盘按键类型
    {
        if (this.WindowState == FormWindowState.Maximized) //判断窗体是否最大化
        {
            this.WindowState = FormWindowState.Normal; //窗体还原
        }
        else if (this.WindowState == FormWindowState.Normal)//判断窗体是否默认大小
        {
            this.WindowState = FormWindowState.Maximized; //窗体最大化
        }
    }
}

该实例通过函数OnKeyDown的KeyEventArgs参数对象的KeyData属性来获取键盘按下的键。通过判断属性值是否为Ctrl+Enter组合键的值来决定是否将窗体最大化。

九、屏蔽Alt+F4组合键

protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.KeyData == (Keys.Alt | Keys.F4))
    {
        MessageBox.Show("Alt+F4组合键已被屏蔽,无法关闭当前窗体!");
        e.Handled = true; //标记为已处理状态
    }
}

如果检测到Alt+F4组合键按下,则将参数对象KeyEventArgs的Handled属性设置为true,标记Alt+F4组合键消息已经被处理过。

十、使用Shift+方向键调整窗体大小

“Shift+方向”组合键,调节窗体大小

protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.KeyData == (Keys.Shift | Keys.Left)) this.Width -= 10; //减小宽度
    if (e.KeyData == (Keys.Shift | Keys.Right)) this.Width += 10; //增加宽度
    if (e.KeyData == (Keys.Shift | Keys.Up)) this.Height -= 10; //减小高度
    if (e.KeyData == (Keys.Shift | Keys.Down)) this.Height += 10; //增加高度
    this.Text = "使用Shift+方向键调整窗体大小" + string.Format("({0},{1})", this.Width, this.Height);
}

十一、录制并回放鼠标移动轨迹

按下Ctrl,记录鼠标轨迹;抬起Ctrl,绘制记录的轨迹

Queue mousePoints = new Queue(); //保存鼠标轨迹
bool isCtrlDown = false; //标记Ctrl键是否按下
protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.KeyData == (Keys.LButton | Keys.ShiftKey | Keys.Control))
    {//判断Ctrl键按下
        isCtrlDown = true; //标记Ctrl键按下
        timer1.Start(); //启动计时器
    }
}
private void timer1_Tick(object sender, EventArgs e)
{
    if (isCtrlDown) mousePoints.Enqueue(Cursor.Position); //添加轨迹
    else if (mousePoints.Count > 0) //鼠标轨迹数大于0
    {
        Cursor.Position = mousePoints.Dequeue(); //从轨迹中取出一个点设置为鼠标当前值
        Graphics g = this.CreateGraphics(); //获取窗体绘图表面                           
        Cursor.Draw(g, new Rectangle(PointToClient(Cursor.Position), Cursor.Size)); //绘制鼠标图标
        g.Dispose();
    }
    else timer1.Stop(); //停止录像或回放
}
protected override void OnKeyUp(KeyEventArgs e)
{
    if (e.KeyData == (Keys.LButton | Keys.ShiftKey)) //判断Ctrl键抬起
        isCtrlDown = false; //标记Ctrl键抬起
}

Ctrl键按下,启动计时器,将鼠标的实时位置添加到鼠标轨迹中。

Ctrl键抬起,将记录的鼠标轨迹逐一出队,调用Cursor的Draw方法来绘制鼠标的动作轨迹,停止计时器。

十二、模拟键盘输入

private void button1_Click(object sender, EventArgs e)//按钮1,输入字符
{
    textBox1.Focus(); //获取文本框焦点
    SendKeys.Send("1234ABCD - ");
    SendKeys.Flush(); //处理所有的Windows消息
}
private void button2_Click(object sender, EventArgs e)//按钮2,退格
{
    textBox1.Focus();
    SendKeys.Send("{BS}");
    SendKeys.Flush();
}
private void button3_Click(object sender, EventArgs e)//按钮3,输入10个A
{
    textBox1.Focus();
    SendKeys.Send("{A 10}");
    SendKeys.Flush();
}
private void button4_Click(object sender, EventArgs e)//按钮4,输入Alt+F4键
{
    MessageBox.Show("窗口即将关闭!");
    SendKeys.Send("%{F4}");
}

通过SendKeys类的Send方法来实现向当前获取焦点的控件发送键盘按键。


参考:《C#程序设计经典300例》

展开阅读全文

页面更新:2024-03-07

标签:计时器   定时器   窗体   坐标   程序设计   函数   轨迹   按钮   屏幕   键盘   参数   事件

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top