| Author |
Message |
Ennair
Joined: 03 May 2010 Posts: 4
|
Posted: Mon May 03, 2010 8:31 pm Post subject: Window goes off screen after Restoring from minimized state |
|
|
Hi
I can't find an eventHandler in C# that would trigger when user restores a window from minimized state. So i am looking to p/invoke the window so that it displays in the location where it was before it was minimized.
Currently, when user clicks on minimize, window is minimized but when user restores window, the window is opened off screen on the top left of the screen.
I tried to listen to Window messages and then use a hook to restore the window to a predefined location. But each time I restore, a number of messages are shown, so I don't know which one to use. How can I listen to messages effectively?
Is this possible? _________________ http://www.cheap.ennair.com |
|
| Back to top |
|
 |
Paul.Martin
Joined: 03 Feb 2010 Posts: 83 Location: Cambridgeshire
|
Posted: Tue May 04, 2010 3:50 pm Post subject: |
|
|
You can override the WndProc and look for the SC_RESTORE message.
The only downside with this method is that SC_RESTORE is passed both when you restore from minimized and when you restore from maximized. So you also have to check the window state as well and only act when the window is currently minimized.
| Code: |
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_RESTORE = 0xF120;
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_SYSCOMMAND:
int command = m.WParam.ToInt32() & 0xfff0;
if (command == SC_RESTORE && WindowState == FormWindowState.Minimized )
{
this.Location = // Point the window was previously
this.Size = // Size the window was previously
}
break;
}
base.WndProc(ref m);
}
|
|
|
| Back to top |
|
 |
Ennair
Joined: 03 May 2010 Posts: 4
|
Posted: Thu May 06, 2010 8:16 pm Post subject: |
|
|
SC_RESTORE = 0xF120 is never passed! I am testing on Vista - could that be why? _________________ http://www.cheap.ennair.com |
|
| Back to top |
|
 |
Paul.Martin
Joined: 03 Feb 2010 Posts: 83 Location: Cambridgeshire
|
Posted: Fri May 07, 2010 11:43 am Post subject: |
|
|
How is the user restoring the window?
WM_SYSCOMMAND & SC_RESTORE will be sent if the user uses the system menus (i.e. taskbar or an applications top bar).
If they are using something which uses the ShowWindow(...) or similar api call then you will be sent WM_WINDOWPOSCHANGED (0x47) message instead. The sort of things that would use the ShowWindow(...) command is the Task Manager, Show/Restore All (Win+D) and other programs.
You could check the WM_SIZE (0x5) message with WParam.ToInt32() == SIZE_RESTORED (0). However, this is more complicated as you will be passed this whenever your window is resized to any size which is not minimised or maximised.
Also if it is a multi-window application, make sure that none of your other windows are handling the WndProc message. |
|
| Back to top |
|
 |
Ennair
Joined: 03 May 2010 Posts: 4
|
Posted: Sat May 08, 2010 11:44 am Post subject: |
|
|
The user is restoring the window from the Taskbar.
These are the messages I get when i restore the window from the Taskbar:
WndProc messages: 70
WndProc messages: 71
WndProc messages: 28
WndProc messages: 134
WndProc messages: 13
WndProc messages: 6
WndProc messages: 274
WndProc messages: 274
WndProc messages: 19
WndProc messages: 13
WndProc messages: 70
WndProc messages: 36
WndProc messages: 131
WndProc messages: 71
WndProc messages: 3
WndProc messages: 70
WndProc messages: 36
WndProc messages: 70
WndProc messages: 36
WndProc messages: 5
WndProc messages: 641
WndProc messages: 642
WndProc messages: 7
WndProc messages: 6
WndProc messages: 49935
WndProc messages: 134
WndProc messages: 6
WndProc messages: 28
WndProc messages: 8
WndProc messages: 641
WndProc messages: 642
None of them corresponds to 61728 which is the value of SC_RESTORE. _________________ http://www.cheap.ennair.com |
|
| Back to top |
|
 |
Paul.Martin
Joined: 03 Feb 2010 Posts: 83 Location: Cambridgeshire
|
Posted: Sun May 09, 2010 9:12 pm Post subject: |
|
|
Sorry I wasn't very clear on my first post. The WinProc message you are looking for is WM_SYSCOMMAND (0x112 = 274).
The WM_SYSCOMMAND message will have a wParam value of SC_RESTORE (well it will if you ignore the least significant 4 bits - i.e. "& 0xfff0").
The code in my first post gives a better explanation that my description, sorry. |
|
| Back to top |
|
 |
Ennair
Joined: 03 May 2010 Posts: 4
|
|
| Back to top |
|
 |
Hippno
Joined: 17 Oct 2011 Posts: 1
|
Posted: Mon Oct 17, 2011 7:54 am Post subject: |
|
|
| Thanks for step-by-step explanation, Paul, my issue has already been resolved. I'm very glad! |
|
| Back to top |
|
 |
|