Window goes off screen after Restoring from minimized state

Announcements and discussion for PInvoke

Moderators: Luke Jefferson, Robert

Window goes off screen after Restoring from minimized state

Postby Ennair » Mon May 03, 2010 8:31 pm

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?
Ennair
 
Posts: 4
Joined: Mon May 03, 2010 8:20 pm

Postby Paul.Martin » Tue May 04, 2010 3:50 pm

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: Select all
        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);
        }
Paul.Martin
 
Posts: 83
Joined: Wed Feb 03, 2010 11:20 am
Location: Cambridgeshire

Postby Ennair » Thu May 06, 2010 8:16 pm

SC_RESTORE = 0xF120 is never passed! I am testing on Vista - could that be why?
Ennair
 
Posts: 4
Joined: Mon May 03, 2010 8:20 pm

Postby Paul.Martin » Fri May 07, 2010 11:43 am

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.
Paul.Martin
 
Posts: 83
Joined: Wed Feb 03, 2010 11:20 am
Location: Cambridgeshire

Postby Ennair » Sat May 08, 2010 11:44 am

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.
Ennair
 
Posts: 4
Joined: Mon May 03, 2010 8:20 pm

Postby Paul.Martin » Sun May 09, 2010 9:12 pm

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.
Paul.Martin
 
Posts: 83
Joined: Wed Feb 03, 2010 11:20 am
Location: Cambridgeshire

Postby Ennair » Fri May 14, 2010 7:54 am

no worries :) I got it now - thanks
Ennair
 
Posts: 4
Joined: Mon May 03, 2010 8:20 pm

Postby Hippno » Mon Oct 17, 2011 7:54 am

Thanks for step-by-step explanation, Paul, my issue has already been resolved. I'm very glad!
Hippno
 
Posts: 1
Joined: Mon Oct 17, 2011 7:43 am


Return to PInvoke.Net

Who is online

Users browsing this forum: No registered users and 0 guests

cron