C# 获取没有标题的窗口的句柄。。(C)
我们如何获得一个没有标题的窗口的句柄?有没有办法枚举桌面上的所有窗口并过滤没有标题的窗口?在我的例子中,只有一个窗口并得到了它的句柄。。或者通过指定其他属性,例如具有特定按钮或列表框等的窗口。类似的内容应该可以工作,而不是测试C# 获取没有标题的窗口的句柄。。(C),c#,handle,C#,Handle,我们如何获得一个没有标题的窗口的句柄?有没有办法枚举桌面上的所有窗口并过滤没有标题的窗口?在我的例子中,只有一个窗口并得到了它的句柄。。或者通过指定其他属性,例如具有特定按钮或列表框等的窗口。类似的内容应该可以工作,而不是测试 像这样的东西应该有用,而不是测试 WindowFromPoint返回句柄,如果光标下没有窗口,则返回null。这是可以使用的,还是您正在尝试自动化该过程 WindowFromPoint返回句柄,如果光标下没有窗口,则返回null。是否可以使用此功能,或者您正在尝试自动执行
像这样的东西应该有用,而不是测试 WindowFromPoint返回句柄,如果光标下没有窗口,则返回null。这是可以使用的,还是您正在尝试自动化该过程
WindowFromPoint返回句柄,如果光标下没有窗口,则返回null。是否可以使用此功能,或者您正在尝试自动执行此过程?请查看EnumChildWindows函数 我认为,如果您将主窗口(即桌面)的指针传递到此函数,您将能够获得所有窗口及其子窗口的列表
与FindWindow结合使用,一旦找到所需的子控件,就可以获得所需窗口的句柄。查看EnumChildWindows函数 我认为,如果您将主窗口(即桌面)的指针传递到此函数,您将能够获得所有窗口及其子窗口的列表 与FindWindow结合使用,一旦找到所需的子控件,就可以获得所需窗口的句柄。您可以找到一个库来处理托管代码中的windows API内容。 下载dll并在您的项目中引用它,从那里很容易 获取您想要的任何窗口的任何信息
void ForAllSystemWindows()
{
foreach (SystemWindow window in SystemWindow.AllToplevelWindows)
{
if (window.Title == string.Empty)
{
//Found window without title
//Get window handle
IntPtr windowhandle = window.HWnd;
//Do other stuff ..
}
}
}
您可以找到一个库来处理托管代码中的windows API内容。
下载dll并在您的项目中引用它,从那里很容易
获取您想要的任何窗口的任何信息
void ForAllSystemWindows()
{
foreach (SystemWindow window in SystemWindow.AllToplevelWindows)
{
if (window.Title == string.Empty)
{
//Found window without title
//Get window handle
IntPtr windowhandle = window.HWnd;
//Do other stuff ..
}
}
}
这应该做到:
...
using System.Runtime.InteropServices;
using System.Diagnostics;
...
public class foo()
{
...
[DllImport ("user32")]
internal static extern int GetWindowText (int hWnd, String text, int nMaxCount);
[DllImport ("user32.dll")]
public static extern int GetWindowTextLength (int hWnd);
[DllImport ("user32.dll")]
public static extern int FindWindow (String text, String class_name);
[DllImport ("user32.dll")]
public static extern int FindWindowEx (int parent, int start, String class_name);
[DllImport ("user32.dll")]
public static extern int GetWindow (int parent, uint cmd);
public List<int> FindTitlelessWindows()
{
List<int> titleless = new List<int> ();
Process [] procs = Process.GetProcesses ();
IntPtr hWnd;
foreach (Process proc in procs)
{
hWnd = proc.MainWindowHandle;
if (hWnd != IntPtr.Zero)
{
TraverseHierarchy (hWnd.ToInt32 (), 0, titleless);
}
}
foreach (int i in titleless)
{
System.Console.WriteLine (i);
}
return titleless;
}
public void TraverseHierarchy (int parent, int child, List<int> titleless)
{
String text = "";
GetWindowText (parent, text, GetWindowTextLength (parent));
if (String.IsNullOrEmpty (text))
{
titleless.Add (parent);
}
TraverseChildern (parent, titleless);
TraversePeers (parent, child, titleless);
}
public void TraverseChildern(int handle, List<int> titleless)
{
// First traverse child windows
const uint GW_CHILD = 0x05;
int child = GetWindow (handle, GW_CHILD);
if (0 != child)
{
TraverseHierarchy (child, 0, titleless);
}
}
public void TraversePeers(int parent, int start, List<int> titleless)
{
// Next traverse peers
int peer = FindWindowEx(parent, start, "");
if (0 != peer)
{
TraverseHierarchy (parent, peer, titleless);
}
}
}
这应该做到:
...
using System.Runtime.InteropServices;
using System.Diagnostics;
...
public class foo()
{
...
[DllImport ("user32")]
internal static extern int GetWindowText (int hWnd, String text, int nMaxCount);
[DllImport ("user32.dll")]
public static extern int GetWindowTextLength (int hWnd);
[DllImport ("user32.dll")]
public static extern int FindWindow (String text, String class_name);
[DllImport ("user32.dll")]
public static extern int FindWindowEx (int parent, int start, String class_name);
[DllImport ("user32.dll")]
public static extern int GetWindow (int parent, uint cmd);
public List<int> FindTitlelessWindows()
{
List<int> titleless = new List<int> ();
Process [] procs = Process.GetProcesses ();
IntPtr hWnd;
foreach (Process proc in procs)
{
hWnd = proc.MainWindowHandle;
if (hWnd != IntPtr.Zero)
{
TraverseHierarchy (hWnd.ToInt32 (), 0, titleless);
}
}
foreach (int i in titleless)
{
System.Console.WriteLine (i);
}
return titleless;
}
public void TraverseHierarchy (int parent, int child, List<int> titleless)
{
String text = "";
GetWindowText (parent, text, GetWindowTextLength (parent));
if (String.IsNullOrEmpty (text))
{
titleless.Add (parent);
}
TraverseChildern (parent, titleless);
TraversePeers (parent, child, titleless);
}
public void TraverseChildern(int handle, List<int> titleless)
{
// First traverse child windows
const uint GW_CHILD = 0x05;
int child = GetWindow (handle, GW_CHILD);
if (0 != child)
{
TraverseHierarchy (child, 0, titleless);
}
}
public void TraversePeers(int parent, int start, List<int> titleless)
{
// Next traverse peers
int peer = FindWindowEx(parent, start, "");
if (0 != peer)
{
TraverseHierarchy (parent, peer, titleless);
}
}
}
任何阻止您将空字符串传递到FindWindow的内容?旁注:您不能假定您的窗口是唯一一个没有标题的窗口。任何阻止您将空字符串传递到FindWindow的内容?旁注:您不能假定您的窗口是唯一一个没有标题的窗口。这将只查找没有标题的顶级窗口。您仍然需要遍历所有子窗口和对等窗口。您假设OP希望包含子窗口,但可能不希望。正如您所暗示的,并不是每个顶级窗口都是流程的主窗口……这只会找到没有标题的顶级窗口。您仍然需要遍历所有子窗口和对等窗口。您假设OP希望包含子窗口,但可能不希望。正如您所暗示的,并非每个顶级窗口都是流程的主窗口……如果顶级窗口足够,则更简单,但还要检查该窗口是否是特定程序的一部分,例如,您的:foreach process proc in procs{hWnd=proc.MainWindowHandle;如果hWnd!=IntPtr.Zero&&proc.MainWindowTitle==&proc.ProcessName.StartsWithmy进程名{titleleless.AddhWnd.ToInt32;}如果顶部窗口足够简单,还可以检查窗口是否是特定程序的一部分,例如您的:foreach Process proc in procs{hWnd=proc.MainWindowHandle;if hWnd!=IntPtr.Zero&&proc.MainWindowTitle==&proc.ProcessName.StartsWithmy Process name{titleleless.AddhWnd.ToInt32;}