Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to restore / set focus to an application after re-running the executable Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
04-Oct-02
Category
System
Language
Delphi 5.x
Views
99
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius 

I'm trying to restore/ set focus to my app after re-running the exe. I've tried 
using the Windows.Setfocus(FormHandle) command without success. I've also tried 
using ShowWindow. Doing this doesn't set focus to the window. If the the window is 
minimizied it restores it to the screen ok but the application still believes it is 
minimized, thus you can't minimize the window. You can overcome this by first right 
clicking the app's taskbar button and selecting restore. The minimize button then 
works correctly.

Answer:

You have to deal with the first instances Application window, not with the main 
form.
1   
2   {$R *.RES}
3   
4   function AlreadyRunning: Boolean;
5   var
6     wndmain, wndapp: HWND;
7   begin
8     wndmain := FindWindow('TMDIForm', nil);
9     {should really use a more unique classname}
10    result := wndmain <> 0;
11    if result then
12    begin
13      wndapp := GetWindowLong(wndmain, GWL_WNDPARENT);
14      if IsIconic(wndapp) then
15        SendMessage(wndapp, WM_SYSCOMMAND, SC_RESTORE, 0)
16      else
17        SetForegroundWindow(wndapp);
18    end;
19  end;
20  
21  begin
22    if AlreadyRunning then
23      Exit;
24    Application.Initialize;
25    Application.Title := 'J&S Library Manager';
26    Application.CreateForm(TMDIForm, MDIForm);
27    Application.CreateForm(TEditTextForm, EditTextForm);
28    Application.CreateForm(TOptionForm, OptionForm);
29    Application.CreateForm(TAboutBox, AboutBox);
30    Application.Run;
31  end.


I have a deep aversion against directly manipulating a window from outside, so I usually don't restore/show the first instances window from the second instance but instead send a message to the first instances main form and have it restore/show itself in a handler for the message. Using WM_COPYDATA it is also easy to pass on a commandline to the first instance this way.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC