Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Restore original window on previous instance
Author:
Bernie Madigan
Website:
http://bernie.id.au
Submitted:
5/13/2005
Version:
VB6
Compatibility:
VB6
Category:
Miscellaneous
Downloads:
3974
RESTORE WINDOW TO ITS ORIGINAL STATE WHEN PREVIOUS INSTANCE IS INVOKED OVERVIEW Whenever a Visual Basic program is already running and its icon is double clicked, a new instance will be started. It is commonly expected that the running program would be restored instead of a second instance commencing. Whilst Visual Basic provides the App.PrevInstance test, restoring the window from minimized to its former position is not straight forward. Once the program is already running its window handle (hWnd) will be lost when trying to call it from a second instance, which will generate a new handle. A common way to find the original handle, which is defined by the window's caption, is to rename the form's caption when the second instance starts then search for the window with the original one. The handle is the value of the function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long The problem with this method is that this API declaration will return the handle of any window open which has the same caption name. Accordingly, if the same program is running from two different paths then the icon from one can restore a window from the other, and vice-versa. It can also restore any other minimized folder window with the same caption as the program. OPERATION To make a visual basic program reliably restore as expected when its second instance was invoked, I wrote this module. The first time the program is run, it saves the hWnd to the registry in a key which includes up to the last 60 characters of the program path to guarantee it is unique to that particular program. Then if the same program is run again again it must access the same key. Due to limitations of the windows API used to restore windows it was necessary to draw the distinction between restoring a previously maximized window to a normal one. Therefore the WindowState is also written to the registry on minimising allowing it to be restored to the original state. CODING To test it, just add the module to your program. Next, add the following commands to the Form events. Form_Load() CheckForPreviousInstance Me Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) KillPreviousInstanceRegistryEntry Form_Resize() RecordWindowStateInRegistry Me REGISTRY KEYS It deletes all registry information on closing. The address of the key is: HKEY_CURRENT_USER\Software\VB and VBA Program Settings\VB6 Temporary Scratch Pad {blahblahblah} You might like to open this key and observe what happens whilst the program is opened, minimized, restored and closed. You will see that a unique key is created each time the routine is run and deleted each time it is stopped. FINALLY If you have any questions just make contact by visiting my personal home page at www.bernie.id.au
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement