简体   繁体   中英

Positioning Windows under WorkerW

I am trying to achieve something and having an unexpected behaviour which after a day of research, I suspect it's related to the difference between client coordinates and screen coordinates. But I come from a web background and have very limited understanding and experience in C++.

So below is the code I am trying to have to work:

bool wallpaper::attach(Napi::Buffer<void *> handle, int x, int y) {
    HWND target = static_cast<HWND>(*reinterpret_cast<void **>(handle.Data()));

    HWND progman = FindWindowA("Progman", NULL);

    LRESULT result = SendMessageTimeoutA(
            progman,
            0x052C,
            NULL,
            NULL,
            SMTO_NORMAL,
            1000,
            NULL);

    EnumWindows(&FindWorkerW, reinterpret_cast<LPARAM>(&workerw));

    SetWindowPos(
            target,
            workerw,
            x,
            y,
            NULL,
            NULL,
            SWP_NOSIZE
    );

    SetParent(target, workerw);
//    ShowWindow(target, SW_SHOWMAXIMIZED);

    return true;
}

I have a setup of three monitors each with a resolution of 1980x1080 next to each other horizontally. The setup is in the below order:

Display 3 (left)

Display 1 (middle)

Display 2 ( right)

When I fetch information from the node module system information, I get the coordinates for each display as per below:

  • Display 1 0, 0

  • Display 2 1920, 0

  • Display 3 -1920, 0

The problem I have is that when using these same values through x and y, they do set the window fine on each monitor. But when the code goes through the line SetParent, the coordinate becomes irrelevant and I get the following result:

  • Window for Display 1 shows on Display 3

  • Window for Display 2 shows on Display 1

  • Window for Display 3 doesn't show

If though I hardcode the -1920 value to 3840, it does show the Window for Display 3 on Display 2.

I've read around and saw the following methods, ClientToScreen, ScreenToClient, MapWindowPoints. But I am unable to understand exactly how to adapt these for my use case.

If any anyone would be kind to hint me in the right direction (I read about the difference between client and screen coordinates but I still didn't understand it and values returned by ClientToScreen method and such are in LONG format).

After spending a day reading around and trying to make sense of C++ concepts and the functions available from Windows, the code below worked for me


    POINT pt = {};

    pt.x = 0;

    pt.y = 0;

    SetParent(hwnd, workerw);

    ScreenToClient(workerw, &pt);

    SetWindowPos(hwnd, HWND_TOP, pt.x, pt.y, NULL, NULL, SWP_NOSIZE);

I did hardcode each coordinate which are being translated accordingly and set on the relevant display.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM