Search code examples
c#winformscapturewindowstatecross-application

C#: capture windowstate changes of another application (wrote in c/c++ i think)


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


Solution

  • //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;
        }
    
    }