We help IT Professionals succeed at work.

ShowMainForm := False; // not working

Oli2
Oli2 asked
on
could someone explain to me why the f..k my Form still gets displayed?

procedure TfTest.FromCreate(Sender: TObject);
begin
//of course StartupMode := Silent ...
if StartupMode = Silent then
   begin
     Application.ShowMainForm := False;
   end;
end;

If I pause on the last end and check value of
Application.ShowMainForm it is False!!
MainForm of course IS fTest (in this example).

Anyone got a clue?

Regards,
Oli
Comment
Watch Question

Commented:
I tested the following under D5 pro & D6 personal, they both fail to show the form.  Perhaps you're not setting startupmode early enough, or there may be a problem somwhere else:

procedure TForm1.FormCreate(Sender: TObject);
begin
     application.ShowMainForm := false;
end;


GL
Mike

Author

Commented:
naw, that can't be it.
I changed my proc to this:


procedure TfTest.FromCreate(Sender: TObject);
begin
  Application.ShowMainForm := False;

//lots of initialization stuff here...

//of course StartupMode := Silent ...
if StartupMode = Silent then
  begin
    Application.ShowMainForm := False;
  end else
    Application.ShowMainForm := True;
end;

still the Form comes up...

Commented:
Application.ShowMainForm should be set in the project source

Project->View source menu

begin
  Application.Initialize;
  Application.ShowMainForm := False;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Pete
====
http://www.HowToDoThings.com (Delphi articles)
http://www.Stuckindoors.com/delphi (Open source)

Author

Commented:
I'm using Frames on my Main Form, could that cause this behaviour?

regards,
Oli

Author

Commented:
cubud,
that doesn't make a difference.
Still the Form comes up...

Now I did:

Project->View source menu
begin
 Application.Initialize;
 Application.ShowMainForm := False;
 Application.CreateForm(TForm1, Form1);
 Application.Run;
end

and in
procedure TForm1.Create(Sender: TObject);
//I disabled everything!! so this is now
begin
end;

Still the Form comes up...

regards,
Oli

Commented:
Hmmm...
I checked the help files and, although putting it in the OnCreate handler worked, it said to set showMainForm _before_ the call to application.Run (in the project file).

Also any call to show will, of course, show the form.  I'm not sure what else will. I tried various combination of setting focus to the form & children of the form but they didn't cause problems except for setting the form's focus as you get an axeption when trying to focus an invisible window.  I also tried application.bringToFront, but that didn't show the form either.  So I'm guessing, either the location of you showMainForm is off a little, or something is calling TForm.show or parent.show (perhaps a component?) or something similar.

GL
Mike

Commented:
I tried adding a frame to an otherwise empty project and the showMainForm := false worked as expected so i don't think that it's a bug.

GL
Mike

Author

Commented:
edey,
since I deleted everything inside OnCreate, nothing could possibly call TFrom.Show or parent.show.

I'm really confused...

regards,
Oli

Author

Commented:
I did set the OnCreate event to nil. (Inside the ObjectInspector, not in code!)

Still the Form shows up.

  Application.Initialize;
  Application.ShowMainForm := False;
  Application.CreateForm(TForm1, Form1);
  //after this call the Forms comes up, but not painted all the way...
  Application.Run;
  //after this call, the Form gets painted all the way...

wtf?

Commented:
I've also had that problem and I've solved that by doing this:

In onpaint event i put this code:
begin
     mainform.hide;
     mainform.onpaint:=nil;//or some another function

end;

Commented:
hmmm, I'm really at a loss. The only thing I can sugest is to try & track down any WM_SHOWWINDOW messages like so:

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    procedure doWMSHOWWINDOW(var msg : TWMShowWindow);message WM_SHOWWINDOW;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.doWMSHOWWINDOW(var msg : TWMShowWindow);
begin
     inherited;
     messageDLG(
                'WM_SHOW Called',
                mtinformation,
                [mbOk],
                0);
end;


with some judical use of flags you might be able to to see when you're getting the message.

GL
Mike

Author

Commented:
f15iaf,
unfortunaly this doesn't work in my case.

Now I captured the WndProc Messages and strange enough although the Form gets displayed,
a breakpoint set at

case Msg.Msg of
WM_PAINT,
WM_ACTIVATE,
WM_SHOWWINDOW,
WM_SETFOCUS: if Application.ShowMainForm then
                inherited; //<- breakpoint here
end;

never gets reached!!! And I thought I _was_ confused...

regards,
Oli

Author

