2007. január 10., szerda

Giving a MDI window a background image/tile


Problem/Question/Abstract:

How do I give my MDI window a background image or tile?

Answer:

This is a handy trick I found somewhere:

Put an image called Image1 on your main form.
Add the following routine to your main form:

Make sure you have the following variables in your main form object:

FClientInstance: TFarProc;
FPrevClientProc: TFarProc;

{ MDI Background code }

procedure TMainForm.ClientWndProc(var Message: TMessage);
var
  Dc: hDC;
  Row: Integer;
  Col: Integer;
begin
  with Message do
    case Msg of
      WM_ERASEBKGND:
        begin
          Dc := TWMEraseBkGnd(Message).Dc;
          // Tile Image on DC
          for Row := 0 to ClientHeight div Image1.Picture.Height do
            for Col := 0 to ClientWidth div Image1.Picture.Width do
              BitBlt(Dc,
                Col * Image1.Picture.Width,
                Row * Image1.Picture.Height,
                Image1.Picture.Width,
                Image1.Picture.Height,
                Image1.Picture.Bitmap.Canvas.Handle,
                0,
                0,
                SRCCOPY);
          Result := 1;
        end;
    else // Pass on other msg's
      Result := CallWindowProc(FPrevClientProc,
        ClientHandle,
        Msg,
        wParam,
        lParam);
    end;
end;

And put this in your mainform OnShow event:

// MDI background tiles stuff, chain in de WndProc.
FClientInstance := MakeObjectInstance(ClientWndProc);
FPrevClientProc := Pointer(GetWindowLong(ClientHandle, GWL_WNDPROC));
SetWindowLong(ClientHandle, GWL_WNDPROC, LongInt(FClientInstance));

You now have a background!

Nincsenek megjegyzések:

Megjegyzés küldése