从窗口类名获取窗口句柄 [英] Get window handle from window class name

查看:84
本文介绍了从窗口类名获取窗口句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的进程中获得一个子窗口的窗口句柄,我拥有的唯一信息是窗口类名.有没有我可以使用的 win32 函数?我是从 C# 做这个的.

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#.

更多细节:这是一个用 C# 编写的 Visual Studio 插件.所以我的过程是visual studio,它有很多窗口.其中之一有一个窗口类VsTipWindow".我不知道那个窗口的直接父窗口,我只有类名.有什么办法可以让我从中获取窗口句柄吗?

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?

推荐答案

首先要注意的是,窗口和窗口类之间没有一对一的关系,多个窗口可以使用同一个类.

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.

>

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

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.

既然你谈到了未知的父窗口,我假设你在一个子窗口之后,但是如果这个窗口是一个顶级窗口,你需要使用 EnumWindows (你可能应该使用 GetWindowThreadProcessId 来确保你得到正确的在找到具有该类名的窗口后也进行处理)

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)

(我确定 .NET 具有与本机 api 相同的功能,否则您必须 PInvoke)

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

这篇关于从窗口类名获取窗口句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