Click here to Skip to main content
15,887,886 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I use ShowWindow(hWnd, SW_RESTORE)function to restore (means display and activate) a minimized window form application and shows this on the screen. This function is working for one window form application but is not working for the another one that I need.
I used following code
[DllImport("user32.dll")]
        private static extern bool IsIconic(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern int SetForegroundWindow(IntPtr hWnd);
        private const int SW_RESTORE = 9;

IntPtr hWnd =(IntPtr)System.Diagnostics.Process.GetProcessById(proc[0].Id).MainWindowHandle;
                if (!hWnd.Equals(IntPtr.Zero))
                {
                    if (IsIconic(hWnd))
                    {
                        int x = ShowWindow(hWnd, SW_RESTORE);                                               
                    }
                    SetForegroundWindow(hWnd);
                }
Posted
Updated 3-May-11 1:06am
v3

1 solution

When you say "not working", are you getting a (non-zero) window handle OK or not?
Is IsIconic returning true (non-zero)?
What dows ShowWindow return?
When minimised, is it still showing in the task bar?
Was it visible before it was minimised?
Was a sensible size set before it was minimised?
Do the main windows have any child / owned windows that could be interfering (modal dialogs in progress?)?
Are you sure that the window you are after is the MAIN window - see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx[^] -
SQL
The main window is the window that is created when the process is started. After initialization, other windows may be opened, including the Modal and TopLevel windows, but the first window associated with the process remains the main window.
and subsequent notes for various possible issues.
 
Share this answer
 
Comments
Varshney Manish 3-May-11 7:46am    
yes showwindow function returns a non zero value ie 16 means it runs successfully but the minimized window does not populate on the screen.
yes one more issue, IsIconic returns true for the first window application and false for the minimized one that does not populate on the screen.
yes when minimized, it is shown in the taskbar.
yes it was visible before it was minimized.
no, the main window(minimized) does not have any child window.
NuttingCDEF 3-May-11 8:36am    
IsIconic returns false - so Windows doesn't think it was a minimised window in the first place (see http://msdn.microsoft.com/en-us/library/ms633527(v=vs.85).aspx).

ShowWindow returns 16 - so window was previously visible (see http://msdn.microsoft.com/en-us/library/ms633548(VS.85).aspx).

Can you use Spy++ to check whether the hWnd you have is the right one - restore the window you are after manually and use Spy Find Window to find out what its handle is - then minimise it and then try your app to see what hWnd it is getting. If it's not the same, then the window you are after probably isn't the app's MAIN window, so MainWindowHandle isn't what you want.

I'll see if I can work out an easy way to enumerate top level windows of a process!!!!!
Varshney Manish 4-May-11 3:24am    
Thanks..
NuttingCDEF 3-May-11 8:53am    
Haven't tried it, but if the MAIN window isn't iconic, try using it as a starting point for iteration through windows with the GetNextWindow API - but one thing I don't know is whether this will iterate all top level windows for all processes or whether it will restrict to a single process. So maybe also need to use GetWindowThreadProcessId to check what process owns the window (note the 2nd out parameter that gives the process ID - return value is thread ID).

This all feels very messy for what seems as though it ought to be simple . . .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900