0

I have a situation where I need to capture windowstate changes of another window (which is not owned by my application and I didn't wrote it. I think it's written in C++).

Actually I'm using a separate thread where I constantly do GetWindowState and fire custom events when this value changes (I have the handle of the window), but I would like to know if there is a better method

Thanks,

P.S. I'm using winform if can be useful in any way

Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147

2 Answers2

2
//use this in a timer or hook the window
//this would be the easier way
using System;
using System.Runtime.InteropServices;

public static class WindowState
{
    [DllImport("user32")]
    private static extern int IsWindowEnabled(int hWnd);

    [DllImport("user32")]
    private static extern int IsWindowVisible(int hWnd);

    [DllImport("user32")]
    private static extern int IsZoomed(int hWnd);

    [DllImport("user32")]
    private static extern int IsIconic(int hWnd);

    public static bool IsMaximized(int hWnd) 
    {
        if (IsZoomed(hWnd) == 0)
            return false;
        else
            return true;
    }

    public static bool IsMinimized(int hWnd)
    {
        if (IsIconic(hWnd) == 0)
            return false;
        else
            return true;
    }

    public static bool IsEnabled(int hWnd)
    {
        if (IsWindowEnabled(hWnd) == 0)
            return false;
        else
            return true;
    }

    public static bool IsVisible(int hWnd)
    {
        if (IsWindowVisible(hWnd) == 0)
            return false;
        else
            return true;
    }

}
murktinez
  • 36
  • 2
  • of course this assumes you know how to obtain the window's handle. if you don't just read a tutorial on the Win32 API. particularly, the FindWindow and FindWindowEx functions. An API spy would be useful as well. check our patorjk.com, they have a good API spy for free. – murktinez Jun 17 '11 at 06:10
  • I don't see the point here, I asked how to **capture** windowstate changes, not how to check it which is far easier – Francesco Belladonna Jun 17 '11 at 12:54
  • If you use this class in a timer, saving the window state everytime the timer executes then you will know when the window state has changed. It's very simple. If you are trying to prevent a window's state very being changed thats something different, and something you would need to be more clear about. I could provide code to do that as well – murktinez Jun 18 '11 at 05:29
  • I'm doing it in this way at the moment, maybe this is the best solution if you don't wanna do **a lot** of work – Francesco Belladonna Jun 18 '11 at 09:12
1

You can hook WNDPROC and intercept messages with that. You can either inject a DLL into the target process, open the process with debug priveleges or set a global hook to WH_CALLWNDPROC.

foxy
  • 7,599
  • 2
  • 30
  • 34