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
Solved

Issues with putting a form behind everything else on the desktop

Posted on 2004-10-13
20
186 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Machine not responding during CopyFile() 3 103
Create Database on Android via Delphi dbExpress 3 108
Downloading email attachments 2 79
Convert MS Word document to a PDF file 9 90
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 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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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