简体   繁体   English

在Visual Studio C#中控制外部窗口

[英]Control external window in visual studio c#

[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. [编辑]我打算在VLC播放器上构建一些外部附件,为此,我正在使用FindWindow类型的函数,以便从程序中控制VLC播放器。

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? FindWindowEx是最佳实践,还是FindWindow更好,还是为此提供了一些开发的库? Any tips for using FindWindowEx like it should be used? 应该使用任何使用FindWindowEx技巧吗?

[edit] Also, in many cases the titles may vary along different languages, [edit]而且,在许多情况下,标题可能会随着语言的不同而变化,
so hwnd = FindWindow(null, "Calculator") may not work if the title is not "Calculator" but "Simulateur" [French] or "Rechner" [German]. 因此hwnd = FindWindow(null, "Calculator")如果标题不是“ Calculator”而是“ Simulateur” [French]或“ Rechner” [German] hwnd = FindWindow(null, "Calculator")可能不起作用。 How can I make sure it finds the window, no matter what is the OS language? 无论OS语言是什么,如何确保它找到窗口?

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? 另一个问题是-是否有可能将我的应用程序锚定在VLC播放器旁边,以使vlc Player窗口的任何移动也会移动我的应用程序? I couldn't find any appropriate function for that. 我找不到适合的任何功能。

You didnt describe a sample of that you are intended to do, but .NET automation may help you. 您没有描述要执行的示例,但是.NET自动化可能会为您提供帮助。 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. [编辑]好吧,要获得独立于标题值的查找窗口的可能性,您应该仅使用FindWindEx函数,并依赖于外部窗口的className。 Class is locale independent. 类是与语言环境无关的。 UI automation in my opinion devided into two classes: 我认为UI自动化分为两类:

  • window based. 基于窗口。 This approach suppose that you are use FindWindow and SendMessage to manipulation of external windows 此方法假定您使用FindWindow和SendMessage来操纵外部窗口
  • coordinate based. 基于坐标。 In this approach you use only coordinates of controls on external window. 在这种方法中,您仅使用外部窗口上控件的坐标。 So you are sending messages to top level window with specific coordinates. 因此,您正在将消息发送到具有特定坐标的顶级窗口。

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. 因此,我建议您在可以应用的任何地方使用FindWindowEx和SenMessage。

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. 第一种简单的方法是在应用内创建计时器并检查目标应用的坐标并相应地移动您的ap窗口,但是这种方法似乎是解决方法。 Second and more complex way is to create Hook and inject it in target process. 第二种也是更复杂的方法是创建Hook并将其注入目标进程。 You can set hook with SetWindowHookEx function. 您可以使用SetWindowHookEx函数设置挂钩。 Also read about mouse hook in C# . 另请阅读有关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. 还有一个提示,以防万一您将写一个钩子,不要在所有进程中注入.NET钩子,如果它们为不同的平台版本而不是您的钩子编写,则这可能会在托管应用程序中出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM