从窗口类名获取窗口句柄

发布于 2024-09-03 06:31:18 字数 211 浏览 8 评论 0原文

我试图在我的进程中获取子窗口的窗口句柄,我拥有的唯一信息是窗口类名称。我可以使用任何 win32 函数吗?我是用 C# 做的。

更详细一点:这是一个 Visual Studio 插件,用 C# 编写。所以我的流程是 Visual Studio,它有很多窗口。其中之一有一个窗口类“VsTipWindow”。我不知道该窗口的直接父窗口,我所拥有的只是类名。有什么方法可以让我从中获取窗口句柄吗?

I'm trying to get a window handle on a child window in my process and the only information I have is the window class name. Are there any win32 functions I can use for that? I'm doing this from C#.

A bit more detail: This is a Visual Studio plugin, written in C#. So my process is visual studio, which has lots of windows. One of them has a window class "VsTipWindow". I don't know the immediate parent window of that window, all I have is the class name. Is there any way for me to get the window handle from just that?

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

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

发布评论

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

评论(4

孤独陪着我 2024-09-10 06:31:18

FindWindow 是您想要的 Win32 调用(或 FindWindowEx 如果有多个窗口具有该特定类名,并且 FindWindow 未返回您正在寻找的那个)。

FindWindow is the Win32 call you want for this (or FindWindowEx if there's more than one window with that particular class name, and FindWindow isn't returning the one you're looking for).

岁月如刀 2024-09-10 06:31:18

只是附加信息..
也许知道您可以从一个点获取窗口句柄很有用
窗口自点
http://msdn.microsoft.com/en-us /library/ms633558(VS.85).aspx

just additional information..
maybe it's useful to know that you can get the handle of a window from a point
WindowFromPoint
http://msdn.microsoft.com/en-us/library/ms633558(VS.85).aspx

π浅易 2024-09-10 06:31:18

首先应该注意的是,窗口和窗口类之间不存在一对一的关系,多个窗口可以使用同一类。

我猜你唯一的选择是从顶级 Visual Studio 窗口开始递归调用 EnumChildWindows (或者窗口层次结构中更高的其他窗口,如果你知道它是 VsTipWindow 窗口的祖父母)在 EnumChildWindows 的回调函数中,你可以调用GetClassName 并将该字符串与 VsTipWindow 进行比较,直到找到该窗口。

由于您谈到了未知的父级,我假设您正在寻找一个子窗口,但如果该窗口是顶级窗口,则需要使用 EnumWindows (并且您可能应该使用 GetWindowThreadProcessId 来确保在之后也获得正确的进程你找到一个具有该类名的窗口)

(我确信 .NET 具有与本机 api 执行相同操作的函数,否则你必须 PInvoke)

First off it should be noted that there is not a 1 to 1 relationship between windows and window classes, more than one window could use the same class.

I guess your only option is to call EnumChildWindows recursivly starting with the top level Visual Studio window (Or some other window higher in the window hierarchy if you know one that is a grandparent of the VsTipWindow window) In the callback function from EnumChildWindows you would call GetClassName and compare the string with VsTipWindow until you find the window.

Since you talked about unknown parent I'm assuming that you are after a child window, but if this window is a top level window, you need to use EnumWindows (And you should probably use GetWindowThreadProcessId to make sure you get the correct process also after you find a window with that classname)

(I'm sure .NET has functions that do the same thing as the native api, or you'd have to PInvoke)

睫毛溺水了 2024-09-10 06:31:18

Win32窗口类是控件的通用实现,窗口的句柄是控件的实例。因此,您将拥有具有相同窗口类的多个窗口句柄(例如:编辑)。严格来说,窗口类是指向窗口过程的指针。

像.net(甚至MFC)这样的框架倾向于为所有窗口控件共享很少的窗口类,然后它们将分派到适当的控件(即它们有一个通用窗口过程)。对于 Visual Studio 或 Office 等大型应用程序来说也是如此。

因此,仅通过窗口类来检测特定窗口非常困难。但是,您可以使用 FindWindow 枚举具有特定窗口类的所有窗口,您还将获得可能对您有帮助的窗口文本。使用 GetWindowThreadProcessId 您可以检测窗口是否属于 Visual Studio。

A Win32 window class is the generic implementation of a control, the handle of the windows is the instance of the control. So you will have multiple window handles with the same window class (e.g.: EDIT). Strictly speaking, a window class is the pointer to the window procedure.

Frameworks like .net (and even MFC) tend to share few window class for all the windows controls, and then they will dispatch to the appropriate controls (i.e. they have a single generic window procedure). This is the same also for big applications like Visual Studio or Office.

So this renders very difficult to detect a specific windows just through its window class. However, you can enumerate all the windows that have a specific windows class with FindWindow, you will get also the window text that may help you. Using GetWindowThreadProcessId you can detect if the window is belonging to Visual Studio.

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