将鼠标事件发送到另一个窗口,Win 7下的C#

发布于 2024-09-10 09:57:27 字数 285 浏览 1 评论 0原文

有没有办法将鼠标事件发送到 Window 7 中的另一个窗口?
我曾经这样做过:
<代码> [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 公共静态外部无效mouse_event(长dwFlags,长dx,长dy,长cButtons,长dwExtraInfo);

但这在 Win7 中似乎不再起作用了。
有什么想法吗?

谢谢。

Is there a way to send mouse events to another window in Window 7 ?
I used to do this :

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

But this does not seem to work anymore in Win7.
Any ideas ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为一人 2024-09-17 09:57:27

不工作怎么办?

可能相关的一件事(这很难,因为您提供的细节太少)是不允许非提升的(在 UAC 下)应用程序与提升的应用程序进行通信。因此,如果一个应用程序被提升而另一个应用程序没有提升,您会说它“似乎不起作用”。但您无法通过更改您使用的 API 来解决这个问题。

Not work how?

One thing that might be relevant (it's hard because you've given so little detail) is that non-elevated (under UAC) apps are not allowed to communicate with elevated ones. So if one app is elevated and one not, you would say it "doesn't seem to work". But you can't fix that by changing the API you use.

神经暖 2024-09-17 09:57:27

这对您不起作用的最可能原因是您使用的 P/Invoke 签名不正确。您已将参数指定为 long,它在 .NET 中表示 64 位整数。 Win32 API 声明 的参数定义为DWORD,表示32位整数,这会导致堆栈不平衡。将您的签名更改为以下内容,您应该会有更好的运气。

[DllImport("user32.dll")]
public static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 cButtons, Int32 dwExtraInfo);

另外,您应该考虑凯特的观点,一旦签名修复,这也可能会对您的结果产生影响。

The most likely reason that this is not working for you is the fact that the P/Invoke signature that you are using is incorrect. You have specified the arguments as long which in .NET represents a 64bit integer. The Win32 API decleration has the arguments defined as DWORD, which represents 32 bit integers, this will result in a stack imbalance. Change your signature to the following and you should have better luck.

[DllImport("user32.dll")]
public static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 cButtons, Int32 dwExtraInfo);

Also, you should consider Kate's point and this might also have a bearing on your results once the signature is fixed.

寒冷纷飞旳雪 2024-09-17 09:57:27

user32.dll 中的 SendMessage:就可以了

您可能还需要 FindWindowWM 常量

pinvoke.net 非常适合此类内容。

SendMessage in user32.dll: would do the trick.

You'll also probably need FindWindow and the WM constants.

pinvoke.net is good for this sort of stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文