C#: capture windowstate changes of another applica

2019-02-21 04:52发布

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

2条回答
叼着烟拽天下
2楼-- · 2019-02-21 05:09
//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;
    }

}
查看更多
倾城 Initia
3楼-- · 2019-02-21 05:27

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.

查看更多
登录 后发表回答