Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Bring application to foreground if not on top

Posted on 2002-04-10
14
Medium Priority
?
218 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
ID: 6931281
Hi,

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

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 6931301
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
ID: 6931339
Thanx for the reply. Geo answered my 2nd question. I still need my first question answered. Thanx.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:ginsonic
ID: 6931511
listening
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6931595
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 200 total points
ID: 6931610
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
ID: 6931647
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
 
LVL 4

Expert Comment

by:jsweby
ID: 6931654
Out of interest, why?
0
 
LVL 17

Expert Comment

by:geobul
ID: 6931679
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
ID: 6932131
one idea GetForeGroundWindow(), will give the handle of current foreground window check it with the handle of your form.
0
 

Author Comment

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

Author Comment

by:skynergy
ID: 6933454
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
ID: 6933665
its actually better than comparing handles as always  possibility(although really a remote one) that handle could change
0
 

Author Comment

by:skynergy
ID: 6933709
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

971 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