﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » .NET Forums » General .NET Discussion  » How to Restore application from minimized state</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Tue, 09 Jun 2026 05:28:54 GMT</lastBuildDate><ttl>20</ttl><item><title>How to Restore application from minimized state</title><link>http://forum.strataframe.net/FindPost7408.aspx</link><description>If I launch an exe from my menu system and minimizes it, how can I restore the application or bring focus back to the application if they happen to click on the menu option again?</description><pubDate>Tue, 13 Mar 2007 11:01:19 GMT</pubDate><dc:creator>Tim Dol</dc:creator></item><item><title>RE: How to Restore application from minimized state</title><link>http://forum.strataframe.net/FindPost7445.aspx</link><description>Well, since you are using n MDI form you can cycle through all of the instantiated forms through the forms collection:&lt;/P&gt;&lt;P&gt;[codesnippet]Public Function RestoreForm(ByVal FormType As System.Type) As Boolean&lt;BR&gt;Dim loForm As System.Windows.Forms.Form&lt;BR&gt;Dim llReturn As Boolean = False&lt;/P&gt;&lt;P&gt;For Each loForm In MyMDI.MdiChildren&lt;BR&gt;&amp;nbsp;&amp;nbsp; If loForm.GetType() Is FormType&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; llReturn = True&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If loForm.WindowState = FormWindowState.Minimized Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; loForm.WindowState = FormWindowState.Normal&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;BR&gt;&amp;nbsp;&amp;nbsp; End If&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;Next&lt;/P&gt;&lt;P&gt;Return llReturn&lt;BR&gt;End Function[/codesnippet]&lt;/P&gt;&lt;P&gt;You would call it like this:&lt;/P&gt;&lt;P&gt;[codesnippet]If Not RestoreForm(GetType(MyForm)) Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Launch your form&lt;BR&gt;End If[/codesnippet]</description><pubDate>Tue, 13 Mar 2007 11:01:19 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: How to Restore application from minimized state</title><link>http://forum.strataframe.net/FindPost7436.aspx</link><description>Thanks Trent, this is exactly what I was looking for.&lt;/P&gt;&lt;P&gt;How do you handle something like the security dialog.&amp;nbsp;&amp;nbsp;Right now I &amp;nbsp;launch it in a separate window using loDialog.Show() because I have problems with scroll bars when I launch inside my MDI. (I just haven't had time to investigate why you can't scroll properly).&lt;/P&gt;&lt;P&gt;If I minimize the security dialog and then try to activate again through the menu system, I want to restore the existing instance to prevent multiple instances. How do I accomplish something like this.&lt;/P&gt;&lt;P&gt;Thanks</description><pubDate>Tue, 13 Mar 2007 08:33:34 GMT</pubDate><dc:creator>Tim Dol</dc:creator></item><item><title>RE: How to Restore application from minimized state</title><link>http://forum.strataframe.net/FindPost7416.aspx</link><description>If the application that your menu launched is not a .NET form then you have to use WinAPI to restore the application.&amp;nbsp; You will need to retrieve the window handle of the applcation.&amp;nbsp; You can do this a number of different ways, such as the GetWindow method.&amp;nbsp; You can use many of the APIs in conjunction with one another in order to retrieve the correct window handle.&lt;P&gt;In our medical application, we store off the window handle when we call the external program:&lt;/P&gt;&lt;P&gt;[codesnippet]Private _ProgramHandle As IntPtr&lt;/P&gt;&lt;P&gt;Private Sub LaunchExternalEXE()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Establish Locals&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim loStart As New System.Diagnostics.ProcessStartInfo("C:\MyPath\MyExe.EXE")&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim loProcess As System.Diagnostics.Process&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; loStart.WindowStyle = Diagnostics.ProcessWindowStyle.Normal&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Start the process&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; loProcess = System.Diagnostics.Process.Start(loStart)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Wait until the window handle can be obtained&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Do While _ProgramHandle.ToInt32() = 0&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _ProgramHandle = System.Diagnostics.Process.GetProcessById(loProcess.Id).MainWindowHandle&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Loop&lt;BR&gt;End Sub[/codesnippet]&lt;/P&gt;&lt;P&gt;Now that you have the handle stored off, you can restore the window using WinAPI when the user clicks the menu item again.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a list of the WinAPIs you will have to declare:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [codesnippet]Public Const SW_SHOWMAXIMIZED As Integer = 3&lt;BR&gt;Public Const SW_RESTORE As Integer = 9&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;DllImport("user32.dll")&amp;gt; _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Function ShowWindow(ByVal WindowHandle As Integer, ByVal Command As Integer) As Boolean&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Function&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;DllImport("user32.dll")&amp;gt; _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Function SetForegroundWindow(ByVal WindowHandle As Integer) As Boolean&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Function&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;DllImport("user32.dll")&amp;gt; _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Function SetActiveWindow(ByVal WindowHandle As Integer) As Boolean&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Function&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;DllImport("user32.dll")&amp;gt; _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Function SetFocus(ByVal hwnd As Integer) As Integer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Function[/codesnippet]&lt;/P&gt;&lt;P&gt;I added the SW_MAXIMIZED value above in case you needed to test on a maximized &amp;#119;indow.&amp;nbsp; Then you can use those declared WinAPI methods like this:&lt;/P&gt;&lt;P&gt;[codesnippet]SetForegroundWindow(_ProgramHandle.ToInt32())&lt;BR&gt;SetActiveWindow(_ProgramHandle.ToInt32())&lt;BR&gt;ShowWindow(_ProgramHandle.ToInt32(), SW_RESTORE)[/codesnippet]</description><pubDate>Mon, 12 Mar 2007 12:26:27 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item></channel></rss>