views:

45

answers:

1

I'm using the SetWindowPlacement function to (1) maximize and (2) change the restore position of external windows on the desktop. But when I use this command on WinForm windows, it doesn't seem to set the restore location correctly. (The WinForm window I'm testing with is just a VS2008 WinForms application run without modification.) So, for example, if the window is in a restored state located at (0, 0) and I use SetWindowPlacement to maximize it and set its restore position to (100,100), then click the window's restore button, it will restore not to (100,100) but to (0,0). It actually seems like the window first restores to the location I set, THEN moves back to its last restore location prior to being manipulated programmatically. I'm confused as to why this would happen only on WinForm windows - every non-WinForm window I try this on restores correctly to the position I indicated with SetWindowPlacement.

I know that's not much to go on, but I was wondering if anyone here had any ideas about why this is happening. Thanks.

+1  A: 

Yes, this is by design. The Form class keeps track of the restore bounds itself, necessary so it can properly reposition the window after it was re-created. Windows Forms often recreates the window on-the-fly to implement property setters for properties that can only be specified by CreateWindowEx(). Like ShowInTaskbar.

The private RestoreWindowBoundsIfNecessary() method puts the window back, it will run when the window is restored. From what I can tell, the restore bounds are latched just before the window is minimized or maximized. If you want to modify the restore position while the window is min/maximized then you'll have to use MoveWindow to move it where you want it to go after restoring the window. Ought to produce some flicker.

Hans Passant
Hmmm... very interesting. Yeah, I already tried restoring first before moving which did produce some flicker. Is there any documentation of RestoreWindowBoundsIfNecessary() you could point me to? I found very little myself. Thanks!
Nick
The source code for the Form class is available from the Reference Source: http://referencesource.microsoft.com/
Hans Passant
Very helpful. Thank you. :-)
Nick