Board index » delphi » Placing form at MDI Client 0,0

Placing form at MDI Client 0,0

I need to place a normal form over a MDI client space. The Left,Top coord.
should be at Client 0,0. Tried using ClientToScreen but cannot get it to do
the trick.

Jacob Pedersen

 

Re:Placing form at MDI Client 0,0


Quote
In article <8EEC9A4D6jpedersentecho...@207.105.83.62>, Jacob Pedersen wrote:
> I need to place a normal form over a MDI client space. The Left,Top coord.
> should be at Client 0,0. Tried using ClientToScreen but cannot get it to do
> the trick.

Jacob,

try something like this:

 Var
   r: TRect;
   client: HWND;
 Begin
   client := {application.mainform.}clienthandle;
   GetWindowRect( client, r );
   MapWindowPoints( client, HWND_DESKTOP, r, 2 );
   formX.BoundsRect := r;

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Sent using Virtual Access 5.00 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe

Re:Placing form at MDI Client 0,0


Thanks Peter,

always a knight to the rescue. However your code doesn't work for me.
The form is not placed with (Left, Top) begining at the mainforms client
space.

Another nice thing would be if I created another instance of the form, if
should be placed at the same point only alittle off (Left+16, Top+16), like
cascading windows.

Any idea what the code doesn't work for me?

Jacob Pedersen

100113.1...@compuXXserve.com (Peter Below (TeamB)) wrote in
<VA.00004982.00913...@antispam.compuserve.com>:

Quote
>try something like this:

> Var
>   r: TRect;
>   client: HWND;
> Begin
>   client := {application.mainform.}clienthandle;
>   GetWindowRect( client, r );
>   MapWindowPoints( client, HWND_DESKTOP, r, 2 );
>   formX.BoundsRect := r;

Re:Placing form at MDI Client 0,0


Quote
In article <8EF06D58Cjpedersentecho...@207.105.83.62>, Jacob Pedersen wrote:
> always a knight to the rescue. However your code doesn't work for me.
> The form is not placed with (Left, Top) begining at the mainforms client
> space.

Jacob, i must have been half asleep when i posted that code. Try this slightly
modified version:

procedure TFormX.FormCreate(Sender: TObject);
 Var
   r: TRect;
   client: HWND;
 Begin
   client := application.mainform.clienthandle;
   Windows.GetClientRect( client, r );
   MapWindowPoints( client, HWND_DESKTOP, r, 2 );
   BoundsRect := r;
 End;

Quote
> Another nice thing would be if I created another instance of the form, if
> should be placed at the same point only alittle off (Left+16, Top+16), like
> cascading windows.

Well, you need to keep track of the number of forms already created and just
correct the R rectangle used in the function before you assign it to
boundsRect. One simple solution would be to place a integer variable into a
Var section in the forms unit, e.g.

 Var
   formcount: Integer = 0;

Placed after the Uses clause in the Implementation section this var will only
be visible for the form itself, but that is enough. There is a serious Z-order
problem with your scheme, however, since the forms are not parented to the
main form. Every time you click on a main form toolbar button or do something
with the main forms menu the main form will come to front and hide your
secondary form. To fix that use something like this in the onCreate handler of
the secondary forms:

Var
  formcount: Integer = 0;

procedure TForm2.FormCreate(Sender: TObject);
 Var
   r: TRect;
   client: HWND;
 Begin
   client := application.mainform.clienthandle;
   Windows.GetClientRect( client, r );
   MapWindowPoints( client, HWND_DESKTOP, r, 2 );
   OffsetRect( r, 16*formcount, 16*formcount );
   BoundsRect := r;
   Inc( formcount );
   SetWindowLong( handle, GWL_HWNDPARENT,
                  Application.Mainform.Handle );
 end;

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Sent using Virtual Access 5.00 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe

Re:Placing form at MDI Client 0,0


Thanks Peter!

You are truely a wizard. I appreciate all the help!

Jacob Pedersen

Re:Placing form at MDI Client 0,0


I wanted to go further and place dialogs and other forms over their
parents. I found a bit of code which I tried to modify to also work with
MDI children, but...

procedure CenterOverParent(PForm,CForm: TForm);
var
  ParentLeft, ParentTop,
  i1, i2 : Integer;
  FromPoint: TPoint;
  r: TRect;
begin
  Windows.GetClientRect(Application.Mainform.ClientHandle, r);
  MapWindowPoints(Application.Mainform.ClientHandle, HWND_DESKTOP, r, 2);

  FromPoint.X := PForm.Left;
  FromPoint.Y := PForm.Top;
  FromPoint := Application.Mainform.ClientToScreen (FromPoint); // *

  if PForm.FormStyle = fsMDIChild then
  begin
    ParentLeft := FromPoint.X+r.Left;
    ParentTop := FromPoint.Y+r.Top;
  end
  else
  begin
    ParentLeft := PForm.Left;
    ParentTop := PForm.Top;
  end;

  i1 := ParentLeft +((PForm.Width - CForm.Width) div 2);
  i2 := GetSystemMetrics(SM_CXSCREEN);  // here's where we ensure the form
doesn't fall off
  if i1 + CForm.Width > i2 then
    i1 := i2 - (CForm.Width + 20)
  else if i1 < 0 then
    i1 := 20;
  CForm.Left := i1;

  i1 := ParentTop+((PForm.Height - CForm.Height) div 2);
  i2 := GetSystemMetrics(SM_CYSCREEN);
  if i1 + CForm.Height > i2 then
    i1 := i2 - (CForm.Height + 20)
  else if i1 < 0 then
    i1 := 20;
  CForm.Top := i1;
end;

* when the MDI Client is placed at 0,0 in the client area, the FromPoint is
0,38. This makes the centered form appear 38 pixels lower than the center.
The x coord. is ok.

Do you have an idea what's going on?

Thanks,
Jacob Pedersen

Re:Placing form at MDI Client 0,0


Quote
In article <8EF77A952jpedersentecho...@207.105.83.62>, Jacob Pedersen wrote:
> I wanted to go further and place dialogs and other forms over their
> parents. I found a bit of code which I tried to modify to also work with
> MDI children, but...

Jacob, if the goal of your function is to center form CForm over PForms client
area then your code is way too complicated, IMO. Whatever type of form PForm
is, if you do a

    r:= PForm.ClientRect;
    MapWindowPoints( PForm.handle, HWND_DESKTOP, r, 2 );

you get the forms client rect in screen coordinates. Centering CForm over that
is then pretty trivial.    

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Other Threads