Solved

Issues with putting a form behind everything else on the desktop

Posted on 2004-10-13
20
183 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
  • 10
  • 4
  • 4
  • +1
20 Comments
 
LVL 6

Expert Comment

by:vadim_ti
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 6

Expert Comment

by:vadim_ti
Comment Utility
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
Comment Utility
Ok, fair enough

Hypoviax
0
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
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
Comment Utility
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
Comment Utility
Nah, I'm not really good in all these speed challenges ;-)
0
 
LVL 5

Expert Comment

by:Hypoviax
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:Hypoviax
Comment Utility
>>>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
Comment Utility
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
Comment Utility
Like:

 vNewMailWin := Application.CreateForm(TfrmVNewMail);

Havn't tested - just an idea

Hypoviax
0
 

Author Comment

by:AncientDreamer
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
if you do not want form2 grab focus
add to timer handle event
form1.SetFocus
0
 

Author Comment

by:AncientDreamer
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

12 Experts available now in Live!

Get 1:1 Help Now