열려있는 모든 창을 폴링하려면 EnumWindows()을 사용할 수 있습니다. 이 코드를 컴파일하지는 않았지만 기능에 꽤 가깝습니다.
public class ProcessWindows
{
List<Window> visibleWindows = new List<Window>();
List<IntPtr> allWindows = new List<IntPtr>();
/// <summary>
/// Contains information about visible windows.
/// </summary>
public struct Window
{
public IntPtr Handle { get; set; }
public string Title { get; set; }
}
[DllImport("user32.dll")]
static extern int EnumWindows(EnumWindowsCallback lpEnumFunc, int lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern void GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
delegate bool EnumWindowsCallback(IntPtr hwnd, int lParam);
public ProcessWindows()
{
int returnValue = EnumWindows(Callback, 0);
if (returnValue == 0)
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "EnumWindows() failed");
}
}
private bool Callback(IntPtr hwnd, int lParam)
{
const int WS_BORDER = 0x800000;
const int WS_VISIBLE = 0x10000000;
const int GWL_STYLE = (-16);
// You'll have to figure out which windows you want here...
int visibleWindow = WS_BORDER | WS_VISIBLE;
if ((GetWindowLong(hwnd, GWL_STYLE) & visibleWindow) == visibleWindow)
{
StringBuilder sb = new StringBuilder(100);
GetWindowText(hwnd, sb, sb.Capacity);
this.visibleWindows.Add(new Window()
{
Handle = hwnd,
Title = sb.ToString()
});
}
return true; //continue enumeration
}
public ReadOnlyCollection<Window> GetVisibleWindows()
{
return this.visibleWindows.AsReadOnly();
}
}
}
한 개의 의견으로, 귀하가 WindowTitle.GetText()의 의미를 파악할 수 없었습니다. 나는 어떤 도서관을 가져 오지 못하고 내가 무엇을 놓쳤는 지 모른다. 나는 대신 다른 user32 함수를 사용했다. 정적 extern int GetWindowText (int hWnd, StringBuilder text, int count); – ashurexm
오, 죄송합니다. 그것은 내 응용 프로그램 중 하나의 다른 클래스에 대한 참조입니다. 내 대답을 편집하여 내가하고있는 것을 보여줄거야 ... –
좋아, 고쳤어. GetWindowText에 대한 새로운 pinvoke 문과 WindowTitle.GetText()가 사용 된 호출에 주목하십시오. (근본적으로 어쨌든 당신이 한 일입니다.) –