C++ Win32:将黑色边框添加到全屏窗口
我试图在Windows中以全屏模式保留内容纵横比。如果显示纵横比与内容纵横比不同,我想将桌面的其余部分隐藏在黑色边框后面。是否可以使用Win32 api创建具有中心内容和黑色边框的全屏窗口 在OS X中,这可以通过以下代码轻松实现:C++ Win32:将黑色边框添加到全屏窗口,c++,winapi,window,fullscreen,C++,Winapi,Window,Fullscreen,我试图在Windows中以全屏模式保留内容纵横比。如果显示纵横比与内容纵横比不同,我想将桌面的其余部分隐藏在黑色边框后面。是否可以使用Win32 api创建具有中心内容和黑色边框的全屏窗口 在OS X中,这可以通过以下代码轻松实现: CGSize ar; ar.width = 800; ar.height = 600; [self.window setContentAspectRatio:ar]; [self.window center]; [self.window toggleFullScre
CGSize ar;
ar.width = 800;
ar.height = 600;
[self.window setContentAspectRatio:ar];
[self.window center];
[self.window toggleFullScreen:nil];
如果我在16:9显示模式下运行上述代码,我的应用程序将进入全屏模式,内容居中(因为它是4:3),屏幕两侧都有黑色边框
我曾尝试在Windows中实现相同的功能,但我开始怀疑这是否可行。我当前的全屏代码保持纵横比和
如果fullscreenWidth
和fullscreenHeight
不等于displayWidth
和displayHeight
,则内容居中,但在窗口两侧显示桌面:
bool enterFullscreen(int fullscreenWidth, int fullscreenHeight)
{
DEVMODE fullscreenSettings;
bool isChangeSuccessful;
int displayWidth = GetDeviceCaps(m_hDC, HORZRES);
int displayHeight = GetDeviceCaps(m_hDC, VERTRES);
int colourBits = GetDeviceCaps(m_hDC, BITSPIXEL);
int refreshRate = GetDeviceCaps(m_hDC, VREFRESH);
EnumDisplaySettings(NULL, 0, &fullscreenSettings);
fullscreenSettings.dmPelsWidth = fullscreenWidth;
fullscreenSettings.dmPelsHeight = fullscreenHeight;
fullscreenSettings.dmBitsPerPel = colourBits;
fullscreenSettings.dmDisplayFrequency = refreshRate;
fullscreenSettings.dmFields = DM_PELSWIDTH |
DM_PELSHEIGHT |
DM_BITSPERPEL |
DM_DISPLAYFREQUENCY;
SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_TOPMOST);
SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, displayWidth, displayHeight, SWP_SHOWWINDOW);
isChangeSuccessful = ChangeDisplaySettings(&fullscreenSettings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL;
ShowWindow(m_hWnd, SW_MAXIMIZE);
RECT rcWindow;
GetWindowRect(m_hWnd, &rcWindow);
// calculate content position
POINT ptDiff;
ptDiff.x = ((rcWindow.right - rcWindow.left) - fullscreenWidth) / 2;
ptDiff.y = ((rcWindow.bottom - rcWindow.top) - fullscreenHeight) / 2;
AdjustWindowRectEx(&rcWindow, GetWindowLong(m_hWnd, GWL_STYLE), FALSE, GetWindowLong(m_hWnd, GWL_EXSTYLE));
SetWindowPos(m_hWnd, 0, ptDiff.x, ptDiff.y, displayWidth, displayHeight, NULL);
return isChangeSuccessful;
}
完成所需内容的最简单方法是创建一个子窗口(C)来呈现内容,将多余的空间留给父窗口(p) p应使用黑色笔刷作为背景创建。注册窗口类()时为的
hbrBackground
成员指定。为了防止在清除背景时闪烁,P应该有WS\u CLIPCHILDREN
每当p更改其大小时,都会将a发送到p的窗口过程。然后处理程序可以调整C的位置和大小以保持纵横比
要创建无边界子窗口C,请在调用中使用WS|u child | WS|u VISIBLE
窗口样式。如果您想在父窗口P中处理鼠标输入,请添加WS\u DISABLED
窗口样式。
示例代码(为简洁起见,省略了错误检查): 注册窗口类后,我们可以继续并创建每个类的实例。请注意,内容窗口最初的大小为零。实际大小在父级的
WM_size
处理程序中进一步向下计算
// Create main window
HWND hWndMain = ::CreateWindowW( classNameMain,
L"Constant AR",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 800,
NULL,
NULL,
hInstance,
NULL );
// Create content window
g_hWndContent = ::CreateWindowW( classNameContent,
NULL,
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0,
hWndMain,
NULL,
hInstance,
NULL );
其余部分是样板Windows应用程序代码:
// Show application
::ShowWindow( hWndMain, nCmdShow );
::UpdateWindow( hWndMain );
// Main message loop
MSG msg = { 0 };
while ( ::GetMessageW( &msg, NULL, 0, 0 ) > 0 )
{
::TranslateMessage( &msg );
::DispatchMessageW( &msg );
}
return (int)msg.wParam;
}
窗口类的行为在其内部实现:
除了标准的消息处理外,主窗口的窗口过程还会调整内容的大小,以适应主窗口大小的变化:
case WM_SIZE: {
const SIZE ar = { 800, 600 };
// Query new client area size
int clientWidth = LOWORD( lParam );
int clientHeight = HIWORD( lParam );
// Calculate new content size
int contentWidth = ::MulDiv( clientHeight, ar.cx, ar.cy );
int contentHeight = ::MulDiv( clientWidth, ar.cy, ar.cx );
// Adjust dimensions to fit inside client area
if ( contentWidth > clientWidth ) {
contentWidth = clientWidth;
contentHeight = ::MulDiv( contentWidth, ar.cy, ar.cx );
} else {
contentHeight = clientHeight;
contentWidth = ::MulDiv( contentHeight, ar.cx, ar.cy );
}
// Calculate offsets to center content
int offsetX = ( clientWidth - contentWidth ) / 2;
int offsetY = ( clientHeight - contentHeight ) / 2;
// Adjust content window position
::SetWindowPos( g_hWndContent,
NULL,
offsetX, offsetY,
contentWidth, contentHeight,
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER );
return 0;
}
}
return ::DefWindowProcW( hWnd, message, wParam, lParam );
}
内容窗口的窗口过程不实现任何自定义行为,只是将所有消息转发到默认实现:
LRESULT CALLBACK WndProcContent( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {
return ::DefWindowProcW( hWnd, message, wParam, lParam );
}
完成所需内容的最简单方法是创建一个子窗口(C)来呈现内容,将多余的空间留给父窗口(p) p应使用黑色笔刷作为背景创建。注册窗口类()时为的
hbrBackground
成员指定。为了防止在清除背景时闪烁,P应该有WS\u CLIPCHILDREN
每当p更改其大小时,都会将a发送到p的窗口过程。然后处理程序可以调整C的位置和大小以保持纵横比
要创建无边界子窗口C,请在调用中使用WS|u child | WS|u VISIBLE
窗口样式。如果您想在父窗口P中处理鼠标输入,请添加WS\u DISABLED
窗口样式。
示例代码(为简洁起见,省略了错误检查): 注册窗口类后,我们可以继续并创建每个类的实例。请注意,内容窗口最初的大小为零。实际大小在父级的
WM_size
处理程序中进一步向下计算
// Create main window
HWND hWndMain = ::CreateWindowW( classNameMain,
L"Constant AR",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 800,
NULL,
NULL,
hInstance,
NULL );
// Create content window
g_hWndContent = ::CreateWindowW( classNameContent,
NULL,
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0,
hWndMain,
NULL,
hInstance,
NULL );
其余部分是样板Windows应用程序代码:
// Show application
::ShowWindow( hWndMain, nCmdShow );
::UpdateWindow( hWndMain );
// Main message loop
MSG msg = { 0 };
while ( ::GetMessageW( &msg, NULL, 0, 0 ) > 0 )
{
::TranslateMessage( &msg );
::DispatchMessageW( &msg );
}
return (int)msg.wParam;
}
窗口类的行为在其内部实现:
除了标准的消息处理外,主窗口的窗口过程还会调整内容的大小,以适应主窗口大小的变化:
case WM_SIZE: {
const SIZE ar = { 800, 600 };
// Query new client area size
int clientWidth = LOWORD( lParam );
int clientHeight = HIWORD( lParam );
// Calculate new content size
int contentWidth = ::MulDiv( clientHeight, ar.cx, ar.cy );
int contentHeight = ::MulDiv( clientWidth, ar.cy, ar.cx );
// Adjust dimensions to fit inside client area
if ( contentWidth > clientWidth ) {
contentWidth = clientWidth;
contentHeight = ::MulDiv( contentWidth, ar.cy, ar.cx );
} else {
contentHeight = clientHeight;
contentWidth = ::MulDiv( contentHeight, ar.cx, ar.cy );
}
// Calculate offsets to center content
int offsetX = ( clientWidth - contentWidth ) / 2;
int offsetY = ( clientHeight - contentHeight ) / 2;
// Adjust content window position
::SetWindowPos( g_hWndContent,
NULL,
offsetX, offsetY,
contentWidth, contentHeight,
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER );
return 0;
}
}
return ::DefWindowProcW( hWnd, message, wParam, lParam );
}
内容窗口的窗口过程不实现任何自定义行为,只是将所有消息转发到默认实现:
LRESULT CALLBACK WndProcContent( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {
return ::DefWindowProcW( hWnd, message, wParam, lParam );
}