怎样在Panel控件里进行背景图的平铺?

解决方案 »

  1.   

    在panel的onResize里面写代码,根据其尺寸动态生成TImage,然后计算她的位置不就行了,是不是嫌这个方法太笨了?
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}type  TMyPanel = class(TPanel)
      private
        FBitmap: TBitmap;
        procedure SetBitmap(const Value: TBitmap);
      protected
        procedure Paint; override;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;    property Bitmap: TBitmap read FBitmap write SetBitmap; //需要平铺画的图
      end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      with TMyPanel.Create(Self) do
      begin
        { 画图测试 }
        Bitmap.LoadFromFile('C:\WINDOWS\Gone Fishing.bmp');
        Parent := Self;
        Align := alClient;
      end;
    end;{ TMyPanel }constructor TMyPanel.Create(AOwner: TComponent);
    begin
      inherited;
      FBitmap := TBitmap.Create
    end;destructor TMyPanel.Destroy;
    begin
      FBitmap.Free;
      inherited;
    end;procedure TMyPanel.Paint;
    var
      I, J: Integer;
      Rect: TRect;
    begin
      { 平铺画图 }
      Rect := ClientRect;
      for I := Rect.Top div FBitmap.Height to Rect.Bottom div FBitmap.Height do
        for J:= Rect.Left div FBitmap.Width to Rect.Right div FBitmap.Width do
          BitBlt(Canvas.Handle, J * FBitmap.Width - Rect.Left, I * FBitmap.Height - Rect.Top,
            FBitmap.Width, FBitmap.Height, FBitmap.Canvas.Handle, 0, 0, SRCCOPY);
    end;procedure TMyPanel.SetBitmap(const Value: TBitmap);
    begin
      FBitmap.Assign(Value);
    end;end.