Discussion:
A second ShowWindow(SW_MAXIMIZED) has no effect
(too old to reply)
Woody
2010-07-19 02:28:05 UTC
Permalink
[This is a repost of a message I put on MSDN, with no responses at
all]

I have a main frame window whose size is changed by my MFC app. If a
certain variable is true, the window is to be maximized (fill the
screen). I can do this successfully by doing
ShowWindow(SW_SHOWMAXIMIZED).

Later in the app, I want to change the same window back to its
original size. I am doing that successfully by SetWindowPos(NULL, 0,
0, originalWidth, originalHeight, SWP_NOZORDER | SWPNOMOVE).

The problem comes when I attempt to maximize the window a second time.
Then ShowWindow(SW_MAXIMIZED) has no effect.

I also tried ShowWindow(SW_SHOWNORMAL) to change back to the original
size, and this seems to work, but I am wondering why the second
SW_MAXIMIZED doesn't work.
David Lowndes
2010-07-19 07:05:23 UTC
Permalink
Post by Woody
I have a main frame window whose size is changed by my MFC app. If a
certain variable is true, the window is to be maximized (fill the
screen). I can do this successfully by doing
ShowWindow(SW_SHOWMAXIMIZED).
Later in the app, I want to change the same window back to its
original size. I am doing that successfully by SetWindowPos(NULL, 0,
0, originalWidth, originalHeight, SWP_NOZORDER | SWPNOMOVE).
The problem comes when I attempt to maximize the window a second time.
Then ShowWindow(SW_MAXIMIZED) has no effect.
I also tried ShowWindow(SW_SHOWNORMAL) to change back to the original
size, and this seems to work, but I am wondering why the second
SW_MAXIMIZED doesn't work.
Can you show us how to reproduce this - by small changes to a stock
MFC project for instance?

Dave
Woody
2010-07-19 18:51:38 UTC
Permalink
Post by David Lowndes
Can you show us how to reproduce this - by small changes to a stock
MFC project for instance?
The setup I have is complex, and it isn't your typical MFC project. I
will try to describe what I think are the important points. Perhaps
you can see what I'm doing wrong from this.

1) The main window class is based on CWnd. I am overriding OnSize
(because the window is user-resizeable). OnSize is called by the
WindowProc when the app is started (type=SIZE_RESTORED). In OnSize, I
move some controls that are in the main window, and invalidate/update
the main window.

2) Later, depending on a user-set parameter, the main window is
maximized (ShowWindow(SW_SHOWMAXIMIZED)).

3) After a time, the main window needs to be returned to its original
size. My initial attempt at doing this was to use
ShowWindow(SW_RESTORE) or SW_NORMAL, but these don't give the original
size if user resized the main window between steps (2) and (3).
Instead, they change the main window's size to match the user-resized
one.

4) My next attempt was to make the main window its original size by
simply using SetWindowPos(NULL, 0,
Post by David Lowndes
0, originalWidth, originalHeight, SWP_NOZORDER | SWPNOMOVE), instead of using SW_RESTORE or SW_NORMAL. The main window is sized properly in this case, but when step (2) is repeated, the main window is not maximized; its size doesn't change.
Woody
2010-07-19 19:43:27 UTC
Permalink
The last lines of the previous post were cut off. It was

0, originalWidth, originalHeight, SWP_NOZORDER | SWPNOMOVE), instead
of using SW_RESTORE or SW_NORMAL. The main window is sized properly in
this case, but when step (2) is repeated, the main window is not
maximized; its size doesn't change.
David Lowndes
2010-07-19 21:54:13 UTC
Permalink
Post by Woody
Post by David Lowndes
Can you show us how to reproduce this - by small changes to a stock
MFC project for instance?
The setup I have is complex, and it isn't your typical MFC project. I
will try to describe what I think are the important points. Perhaps
you can see what I'm doing wrong from this.
Sorry.

I suggest that you start with a stock MFC framework application, and
add the minimum key aspects of your application that allow you to
reproduce the problem.

Without doing something like that the problem is too complex (for me
at least) to decipher this way.

Dave
Joseph M. Newcomer
2010-07-28 20:45:39 UTC
Permalink
I've done things like this a number of times, and I've had windows, for example, that
could only be minimized and maximized but never restored. Lots of clever encodings of
state; for example, if it is maximized, then "restore" means "minimize" and if it is
minimized, "restore" means "maximize". I usually end up with a switch statement in the
OnSize handler. Key here is the OnSize handler is called twice: once to actually and
then, having discovered that it is handling a restore from maximize or minimize and then
does the resize, gets called again. So I end up needing a "previous state" variable that
helps drive the state machine.

Again, the key to understanding this is the knowledge that a window that is in the
"maximized" state believes its size to be proper size for a maximized window, and if you
brute-force resize a maximized window (e.g., SetWindowPos), then it is still considered
"maximized" and attempts to re-maximize it will be ignored. "Maximiized" is a state
determined NOT by the window dimensions, but by an internal state flag. So if you set the
window size to be the monitor size of the monitor it is shown on, you visually cannot tell
this from a maximized window. But a restored window can be dragged around, and a
maximized window cannot be dragged around. If you manage to get the mouse on the edge of
a window that is the size of the display, it will allow you to drage the edges and resize
it with the mouse; if the window is physically the same size as a restored window but is
actually maximized, the edge-dragging is inoperable. So "maximized" is a state of mind
which is completely unrelated to its screen size.

