We help IT Professionals succeed at work.

Form to be put on top

HansB
HansB asked
on
I have designed a "Notifier" that is supposed to remind me about things I have to do at a certain hour.

The form shows itself and is active by means of the SetFocus command when the time has come but NOT on top of the application that I am using at the very moment i.g. Excel.

Thus, I do not get aware of the alarm. Of course there could be an acoustic alarm also but I would prefer it more discreet.

Hans
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004

Commented:
? maybe

set the formstyle to fsStayOnTop

Commented:
go to
www.swissdelphicenter.ch 
to programing tips to system section you'll find it there
When the particular event triggers you basically need to setfocus AND BringToFront..

Application.SetFocus;
Application.BringToFront;

Thes two lines will bring the form/application to the foreground of you display.
Is that what you want????

Madmarlin

Commented:
I have had trouble getting my programs to the front in versions of windows later than 98, where they are made NOT to set another window into focus when it is getting keyboard input, here is a function I have used to force my app to the top without being "Stay on top"


function ForceForeground: boolean;
const
  SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
  SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
var
  ForegroundThreadID: DWORD;
  ThisThreadID      : DWORD;
  timeout           : DWORD;
  OSVersionInfo: TOSVersionInfo;
  Win32Platform: Integer;
begin
  Result := False;
  if IsIconic(Form1.Handle) then ShowWindow(Form1.Handle, SW_RESTORE);

  if (GetForegroundWindow = Form1.Handle) then Result := true
  else
    begin // first GetForeground
    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 // OS is New
      ForegroundThreadID := GetWindowThreadProcessID(GetForegroundWindow,nil);
      ThisThreadID := GetWindowThreadPRocessId(AppHandle,nil);
      if AttachThreadInput(ThisThreadID, ForegroundThreadID, true) then
        begin // attatchThread
        SetForegroundWindow(Form1.Handle);
        AttachThreadInput(ThisThreadID, ForegroundThreadID, false);
        Result := (GetForegroundWindow = Form1.Handle);
        end; // attatchThread
if Result then Exit else
  begin // Result
  SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);
  SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0), SPIF_SENDCHANGE);
  SetForegroundWindow(Form1.Handle);
  SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(timeout), SPIF_SENDCHANGE);
  end; // Result
Result := (GetForegroundWindow = Form1.Handle);
if Result then Exit else
   begin // Result
{in some win 2000 systems you need to use this}
     ShowWindow(Form1.Handle,SW_HIDE);
     ShowWindow(Form1.Handle,SW_SHOWMINIMIZED);
     ShowWindow(Form1.Handle,SW_SHOWNORMAL);
     BringWindowToTop(Form1.Handle);
     SetForegroundWindow(Form1.Handle);
   end; // Result
    end // OS is New
    else
    begin // else OS is New
      SetForegroundWindow(Form1.Handle);
    end; // else OS is new

    Result := (GetForegroundWindow = Form1.Handle);
  end; // first GetForeground
end;


 - - - - - - - - - - - - - -

if the function returns false you can make a sound

Author

Commented:
Application.SetFocus  did not work.

but this did !

Thank You !

Application.BringToFront
AttentionForm.SetFocus

Hans

Explore More ContentExplore courses, solutions, and other research materials related to this topic.