문제
제목이없는 창의 손잡이를 어떻게 얻을 수 있습니까? 데스크탑의 모든 창을 열거하고 제목이없는 창을 필터링하는 방법이 있습니까 (내 경우에는 하나만 있습니다). 특정 버튼 또는 목록 상자 등 ...
해결책
이렇게해야합니다.
...
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);
}
}
}
다른 팁
EnumchildWindows 기능을 살펴보십시오.
메인 창 (예 : 데스크탑)의 포인터를이 기능으로 전달하면 모든 Windows와 해당 어린이 창의 목록을 얻을 수 있다고 생각합니다.
FindWindow와 함께 예상되는 아동 통제를 찾으면 원하는 창으로 핸들을 가져올 수 있어야합니다.
이와 같은 것이 효과가 있어야합니다. 검증되지 않은
[DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);
....
Process[] procs = Process.GetProcesses();
IntPtr hWnd;
foreach(Process proc in procs)
{
hWnd = proc.MainWindowHandle;
if (hWnd != IntPtr.Zero)
{
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(hWnd, sb, length);
if (String.IsNullOrEmpty(sb.ToString())
{
// we have a window with no title!
}
}
}
http://msdn.microsoft.com/en-us/library/ms633558(vs.85).aspx
Windowfrompoint는 핸들을 반환하거나 커서 아래에 창이없는 경우 NULL을 반환합니다. 이것이 사용될 수 있습니까, 아니면 프로세스를 자동화하려고합니까?
여기 관리 코드에서 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 ..
}
}
}
제휴하지 않습니다 StackOverflow