:::菜菜鸟:::问:这个控件怎么做?我这样错在何处? (90分)

  • 主题发起人 主题发起人 shadow_x
  • 开始时间 开始时间
S

shadow_x

Unregistered / Unconfirmed
GUEST, unregistred user!
[:(]
本人想做个控件!效果和ACDsee的缩约图一样,可以被选中。
思路是:在panel上放image,statictext.
image 用来放图片的缩约图,statictext显示图像文件名。
我想问:可以这样实现吗?
下面是几行代码,我还不能初始化很多东西!
求高手 帮忙!!!

unit imagepanel
TImagepanel = class(TPanel)
private
{ Private declarations }

protected
{ Protected declarations }
top_p : integer;
left_p : integer;
bac_color : Tcolor;
image_1: Timage;
panel_1: Tpanel;
statictext_1: Tstatictext;
sta_txt : string;


public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
Property picture :Timage read image_1 write image_1 default nil; {要显示的image}
Property text :string read sta_txt write sta_txt ; {图形文件的文件名}
Property color : Tcolor read bac_color write bac_color default $00ba8795;
Property top :integer read top_p write top_p default 0;
Property left :integer read left_p write left_p default 0;
end;

procedure Register;

implementation

//------------------------------------------------------------------------------
procedure Register;
begin
RegisterComponents('w3_20', [Timagepanel]);
end;
//------------------------------------------------------------------------------
destructor Timagepanel.Destroy;
begin
statictext_1.free;
image_1.free;
panel_1.free;
inherited destroy;
end;
//------------------------------------------------------------------------------
constructor Timagepanel.Create(AOwner: TComponent);
begin
inherited create(AOwner);
bac_color:=$00ba8795 ;
//----------------初始化 panel_1
panel_1:=TPanel.create(self.owner);
panel_1.parent:=self;
panel_1.Width :=80;
panel_1.Height :=90;
//---------------初始化 statictext_1
statictext_1:=TStatictext.create(self);
statictext_1.Parent:=panel_1; {把statictext置于panel_1上}
statictext_1.Align:=albottom;
statictext_1.Caption:=sta_txt;
statictext_1.color:=bac_color; {置背景颜色}
//---------------初始化 image_1
image_1:=TImage.create(self);
image_1.Parent :=panel_1; {把image_1置于panel_1上}
image_1.Align:=alclient;

end;
//------------------------------------------------------------------------------
end.
 
只是看看???
是不是太菜???????
求……………………………………………………………………………………………………
助……………………………………………………………………………………………………
 
你这样不行吗?如果不行的话把panel_1:=TPanel.create(self.owner);
改成panel_1:=TPanel.create(aowner);
 
做图形控件其实是用程序画图,你的程序中没有procedure Paint; override;
这类的过程。
以下是我写的一个小控件请参考
unit ImgButton;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, Graphics;

type
TDrawState = (dsP1, dsP2);
TImgButton = class(TGraphicControl)
private
{ Private declarations }
FDrawState: TDrawState;
FPicture1: TPicture;
FPicture2: TPicture;
procedure SetPicture1(Value: TPicture);
procedure SetPicture2(Value: TPicture);
protected
{ Protected declarations }
procedure Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
function DestRect: TRect;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Picture1: TPicture read FPicture1 write SetPicture1;
property Picture2: TPicture read FPicture2 write SetPicture2;
property OnClick;
end;



procedure Register;

implementation

constructor TImgButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDrawState:= dsP1;
FPicture1:= TPicture.Create;
FPicture2:= TPicture.Create;
Width:= 105;
Height:= 105;
end;

destructor TImgButton.Destroy;
begin
FPicture1.Free;
FPicture2.Free;
inherited Destroy;
end;

procedure TImgButton.SetPicture1(Value: TPicture);
begin
FPicture1.Assign(Value);
end;

procedure TImgButton.SetPicture2(Value: TPicture);
begin
FPicture2.Assign(Value);
end;

procedure TImgButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);
if (Button = mbLeft) then
begin
FDrawState:= dsP2;
Invalidate;
end;
end;

