Problem when maximizing window in C++

核能气质少年 提交于 2019-12-23 10:17:45

问题


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:

Everything seems fine, except for the fact that the window has not been positioned correctly, i.e. the window 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:

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


回答1:


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);



回答2:


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



来源:https://stackoverflow.com/questions/4687400/problem-when-maximizing-window-in-c

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