把滚动箱的样式做如下调整来模拟 TPanel

程序中用 TPanel 做了容器, 需要给它一个背景图片; 发现这竟是个难题!

发现我经常使用的滚动箱控件 TScrollBox, 是一个很好的替代品.

本例需要先添加两个图片资源, 添加方法可以参考: http://www.cnblogs.com/del/archive/2008/08/23/1274591.html



本例效果图:



代码文件:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ScrollBox1: TScrollBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  bg1,bg2: TBitmap; {做背景需要持久对象, 不能为局部变量}

procedure TForm1.FormCreate(Sender: TObject);
begin
  bg1 := TBitmap.Create;
  bg2 := TBitmap.Create;

  {已经在资源中添加好了两个背景图片, 分别命名为: Bitmap1、Bitmap2}
  bg1.LoadFromResourceName(HInstance, 'Bitmap1');
  bg2.LoadFromResourceName(HInstance, 'Bitmap2');
  {如果添加本地文件可用 LoadFromFile}
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  bg1.Free;
  bg2.Free;
end;

{把滚动箱的样式做如下调整来模拟 TPanel}
procedure TForm1.Button1Click(Sender: TObject);
begin
  with ScrollBox1 do
  begin
    BevelKind := bkSoft;
    BevelOuter := bvRaised;
    BorderStyle := BsNone;
    AutoScroll := False;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Self.Brush.Bitmap := bg1;
  Self.Refresh;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ScrollBox1.Brush.Bitmap := bg2;
  ScrollBox1.Refresh;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  if Self.Brush.Bitmap = bg1 then
  begin
    Self.Brush.Bitmap := bg2;
    ScrollBox1.Brush.Bitmap := bg1;
  end else begin
    Self.Brush.Bitmap := bg1;
    ScrollBox1.Brush.Bitmap := bg2;
  end;
  Self.Refresh;
  ScrollBox1.Refresh;
end;

end.

 

http://www.cnblogs.com/del/archive/2008/09/01/1281345.html

posted @ 2016-11-03 21:39  findumars  Views(534)  Comments(0Edit  收藏  举报