C# 挥动鼠标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 12:59:42  来源:igfitidea点击:

Wiggling the mouse

提问by Bruce the Hoon

OK. This is a bit of a vanity app, but I had a situation today at work where I was in a training class and the machine was set to lock every 10 minutes. Well, if the trainers got excited about talking - as opposed to changing slides - the machine would lock up.

好的。这是一个有点虚荣的应用程序,但我今天在工作中遇到了一个情况,我在培训班上,机器被设置为每 10 分钟锁定一次。好吧,如果培训师对谈话感到兴奋——而不是更换滑梯——机器就会锁死。

I'd like to write a teeny app that has nothing but a taskbar icon that does nothing but move the mouse by 1 pixel every 4 minutes.

我想编写一个很小的应用程序,它只有一个任务栏图标,除了每 4 分钟将鼠标移动 1 个像素外什么都不做。

I can do that in 3 ways with Delphi (my strong language) but I'm moving to C# for work and I'd like to know the path of least resistance there.

我可以用 Delphi(我的强语言)以 3 种方式做到这一点,但我正在转向 C# 工作,我想知道那里阻力最小的路径。

采纳答案by lubos hasko

for C# 3.5

对于 C# 3.5

without notifyicon therefore you will need to terminate this application in task manager manually

没有通知图标,因此您需要在任务管理器中手动终止此应用程序

using System;
using System.Drawing;
using System.Windows.Forms;

static class Program
{
    static void Main()
    {
        Timer timer = new Timer();
        // timer.Interval = 4 minutes
        timer.Interval = (int)(TimeSpan.TicksPerMinute * 4 / TimeSpan.TicksPerMillisecond);
        timer.Tick += (sender, args) => { Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y + 1); };
        timer.Start();
        Application.Run();
    }
}

回答by DylanJ

Something like this should work (though, you willwant to change the interval).

像这样的东西应该工作(虽然,你想改变的时间间隔)。

public Form1()
{
    InitializeComponent();
    Timer Every4Minutes = new Timer();
    Every4Minutes.Interval = 10;
    Every4Minutes.Tick += new EventHandler(MoveNow);
    Every4Minutes.Start();
}

void MoveNow(object sender, EventArgs e)
{
    Cursor.Position = new Point(Cursor.Position.X - 1, Cursor.Position.Y - 1);
}

回答by Zooba

The "correct" way to do this is to respond to the WM_SYSCOMMAND message. In C# this looks something like this:

执行此操作的“正确”方法是响应 WM_SYSCOMMAND 消息。在 C# 中,这看起来像这样:

protected override void WndProc(ref Message m)
{
    // Abort screensaver and monitor power-down
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_MONITOR_POWER = 0xF170;
    const int SC_SCREENSAVE = 0xF140;
    int WParam = (m.WParam.ToInt32() & 0xFFF0);

    if (m.Msg == WM_SYSCOMMAND &&
        (WParam == SC_MONITOR_POWER || WParam == SC_SCREENSAVE)) return;

    base.WndProc(ref m);
}

According to MSDN, if the screensaver password is enabled by policy on Vista or above, this won't work. Presumably programmatically moving the mouse is also ignored, though I have not tested this.

根据MSDN,如果在 Vista 或更高版本上通过策略启用了屏幕保护程序密码,这将不起作用。据推测,以编程方式移动鼠标也被忽略,尽管我没有对此进行测试。

回答by Heywoody

When I work from home, I do this by tying the mouse cord to a desktop fan which oscillates left to right. It keeps the mouse moving and keeps the workstation from going to sleep.

当我在家工作时,我将鼠标线系在一个左右摆动的桌面风扇上。它使鼠标保持移动并防止工作站进入睡眠状态。