Commented:
Ups, sorry, I just saw my mistake, breakpoint is set wrong, because Application.ShowMainForm IS False.
I'll test again...

Oli

Author

Commented:
I tried:

procedure TForm1.WndProc(var Msg: TMessage);
begin
    case Msg.Msg of
        WM_SHOWWINDOW: Msg.Msg := WM_NULL;
    else
       inherited;
    end;
end;

could someone explain why my Form still shows up????

Oli

Commented:
Hmmm, this:

WM_SHOWWINDOW: Msg.Msg := WM_NULL

AFAIK shouldn't keep the form from showing as WM_SHOWWINDOW is just a notification. I'm confused to as i don't know of anything else that could make the form show.  One way or another, if you're seeing the form, I would think that this breakpoint should be reached:

WM_PAINT,
WM_ACTIVATE,
WM_SHOWWINDOW,
WM_SETFOCUS: if Application.ShowMainForm then
               inherited; //<- breakpoint here

I'm not sure what else i can sugest at this point. I am wondering though, what happenens if you add another form (a dummy), hide it & make it your main form?

GL
Mike

Commented:
try to use
SetwindowsHookex function which hooks this messages and use getcurrenthtreadid in the threadid parameter

Author

Commented:
I captured the Messages:

000E     WM_GETTEXTLENGTH
000C     WM_SETTEXT
0046     WM_WINDOWPOSCHANGING
0083     WM_NCCALCSIZE
0005     WM_SIZE
0047     WM_WINDOWPOSCHANGED
0081     WM_NCCREATE
0083     WM_NCCALCSIZE
0001     WM_CREATE
0005     WM_SIZE
0003     WM_MOVE
0030     WM_SETFONT
0080     WM_SETICON
0210     WM_PARENTNOTIFY
0046     WM_WINDOWPOSCHANGING
0083     WM_NCCALCSIZE    
0083     WM_NCCALCSIZE
0005     WM_SIZE
0047     WM_WINDOWPOSCHANGED
0046     WM_WINDOWPOSCHANGING
0083     WM_NCCALCSIZE
0083     WM_NCCALCSIZE
0005     WM_SIZE
0047     WM_WINDOWPOSCHANGED
0018     WM_SHOWWINDOW
0046     WM_WINDOWPOSCHANGING
0046     WM_WINDOWPOSCHANGING
001C     WM_ACTIVATEAPP
0086     WM_NCACTIVATE
0006     WM_ACTIVATE
0047     WM_WINDOWPOSCHANGED

I think it has something to do with Delphi parsing the
TForm.Position property or something like that.

regards,
Oli

Author

Commented:
Here's the capture of a test app that succesfully does NOT show the window:

000E     WM_GETTEXTLENGTH
000C     WM_SETTEXT
0024     WM_GETMINMAXINFO
0046     WM_WINDOWPOSCHANGING
0083     WM_NCCALCSIZE
0003     WM_MOVE
0005     WM_SIZE
0047     WM_WINDOWPOSCHANGED
0081     WM_NCCREATE
0083     WM_NCCALCSIZE
0001     WM_CREATE
0030     WM_SETFONT
0080     WM_SETICON

regards,
Oli

Author

Commented:
another test.
I changed my Test app so all TForm properties match my problem app. Here's the result: (..... indicates this call is NOT in the test app, but in my problem app)
They're almost the same:

000E     WM_GETTEXTLENGTH
000C     WM_SETTEXT
0046     WM_WINDOWPOSCHANGING
0083     WM_NCCALCSIZE
0005     WM_SIZE
0047     WM_WINDOWPOSCHANGED
0081     WM_NCCREATE
0083     WM_NCCALCSIZE
0001     WM_CREATE
0005     WM_SIZE
0003     WM_MOVE
0030     WM_SETFONT
0080     WM_SETICON
................................0210  WM_PARENTNOTIFY
0046     WM_WINDOWPOSCHANGING
0083     WM_NCCALCSIZE    
0083     WM_NCCALCSIZE
0005     WM_SIZE
0047     WM_WINDOWPOSCHANGED
0046     WM_WINDOWPOSCHANGING
0083     WM_NCCALCSIZE
0083     WM_NCCALCSIZE
0005     WM_SIZE
0047     WM_WINDOWPOSCHANGED
................................0018  WM_SHOWWINDOW
................................0046  WM_WINDOWPOSCHANGING
................................0046  WM_WINDOWPOSCHANGING
................................001C  WM_ACTIVATEAPP
................................0086  WM_NCACTIVATE
................................0006  WM_ACTIVATE
................................0047  WM_WINDOWPOSCHANGED

