Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

running an application in foreground?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
one2001boy@yahoo.com - 15 Mar 2005 05:09 GMT
Hello,

I have an executable file lanuched from another application.
is there a way to make thie executable file foreground if it is launched
from other application? Thanks.
William DePalo [MVP VC++] - 15 Mar 2005 05:52 GMT
> I have an executable file lanuched from another application.
> is there a way to make thie executable file foreground if it is launched
> from other application? Thanks.

Check the docs for SwitchToThisWindow()

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/win
dowsuserinterface/windowing/windows/windowreference/windowfunctions/switchtothis
window.asp


and read the remarks section for the caveats.

Regards,
Will
one2001boy@yahoo.com - 16 Mar 2005 07:26 GMT
>>I have an executable file lanuched from another application.
>>is there a way to make thie executable file foreground if it is launched
[quoted text clipped - 5 lines]
>
> and read the remarks section for the caveats.

Will,

Thanks for the help.

I also find the function SetForegroundWindow().

My problem is that a1.exe launches a2.exe, and then a2.exe
launches a3.exe.

However, a3.exe cannot be displayed immediately.
Only after a2.exe is terminated, then a3.exe then can be displayed.
I think SetForegroundWindow() or SwitchToThisWindow() might be helpful,
but I am not sure if I should  put this function in a3.exe or a2.exe?

Thanks.

> Regards,
> Will
William DePalo [MVP VC++] - 19 Mar 2005 05:43 GMT
> Thanks for the help.

You are welcome.

> I also find the function SetForegroundWindow().

It works on '95 and NT4. It has been intentionally crippled on 98/Me/2K/XP
and 2K+3.

> My problem is that a1.exe launches a2.exe, and then a2.exe
> launches a3.exe.
> However, a3.exe cannot be displayed immediately.
> Only after a2.exe is terminated, then a3.exe then can be displayed.
> I think SetForegroundWindow() or SwitchToThisWindow() might be helpful,
> but I am not sure if I should  put this function in a3.exe or a2.exe?

If you must, try calling SwitchToThisWindow() in a3 passing the handle of
its main window.

Note that many people, including me, will immediately uninstall an
application which "steals" the forreground. The bad behavior of so many
applications that did this is the reason why SetForegroundWindow() has been
crippled and why SwitchToThisWindow() is on its way out.

Regards,
Will
one2001boy@yahoo.com - 20 Mar 2005 21:43 GMT
>>Thanks for the help.
>
[quoted text clipped - 19 lines]
> applications that did this is the reason why SetForegroundWindow() has been
> crippled and why SwitchToThisWindow() is on its way out.

I have an applicaiton a1.exe calls a2.exe, a2.exe calls a3.exe.
I have to kill a1.exe and then a3.exe will run.
I added SwitchToThisWindow() on a3.exe, I still need to kill a1.exe to
get a3.exe to run. Not sure how to fix this problem.

If used notepad.exe to
replace a3.exe, I find that I can run a3.exe without the need to kill
a1.exe.

Not sure if there are some tricks over there?

Thanks.

> Regards,
> Will
Fredrik Wahlgren - 16 Mar 2005 14:12 GMT
> Hello,
>
> I have an executable file lanuched from another application.
> is there a way to make thie executable file foreground if it is launched
> from other application? Thanks.

http://216.239.59.104/search?q=cache:R77OL2MU0KMJ:www.etree.com/tech/Articles/at
tachthreadinput.pdf+foreground+GetWindowThreadProcessId&hl=sv


The trick is to use GetWindowThreadProcessId and  AttachThreadInput

/Fredrik
Fredrik Wahlgren - 16 Mar 2005 14:19 GMT
> Hello,
>
> I have an executable file lanuched from another application.
> is there a way to make thie executable file foreground if it is launched
> from other application? Thanks.

+void ReallySetForegroundWindow( HWND ahWnd )
+{
+    DWORD foregroundThreadID; // foreground window thread
+    DWORD ourThreadID; // our active thread
+    static char foo[1024];
+    HWND h = GetForegroundWindow();
+
+    // If the window is in a minimized state, maximize now
+    if (GetWindowLong(ahWnd, GWL_STYLE) & WS_MINIMIZE)
+    {
+        ShowWindow(ahWnd, SW_MAXIMIZE);
+        UpdateWindow(ahWnd);
+    }
+
+    // Check to see if we are the foreground thread
+    foregroundThreadID = GetWindowThreadProcessId(GetForegroundWindow(),
NULL);
+    ourThreadID = GetCurrentThreadId();
+
+    // If not, attach our thread's 'input' to the foreground thread's
+    if (foregroundThreadID != ourThreadID)
+        AttachThreadInput(foregroundThreadID, ourThreadID, TRUE);
+
+    // Bring our window to the foreground
+    SetForegroundWindow(ahWnd);
+
+    // Set focus.
+    SetFocus( ahWnd );
+
+    // If we attached our thread, detach it now
+    if (foregroundThreadID != ourThreadID)
+        AttachThreadInput(foregroundThreadID, ourThreadID, FALSE);
+
+    // Force our window to redraw
+    InvalidateRect(ahWnd, NULL, TRUE);
+}/Fredrik
one2001boy@yahoo.com - 19 Mar 2005 05:22 GMT
>>Hello,
>>
[quoted text clipped - 38 lines]
> +    InvalidateRect(ahWnd, NULL, TRUE);
> +}/Fredrik

I tried the above code, and the function SwitchToThisWindow(). It
doesn't help. My problem is that I have a1.exe calls a2.exe, and a2.exe
calls a3.exe. a3.exe is a GUI application, it is not started untill I
kills a1.exe. I am not sure if there is a way to bring a3.exe to the
foreground and make it run without killing a1.exe?

Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.