Problem when maximizing window in C++
Asked Answered
P

2

5

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:

alt text

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:

alt text

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

Pigheaded answered 14/1, 2011 at 2:19 Comment(0)
Y
13

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);
Yahweh answered 14/1, 2011 at 4:1 Comment(0)
L
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

Louvre answered 16/4, 2015 at 4:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.