0

I am basically writing a specialized macro player/recorder in C#. One thing I need to be able to do is wait for a pop up window (something like a Save As... dialog box) that I can then select to continue playing macro input into. Ideally, I would like to be able to poll for open windows and search through their titles for a matching window title. Obviously I can't use Processes.GetProcesses() because a dialog most likely will not show up as a new process.

Where do I look to get open windows and their titles?

ashurexm
  • 6,209
  • 3
  • 45
  • 69

2 Answers2

2

If you want to poll all open windows, you might use EnumWindows(). I didn't compile this code, but it should be pretty close to functional.

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();
    }
}
}
Nick Spreitzer
  • 10,242
  • 4
  • 35
  • 58
  • One comment, I wasn't able to figure out what you meant by WindowTitle.GetText(). I don't know what library I'm not importing or what I'm missing. I instead used another user32 function, static extern int GetWindowText(int hWnd, StringBuilder text, int count); – ashurexm Nov 22 '10 at 23:23
  • Oh, sorry. That's a reference to another class in one of my applications. I'll edit my answer to show what I was doing... – Nick Spreitzer Nov 22 '10 at 23:29
  • Ok, fixed. Notice the new pinvoke statement for GetWindowText and its invocation where WindowTitle.GetText() used to be. (Which is basically what you did anyways, obviously.) – Nick Spreitzer Nov 22 '10 at 23:40
1

I think you want FindWindow().

cdhowie
  • 158,093
  • 24
  • 286
  • 300