C
coolqiang
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是《Delphi高手突破》中的一个可显示Image的Panel控件的源码:
unit ImagePanel;
interface
uses
Windows, Classes, Graphics, Controls, ExtCtrls;
type
TImagePanel = class(TCustomPanel)
private
FPicture: TPicture;
FTransparent: Boolean;
FAutoSize: Boolean;
procedure PictureChanged(Sender: TObject);
procedure SetPicture(const Value: TPicture);
procedure SetTransparent(const Value: Boolean);
procedure SetAutoSize(const Value: Boolean); reintroduce; //问题一
procedure SetFont(const Value: TFont);
procedure SetCaption(const Value: TCaption);
procedure SetAlignment(const Value: TAlignment);
protected
procedure Paint; override;
public
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;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('我的控件', [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.SetColor(const Value: TColor);
begin
inherited Color := 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.
问题一:此处的reintroduce是什么意思?有何用途?
问题二:为何不是简单的inherited就行了?为何Create要加AOwner参数?是否为了指明其拥有者?
问题三:为何这里要加inherited?和Caption为Published有关吗?
问题四:Image控件可在设计时双击弹出“Picture Editor”,如何在此控件中实现?
问题五:在新建的From上添加一个Button时,代码如下:
TForm1 = class(TForm)
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
那么,Button1未指明是何类别(private、public等),它有默认值吗?
在下初学控件开发,让各位见笑了,请各位大虾不吝赐教!谢谢!
unit ImagePanel;
interface
uses
Windows, Classes, Graphics, Controls, ExtCtrls;
type
TImagePanel = class(TCustomPanel)
private
FPicture: TPicture;
FTransparent: Boolean;
FAutoSize: Boolean;
procedure PictureChanged(Sender: TObject);
procedure SetPicture(const Value: TPicture);
procedure SetTransparent(const Value: Boolean);
procedure SetAutoSize(const Value: Boolean); reintroduce; //问题一
procedure SetFont(const Value: TFont);
procedure SetCaption(const Value: TCaption);
procedure SetAlignment(const Value: TAlignment);
protected
procedure Paint; override;
public
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;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('我的控件', [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.SetColor(const Value: TColor);
begin
inherited Color := 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.
问题一:此处的reintroduce是什么意思?有何用途?
问题二:为何不是简单的inherited就行了?为何Create要加AOwner参数?是否为了指明其拥有者?
问题三:为何这里要加inherited?和Caption为Published有关吗?
问题四:Image控件可在设计时双击弹出“Picture Editor”,如何在此控件中实现?
问题五:在新建的From上添加一个Button时,代码如下:
TForm1 = class(TForm)
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
那么,Button1未指明是何类别(private、public等),它有默认值吗?
在下初学控件开发,让各位见笑了,请各位大虾不吝赐教!谢谢!