C# 为什么要得到;类;姓名和真实姓名;获取;窗口&xAD;类是否返回相同的值?
目标 我在Unity3D(C#)中开发了一个键盘,希望当用户单击“编辑”类型控件(如地址栏或输入字段)时,它能弹出。因此,我需要检测何时单击“编辑”控件 我尝试过的 目前,我使用并侦听事件来获取获取焦点的对象的句柄。在那之后,我用来查看聚焦对象是否是一个“编辑”控件,单击该控件时会显示一个闪烁的插入符号。但是,以Google Chrome为例,无论单击地址栏还是页面的纯文本,我都会得到Chrome_WidgetWin_1。在做了一些谷歌搜索之后,我发现这篇博文说可以得到基类,我认为它会像“编辑”或“组合框”一样列出。事情进展得不太顺利。我尝试使用Chrome_WidgetWin_1,但仍然得到相同的结果 问题 为什么Get-Class-Name和Real-Get-Window-Class返回相同的值?我应该如何使Real-Get-Window-Class返回基类 代码C# 为什么要得到;类;姓名和真实姓名;获取;窗口&xAD;类是否返回相同的值?,c#,.net,windows,pinvoke,editcontrol,C#,.net,Windows,Pinvoke,Editcontrol,目标 我在Unity3D(C#)中开发了一个键盘,希望当用户单击“编辑”类型控件(如地址栏或输入字段)时,它能弹出。因此,我需要检测何时单击“编辑”控件 我尝试过的 目前,我使用并侦听事件来获取获取焦点的对象的句柄。在那之后,我用来查看聚焦对象是否是一个“编辑”控件,单击该控件时会显示一个闪烁的插入符号。但是,以Google Chrome为例,无论单击地址栏还是页面的纯文本,我都会得到Chrome_WidgetWin_1。在做了一些谷歌搜索之后,我发现这篇博文说可以得到基类,我认为它会像“编辑”
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWinEventHook(int eventMin, int eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, int dwflags);
[DllImport("user32.dll", SetLastError = true)]
private static extern int UnhookWinEvent(IntPtr hWinEventHook);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RealGetWindowClass(IntPtr hwnd, [Out] StringBuilder pszType, uint cchType);
private delegate void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
private const int WINEVENT_SKIPOWNPROCESS = 2;
private IntPtr windowEventHook;
private const int EVENT_OBJECT_FOCUS = 0x8005;
private void Start()
{
if (windowEventHook == IntPtr.Zero)
{
windowEventHook = SetWinEventHook(
EVENT_OBJECT_FOCUS,
EVENT_OBJECT_FOCUS,
IntPtr.Zero,
WindowEventCallback,
0,
0,
WINEVENT_SKIPOWNPROCESS);
if (windowEventHook == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
}
private void OnDestroy()
{
UnhookWinEvent(windowEventHook);
}
private void WindowEventCallback(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
UnityEngine.Debug.Log($"[EventType]: {(EventEnum)eventType} [Class Name]: {GetClassName(hWnd)} [Real Class]: {RealGetWindowClassM(hWnd)}");
// Will print out the same log whether I click an address bar or plain text.
// [EventType]: EVENT_OBJECT_FOCUS [Class Name]: Chrome_WidgetWin_1 [Real Class]: Chrome_WidgetWin_1
}
private string GetClassName(IntPtr hWnd)
{
StringBuilder className = new StringBuilder(256);
GetClassName(hWnd, className, className.Capacity);
return className.ToString();
}
private string RealGetWindowClassM(IntPtr hWnd)
{
StringBuilder className = new StringBuilder(256);
RealGetWindowClass(hWnd, className, (UInt32)className.Capacity);
return className.ToString();
}
这只是告诉你真相,无论是哪种方式,浏览器(如Chrome)都不使用编辑或组合框控件。他们绘制像素使其看起来像这样一个控件。请使用64位版本的Spy++或WinSpy亲自查看。谢谢您提供的信息!我会试试Spy++看看。您知道有没有其他方法可以在编辑控件(如文本框或输入字段)上全局检测焦点变化?