隐藏鼠标光标特定时间

时间:2017-06-07 09:00:38

标签: c# .net winforms cursor

public partial class Form3 : Form
{
    private readonly KeyboardHookListener m_KeyboardHookManager;
    private readonly MouseHookListener m_MouseHookManager;

    [DllImport("user32")]
    private static extern Int32 ClipCursor(RECT lpRect);

    [DllImport("user32")]
    private static extern Int32 ShowCursor(Int32 bShow);

    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out POINT lpPoint);

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int X;
        public int Y;

        public static implicit operator Point(POINT point)
        {
            return new Point(point.X, point.Y);
        }
    }

    public static Point GetCursorPosition()
    {
        POINT lpPoint;
        GetCursorPos(out lpPoint);
        //bool success = User32.GetCursorPos(out lpPoint);
        // if (!success)

        return lpPoint;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public Int32 Left;
        public Int32 Top;
        public Int32 Right;
        public Int32 Bottom;
    }

    public Form3()
    {
        InitializeComponent();

        m_KeyboardHookManager = new KeyboardHookListener(new GlobalHooker());
        m_KeyboardHookManager.Enabled = true;
        m_KeyboardHookManager.KeyDown += HookManager_KeyDown;
        m_KeyboardHookManager.KeyUp += HookManager_KeyUp;

        m_MouseHookManager = new MouseHookListener(new GlobalHooker());
        m_MouseHookManager.Enabled = true;
        m_MouseHookManager.MouseDown += HookManager_MouseDown;
        m_MouseHookManager.MouseUp += HookManager_MouseUp;
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        //ShowCursor(0);
        //MoveCursor();
    }
    private void HookManager_KeyDown(object sender, KeyEventArgs e)
    {
        label1.Text = e.KeyData.ToString() + " Pressed";
    }

    private void HookManager_KeyUp(object sender, KeyEventArgs e)
    {
        label1.Text = e.KeyData.ToString() + " Released";
    }

    private void HookManager_MouseUp(object sender, MouseEventArgs e)
    {
        label1.Text = e.Button.ToString() + " Released";
    }

    private void HookManager_MouseDown(object sender, MouseEventArgs e)
    {
        label1.Text = e.Button.ToString() + " Pressed";
        ShowCursor(0);
        MoveCursor();
        System.Threading.Thread.Sleep(5000);//5 sec   
        RestoreCursor(this);
        ShowCursor(1);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //RestoreCursor(this);
        //ShowCursor(1);
    }

    private void Form3_MouseMove(object sender, MouseEventArgs e)
    {
        //MoveCursor();
    }

    private RECT MouseTrap;
    public void RestoreCursor(System.Windows.Forms.Form ThisForm)
    {
        var _with1 = MouseTrap;
        _with1.Top = 0;
        _with1.Left = 0;
        _with1.Right = Screen.PrimaryScreen.Bounds.Width;
        _with1.Bottom = Screen.PrimaryScreen.Bounds.Height;

        ClipCursor(_with1);
    }

    private void MoveCursor()
    {
        // set the Current cursor, move the cursor's Position,
        // and set its clipping rectangle to the form. 
        this.Cursor = new Cursor(Cursor.Current.Handle);
        Cursor.Position = new Point(this.Left, this.Top);
        Cursor.Clip = new Rectangle(this.Left, this.Top, this.Width, this.Height);            
    }
}

我正在开发一个示例应用程序我的要求是当用户点击桌面上的任何位置时,例如单击Windows“开始”按钮然后隐藏光标2秒,然后在2秒后在同一位置显示光标。

为此,我编写了上面的代码并使用Thread.Sleep(2000)进行等待,但光标位置恢复到Form的左侧。

https://stackoverflow.com/questions/16781948/how-can-i-hook-globaly-all-the-keys-and-all-the-mouse-buttons/16782294

GlobalMouseHookDLL

1 个答案:

答案 0 :(得分:0)

您可以使用System.Windows.Forms.Timer来实现这一目标。

在表单上声明Timer

public partial class Form3 : Form
{
    private Timer timer = new Timer();

将其设置为Form_Load

timer.Enabled = false;
timer.Tick += Timer_Tick;
timer.Interval = 5000;

当用户移动鼠标时,而不是Thread.Sleep激活计时器。当计时器滴答时(当指定5000毫秒通过时),将执行Timer_Tick方法。在其中,显示指针。

private void HookManager_MouseDown(object sender, MouseEventArgs e)
{
    label1.Text = e.Button.ToString() + " Pressed";
    ShowCursor(0);
    MoveCursor();
    timer.Start();
}

private void Timer_Tick(object sender, EventArgs e)
{

    RestoreCursor(this);
    ShowCursor(1);
    timer.Stop();
}