2017-07-06 114 views
-1

首先,我使用Createprocess()來打開所有需要的窗口。 (未列出)SetWindowPos()有時不會調整子窗口的大小

成功打開流程後,我找到了FindWindow()FindWindowEx()所有可用的窗口。 (列出)

有時功能SetWindowPos()無法正常工作。幾個窗口被整理出一些沒有。但這種情況並不總是會發生!

爲了改進我總是有一個開放的耳朵!

//Some Variables comming from other code : numberOfWindows/numberOfStartedWindows 
//Variables 
     bool trigger = true; 
     bool targetProcess = true; 
     HWND hwnd, hwndChilds = NULL; 
     int targetsFound = 0; 
     int targetsFoundMath = 0; 
     bool searchWindows = true; 

     //Start targetProcess 
     while (targetProcess) { 

      //If targetsFround == numberOfWindows then exit targetProcess. 
      if (targetsFound == numberOfWindows) 
      { 
       targetProcess = false; 
      } 

      //If all Windows are Started then start search loop 
      if (numberOfStartedWindows == numberOfWindows) 
      { 
       trigger = false; 
      } 

      //We starting search loop... 
      if (!trigger && targetProcess) { 

       //Find the Main Window. 
       hwnd = FindWindow(0, _T("World of Warcraft")); 
       if (hwnd) 
       { 
        targetsFound += 1; 

        //Set Window Foreground and call SetWindowPos to change Position 
        if (SetForegroundWindow(hwnd)) { 
         if (!SetWindowPos(hwnd, HWND_TOP, 0, 0, 1920, 800, SWP_SHOWWINDOW)) 
         { 
          cout << "Error: HWND Failed with SETWINDOWPOS" << endl; 
         } 
        } 
        //Store HWND in StoreWindows 
        StoreWindows.emplace_back(hwnd); 

        //Now we search for child Windows 
        while (searchWindows) 
        { 

         //do it if targetsFound not numberOfWindows 
         if (targetsFound != numberOfWindows) 
         { 
          //Get Child Window 
          hwndChilds = FindWindowEx(NULL, hwnd, NULL, _T("World of Warcraft")); 
          if (hwndChilds != NULL) 
          { 

           //Little Math for Position 
           targetsFoundMath = (targetsFound - 1) * 384; 

           //if window in foreground SetWindowPos. 
           if(SetForegroundWindow(hwndChilds)){ 
            if (!SetWindowPos(hwndChilds, HWND_TOP, targetsFoundMath, 800, 384, 280, SWP_SHOWWINDOW)) 
            { 
             cout << "Error: HWNDChilds Failed with SETWINDOWPOS" << endl; 
            } 
           } 

           /*targetsFound += 1; 
           StoreWindows.emplace_back(hwndChilds);*/ 

           //If all targetsFound then quit this loop after finish 
           if (targetsFound == numberOfWindows) { 
            searchWindows = false; 
            cout << "false" << targetsFound << endl; 
           } 

           //StoreChild Window and Add targetsfround +1 
           targetsFound += 1; 
           StoreWindows.emplace_back(hwndChilds); 
          } 
          else { 
           searchWindows = false; 
           cout << "no more child objects found!" << endl; 
          } 
         } 
         else 
         { 
          searchWindows = false; 
         } 
        } 
       } 
      } 
     } 
     cout << "Window Size: " << StoreWindows.size() << endl; 

有趣,當我調試:

RECT debugRect; 
for (int x = 0; x < StoreWindows.size(); x++) 
{ 
    GetClientRect(StoreWindows[x], &debugRect); 

    cout << debugRect.left << " " << debugRect.right<< " " << debugRect.bottom<< " " << debugRect.top << endl; 
    //debugRect.right = breite vom fenster // debugRect.bottom = Höhe 
} 

從該位置輸出看起來不錯,但在正確的地方窗口的心不是:/ 調試: Debug

林的想法和希望你能幫我!如果你需要更多的解釋,不要猶豫,問。

有了它看起來是那麼的問題..

Fold proccess

如果一切沒有問題,它看起來像這樣:

Success process

+0

您是否重寫了子窗口的'WM_WINDOWPOSCHANGING'處理程序中的位置? –

+0

即時通訊不知道^^(我認爲不是) – Lendoria

+0

你是怎麼說的那麼好? SetWindowPos()做新的位置。或者我忘記了什麼? Sry im new in C++ – Lendoria

回答

0

的問題是重複的(相同的)窗口偏被發現。因爲啓動過程需要比Find Func更長的時間。現在我已經Fixxed循環。 (它現在等待所有成功窗口。)

bool mainWindow = false; 
    bool searchHwndStrike = false; 
    int targetResult = 0; 
    while (true) 
    { 
     //Search first Window 
     if (!mainWindow) { 
      hwnd = FindWindow(0, _T("World of Warcraft")); 
      if (hwnd != NULL) 
      { 

       if (SetForegroundWindow(hwnd)) { 
        if (!SetWindowPos(hwnd, HWND_TOP, 0, 0, 1920, 800, SWP_SHOWWINDOW)) 
        { 
         cout << "Error: HWND Failed with SETWINDOWPOS" << endl; 
        } 
       } 

       cout << "Main window found." << endl; 
       mainWindow = true; 
       StoreWindows.emplace_back(hwnd); 
      } 
     }else { 

      if (StoreWindows.size() < numberOfWindows) { 
       hwnd = FindWindowEx(NULL, hwnd, NULL, _T("World of Warcraft")); 
       if (hwnd != NULL) 
       { 
        for (int x = 0; x < StoreWindows.size(); x++) 
        { 
         if (StoreWindows[x] == hwnd) 
         { 
          searchHwndStrike = true; 
         } 
        } 
        if (!searchHwndStrike) 
        { 
         cout << "Child window found." << endl; 
         cout << targetResult << endl; 
         targetResult = (StoreWindows.size()-1) * 384; 

         //if window in foreground SetWindowPos. 
         if (SetForegroundWindow(hwnd)) { 
          if (!SetWindowPos(hwnd, HWND_TOP, targetResult, 800, 384, 280, SWP_SHOWWINDOW)) 
          { 
           cout << "Error: HWNDChilds Failed with SETWINDOWPOS" << endl; 
          } 
         } 
         StoreWindows.emplace_back(hwnd); 

        } 
        searchHwndStrike = false; 
       } 
      } 
      else { 
       break; 
      } 
     } 
    } 
    cout << StoreWindows.size() << endl;