DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: namnvh
今日帖子: 1
在线用户: 3
导航: 论坛 -> 移动应用开发 斑竹:flyers,iamdream  
作者:
男 jiangminge (jiangminge) ▲▲▲▲▲ -
普通会员
2014/6/23 22:28:12
标题:
有没有一个既能显示背景图片又能存放控件的容器类控件? 浏览:1286
加入我的收藏
楼主: 如题,兄弟们帮帮忙啊
----------------------------------------------
-
作者:
男 nevergrief (孤独骑士) ★☆☆☆☆ -
盒子活跃会员
2014/6/23 23:34:00
1楼: unit ImagePanel;

interface

uses
  Windows, ExtCtrls, Graphics, Classes, Controls; // SysUtils

type
  TImagePanel = class(TCustomPanel)
  private
    { Private declarations }
    FPicture : TPicture;
    FTransparent : Boolean;
    FAutoSize : Boolean;

    procedure PictureChanged(Sender: TObject);
    procedure SetPicture(const Value: TPicture);
    procedure SetAutoSize(const Value: Boolean); reintroduce;
    procedure SetTransparent(const Value: Boolean);
    procedure SetFont(const Value: TFont);
    procedure SetCaption(const Value: TCaption);
    procedure SetAlignment(const Value: TAlignment);
  protected
    { Protected declarations }
    procedure Paint(); override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy(); override;
  published
    property Picture: TPicture read FPicture write SetPicture;
    property Transparent: Boolean read FTransparent write SetTransparent default false;
    property AutoSize: Boolean read FAutoSize write SetAutoSize;

    property Font write SetFont;
    property Caption write SetCaption;
    property Alignment write SetAlignment;
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Sunisoft', [TImagePanel]);
end;

{ TImagePanel }

constructor TImagePanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FPicture:=TPicture.Create();
  FPicture.OnChange := PictureChanged;

  Repaint();
end;

destructor TImagePanel.Destroy;
begin
  FPicture.Free();
  FPicture:=nil;

  inherited;
end;

procedure TImagePanel.Paint;
const
  Alignments: array[TAlignment] of Longint = (DT_LEFT, DT_RIGHT, DT_CENTER);
var
  Flags: longint;
  Rect: TRect;
  FontHeight: Integer;
begin
  Canvas.Brush.Style := bsClear;
  Canvas.Font := Font;

  if Assigned(FPicture.Graphic) then
  begin
    if FAutoSize then
    begin
      Width := FPicture.Width;
      Height := FPicture.Height;
    end;

    if FPicture.Graphic.Transparent<> FTransparent then
      FPicture.graphic.Transparent := FTransparent;

    Canvas.stretchDraw(ClientRect, FPicture.Graphic);
  end
  else
  begin
    Canvas.Brush.Color := Color;
    Canvas.FillRect(ClientRect);
  end;

  if Caption<>'' then
  begin
    Rect := GetClientRect;
    FontHeight := Canvas.TextHeight('W');

    Rect.Top := ((Rect.Bottom + Rect.Top) - FontHeight) div 2;
    Rect.Bottom := Rect.Top + FontHeight;
    Flags := DT_EXPANDTABS or DT_VCENTER or Alignments[Alignment];
    Flags := DrawTextBiDiModeFlags(Flags);
    DrawText(Canvas.Handle, PChar(Caption), -1, Rect, Flags);
  end;
end;

procedure TImagePanel.PictureChanged(Sender: TObject);
begin
  Repaint();
end;

procedure TImagePanel.SetAlignment(const Value: TAlignment);
begin
  inherited Alignment := Value;
  Repaint();
end;

procedure TImagePanel.SetAutoSize(const Value: Boolean);
begin
  FAutoSize := Value;
  Repaint();
end;

procedure TImagePanel.SetCaption(const Value: TCaption);
begin
  inherited Caption := Value;
  Repaint();
end;

procedure TImagePanel.SetFont(const Value: TFont);
begin
  inherited Font := Value;
  Repaint();
end;

procedure TImagePanel.SetPicture(const Value: TPicture);
begin
  FPicture.Assign(Value);
  Repaint();
end;

procedure TImagePanel.SetTransparent(const Value: Boolean);
begin
  FTransparent := Value;
  Repaint();
end;

end.
----------------------------------------------
只有偏执狂才能生存!
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行69.33594毫秒 RSS