Solved

Bring application to foreground if not on top

Posted on 2002-04-10
14
207 Views
Last Modified: 2010-04-04
I have a tray icon application. If I click on the tray icon I want my application, if already visible with (Application.ShowMainForm := True;), to be displayed on top of all the other windows. Also if my application is not visible then it must be showed. So there are two states: 1. Application is visible but not on top & 2. Application is not visible. How will I know if it's visible and not on top so that I can put it on top? And how would I put it on top? Thanx in advance!
0
Comment
Question by:skynergy
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 17

Expert Comment

by:geobul
Comment Utility
Hi,

...
ShowWindow(Form1.Handle, SW_RESTORE);
SetForegroundWindow(Form1.Handle);
...

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
You don't need to know the current state. Just execute that two lines above when you want the form to be displayed.

Regards, Geo
0
 

Author Comment

by:skynergy
Comment Utility
Thanx for the reply. Geo answered my 2nd question. I still need my first question answered. Thanx.
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
listening
0
 
LVL 4

Expert Comment

by:jsweby
Comment Utility
Surely geobul answered both of your questions in one? The code he supplied will show the form and bring it to the top, regardless of whether it is currently showing or not.

J.
0
 
LVL 17

Accepted Solution

by:
geobul earned 50 total points
Comment Utility
If you mean 'Always on top' then set your form's FormStyle property to fsStayOnTop.

Otherwise I gave a solution. When your form is visible but overlapped by other windows, 'ShowWindow(Form1.Handle, SW_RESTORE);' does nothing and SetForegroundWindow brings the form over other normal windows. The form gets focused also.

Regards, Geo
0
 

Author Comment

by:skynergy
Comment Utility
I need to know if my form is below some other windows so that I can use the code provided by GEO. I am aware that if I use the code the window will go to the top. But I am looking for the windows API to determine if my application is not the foreground window and then bring it to the top if I want to. Sorry, guys it might sound silly but that's more imortant than getting it to the top. Thanx!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:jsweby
Comment Utility
Out of interest, why?
0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
Well, IsIconic(Form1.Handle) will return true if the window is minimized and false if it's not.

Regards, Geo
0
 
LVL 17

Expert Comment

by:inthe
Comment Utility
one idea GetForeGroundWindow(), will give the handle of current foreground window check it with the handle of your form.
0
 

Author Comment

by:skynergy
Comment Utility
Good idea Inthe. I will come back to you thanx!
0
 

Author Comment

by:skynergy
Comment Utility
var
   pCh: PChar;
begin
  pCh := StrAlloc(1024);
  GetWindowText(GetForegroundWindow, pCh, 1024);
  if Application.Title = StrPas(pCh) then
     DoSomeThing;
  StrDispose(pCh);


This is working well in my application. What do you think? Just comparing the Handles didn't do it. Unless I didn't do it incorrectly.
0
 
LVL 17

Expert Comment

by:inthe
Comment Utility
its actually better than comparing handles as always  possibility(although really a remote one) that handle could change
0
 

Author Comment

by:skynergy
Comment Utility
Thanx for the help guys. Geo I'm going to give you the points seeing that you answered first and helped me along the way, thanx! Thanx for all the other input.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now