获取wincontrol的类型 - c#

时间:2017-05-31 12:09:41

标签: c# windows api winapi handle

我需要使用鼠标图标位置在窗口中获取wincontrol的类型。 现在我可以识别类而不是类型。我怎么能得到它?

enter image description here

[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);

private void test()
{
Point pt = MousePosition;
IntPtr w = WindowFromPoint(pt.X, pt.Y);

MessageBox.Show(GetWinClass(w));


}

public static string GetWinClass(IntPtr hwnd)
{
if (hwnd == IntPtr.Zero)
return null;
StringBuilder classname = new StringBuilder(100);
IntPtr result = GetClassName(hwnd, classname, classname.Capacity);
if (result != IntPtr.Zero)
return classname.ToString();
return null;

}

2 个答案:

答案 0 :(得分:0)

没有明确定义的"窗口类型"在Windows中。如您所见,您可以从特定点获取HWND,然后从那里检索类名。但是,这就是你可以做到的所有事情。基于类名的窗口类型的任何决定都是纯粹推测性的,通常依赖于将众所周知的类名映射到任意类型定义。

一个开始的地方是MSDN文档,例如:https://msdn.microsoft.com/en-us/library/windows/desktop/ms633574(v=vs.85).aspx。然而,这并不包括许多使用第三方IDE编写的自定义应用程序(例如Delphi具有带有类名' TwwPopupGrid'的弹出式组合框)。

答案 1 :(得分:-1)

MenuItem是来自WinForms技术的类的实例。

Windows不知道哪个类用于在屏幕上放置文本,实际上它不知道使用了哪种技术(WinFormsWPFMFC ,. ..)所以你不能知道光标下的视觉元素的类型(类)。

您可以使用main()方法创建视觉元素,并将所有必需的方法放入其中,以便您拥有哪种类型?