在 Visual Studio C# 中控制外部窗口
[编辑] 我打算为 VLC 播放器构建一些外部插件,因此我使用 FindWindow 类型的函数来从我的程序控制 VLC 播放器。
从您自己的窗口控制外部窗口的最佳编程实践是什么?FindWindowEx
是最佳实践,还是 FindWindow
更好,或者是否有一些开发的库?有什么使用 FindWindowEx
的技巧吗?
[编辑] 此外,在许多情况下,不同语言的标题可能会有所不同,
因此,如果标题不是“Calculator”而是“Simulateur”[法语] 或“Rechner”[德语],则 hwnd = FindWindow(null, "Calculator")
可能不起作用。无论操作系统语言是什么,如何确保它找到窗口?
另一个附带问题 - 是否有可能将我的应用程序锚定在 VLC 播放器旁边,这样 vlc 播放器窗口的任何移动也会移动我的应用程序?我找不到任何合适的功能。
[edit] I intend to build some external add on to VLC player, so for that I'm using the FindWindow
type of functions in order to control the VLC player from my program.
What's the best programming practice to control external window from your own window?
Is the FindWindowEx
the best practice, or FindWindow
better for that, or is there some developed lib for it? Any tips for using FindWindowEx
like it should be used?
[edit] Also, in many cases the titles may vary along different languages,
so hwnd = FindWindow(null, "Calculator")
may not work if the title is not "Calculator" but "Simulateur" [French] or "Rechner" [German]. How can I make sure it finds the window, no matter what is the OS language?
And another side question - is there any possibility to anchor my application next to the VLC player, in way that any movement of the vlc player window will also move my application? I couldn't find any appropriate function for that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有描述您打算做的示例,但 .NET 自动化可能会帮助您。请查看这篇文章。另请注意此
[编辑]
好吧,为了获得查找窗口独立于标题值的可能性,您应该仅使用 FindWindEx 函数并依赖外部窗口的 className。类与语言环境无关。
我认为 UI 自动化分为两类:
第二种方法更简单,但完全不可靠。它将一直工作,直到目标应用程序更改控制布局。因此,我建议您在任何可以应用的地方使用 FindWindowEx 和 SenMessage。
关于将您的应用程序与目标应用程序锚定。是的,这是可能的,您有两种方法可以做到这一点:
第一个也是简单的方法是在您的应用程序内创建计时器并检查目标应用程序的坐标并相应地移动您的应用程序窗口,但这种方法似乎是解决方法。
第二种也是更复杂的方法是创建 Hook 并将其注入目标进程中。您可以使用 SetWindowHookEx 设置挂钩功能。另请阅读C# 中的鼠标钩子。如果您要编写一个钩子,请不要在所有进程中注入 .NET 钩子,如果托管应用程序是为不同的平台版本而不是您的钩子编写的,这可能会在托管应用程序中出现问题。
You didnt describe a sample of that you are intended to do, but .NET automation may help you. Check out this article. Also pay attention to this
[EDIT]
Well, To obtain possibility of find window to be independed of title value you should use only FindWindEx function and rely to className of external window. Class is locale independent.
UI automation in my opinion devided into two classes:
Second approach is more easy but completly unreliable. It will work until target application change control layout. So I will suggest you to use FindWindowEx and SenMessage wherever it can be applyed.
About anchoring your application with target app. Yes it possible, and you have two ways to do that:
first and easy way is to create timer inside your app and check coordinates of target app and move your's ap window accordingly, but such approach seems to be workaround.
Second and more complex way is to create Hook and inject it in target process. You can set hook with SetWindowHookEx function. Also read about mouse hook in C#. And one tip for you in case you will write a hook, dont inject .NET hook in all processes, this may course problems in managed apps if they writen for different platform versions rather your hook.