procedure TImgButton.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited MouseUp(Button, Shift, X, Y);
if (Button = mbLeft) then
begin
FDrawState:= dsP1;
Invalidate;
end;
end;

procedure TImgButton.Paint;
begin
if csDesigning in ComponentState then
with inherited Canvas do
begin
Pen.Style := psDash;
Brush.Style := bsClear;
Rectangle(0, 0, Width, Height);
end;
try
with inherited Canvas do
if FDrawState = dsP1 then StretchDraw(DestRect, Picture1.Graphic)
else if FDrawState = dsP2 then StretchDraw(DestRect, Picture2.Graphic);
if (Picture1.Width > 0) or (Picture1.Height > 0) then
SetBounds(Left, Top, Picture1.Width, Picture1.Height);
finally
end;
end;

function TImgButton.DestRect: TRect;
var
w, h, cw, ch: Integer;
xyaspect: Double;
begin
w := Picture1.Width;
h := Picture1.Height;
cw := ClientWidth;
ch := ClientHeight;
if (w > 0) and (h > 0) then
begin
xyaspect := w / h;
if w > h then
begin
w := cw;
h := Trunc(cw / xyaspect);
if h > ch then // woops, too big
begin
h := ch;
w := Trunc(ch * xyaspect);
end;
end
else
begin
h := ch;
w := Trunc(ch * xyaspect);
if w > cw then // woops, too big
begin
w := cw;
h := Trunc(cw / xyaspect);
end;
end;
end
else
begin
w := cw;
h := ch;
end;
with Result do
begin
Left := 0;
Top := 0;
Right := w;
Bottom := h;
end;
end;


procedure Register;
begin
RegisterComponents('Samples', [TImgButton]);
end;

end.
 
好的!试一试!
我做好之后再来聊聊
 
to:天真
不是编译出错!而是实现不了功能!希望你继续关注
 
效果是出来了!问题是控件的大小老是需要自己改!
然后
onclick 等 事件要到控件边缘才能响应
大家试试,帮忙看看
###################################
1。那大小我可不想每次都改,最好能设置成我的默认值!80×90
2。那些事件的响应如何解决!我知道是应该在image,statictext的事件里编程,
但是,做成控件后,那可是不行的!难道要自己写事件?????

###################################
unit imagepanel;
interface
uses
Windows,SysUtils, Classes, Controls, Graphics,stdctrls,
extctrls;
type
TImagepanel = class(TPanel)
private
{ Private declarations }
image_1: Timage;
panel_1: Tpanel;
statictext_1: Tstatictext;
protected
{ Protected declarations }

public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
Property image :Timage read image_1 write image_1 default nil; {要显示的image}
Property filename:Tstatictext read statictext_1 write statictext_1 ; {文件名}
end;

procedure Register;

implementation

//------------------------------------------------------------------------------
procedure Register;
begin
..........
end;
//------------------------------------------------------------------------------
destructor Timagepanel.Destroy;
begin
statictext_1.free;
image_1.free;
panel_1.free;
inherited destroy;
end;
//------------------------------------------------------------------------------
constructor Timagepanel.Create(AOwner: TComponent);
begin
inherited create(AOwner);
//----------------初始化 panel_1
panel_1:=TPanel.create(self);
panel_1.parent:=self;
panel_1.Width :=80;
panel_1.Height :=90;
//---------------初始化 statictext_1
statictext_1:=TStatictext .create(self);
statictext_1.Parent:=panel_1; {把statictext置于panel_1上}
statictext_1.Height:=20;
statictext_1.Align:=albottom;
statictext_1.Color:=$00EAB3C4;
statictext_1.Alignment:=taCenter;
//---------------初始化 image_1
image_1:=TImage.create(self);
image_1.Parent :=panel_1; {把image_1置于panel_1上}
image_1.Align:=alclient;
end;
end.
 
多人接受答案了。
 
后退
顶部