Solved

"Always on top" with several forms

Posted on 2001-07-19
21
3,720 Views
Last Modified: 2012-05-04
Hi

I've got 2 forms: Form1 and Form2
when I set Form1's 'FormStyle' property to 'StayOnTop' Form1 stays on top of any other on the DESKTOP
(just like I want it to). However, if I set Form2's FormStyle property to StayOnTop Form2 still stays
(always) hover Form1 but they both stay below any other window (other application) that comes along.

I want the forms to be independent from each other (to set one of them to stay on top of any other
window in the desktop, or to set both of them to stay on top of any other window in the desktop) and
to be selected independently (when I select one of them it should come to the foreground leaving the
other form in the Z order in wich it was).


I got this far:

I've set the 'ParentWindow' property as follows:


     parentWindow := getDesktopWindow;
     parent := nil;

and this seemed to had worked. The only problem now is that both forms can't seem to get focus.


I'm not sure this is the right approach...


    Thanks in advance

      Filipe
0
Comment
Question by:filipecorreia
  • 7
  • 4
  • 2
  • +6
21 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6299243
I can't reproduce your problem...
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6299341
try this in somewhere event (not tested)

SetWindowPos(Form2.Handle,hwnd_Topmost,0,0,0,0,swp_NoMove or swp_NoSize or swp_NoActivate);
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6299343
Set Form2.Visible := true in Form1.OnActivate;
0
 
LVL 1

Author Comment

by:filipecorreia
ID: 6300099
Epsylon: what's the part you can't reproduce?


kretzschmar: I had already tried that... no luck


gemarti: No result when I apply your suggestion. What exactly was your idea?
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6300130
Maybe I just don't understand your question...
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6300375
Well with one of them you could use a timer forcing that form to stay on top but if you use a timer for both them they will continuely flip-flop, which I imagine that is not the desire affect you want.


The Crazy One
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6300383
>>>The only problem now is that both forms can't seem to get focus

Umm I believe only one form at a time can get focus.
0
 
LVL 1

Expert Comment

by:alx512
ID: 6301130
If you use ShowModal function replace it to Show;
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 220 total points
ID: 6301155
found this in the paq (from dumani)

--paste begin

Application.OnDeactivate := OnTop;

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

that will be real stayontop form for child forms...

--paste end

assign the ontop procedure in the formcreate-event of form1 to the application.ondeactivate-event

hope this helps
(not tested by maself)

meikl ;-)

0
 
LVL 20

Expert Comment

by:Madshi
ID: 6301276
Maybe this helps?

type
  TForm1 = class...
    ...
    procedure CreateParams(var Params: TCreateParams);
  end;

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.Style := (Params.Style and (not WS_CHILD)) or WS_POPUP;
  Params.WndParent := 0;
end;

Try this instead of your...

    parentWindow := getDesktopWindow;
    parent := nil;

... code. Does it make things better?

Regards, Madshi.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:geobul
ID: 6301537
Hi,
kretzschmar's solution does the job. You can even write:

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

and everithing is OK for me.

Regards, Geo
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 6303308
Wow I have wondered how to accomplish this for a long time.  I had messed with SetWindowPos but always placed the code in the form's OnDeactivate event which never seemed to work.  I never even thought to place it in the applications OnDeactivate event!

Geez now I feel kind of guilty, getting the answer and not spending any points :-)
0
 
LVL 1

Author Comment

by:filipecorreia
ID: 6303798

Hello experts!


-----------------
CrazyOne:

a) I don't want to use a timer (even if I could find a way to use one) because I'm afraid it may
become all too slow.

b) >>Umm I believe only one form at a time can get focus.
   Of course I mean one at a time...  ;)

-----------------
kretzschmar:

I've had already tried that... and I get the same behavior. But your post gave me another idea...
I followed what you said and have putted the 'Application.OnDeactivate' pointiong to the same function
as 'Form2.OnDeactivate' making the forms stay above any other on the desktop when I switch beetween apps.

..but here's something else I must solve (check out my next post)...

-----------------
Madshi:

I'm not realy using delphi (but borland's C++ builder) so I found some difficulties trying
out your code (that 'inherited' part specialy).
Anyway... I ended up making a Window's API call like this:


SetWindowLong(Form1.Handle, GWL_STYLE, (GetWindowLong(Form1.Handle, GWL_STYLE) and (not WS_CHILD)) or WS_POPUP);
Form1.ParentWindow=0;


I think it does just about the same as the code you posted (please correct me if that's not right).

I got a very weird behavior:
First I tried to put this code in a button. and when I pushed it the form just
disapeard!!! it was still drawn on the screen but when i refreshed that part of the screen (draging any
other window hover it) reveald that it actualy wasn't there anymore.
I've also tried to put the code in the form's constructor. This way the form doesn't disapear but the
form's borders do.

Using this last way I managed to do my testings but the form's still have the same behavior...

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


  Filipe
0
 
LVL 1

Author Comment

by:filipecorreia
ID: 6303811
Now there is only one problem left:
supposing that I have't (yet) made the forms be 'always on top'...

Imagine that form1 is beind other app's window and form2 is visible. When I click form2, the other
form(form1), should NOT come to the top of the other app's window.

This problem would (supposely) be solved by the code I mentioned

"    parentWindow := getDesktopWindow;
    parent := nil;  "

putting each form independent of each other when selecting any of them.


Do you any other way to accomplish this?


 Thanks in advance


PS:
   MannSoft: If you feel so guilty you can always post a blank question and accept whatever answer I post...  
there is... If you just can't take it...   ;)
0
 
LVL 1

Author Comment

by:filipecorreia
ID: 6309875
no suggestions?
0
 
LVL 1

Author Comment

by:filipecorreia
ID: 6309879
Madshi: do you think that what I did is the same as the code you posted?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6309963
No, not exactly, but I don't know BCB so deep. And I also don't know (at all), whether my suggestion would really help you, if you could use it exactly like that in BCB...  :-(

Regards, Madshi.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6316556
no further suggestion, and now?
0
 
LVL 1

Author Comment

by:filipecorreia
ID: 6330071
Hi!

kretzschmar: I will give YOU the points. but I won't give you the highest grade because:

 you just gave me an idea that helped me to get to the real sollution and

 you cannot give me any hint to how to solve the part of the problem still missing.


Are you all right with that?



 bye

   Filipe Correia
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6330144
do as you want to do, its your decission, and i will accept it, but please no D-Grade :-)
0
 
LVL 1

Author Comment

by:filipecorreia
ID: 6335852
Sorry..  it just wouldn't be right to give you any other grade.

 bye
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
loading dll from external path 1 210
Machine not responding during CopyFile() 3 89
find a node in VST 2 63
Press three keys together and trigger a function 3 50
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now