S
scLizongliang
Unregistered / Unconfirmed
GUEST, unregistred user!
我想在以TQRDBTEXT作父类做一控件,可TQRDBtext(TDBtext,Tlabel,TQrlablel)没有象
Tedit类类似的OnChange事件,怎么加入?
加入的代码(大致)如下(实现无级缩放,代码可能有错):
var
lf:TLogFont;
tf:TFont;
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.Handle,sizeof(lf),@lf);
lf.lfHeight:=(TQRDBTEXT.Height-2);
lf.lfWidth:=TQRDBTEXT.Width div (length(trim(TQRDBTEXT.caption))+2);
end;
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
end;
由于逻辑字体宽度(lf.lfWidth)只能为整数,字多并不能缩放满,望高手看看、解决,自做控件原码如下:
unit AutoFontEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls,Graphics;
type
TAutoFontEdit=class(TEdit)
private
{ Private declarations }
FAutoSize:boolean;
//procedure Startinit(Sender:TObject);
protected
{ Protected declarations }
procedure ChangeText(Sender:TObject);
procedure ExitText(Sender:TObject);
procedure EnterText(Sender:TObject);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor destroy;override;
published
{ Published declarations }
property Font;
property Height;
property Width;
Property AutoSize;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('lzl', [TAutoFontEdit]);
end;
constructor TAutoFontEdit.Create(AOwner: TComponent);
begin
FAutoSize:=False;
inherited;
OnChange:=ChangeText;
OnExit:=ExitText;
OnEnter:=EnterText;
AutoSize:=False;
Font.Name:='宋体';
//Startinit(AOwner as TEdit);
end;
destructor TAutoFontEdit.destroy;
begin
inherited;
end;
procedure TAutoFontEdit.ChangeText(Sender:TObject);
var
lf:TLogFont;
tf:TFont;
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.Handle,sizeof(lf),@lf);
lf.lfHeight:=(Height-2);
if length(text)>0 then begin
lf.lfWidth:=Width div (length(trim(text))+2);
end;
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
end;
procedure TAutoFontEdit.ExitText(Sender:TObject);
var
lf:TLogFont;
tf:TFont;
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.Handle,sizeof(lf),@lf);
lf.lfHeight:=(Height-2);
if length(text)>0 then begin
lf.lfWidth:=Width div length(trim(text));
end;
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
end;
procedure TAutoFontEdit.EnterText(Sender:TObject);
var
lf:TLogFont;
tf:TFont;
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.Handle,sizeof(lf),@lf);
lf.lfHeight:=(Height-2);
if length(text)>0 then begin
lf.lfWidth:=Width div (length(trim(text))+2);
end;
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
end;
{
procedure TAutoFontEdit.Startinit(Sender:TObject);
var
lf:TLogFont;
tf:TFont;
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.Handle,sizeof(lf),@lf);
lf.lfHeight:=(Height-2);
if length(text)>0 then begin
lf.lfWidth:=Width div length(trim(text));
end;
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
end;
}
end.
Tedit类类似的OnChange事件,怎么加入?
加入的代码(大致)如下(实现无级缩放,代码可能有错):
var
lf:TLogFont;
tf:TFont;
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.Handle,sizeof(lf),@lf);
lf.lfHeight:=(TQRDBTEXT.Height-2);
lf.lfWidth:=TQRDBTEXT.Width div (length(trim(TQRDBTEXT.caption))+2);
end;
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
end;
由于逻辑字体宽度(lf.lfWidth)只能为整数,字多并不能缩放满,望高手看看、解决,自做控件原码如下:
unit AutoFontEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls,Graphics;
type
TAutoFontEdit=class(TEdit)
private
{ Private declarations }
FAutoSize:boolean;
//procedure Startinit(Sender:TObject);
protected
{ Protected declarations }
procedure ChangeText(Sender:TObject);
procedure ExitText(Sender:TObject);
procedure EnterText(Sender:TObject);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor destroy;override;
published
{ Published declarations }
property Font;
property Height;
property Width;
Property AutoSize;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('lzl', [TAutoFontEdit]);
end;
constructor TAutoFontEdit.Create(AOwner: TComponent);
begin
FAutoSize:=False;
inherited;
OnChange:=ChangeText;
OnExit:=ExitText;
OnEnter:=EnterText;
AutoSize:=False;
Font.Name:='宋体';
//Startinit(AOwner as TEdit);
end;
destructor TAutoFontEdit.destroy;
begin
inherited;
end;
procedure TAutoFontEdit.ChangeText(Sender:TObject);
var
lf:TLogFont;
tf:TFont;
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.Handle,sizeof(lf),@lf);
lf.lfHeight:=(Height-2);
if length(text)>0 then begin
lf.lfWidth:=Width div (length(trim(text))+2);
end;
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
end;
procedure TAutoFontEdit.ExitText(Sender:TObject);
var
lf:TLogFont;
tf:TFont;
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.Handle,sizeof(lf),@lf);
lf.lfHeight:=(Height-2);
if length(text)>0 then begin
lf.lfWidth:=Width div length(trim(text));
end;
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
end;
procedure TAutoFontEdit.EnterText(Sender:TObject);
var
lf:TLogFont;
tf:TFont;
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.Handle,sizeof(lf),@lf);
lf.lfHeight:=(Height-2);
if length(text)>0 then begin
lf.lfWidth:=Width div (length(trim(text))+2);
end;
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
end;
{
procedure TAutoFontEdit.Startinit(Sender:TObject);
var
lf:TLogFont;
tf:TFont;
begin
tf:=TFont.Create;
tf.Assign(Font);
GetObject(tf.Handle,sizeof(lf),@lf);
lf.lfHeight:=(Height-2);
if length(text)>0 then begin
lf.lfWidth:=Width div length(trim(text));
end;
tf.Handle:=CreateFontIndirect(lf);
Font.Assign(tf);
tf.Free;
end;
}
end.