如何判断用户当前使用的窗口是不是我程序的主窗口或我程序的主窗口的某个子窗口?

解决方案 »

  1.   

    枚举找到用户当前窗口,比较自己的HWND
      

  2.   

    可以试一下用API函数GetParent():GetParent
    The GetParent function retrieves a handle to the specified window's parent or owner. To retrieve a handle to a specified ancestor, use the GetAncestor function. HWND GetParent(
      HWND hWnd   // handle to child window
    );
    Parameters
    hWnd 
    [in] Handle to the window whose parent window handle is to be retrieved. 
    Return Values
    If the window is a child window, the return value is a handle to the parent window. If the window is a top-level window, the return value is a handle to the owner window. If the window is a top-level unowned window or if the function fails, the return value is NULL. To get extended error information, call GetLastError. Res
    Note that, despite its name, this function can return an owner window instead of a parent window. To obtain the parent window and not the owner, use GetAncestor with the GA_PARENT flag.Requirements 
      Windows NT/2000: Requires Windows NT 3.1 or later.
      Windows 95/98: Requires Windows 95 or later.
      Header: Declared in Winuser.h; include Windows.h.
      Library: Use User32.lib.
      

  3.   

    比较两个窗口的hWnd,如果是CWnd类,就比较两个的m_hWnd是否相同。
      

  4.   

    I think you can get the top parent window in this way first;
    HWND GetTopParent(HWND hWnd)
    {
    HWND hwnd;
    if(hWnd ==NULL) return FALSE;
    hwnd =hWnd;
    while(hwnd !=NULL)
    {
    hWnd =hwnd;
    hwnd =GetParent(hWnd);
    }
    return hWnd;
    }
    then you can compare both hwnd.or you can get some property in this window.such as GetClassName,GetWindowText,GetWindowLong API...
      

  5.   

    I think you can get the top parent window in this way first;
    HWND GetTopParent(HWND hWnd)
    {
    HWND hwnd;
    if(hWnd ==NULL) return FALSE;
    hwnd =hWnd;
    while(hwnd !=NULL)
    {
    hWnd =hwnd;
    hwnd =GetParent(hWnd);
    }
    return hWnd;
    }
    then you can compare both hwnd.or you can get some property in this window.such as GetClassName,GetWindowText,GetWindowLong API...