开发者

Problem when maximizing window in C++

开发者 https://www.devze.com 2023-02-04 12:05 出处:网络
My program needs to arbitrarily maximize any window on the current desktop. I achieve this by calling ShowWindow(hWnd, SW_MAXIMIZE), where hWnd is the HWND of the window I want to maximize. When that

My program needs to arbitrarily maximize any window on the current desktop. I achieve this by calling ShowWindow(hWnd, SW_MAXIMIZE), where hWnd is the HWND of the window I want to maximize. When that line of code executes, the window in question (here, Notepad) looks like this:

Problem when maximizing window in C++

Everything seems fine, except for the fact that the window has not been positioned correctly, i.e. the w开发者_JS百科indow seems to be a few pixels to low, and the title bar does not look "squashed" like it should. Compared to how it should look when the maximize button is clicked, the problem is clearly visible:

Problem when maximizing window in C++

Does anyone know why this behaviour occurs, and what I can do to fix it?


Telling the window to maximize itself might bypass some internal adjustments that the program makes when it maximizes via a system menu command. To emulate clicking on the maximize button, send it a SC_MAXIMIZE command:

SendMessage(hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);


Antoher way to use SetWindowPos(); For example you got HWND handleWnd;

RECT rcWnd;
GetWindowRect(handleWnd,&rcWnd);
SetWindowPos(handleWnd,WHND_TOP,rcWnd.left,rcWnd.top,(rcWnd.right-rcWnd.left),(rcWnd.bottom-rcWnd.top),SWP_SHOWWINDOW);

So you got your previous position, place window on top of Z and show

0

精彩评论

暂无评论...
验证码 换一张
取 消