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

x
?
Solved

STAY ON TOP - no matter what.

Posted on 2002-03-19
17
Medium Priority
?
407 Views
Last Modified: 2010-04-05
I need my form to stay on top of everything.  Setting the stay on top property to true is not enough, because when I start another program which also is stay on top, it covers my form.

A good example would be the windows NT/2000/XP task manager.  I want my form to be able to cover it and any other windows/forms that may come along.

I know this is possible because I saw a program that could lock any windows workstation, even XP and 2K.
When you pressed the ctrl-shift-esc key to try to open the task-manager, it was like the program immediately got focus again and covered the task-manager even before I was able to see it.

200 points for a good solution and maybe even more.  This is pretty urgent.
What is a good way to do this?  I'd preferably not use any 3rd-party components though.

Thanks in advance,
Hagur
0
Comment
Question by:hagur
  • 5
  • 3
  • 2
  • +6
17 Comments
 
LVL 3

Assisted Solution

by:tongalite
tongalite earned 201 total points
ID: 6881435
Hi,
I had this in my tips bin... It might be of some help to you. Good luck
T.
...........................................
How to force a window 'always on top' without interference with other programs
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    procedure SetStayOnTop(OnTop: Boolean);
    procedure WinMsg(var Msg: TMsg; var Handled: Boolean);
  public
    property StayOnTop: boolean read FStayOnTop write SetStayOnTop;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const
  WM_ALWAYSONTOP = 99;

procedure TForm1.FormCreate(Sender: TObject);
begin
  inherited Create(AOwner);
  Application.OnMessage := WinMsg;
  AppendMenu(GetSystemMenu(Handle, False), MF_SEPARATOR, 0, '');
  AppendMenu(GetSystemMenu(Handle, False), MF_BYPOSITION, WM_ALWAYSONTOP,
'Always on &Top');
end;

procedure TForm1.WinMsg (var Msg: TMsg; var Handled: Boolean);
begin
  if Msg.message = WM_SYSCOMMAND then
    if Msg.WPARAM = WM_ALWAYSONTOP then
      StayOnTop := not StayOnTop;
end;

procedure TForm1.SetStayOnTop(OnTop: Boolean);
begin
  FStayOnTop := OnTop;

  if FStayOnTop then begin
    SetWindowPos(handle, HWND_TOPMOST, Left, Top, Width, Height, 0);
    CheckMenuItem(GetSystemMenu(Handle, False), WM_ALWAYSONTOP, MF_CHECKED);
  end
  else begin
    SetWindowPos(handle, HWND_NOTOPMOST, Left, Top, Width, Height, 0);
    CheckMenuItem(GetSystemMenu(Handle, False), WM_ALWAYSONTOP,
MF_UNCHECKED);
  end;
end;
end.
.................................................
0
 
LVL 3

Accepted Solution

by:
SteveWaite earned 0 total points
ID: 6881500
hi, just interested :)
0
 
LVL 5

Expert Comment

by:Gwena
ID: 6881900
listening :-)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Assisted Solution

by:geobul
geobul earned 198 total points
ID: 6882267
Hi,
Try this one:

type
  TForm1 = class(TForm)
    procedure OnTop(SendeR: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.OnTop(SendeR: TObject);
begin
  SetWindowPos(form1.handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOACTIVATE or SWP_NOSIZE);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnDeactivate := OnTop;
end;

Regards, Geo
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6882282
listening ... for moment
0
 
LVL 3

Author Comment

by:hagur
ID: 6883508
Thanks guys.

Tongalite: When I try to run your code, I always get an error in this line:
property StayOnTop: boolean read FStayOnTop write SetStayOnTop;

The error I get is: "Field or method identifier expected"
I'm using Delphi 5.0

Geobul: Your code almost works ... my program can now cover the task-manager, but when I use ALT-TAB to switch to the task manager, it sometimes manages to stay on top of my form, but sometimes not.  Weird!

Any solutions?
0
 
LVL 9

Assisted Solution

by:ginsonic
ginsonic earned 201 total points
ID: 6884759
Set the formStyle to fsStayOnTop and then handle WM_ENABLE and WM_WINDOWPOSCHANGING messages.

procedure WMEnable(var Msg: TWMEnable); message WM_ENABLE;
procedure WMWindowPosChanging(var Msg: TWMWindowPosMsg); message WM_WINDOWPOSCHANGING;
................

procedure TForm1.WMEnable(var Msg: TWMEnable);
begin
 inherited;
 if Visible and not Msg.Enabled then
 begin
   SetWindowPos(Handle, HWND_NOTOPMOST, 0, 0, 0, 0,
     SWP_NOSIZE or SWP_NOMOVE or SWP_NOACTIVATE);
   EnableWindow(Handle, True);
   Application.NormalizeAllTopMosts;
   SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
     SWP_NOSIZE or SWP_NOMOVE or SWP_NOACTIVATE);
 end;
end;

procedure TForm1.WMWindowPosChanging(var Msg: TWMWindowPosMsg);
begin
 inherited;
 if Visible then
   with Msg.WindowPos^ do
   begin
     if (flags and SWP_NOZORDER) = 0 then
       hwndInsertAfter := HWND_TOP;
   end;
end;
0
 

Expert Comment

by:surodimedjo
ID: 6886141
put this on formcreate

procedure TForm1.FormCreate(Sender: TObject);
begin
setwindowpos(form1.Handle,hwnd_notopmost,0,0,getsystemmetrics(sm_cxscreen),getsystemmetrics(sm_cyscreen),0);
setwindowpos(form1.Handle,hwnd_topmost,0,0,getsystemmetrics(sm_cxscreen),getsystemmetrics(sm_cyscreen),swp_nomove);
setforegroundwindow(form1.Handle);

SetWindowLongA(Handle,GWL_style,WS_EX_TOOLWINDOW);
end;

don't forget to build back door for your application, i use
  PopupMenu1: TPopupMenu;

and in the exit button
exit1: TMenuItem;

procedure TForm1.exit1Click(Sender: TObject);
begin
   close;
end;

i hope u can enjoy this answer
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6887865
surodimedjo,

I see that you are new expert here at Ex-Ex. We ( the other experts ) have a rule . We just add comments and not give directly answers.

The result of this rule is that we don't block the question and we let the freedom to asker to choice the best solution for her needs .

In same time he can ask more support to improve a comment from list .

Untill your answer will be evaluate ( accepted or rejected ) I doubt that someelse will add comments .

Best regards,
Nick

P.S. Of course you are free to do what you want.
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6889669
Hi hagur! I just warn you that those answers suggesting including setwindowpos in form create event will go wrong after user moves your program to another monitor or alters the screen resolution.
Regards,
Steve
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6890635
For this reason I suggest to hook WMWindowPosChanging messages.
0
 
LVL 17

Expert Comment

by:geobul
ID: 6890774
Hi,
I think that (SWP_NOMOVE or SWP_NOSIZE) in uFlags means that the function will ignore x,y,cx and cy parameters. So the window changes its Z order only. That's why there won't be any problems altering screen resolution or monitor type. Am I wrong?

Regards, Geo
0
 
LVL 3

Author Comment

by:hagur
ID: 6890888
surodimedjo:  Read Ginsonic's post to see why I rejected your answer.
0
 
LVL 3

Author Comment

by:hagur
ID: 6890906
ginsonic:

This isn't working .... the form can not cover other forms that have the "stay on top" form-style.
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6891130
thinking to another way ..........
0
 
LVL 1

Expert Comment

by:xsoft
ID: 6961465
listening
0
 

Expert Comment

by:CleanupPing
ID: 9343243
hagur:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
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…

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