Red Gate forums :: View topic - Window goes off screen after Restoring from minimized state
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net
PInvoke.Net forum

Window goes off screen after Restoring from minimized state

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message
Ennair



Joined: 03 May 2010
Posts: 4

PostPosted: Mon May 03, 2010 8:31 pm    Post subject: Window goes off screen after Restoring from minimized state Reply with quote

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
View user's profile Send private message
Paul.Martin



Joined: 03 Feb 2010
Posts: 83
Location: Cambridgeshire

PostPosted: Tue May 04, 2010 3:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ennair



Joined: 03 May 2010
Posts: 4

PostPosted: Thu May 06, 2010 8:16 pm    Post subject: Reply with quote

SC_RESTORE = 0xF120 is never passed! I am testing on Vista - could that be why?
_________________
http://www.cheap.ennair.com
Back to top
View user's profile Send private message
Paul.Martin



Joined: 03 Feb 2010
Posts: 83
Location: Cambridgeshire

PostPosted: Fri May 07, 2010 11:43 am    Post subject: Reply with quote

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
View user's profile Send private message
Ennair



Joined: 03 May 2010
Posts: 4

PostPosted: Sat May 08, 2010 11:44 am    Post subject: Reply with quote

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
View user's profile Send private message
Paul.Martin



Joined: 03 Feb 2010
Posts: 83
Location: Cambridgeshire

PostPosted: Sun May 09, 2010 9:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ennair



Joined: 03 May 2010
Posts: 4

PostPosted: Fri May 14, 2010 7:54 am    Post subject: Reply with quote

no worries Smile I got it now - thanks
_________________
http://www.cheap.ennair.com
Back to top
View user's profile Send private message
Hippno



Joined: 17 Oct 2011
Posts: 1

PostPosted: Mon Oct 17, 2011 7:54 am    Post subject: Reply with quote

Thanks for step-by-step explanation, Paul, my issue has already been resolved. I'm very glad!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group