Anyone got a clue now?
I don't!

Oli

Author

Commented:
"The WM_PARENTNOTIFY message is sent to the parent of a child window when the child window is created or destroyed, or when the user clicks a mouse button while the cursor is over the child window. When the child window is being created, the system sends WM_PARENTNOTIFY just before the CreateWindow or CreateWindowEx function that creates the window returns. When the child window is being destroyed, Windows sends the message before any processing to destroy the window takes place."

what child window could that be? Anyone got a clue to this?

Commented:
i ws writing a response to  an earlier comment, but lost it when I crashed :(. Anyway, this comment seems to make what I was thinking a little more likely.  After seing all those WM_WINDOWPOS* messages I checked out the setWindowPos api & one of the options is SWP_SHOWWINDOW.  I would have thought that calling setWindowPos that way would send a WM_SHOWWINDOW msg, but perhaps not. So I tried a fresh project, not touching the showMainWindow, but handled WM_WINDOWPOSCHANGING like this:

procedure TForm1.doWMPOSCHANGING(var msg : TWMWINDOWPOSCHANGING);
var
   f : cardinal;
begin
     f := msg.WindowPos.flags;
     f := f and not SWP_SHOWWINDOW;
     msg.WindowPos.flags := f;
     inherited;
end;

and the window does not show.

GL
Mike

Commented:
The parent window, i'd hazard a guess, is prob. your frame.

GL
Mike

Author

Commented:
nope, I put a TFrame on my test app and still it does not receive the WM_PARENTNOTIFY Message.
Im trying out your doWMPOSCHANGING proc... will know more in a few minutes...

Oli

Author

Commented:
This seems to be working... strange...
I'll try to implement that, but that will be tricky, since my App is not supposed to ALWAYS be not visible.
so I have to build in an:

if {something} then f := f and not SWP_SHOWWINDOW;

I'll have to think of the {something} for a while...

Thanx for your help, I'll accept your comment as an answer later.

regards,
Oli

Commented:
I hope it works out ok - it feels awfully kludgy.

GL
Mike

Commented:
Maybe it has something to do with TForm.WindowState or TForm.Position properties ?

Author

Commented:
nope, it doesn't work out. I can hide a Form that way, but I can't bring it back on the screen.
I'm willing to try the SetWindowsHookEx, anyone got some example code for this?

regards,
Oli

Author

Commented:
seems like a hard one, raised points to 300...

Oli

Author

Commented:
sometimes life's so easy....

This works:
 
 Application.Initialize;
 Application.ShowMainForm := False;
 Application.CreateForm(TForm1, Form1);
 Form1.Visible := False;
 Application.Run;

regards,
Oli

Commented:
Hi Oli,

I've had exactly this problem, too. The reason was very simple: All forms have their property "visible" set to false in the object inspector by default. If it somehow happened that your mainform's visible property got set to true in the object inspector, this ends up in exactly the problem you have described here. The solution is simple: Set the main forms "visible" property back to false in the object inspector. This will solve all your problems.

It's a pitty that I saw this question only right now, I knew the answer from the beginning. Would have saved you some time...   :-(

Regards, Madshi.

Author

Commented:
And you could've made some easy points. ;)

regards,
Oli

Commented:
LOL!! :D

Commented:
My time too ;).
Anyways it was an interesting question - and one of the few, recently, that didn't take the form of "Hey, this doesn't work, write some new code for me" :). I'm really glad you found a solution & that it was "clean". It's a tip I'll be sure to remember.

GL
Mike

Author

Commented:
Mike, thanx a lot for your time and help.
It was a pleasure ;)

Oli
Go with CUBUD's answer that is the reason...
I'm sure of it..
Madmarlin
Commented:
Madmarlin,
I had tried CUBUD's solution, but that was not the problem.
The Application.ShowMainForm value was always correctly set to false.
The problem was that the Form.Visible value got lost...
I can do a

//-------------------------------------------
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Form1.Visible := False;
Application.Run;

//-------------------------------------------
procedure Form1.FormCreate(Sender: TObject);
begin
  ShowMainForm := False;
end;
//-------------------------------------------

and it'll work just as fine...

regards,
Oli
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20057428.html
http://www.experts-exchange.com/questions/Q.20052266.html
http://www.experts-exchange.com/questions/Q.20099298.html
http://www.experts-exchange.com/questions/Q.20180999.html
http://www.experts-exchange.com/questions/Q.20184880.html




PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
Oli2:
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.
CERTIFIED EXPERT

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQed and points refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer