C#窗体(二)—鼠标长时间按下事件

鼠标长时间按下事件

创建VS窗体应用程序具体代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MouseLong
{
    public partial class Form1 : Form
    {
        Timer timer = new Timer();//计时器

        int timeout = 0;//超时时间

        MouseEventArgs mouseDown;//鼠标长时间按下时间参数

        public Form1()
        {
            InitializeComponent();
            timer.Interval = 1000;//计时器时间间隔
            timer.Tick += new EventHandler(timer_Tick);//计时器时间发生函数
        }
        void timer_Tick(object sender, EventArgs e)
        {
            if (++timeout == 3)
            {
                OnMouseLongDown(this.mouseDown);//调用鼠标长时间按下事件
            }
        }
        //鼠标按下事件
        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.mouseDown = e;//鼠标按下事件参数

            timeout = 0;//超时时间设置为0

            timer.Start();//启动计时器
        }
        //鼠标抬起事件
        protected override void OnMouseUp(MouseEventArgs e)
        {
            timer.Stop();//停止计时器
        }

        protected virtual void OnMouseLongDown(MouseEventArgs e) {
            MessageBox.Show("鼠标被长时间按下");
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

当鼠标按下后触发了鼠标按下事件函数OnMouseDown,在该函数中启动计时器。当鼠标抬起时触发了鼠标抬起事件函数OnMouseUp,在该函数中停止计时器。在计时器事件函数中进行计时,每隔1秒钟将timeout加1,如果鼠标按下达到 3 秒钟,则在计时器事件函数中触发鼠标长时间按下事件函数OnMouseLongDown。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用`SetWindowsHookEx`函数来监听鼠标事件。下面是一个示例代码,演示如何在C#监听鼠标在其他窗体的点击事件: ```csharp using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows.Forms; public class MouseListener { private const int WH_MOUSE_LL = 14; private const int WM_LBUTTONDOWN = 0x0201; private static LowLevelMouseProc _proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; public static void Main() { _hookID = SetHook(_proc); Application.Run(); UnhookWindowsHookEx(_hookID); } private static IntPtr SetHook(LowLevelMouseProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0); } } private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam); private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && wParam == (IntPtr)WM_LBUTTONDOWN) { int x = Marshal.ReadInt32(lParam); // 获取鼠标点击的X坐标 int y = Marshal.ReadInt32(lParam + 4); // 获取鼠标点击的Y坐标 Console.WriteLine($"Left button down at ({x}, {y})"); } return CallNextHookEx(_hookID, nCode, wParam, lParam); } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName); } ``` 这段代码会在控制台输出鼠标左键点击的坐标。您可以根据需要修改`HookCallback`方法,处理其他鼠标事件。请注意,此代码需要以管理员身份运行,以便安装全局钩子。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值