2010-11-18 1 views
0

기본적으로 C#에서는 특수 매크로 플레이어/레코더를 작성하고 있습니다. 내가 할 수있는 한가지는 팝업 창 (매크로 저장 대화 상자와 같은 것)을 기다려서 매크로 입력을 계속 재생하도록 선택할 수 있습니다. 이상적으로, 열려있는 창을 폴링하고 해당 제목을 검색하여 일치하는 창 제목을 찾을 수 있기를 원합니다. 대화 상자가 새 프로세스로 표시되지 않기 때문에 분명히 Processes.GetProcesses()를 사용할 수 없습니다.C# 팝업 창을 기다리고 입력을 위해 선택하는 방법

열린 창과 그 제목을 보려면 어떻게해야합니까?

답변

1

열려있는 모든 창을 폴링하려면 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(); 
    } 
} 
} 
+0

한 개의 의견으로, 귀하가 WindowTitle.GetText()의 의미를 파악할 수 없었습니다. 나는 어떤 도서관을 가져 오지 못하고 내가 무엇을 놓쳤는 지 모른다. 나는 대신 다른 user32 함수를 사용했다. 정적 extern int GetWindowText (int hWnd, StringBuilder text, int count); – ashurexm

+0

오, 죄송합니다. 그것은 내 응용 프로그램 중 하나의 다른 클래스에 대한 참조입니다. 내 대답을 편집하여 내가하고있는 것을 보여줄거야 ... –

+0

좋아, 고쳤어. GetWindowText에 대한 새로운 pinvoke 문과 WindowTitle.GetText()가 사용 된 호출에 주목하십시오. (근본적으로 어쨌든 당신이 한 일입니다.) –