无法使用以下代码将控制台窗口居中 [英] Can't center my console window by using the following code
本文介绍了无法使用以下代码将控制台窗口居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
void Initialize_Window(void){RECT r控制台;GetWindowRect(GetConsoleWindow(), &rConsole);SetWindowPos(GetConsoleWindow(), NULL, 0, 0, 800, 700, 0);SetWindowLong(GetConsoleWindow(), GWL_STYLE, GetWindowLong(GetConsoleWindow(), GWL_STYLE) & ~(WS_SIZEBOX | WS_MAXIMIZEBOX));SetWindowPos(GetConsoleWindow(), NULL, (GetSystemMetrics(SM_CXSCREEN) - rConsole.right - rConsole.left)/2, (GetSystemMetrics(SM_CYSCREEN) - rConsole.bottom - rConsole.top)/2, 0, 0, SWP_NOSIZE);}
我正在尝试使用上面的代码将控制台窗口居中,但每次执行程序时,窗口似乎只是移动到屏幕上的随机位置,知道如何修复它吗?
解决方案
您需要 (GetSystemMetrics(SM_CXSCREEN) - (rConsole.right - rConsole.left))/2
才能获得居中.><小时>
旁注:您可以使用一个 SetWindowPos
而不是两个(并且不需要获取窗口 Rect
)
const int width = 800;const int 高度 = 700;//SetWindowLong()...SetWindowPos(GetConsoleWindow(), NULL,GetSystemMetrics(SM_CXSCREEN)/2 - 宽度/2,GetSystemMetrics(SM_CYSCREEN)/2 - 高度/2,宽度,高度,SWP_SHOWWINDOW);
void Initialize_Window(void)
{
RECT rConsole;
GetWindowRect(GetConsoleWindow(), &rConsole);
SetWindowPos(GetConsoleWindow(), NULL, 0, 0, 800, 700, 0);
SetWindowLong(GetConsoleWindow(), GWL_STYLE, GetWindowLong(GetConsoleWindow(), GWL_STYLE) & ~(WS_SIZEBOX | WS_MAXIMIZEBOX));
SetWindowPos(GetConsoleWindow(), NULL, (GetSystemMetrics(SM_CXSCREEN) - rConsole.right - rConsole.left) / 2, (GetSystemMetrics(SM_CYSCREEN) - rConsole.bottom - rConsole.top) / 2, 0, 0, SWP_NOSIZE);
}
I'm trying to center my console window by using the code above, but seems like the window just moved to a random position on my screen every time I execute the program, any idea how to fix it?
解决方案
You need (GetSystemMetrics(SM_CXSCREEN) - (rConsole.right - rConsole.left))/2
to get center.
Side note: you can use one SetWindowPos
instead of two (and do not need to get window Rect
)
const int width = 800;
const int height = 700;
//SetWindowLong()...
SetWindowPos(GetConsoleWindow(), NULL,
GetSystemMetrics(SM_CXSCREEN)/2 - width/2,
GetSystemMetrics(SM_CYSCREEN)/2 - height/2,
width, height, SWP_SHOWWINDOW);
这篇关于无法使用以下代码将控制台窗口居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文