What determines the monitor my app runs on?

前端 未结 11 781
Happy的楠姐
Happy的楠姐 2021-01-30 04:48

I am using Windows, and I have two monitors.

Some applications will always start on my primary monitor, no matter where they were when I closed them.

Ot

11条回答
  •  猫巷女王i
    2021-01-30 05:28

    Correctly written Windows apps that want to save their location from run to run will save the results of GetWindowPlacement() before shutting down, then use SetWindowPlacement() on startup to restore their position.

    Frequently, apps will store the results of GetWindowPlacement() in the registry as a REG_BINARY for easy use.

    The WINDOWPLACEMENTroute has many advantages over other methods:

    • Handles the case where the screen resolution changed since the last run: SetWindowPlacement() will automatically ensure that the window is not entirely offscreen
    • Saves the state (minimized/maximized) but also saves the restored (normal) size and position
    • Handles desktop metrics correctly, compensating for the taskbar position, etc. (i.e. uses "workspace coordinates" instead of "screen coordinates" -- techniques that rely on saving screen coordinates may suffer from the "walking windows" problem where a window will always appear a little lower each time if the user has a toolbar at the top of the screen).

    Finally, programs that handle window restoration properly will take into account the nCmdShow parameter passed in from the shell. This parameter is set in the shortcut that launches the application (Normal, Minimized, Maximize):

    if(nCmdShow != SW_SHOWNORMAL)
        placement.showCmd = nCmdShow; //allow shortcut to override
    

    For non-Win32 applications, it's important to be sure that the method you're using to save/restore window position eventually uses the same underlying call, otherwise (like Java Swing's setBounds()/getBounds() problem) you'll end up writing a lot of extra code to re-implement functionality that's already there in the WINDOWPLACEMENT functions.

提交回复
热议问题