SetWindowPos()有时不会调整子窗口

时间:2017-07-06 20:14:42

标签: c++ winapi

首先,我使用Createprocess()打开所有需要的窗口。 (未列出)

成功打开流程后,我找到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
}

位置的输出看起来不错,但Window不在正确的位置:/ 调试: Debug

我没有想法,希望你能帮助我!如果您需要更多解释,请不要犹豫。

有问题,它看起来如此......

Fold proccess

如果一切顺利,它看起来像这样:

Success process

1 个答案:

答案 0 :(得分: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;