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

﹥>﹥吖頭↗ 提交于 2019-12-02 19:04:22

问题


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


回答1:


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

}



回答2:


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.



来源:https://stackoverflow.com/questions/6378678/c-capture-windowstate-changes-of-another-application-wrote-in-c-c-i-think

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!