Now, here's another interesting manifestation. If I am running a program, say, Visual
Studio, on my main development machine (native: 1920x1200 dpi) but I'm doing it via Remote
Desktop from my Summer Office (the back porch), then the window is the size of my laptop
(1364x768). If it gets dark out here, and the floundivorous beasties come out, and I shut
down my laptop and move into my indoor office and re-atach to my desktop, I find that VS
occupies only a part of my screen. Furthermore, I can't drag it or maximize it to fill my
1920x1200 screen. It is *already* maximized. I first have to *restore* it (using the
button on the caption bar), then if I again *maximize* it (using the button on the caption
bar) then, and only then, does it expand to fill the 1920x1200 area. You can reasonably
claim that this is a bug, in that the WM_DISPLAYCHANGE message is not intelligently
handled, but nearly every program I use behaves this way. Only Microsoft Office
mainstream (Word, Excel, PowerPoint) gets it right. FrontPage does not. CorelDraw Suite
has always gotten it right (at least through X3, the highest version I hve installed at
the moment). So if you *really* want to get it right, the problem is more complex.
joe

joe
Post by David Lowndes
Post by Woody
Post by David Lowndes
Can you show us how to reproduce this - by small changes to a stock
MFC project for instance?
The setup I have is complex, and it isn't your typical MFC project. I
will try to describe what I think are the important points. Perhaps
you can see what I'm doing wrong from this.
Sorry.
I suggest that you start with a stock MFC framework application, and
add the minimum key aspects of your application that allow you to
reproduce the problem.
Without doing something like that the problem is too complex (for me
at least) to decipher this way.
Dave
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

GT
2010-07-21 09:05:49 UTC
Permalink
Post by Woody
[This is a repost of a message I put on MSDN, with no responses at
all]
I have a main frame window whose size is changed by my MFC app. If a
certain variable is true, the window is to be maximized (fill the
screen). I can do this successfully by doing
ShowWindow(SW_SHOWMAXIMIZED).
Later in the app, I want to change the same window back to its
original size. I am doing that successfully by SetWindowPos(NULL, 0,
0, originalWidth, originalHeight, SWP_NOZORDER | SWPNOMOVE).
The problem comes when I attempt to maximize the window a second time.
Then ShowWindow(SW_MAXIMIZED) has no effect.
I also tried ShowWindow(SW_SHOWNORMAL) to change back to the original
size, and this seems to work, but I am wondering why the second
SW_MAXIMIZED doesn't work.
Maybe you need to do a ShowWindow(SW_RESTORE) before setting the size back
to original, then subsequent SW_SHOWMAXIMIZED should work.
Joseph M. Newcomer
2010-07-26 16:05:12 UTC
Permalink
Your code is wrong. You should not resize a maximized window. If you want to make it the
original size, first "restore" it, then resize it.

Your ShowWindow(SW_MAXIMIZED) has no effect because the window thinks it is already
maxiimized; just because you brute-force resized it doesn't change the fact that the
window thinks it is maximized.
joe
Post by Woody
[This is a repost of a message I put on MSDN, with no responses at
all]
I have a main frame window whose size is changed by my MFC app. If a
certain variable is true, the window is to be maximized (fill the
screen). I can do this successfully by doing
ShowWindow(SW_SHOWMAXIMIZED).
Later in the app, I want to change the same window back to its
original size. I am doing that successfully by SetWindowPos(NULL, 0,
0, originalWidth, originalHeight, SWP_NOZORDER | SWPNOMOVE).
The problem comes when I attempt to maximize the window a second time.
Then ShowWindow(SW_MAXIMIZED) has no effect.
I also tried ShowWindow(SW_SHOWNORMAL) to change back to the original
size, and this seems to work, but I am wondering why the second
SW_MAXIMIZED doesn't work.
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Woody
2010-07-27 08:22:43 UTC
Permalink
Post by Joseph M. Newcomer
Your ShowWindow(SW_MAXIMIZED) has no effect because the window thinks it is already
maxiimized; just because you brute-force resized it doesn't change the fact that the
window thinks it is maximized.
Your explanation seems reasonable. So I should use GT's suggestion of
restoring it first, then resizing it, so it will maximize the second
time.
Joseph M. Newcomer
2010-07-27 21:18:11 UTC
Permalink
Yes.
joe
Post by Woody
Post by Joseph M. Newcomer
Your ShowWindow(SW_MAXIMIZED) has no effect because the window thinks it is already
maxiimized; just because you brute-force resized it doesn't change the fact that the
window thinks it is maximized.
Your explanation seems reasonable. So I should use GT's suggestion of
restoring it first, then resizing it, so it will maximize the second
time.
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Loading...