SDL2 How to position a window on a second monitor?

爷,独闯天下 提交于 2020-01-12 08:22:09

问题


I am using SDL_SetWindowPosition to position my window. Can I use this function to position my window on another monitor?

UPDATE

Using SDL_GetDisplayBounds will not return the correct monitor positions when the text size is changed in Windows 10. Any ideas how to fix this?


回答1:


SDL2 uses a global screen space coordinate system. Each display device has its own bounds inside this coordinate space. The following example places a window on a second display device:

// enumerate displays
int displays = SDL_GetNumVideoDisplays();
assert( displays > 1 );  // assume we have secondary monitor

// get display bounds for all displays
vector< SDL_Rect > displayBounds;
for( int i = 0; i < displays; i++ ) {
    displayBounds.push_back( SDL_Rect() );
    SDL_GetDisplayBounds( i, &displayBounds.back() );
}

// window of dimensions 500 * 500 offset 100 pixels on secondary monitor
int x = displayBounds[ 1 ].x + 100;
int y = displayBounds[ 1 ].y + 100;
int w = 500;
int h = 500;

// so now x and y are on secondary display
SDL_Window * window = SDL_CreateWindow( "title", x, y, w, h, FLAGS... );

Looking at the definition of SDL_WINDOWPOS_CENTERED in SDL_video.h we see it is defined as

#define SDL_WINDOWPOS_CENTERED         SDL_WINDOWPOS_CENTERED_DISPLAY(0)

so we could also use the macro SDL_WINDOWPOS_CENTERED_DISPLAY( n ) where n is the display index.

Update for Windows 10 - DPI scaling issue

It seems like there is indeed a bug with SDL2 and changing the DPI scale in Windows (i.e. text scale).

Here are two bug reports relevant to the problem. They are both still apparently unresolved.

https://bugzilla.libsdl.org/show_bug.cgi?id=3433

https://bugzilla.libsdl.org/show_bug.cgi?id=2713

Potential Solution

I am sure that the OP could use the WIN32 api to determine the dpi scale, for scale != 100%, and then correct the bounds by that.




回答2:


Yes, you can use SetWindowPosition, if you know the boundaries of the second monitor. You can use the function SDL_GetDisplayBounds(int displayIndex,SDL_Rect* rect) to get them.




回答3:


DPI scaling issue ("will not return the correct monitor positions when the text size is changed")

It's a known issue with SDL2 (I encountered it in those versions: 2.0.6, 2.0.7, 2.0.8, probably the older versions have this issue as well).

Solutions:

1) Use manifest file and set there:

<dpiAware>True/PM</dpiAware>

(you need to include the manifest file to your app distribution)

2) Try SetProcessDPIAware().



来源:https://stackoverflow.com/questions/41745492/sdl2-how-to-position-a-window-on-a-second-monitor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!