Bring application to foreground if not on top

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!
skynergyAsked:
Who is Participating?
 
geobulConnect With a Mentor Commented:
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
 
geobulCommented:
Hi,

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

Regards, Geo
0
 
geobulCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
skynergyAuthor Commented:
Thanx for the reply. Geo answered my 2nd question. I still need my first question answered. Thanx.
0
 
ginsonicCommented:
listening
0
 
jswebyCommented:
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
 
skynergyAuthor Commented:
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
 
jswebyCommented:
Out of interest, why?
0
 
geobulCommented:
Well, IsIconic(Form1.Handle) will return true if the window is minimized and false if it's not.

Regards, Geo
0
 
intheCommented:
one idea GetForeGroundWindow(), will give the handle of current foreground window check it with the handle of your form.
0
 
skynergyAuthor Commented:
Good idea Inthe. I will come back to you thanx!
0
 
skynergyAuthor Commented:
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
 
intheCommented:
its actually better than comparing handles as always  possibility(although really a remote one) that handle could change
0
 
skynergyAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.