Solved

Issues with putting a form behind everything else on the desktop

Posted on 2004-10-13
20
190 Views
Last Modified: 2010-04-05
Hello.

I'm hoping someone can clarify an issue for me, and suggest a solution.

I need to display a form containing data collected, when it arrives, but I
want the form to appear behind whatever other windows are showing on the user's
desktop, so as not to disturb whatever they may be doing at the moment.
(By the way, the application's main window is not displayed, either, but
rather "minimized to the icon tray")

As long as I let Delphi auto-create the form for me, the following worked
fine:

  if PopUnder then
  begin
    ShowWindow(frmNewMail.Handle, SW_SHOWNOACTIVATE);
    frmNewMail.Visible := true;
  end

The form does not receive focus AND it appears behind everything else on the
desktop. The form was removed from view using a standard Close, when the
user clicked a button.


Now, I wish to create this form at run-time, and it has been removed
from Delphi's auto-create list. Instead I use:

  if not VNewMailWinExists then
  begin
    VNewMailWinExists := true;
    vNewMailWin := TfrmVNewMail.Create;
  end;
......
    if PopUnder then
    begin
      vNewMailWin.SendToBack;
      ShowWindow(vNewMailWin.Handle, SW_SHOWNOACTIVATE);
      vNewMailWin.Visible := true;
    end

And the form is set to caFree when asked to Close.
With this scheme, the Form behaves as expected the first time it is
displayed, but on subsequent runs of the above code lines, the form appears
in front of everything else (but unfocused, as expected).

(The forms Create method has been overridden, and the Owner is set to Nil,
rather than Application. Setting Owner to Application (as a test) did not
seem to have any effect, though)


So, the question is, can someone please explain why this is happening, i.e.
why does it appear behind on the first run, and on top after that?
And why is a run time created form behaving differently from a Delphi
created one?
A suggestion for a good way to do this will be appreciated.


FYI, adding:

      SetWindowPos(vNewMailWin.Handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE
or SWP_NOSIZE or SWP_NOACTIVATE);

works, but seems to have the undesireable effect that the user must click
the top, form bar to bring the form into view, instead of just clicking
anywhere on the form.

0
Comment
Question by:AncientDreamer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
  • 4
  • +1
20 Comments
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12303637
i am not sure i understand you right, bur anyway

Main form:
=======
procedure TForm1.Button1Click(Sender: TObject);
begin
   TForm2.Create(self).Show
end;

form2:
====
procedure TForm2.FormShow(Sender: TObject);
begin
  timer1.Enabled:= true;
end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
  SendToBack;
  timer1.Enabled:= false;
end;


0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12304692
vadim_ti , that's exactly what i was thinking of writing! - great minds think alike

You may also want

application.sendtoback; //different from form1.sendtoback - moves whole app to back

Regards,

Hypoviax
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12304694
and... you may want to remove the timer1.Enabled:= false; in order to keep the form continually to the back. If i am wrong ignore this :-)
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 6

Expert Comment

by:vadim_ti
ID: 12304708
if i remove
timer1.Enabled:= false
will be very difficult to see a form :-)


it is from author post
==========================
FYI, adding:

      SetWindowPos(vNewMailWin.Handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE
or SWP_NOSIZE or SWP_NOACTIVATE);

works, but seems to have the undesireable effect that the user must click
the top, form bar to bring the form into view, instead of just clicking
anywhere on the form.
=================================
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12304730
Ok, fair enough

Hypoviax
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12305152
at vNewMailWin's OnClose, perhaps you would like to put a

Action := caFree;

statement, so that you will recreate the window everytime you need it?
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12305209
You seen my Speed post (the one with Workshop_Alex) yet DragonSlayer? - Ill challenge you guys to a speed comp with sorting early november
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12305226
Nah, I'm not really good in all these speed challenges ;-)
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12305250
Haha,you're better than me though. It'll be interesting.

Back to the question:

Would having OnClick events help in terms of the problem with the user clicking anywhere on the form?

Regards,

Hypoviax
0
 

