2015-07-04 167 views
2

我想找出最小化/最大化另一個線程擁有的窗口的正確方法。我的目標窗口可以是全屏或不全屏(我應該能夠最小化和最大化它,而不管它的狀態)。我已經嘗試過ShowWindow SW_MINIMIZE,SW_MAXIMIZE,SW_FORCEMINIMIZE等等的各種組合......但是我已經能夠實現的唯一結果是當它被最小化並且全屏與ShowWindow(hWnd, SW_RESTORE)時恢復它(最大化)。從另一個線程最大化/最小化窗口

這是我使用的檢索我的把手代碼:

#include <Windows.h> 
#include <iostream> 

// I'm a console application 
int main(int argc, char* argv[]) { 
    HWND hWnd = FindWindow(TEXT("MyWindowClass"), NULL); 
    if(IsWindow(hWnd)) { 
     std::cout << "Window found!" << std::endl; 
     SetForegroundWindow(hWnd); // I'll give focus to my window. This is always working. 
     if(IsIconic(hWnd)) 
      ShowWindow(hWnd, SW_RESTORE); // This is working only if the window is minimized while in fullscreen mode 
     Sleep(3000); 
     ShowWindow(hWnd, SW_MINIMIZE); // Not working. SW_FORCEMINIMIZE, SW_HIDE etc are not working either. 
    } 
    return 0; 
} 

回答

0

嘗試ShowWindow第一,然後調用SetForegroundWindow

void show_and_setforeground(HWND hwnd) 
{ 
    WINDOWPLACEMENT place; 
    memset(&place, 0, sizeof(WINDOWPLACEMENT)); 
    place.length = sizeof(WINDOWPLACEMENT); 
    GetWindowPlacement(hwnd, &place); 

    switch (place.showCmd) 
    { 
    case SW_SHOWMAXIMIZED: 
     ShowWindow(hwnd, SW_SHOWMAXIMIZED); 
     break; 
    case SW_SHOWMINIMIZED: 
     ShowWindow(hwnd, SW_RESTORE); 
     break; 
    default: 
     ShowWindow(hwnd, SW_NORMAL); 
     break; 
    } 

    SetForegroundWindow(hwnd); 
} 

除了IsWindow(hWnd)您可能需要使用因爲有些程序使用不可用的不可視窗口。

hwnd = FindWindow(TEXT("MyWindowClass"), NULL); 
if (IsWindow(hwnd)) 
{ 
    if(IsWindowVisible(hwnd))//optional 
    { 
     show_and_setforeground(hwnd); 
     ... 
    } 
} 
+0

謝謝你,但窗口時全屏這只是工作。當窗口被調用時,ShowWindow(hWnd,SW_RESTORE)被調用,但沒有任何事情發生。 – Tabaqui

2

掙扎了一整天后,我發現,無論對於最小化和無論其狀態如何最大化窗口工作的解決方案:Post/SendMessage

將其最大化:

PostMessage(hWnd, WM_SYSCOMMAND, SC_RESTORE, 0); 

爲了儘量減少它:

PostMessage(hWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);