Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to ensure mainform has maximized and fully redrawn before showing a modal form on application load

What is the recommended way to ensure that the mainform is fully maximized and all controls are redrawn before showing a modal form on application first load?

I need to show a modal dialog when the application starts (not a login screen) but if I set the form to wsMaximized whilst the screen maximizes, the controls do not have chance to redraw and you are left with an ugly mess.

I show the modal screen at present using the following:

procedure TForm1.FormActivate(Sender: TObject);
var
  frmOrderLookup:TfrmOrderLookup;
begin
  onactivate := nil;
  frmOrderLookup:=TfrmOrderLookup.Create(nil);
  try
    frmOrderLookup.showmodal;
  finally
    frmOrderLookup.Free;
  end;
end;
like image 539
2 revs Avatar asked Jun 08 '12 11:06

2 revs


1 Answers

What I normally do is post a custom message back to my form. That way it won't get processed until other messages for the form have already been handled. By the time this message gets processed, your form should have already finished redrawing.

type
  TMyForm = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    procedure HandleLookupMessage(var Message: TMessage); message WM_USER + 1;
  end;

procedure TMyForm.HandleLookupMessage(var Message: TMessage);
var
  frmOrderLookup: TfrmOrderLookup;
begin
  frmOrderLookup := TfrmOrderLookup.Create(Application);
  try
    frmOrderLookup.ShowModal;
  finally
    frmOrderLookup.Release;
  end;
end;

procedure TMyForm.FormCreate(Sender: TObject);
begin
  // Maximise form here if needed
  PostMessage(Handle, WM_USER + 1, 0, 0);
end;

If you're worried about the message getting to your application again somehow, you can always add a private boolean variable to indicate that it's been handled once.

like image 69
afrazier Avatar answered Nov 01 '22 15:11

afrazier