这里是我写的程序,是一个由label和bitbtn组成的组件,上半是bitbtn,下半是label!
unit Mywin5;interfaceuses
Windows,Messages,Graphics,Forms,Dialogs,StdCtrls,Buttons,SysUtils, Classes, Controls,Variants;type
TMywin4 = class(TWinControl)
private
{ Private declarations }
FBitbtn:TBitBtn;
FLabel:TLabel;
//FCanvas: TCanvas;
FGlyph: TBitmap;//procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
protected
{ Protected declarations }
procedure WMSize(var Message:TWMSize);message WM_SIZE;
procedure SetText(Value:String);
function GetText:String;
procedure SetFont(Value:TFont);
function GetFont:TFont;
procedure SetCaption(Value:String);
function GetCaption:String;
procedure SetOnButtonClick(Value:TNotifyEvent);
function GetOnButtonClick:TNotifyEvent;
procedure SetGlyph(Value: TBitmap);
function GetGlyph: TBitmap; public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override; published
{ Published declarations }
property Text:String read GetText write SetText;
property Font:TFont read GetFont write SetFont;
property Caption:String read GetCaption write SetCaption;
property OnButtonClick:TNotifyEvent read GetOnButtonClick write SetOnButtonClick; property Enabled;
property Glyph: TBitmap read GetGlyph write SetGlyph; end;procedure Register;implementationprocedure TMywin4.WMSize(var Message:TWMSize);
begin
inherited ;
FLabel.Width := Message.Width;
//FLabel.Height:= Int((Message.Height)/2);
FBitBtn.Width := Message.Width;
//TBitBtn.Height:= (Message.Height)/2;
end;
//Fpanel := Message.Height;constructor TMywin4.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FBitBtn :=TBitbtn.Create(Self);
FBitBtn.Parent := Self;
FBitBtn.Left:=Self.Left+Self.Width;
FBitBtn.Top:=Self.Top; FLabel:=TLabel.Create(Self);
FLabel.Parent:=Self;
FLabel.Left:=Self.Left;
FLabel.Height:=FBitbtn.Height;
FLabel.Top:=Self.Top-FBitBtn.Height;
Width:=FLabel.Width;
Height:=FLabel.Height+FBitbtn.Height;
end;procedure TMywin4.SetText(Value:String);
begin
FLabel.Caption:=Value;
end;function TMywin4.GetText:String;
begin
Result:=FLabel.Caption;
end;procedure TMywin4.SetCaption(Value:String);
begin
FBitbtn.caption:=Value;
end;function TMywin4.Getcaption:String;
begin
Result:=FBitbtn.Caption;
end;procedure TMywin4.SetFont(Value:TFont);
begin
if Assigned(Flabel.Font) then
FLabel.Font.Assign(Value);
FBitbtn.Font.Assign(Value);
end;function TMywin4.GetFont:TFont;
begin
Result:=FLabel.Font;
end;procedure TMywin4.SetOnButtonClick(Value:TNotifyEvent);
begin
FBitbtn.Onclick:=Value;
end;function TMywin4.GetOnButtonClick:TNotifyEvent;
begin
Result:=FBitbtn.onclick;
end;procedure TMywin4.SetGlyph(Value: TBitmap);
begin
if FGlyph<>Value then
begin
FGlyph.Assign(Value);
end;
end;function TMywin4.GetGlyph:TBitmap;
begin
Result:=FGlyph;
end;destructor TMywin4.Destroy;
begin
FBitBtn.Free;
FLabel.Free;
FGlyph.Free;
inherited Destroy;
end;procedure Register;
begin
RegisterComponents('Samples', [TMywin4]);
end;end.
问题1:是在object inspector里面选glyph时后,bitbtn 没有显示。运行出现
project project1.exe raise exception class EreadError with message 'Invalid property path'.
问题2:我不想设死控件高度!我想控制bitbtn和label一样搞,各占高度的一半,应该怎么改?
unit Mywin5;interfaceuses
Windows,Messages,Graphics,Forms,Dialogs,StdCtrls,Buttons,SysUtils, Classes, Controls,Variants;type
TMywin4 = class(TWinControl)
private
{ Private declarations }
FBitbtn:TBitBtn;
FLabel:TLabel;
//FCanvas: TCanvas;
FGlyph: TBitmap;//procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
protected
{ Protected declarations }
procedure WMSize(var Message:TWMSize);message WM_SIZE;
procedure SetText(Value:String);
function GetText:String;
procedure SetFont(Value:TFont);
function GetFont:TFont;
procedure SetCaption(Value:String);
function GetCaption:String;
procedure SetOnButtonClick(Value:TNotifyEvent);
function GetOnButtonClick:TNotifyEvent;
procedure SetGlyph(Value: TBitmap);
function GetGlyph: TBitmap; public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override; published
{ Published declarations }
property Text:String read GetText write SetText;
property Font:TFont read GetFont write SetFont;
property Caption:String read GetCaption write SetCaption;
property OnButtonClick:TNotifyEvent read GetOnButtonClick write SetOnButtonClick; property Enabled;
property Glyph: TBitmap read GetGlyph write SetGlyph; end;procedure Register;implementationprocedure TMywin4.WMSize(var Message:TWMSize);
begin
inherited ;
FLabel.Width := Message.Width;
//FLabel.Height:= Int((Message.Height)/2);
FBitBtn.Width := Message.Width;
//TBitBtn.Height:= (Message.Height)/2;
end;
//Fpanel := Message.Height;constructor TMywin4.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FBitBtn :=TBitbtn.Create(Self);
FBitBtn.Parent := Self;
FBitBtn.Left:=Self.Left+Self.Width;
FBitBtn.Top:=Self.Top; FLabel:=TLabel.Create(Self);
FLabel.Parent:=Self;
FLabel.Left:=Self.Left;
FLabel.Height:=FBitbtn.Height;
FLabel.Top:=Self.Top-FBitBtn.Height;
Width:=FLabel.Width;
Height:=FLabel.Height+FBitbtn.Height;
end;procedure TMywin4.SetText(Value:String);
begin
FLabel.Caption:=Value;
end;function TMywin4.GetText:String;
begin
Result:=FLabel.Caption;
end;procedure TMywin4.SetCaption(Value:String);
begin
FBitbtn.caption:=Value;
end;function TMywin4.Getcaption:String;
begin
Result:=FBitbtn.Caption;
end;procedure TMywin4.SetFont(Value:TFont);
begin
if Assigned(Flabel.Font) then
FLabel.Font.Assign(Value);
FBitbtn.Font.Assign(Value);
end;function TMywin4.GetFont:TFont;
begin
Result:=FLabel.Font;
end;procedure TMywin4.SetOnButtonClick(Value:TNotifyEvent);
begin
FBitbtn.Onclick:=Value;
end;function TMywin4.GetOnButtonClick:TNotifyEvent;
begin
Result:=FBitbtn.onclick;
end;procedure TMywin4.SetGlyph(Value: TBitmap);
begin
if FGlyph<>Value then
begin
FGlyph.Assign(Value);
end;
end;function TMywin4.GetGlyph:TBitmap;
begin
Result:=FGlyph;
end;destructor TMywin4.Destroy;
begin
FBitBtn.Free;
FLabel.Free;
FGlyph.Free;
inherited Destroy;
end;procedure Register;
begin
RegisterComponents('Samples', [TMywin4]);
end;end.
问题1:是在object inspector里面选glyph时后,bitbtn 没有显示。运行出现
project project1.exe raise exception class EreadError with message 'Invalid property path'.
问题2:我不想设死控件高度!我想控制bitbtn和label一样搞,各占高度的一半,应该怎么改?
解决方案 »
- 如何获得当前窗口大小?
- 偶写的水纹模拟组件,请指教。
- DBGrid
- Tmenu有个shortcut,可是为什么没有用呢?
- 高手来看看这段代码,在线等待,关于自定义函数的调用
- 请教如何实现倒入注册表文件及屏蔽掉任务栏上的鼠标右键
- 请教!!!!!
- 程序中如何引用汇编程序
- 调用vc编的dll,返回值是LPCSTR类型的,delphi中用什么类型与它对应?我用PChar可是老出现EAccessViolation的提示!
- 在设置桌面墙纸的时候如何设置其属性?
- ================散分,第一次====================
- 如果你对自己心爱的女孩子坦白. 她用如下的理由来拒绝, 你会怎么说? 如何说又是最好?
procedure TMywin5.SetGlyph(Value: TBitmap);
begin
FGlyph:=TBitmap.Create;
if FGlyph<>Value then
begin
FGlyph.Assign(Value);
end;
end;
begin
FGlyph:=TBitmap.Create;//放到CREATE方法中去啊。
if FGlyph<>Value then
begin
FGlyph.Assign(Value);
end;
end;
begin
inherited Create(AOwner);
FBitBtn :=TBitbtn.Create(Self); FBitBtn.Parent := Self;
FBitBtn.Left:=Self.Left+Self.Width;
FBitBtn.Top:=Self.Top;
FGlyph:=TBitmap.Create;
FLabel:=TLabel.Create(Self);
FLabel.Parent:=Self;
FLabel.Left:=Self.Left;
FLabel.Height:=FBitbtn.Height;
FLabel.Top:=245;//Self.Top-FBitBtn.Height;
Width:=FLabel.Width;
Height:=FLabel.Height+FBitbtn.Height;
end;
这样对了吧?
begin
if FBitBtn.Glyph<>Value then
FBitBtn.Glyph.Assign(Value);
end;function TMywin4.GetGlyph:TBitmap;
begin
Result:=FBitBtn.Glyph;
end;
这样操作也就没有必要再多创建一个TBitMap对象而忽略FBitBtn本身对应对象的存在了!至于高度问题,你直接在WM_SIZE消息处理器使用注释部分的操作不可以吗?
感谢FrameSniper(§绕瀑游龙§) 解决了我Glyph的问题。