Solved

Bringing window to front

Posted on 2002-05-06
5
702 Views
Last Modified: 2010-04-04
Simple question, but I couldnt find the answer :o(

How do I bring a window to front, when it is overlapped by system windows?

Application.BringToFront;
Form2.BringToFront;

This works if an application like Outlook is in front, but if a dos- or folder window is overlapping form2, then it doesnt come to front!

0
Comment
Question by:pede
5 Comments
 
LVL 7

Expert Comment

by:God_Ares
ID: 6991334
formstyle := fsStayOnTop;
0
 
LVL 17

Expert Comment

by:inthe
ID: 6991339
try:

SetForegroundWindow(Form1.handle);
it depends on what operating system your using as there are big diference in how 98/nt/2k handle doing this

this is cut from a question the other day about same topic

<start paste>

ok the following is getting silly but im guesssing your using 2k or something else setforegroundwindow
would be working.
its not small but is nessesary ,it came form the borland newsgroups with parts from different sources.
http://groups.google.com/groups?q=forceforegroundwindow+delphi+ray&hl=en&selm=3A62F81B.7790A088%40ia.comweb.nl&rnum=1

so you can call

 ForceForeground(handle);

function ForceForeground(AppHandle:HWND): boolean;
const
SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
var
ForegroundThreadID: DWORD;
ThisThreadID      : DWORD;
timeout           : DWORD;
OSVersionInfo     : TOSVersionInfo;
Win32Platform     : Integer;
begin
if IsIconic(AppHandle) then ShowWindow(AppHandle, SW_RESTORE);
if (GetForegroundWindow = AppHandle) then Result := true else
begin
 Win32Platform := 0;
 OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
 if GetVersionEx(OSVersionInfo) then Win32Platform := OSVersionInfo.dwPlatformId;

 { Windows 98/2000 doesn't want to foreground a window when some other window has keyboard focus}

 if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (OSVersionInfo.dwMajorVersion > 4)) or
    ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and ((OSVersionInfo.dwMajorVersion > 4) or
    ((OSVersionInfo.dwMajorVersion = 4) and (OSVersionInfo.dwMinorVersion > 0)))) then
 begin
   Result := false;
   ForegroundThreadID := GetWindowThreadProcessID(GetForegroundWindow,nil);
   ThisThreadID := GetWindowThreadPRocessId(AppHandle,nil);
   if AttachThreadInput(ThisThreadID, ForegroundThreadID, true) then
   begin
     BringWindowToTop(AppHandle);
     SetForegroundWindow(AppHandle);
     AttachThreadInput(ThisThreadID, ForegroundThreadID, false);
     Result := (GetForegroundWindow = AppHandle);
   end;
   if not Result then
   begin
     SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);
     SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0), SPIF_SENDCHANGE);
     BringWindowToTop(AppHandle);
     SetForegroundWindow(AppHandle);
     SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(timeout), SPIF_SENDCHANGE);
     Result := (GetForegroundWindow = AppHandle);
     if not Result then
       begin
       ShowWindow(AppHandle,SW_HIDE);
       ShowWindow(AppHandle,SW_SHOWMINIMIZED);
       ShowWindow(AppHandle,SW_SHOWNORMAL);
       BringWindowToTop(AppHandle);
       SetForegroundWindow(AppHandle);
       end;
   end;
 end else
 begin
   BringWindowToTop(AppHandle);
   SetForegroundWindow(AppHandle);
 end;
 Result := (GetForegroundWindow = AppHandle);
end;
end;



see here for details of a new win2k api allowsetforegroundwindow:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/windows_5fw7.asp

<end paste>
0
 
LVL 2

Accepted Solution

by:
freshman3k earned 50 total points
ID: 6992535
Hello!

Call This instead of your code,that you showed above:
SetWindowPos(Form1.handle, HWND_TopMost,0,0,0,0,SWP_NOMove or SWP_NOSize);
SetWindowPos(Form2.handle, HWND_TopMost,0,0,0,0,SWP_NOMove or SWP_NOSize);

Good Luck!

0
 
LVL 1

Author Comment

by:pede
ID: 6993468
Having a look at it, thank you!
0
 
LVL 1

Author Comment

by:pede
ID: 6993505
Freshman3k's solution works with very little code, so I gave him the points. But thanks for all comments!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using idhttp to login to instagram 2 90
Delphi XE10 Round Image 2 129
drawing animated level bar based on numbers 3 97
Error E2158 compiling with Delphi XE10 Seattle 2 100
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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