Author Comment

by:AncientDreamer
ID: 12305750
Thank you all for your responses, but we're not quite there yet.

A few comments to your answers:
DragonSlayer:
 Yes, I do free the form  with Action := caFree;

Hypoviax:
Application.SendToBack is not quite it, because sometimes the main window wil be open, and we would not want that sent to the back along with the data form.
The OnClicks would have to be implemented for all parts of the form, I'm thinking, because you never know where the user might happen to point his cursor?

I will test vadim_ti's initial suggestion to see what happens.

However, I'm still very curious as to why the initial approach worked perfectly when Delphi created the form, while I now get a different result. I.e. in waht ways is my run time created form different from what Delphi created with the Application.CreateForm(..) statement?

Cheers,
AD
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12306042
>>>The OnClicks would have to be implemented for all parts of the form, I'm thinking

That's what i was thinking.

regards,

Hypoviax
0
 
LVL 5

Accepted Solution

by:
Hypoviax earned 250 total points
ID: 12306044
can't you use Application.CreateForm(..) instead of:

 vNewMailWin := TfrmVNewMail.Create;

I havn't tested this but i'm sure it would work

Regards,

Hypoviax
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12306067
Like:

 vNewMailWin := Application.CreateForm(TfrmVNewMail);

Havn't tested - just an idea

Hypoviax
0
 

Author Comment

by:AncientDreamer
ID: 12306331
More information:
I have now tried using SendToBack in the data form, but the results were discouraging.
With a bit of fiddling, I could (again) make it work perfectly the first time the data form is created and shown (It "pops under" and does not grab focus), but after it has been destroyed, all subsequent appearances of the form behave differently; In some versions it appears on top, with focus, or alternatively it "pops under", but still grabs focus.

The third variant had it appear in front, then quickly go to the back, but this causes an annoying "flicker" on the screen.

So, as a follow up to the question about why Delphi created forms and run time created forms behave differently, I would like to know why a run time created form behaves as expected the first time, but not subsequently.
(As far as I can tell from using break points, the same code is executed each time)

0
 

Author Comment

by:AncientDreamer
ID: 12306345
Hypoviax:

I believe I did try vNewMailWin := Application.CreateForm(TfrmVNewMail); at one point, but ended up not using it. I'll try that again now.
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12306370
if you use my example you need in design time to define
Timer1.enabled := false;
timer1.interval := 1

so all must be fine
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12306392
if you do not want form2 grab focus
add to timer handle event
form1.SetFocus
0
 

Author Comment

by:AncientDreamer
ID: 12306645
This is the solution that appears to work the way I want:

  if not VNewMailWinExists then
  begin
    VNewMailWinExists := true;
    Application.CreateForm(TfrmVNewMail, vNewMailWin);
    vNewMailWin.Visible := false;
  end;

........ more code ....

  if not vNewMailWin.Visible then
  begin
    if PopUnder then
    begin
      SetWindowPos(vNewMailWin.Handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE);
      ShowWindow(vNewMailWin.Handle, SW_SHOWNOACTIVATE);
      vNewMailWin.Visible := true;
    end
    else
    begin
      ShowWindow(vNewMailWin.Handle, SW_SHOW);
      vNewMailWin.Visible := true;
      Application.BringToFront;
    end;
  end;


As regards the solution, Hypoviax was the closest and will get the points, unless someone manages to answer my questions about exactly what's going on. I hate applying a fix and not knowing why it works.

I'm leaving this open for another 1-2 days, in the hope that the questions will be answered.
(Btw. is it now possible to split points between contributors? I know many asked for that in the past)
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12313369
You should be able to split points, although some people have had trouble for some reason.

>> exactly what's going on

Not completely sure myself, but it may be because when you create that form (using the other method) it is local to that procedure. With the application.createform(..) it is global and so thus will continue to work after the first time.

If this is correct then if you declare your vNewMailWin in the private or public delaration section it should be global and will work as expected. You can but try, but i think this may be the reason

Regards,

Hypoviax
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12324566
Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

615 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