VCL Inherited Problem |
尚未結案
|
BorlandUser
中階會員 發表:148 回覆:217 積分:73 註冊:2004-02-19 發送簡訊給我 |
我想寫一個自己用的vcl出來,如TMyEdit,這個是inherited from TEdit,請問應該怎樣做? unit TestUnit; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, UnitClass; type
TForm1 = class(TForm)
Edit1: TMyEdit;
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} end. unit UnitClass; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TMyEdit = class(TEdit)
private
{ Private declarations }
public
{ Public declarations }
end; implementation end. 當我compile時會有問題,不能用TMyEdit作為declaration,請問怎麼解決呢?
|
change.jian
版主 發表:29 回覆:620 積分:439 註冊:2003-06-02 發送簡訊給我 |
|
BorlandUser
中階會員 發表:148 回覆:217 積分:73 註冊:2004-02-19 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Want to use your edit component at design time? RegisterComponents in procedure Register... and perhaps create your own package. http://pywong.hk.st http://www.lazybones.ca
|
BorlandUser
中階會員 發表:148 回覆:217 積分:73 註冊:2004-02-19 發送簡訊給我 |
|
BorlandUser
中階會員 發表:148 回覆:217 積分:73 註冊:2004-02-19 發送簡訊給我 |
|
change.jian
版主 發表:29 回覆:620 積分:439 註冊:2003-06-02 發送簡訊給我 |
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
首先您可以在 Component/New Component
建立所繼承的類別。如 TEDIT 事實上,直接參考 StdCtrls,Controls 就很夠用了。 最後元件需要註冊後。才能在設計時建立控件使用。 如:
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('分頁名稱', [TMyEdit]);
end; 以下是我寫的繼承 Tedit 的控件。(寫的不是很好!請見諒!)
您可以參考一下! unit Evedit; interface uses
Sysutils,Classes,StdCtrls,Messages,Controls,Forms,Windows,Graphics; const
NumChars: set of Char = ['0'..'9'];
CM_ChangeAlign = WM_USer 9999;
type
TevEditType = (evNone,evWord,evInteger,evFloat);
TEvEdit = class(TEdit)
private
Alignment : TAlignment;
FFocusAlignment : TAlignment;
FNoFocusAlignment : TAlignment;
FEditType : TevEditType;
FPrecision : Byte;
FDecimals : Byte;
FEnterNext : Boolean;
KeyPressed : Boolean;
IsLoaded : Boolean;
FCTL3D : Boolean;
FBcolor : Tcolor;
CharSet : set of Char;
FBStyle : TBorderStyle; function GetFloat: Double;
function GetInteger: Integer;
function NoValidValue: Boolean;
procedure SetBorderStyle(Value: TBorderStyle);
procedure SetAlignment(Value: TAlignment);
procedure SetNoFocusAlignment(Value: TAlignment);
Procedure SetCTL3D(Value:Boolean);
Procedure SetColor(Value:Tcolor);
procedure SetEditType (Value: TevEditType);
procedure CMChangeAlign(Var MSg:Tmessage);Message CM_ChangeAlign;
protected
procedure DrawBorder;
procedure CreateParams(var Params: TCreateParams); override;
procedure KeyPress(var Key: Char);override;
procedure WndProc(var Message: TMessage); override;
procedure DoEnter; override;
procedure DoExit; override;
procedure Loaded; override;
procedure Change; override;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
Public
constructor Create(AOwner: TComponent); override;
procedure FormatValue;
published
Property BorderColor:Tcolor read FBcolor write SetColor default clgray;
Property Ctl3D:Boolean read FCTL3D write SetCTL3D;
property BorderStyle: TBorderStyle read FBStyle write SetBorderStyle;
property FocusAlignment :TAlignment read FFocusAlignment write FFocusAlignment default taLeftJustify ;
property NoFocusAlignment :TAlignment read FNoFocusAlignment write SetNoFocusAlignment default taLeftJustify;
property Precision : Byte index 0 read FPrecision write FPrecision;
property Decimals : Byte index 1 read FDecimals write FDecimals;
property EditType : TevEditType read fEditType write SetEditType;
Property EnterNextCTRL:Boolean Read fEnterNext Write fEnterNext;
Property Align;
end; procedure Register; implementation procedure Register;
begin
RegisterComponents('Evon', [TEvEdit]);
end; constructor TEvEdit.Create;
begin
inherited Create(AOwner);
FEditType :=evNone;
Alignment :=taLeftJustify;
Precision :=15;
FCTL3D :=True;
FBStyle :=bsSingle;
FBcolor :=clgray;
FFocusAlignment :=taLeftJustify;
FNoFocusAlignment :=taLeftJustify;
end; procedure TEvEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
case Alignment of
taLeftJustify :Params.Style:=Params.Style or ES_LEFT;
taRightJustify :Params.Style:=Params.Style or ES_RIGHT;
taCenter :Params.Style:=Params.Style or ES_CENTER;
end;
end; procedure TEvEdit.SetBorderStyle(Value: TBorderStyle);
begin
if FBStyle <> Value then
begin
FBStyle := Value;
if AutoSize then TEdit(Self).BorderStyle := FBStyle
else TEdit(Self).BorderStyle := BsSingle;
RecreateWnd;
end;
end; procedure TEvEdit.SetAlignment(Value: TAlignment);
begin
if Value <> Alignment then
begin
Alignment:=Value;
RecreateWnd;
end;
end; procedure TEvEdit.SetNoFocusAlignment(Value: TAlignment);
begin
if Value <> FNoFocusAlignment then
begin
FNoFocusAlignment:=Value;
SetAlignment(FNoFocusAlignment);
end;
end; Procedure TEvEdit.SetColor(Value:Tcolor);
begin
if fbColor<>Value then
begin
Fbcolor := Value;
Invalidate;
end;
end; procedure TEvEdit.SetEditType(Value: TevEditType);
begin
FEditType:=Value;
case FEditType of
evInteger : CharSet:=NumChars ['-'];
evFloat : CharSet:=NumChars ['-'] [DecimalSeparator] ['E'];
EvWord : CharSet:=NumChars;
evNone : CharSet:=[];
end;
if csDesigning in ComponentState then
begin
if FEditType <> evNone then
begin
FocusAlignment :=taLeftJustify;
NoFocusAlignment :=taRightJustify;
if NoValidValue then Text:='0';
FormatValue;
end;
end;
end; procedure TEvEdit.KeyPress(var Key: Char);
begin
if FEditType <> evNone then
begin
if Key = '-' then
begin
if Pos('-',Text) > 0 then if SelLength > 0 then Text:='' else Key:=#0;
if (SelLength = 0) and (SelStart > 0) then Key:=#0;
end;
if (Key = DecimalSeparator) and (Pos(DecimalSeparator,Text) > 0) then
if SelLength > 0 then Text:='' else Key:=#0;
if not (((Key>#0) and (Key <#32)) or (Key in CharSet)) then Key:=#0;
end;
if (Key=#13) and EnterNextCTRL then (Owner as TControl).Perform(WM_NEXTDLGCTL,0,0); inherited;
KeyPressed:=True;
end; function TEvEdit.GetFloat: Double;
begin
Result:=0.0;
if not NoValidValue then Result:=StrToFloat(Text);
end; function TEvEdit.GetInteger: Integer;
var
Error: Integer;
begin
Result:=0;
if Length(Text) > 0 then Val(Text,Result,Error);
end; function TEvEdit.NoValidValue: Boolean;
var
N: Byte;
begin
Result:=False;
if FEditType = evNone then Exit;
Text:=Trim(Text);
if Length(Text) > 0 then
begin
for N:=1 to Length(Text) do if not (Text[N] in CharSet) then Result:=True;
if not Result then if (FEditType = evInteger) or (FEditType = evFloat) then
begin
N:=Pos('-',Text);
if (N > 1) or ((N > 0) and (Length(Text) < 2)) then Result:=True;
end;
end
else Result:=True;
end; procedure TEvEdit.FormatValue;
begin
case fEditType of
evNone : if MaxLength > 0 then Text:=Copy(Text,1,Succ(MaxLength));
evFloat : Text:=FloatToStrF(GetFloat,ffFixed,FPrecision,FDecimals);
evInteger,evWord : Text:=IntToStr(GetInteger);
end;
Repaint;
end; procedure TEvEdit.Loaded;
begin
inherited;
FormatValue;
SetAlignment(FNoFocusAlignment);
IsLoaded:=True;
end; procedure TEvEdit.DoEnter;
begin
inherited;
Invalidate;
end; procedure TEvEdit.DoExit;
begin
inherited;
FormatValue;
end; procedure TEvEdit.Change;
begin
inherited;
if IsLoaded and not KeyPressed then FormatValue;
end; procedure TEvEdit.WndProc(var Message: TMessage);
begin
case Message.Msg of
WM_SETFOCUS:PostMessage(Handle,CM_ChangeAlign,1,1);
cm_Exit: begin
PostMessage(Handle,CM_ChangeAlign,0,0);
KeyPressed:=False;
end;
end;
inherited;
end; procedure TEvEdit.CMChangeAlign(Var MSg:Tmessage);
begin
if Msg.WParam=1 then SetAlignment(FFocusAlignment)
else SetAlignment(FNoFocusAlignment);
if (MSG.LParam=1) and autoSelect then SelectAll;
end; Procedure TEvEdit.SetCTL3D(Value:Boolean);
begin
if FCTL3D<>Value then
begin
FCTL3D := Value;
if AutoSize then TEdit(Self).CTL3D := FCTL3D
else TEdit(Self).CTL3D := True;
if FCTL3D then RecreateWnd else Invalidate;
end;
end; procedure TEvEdit.DrawBorder;
var
DC : HDC;
ARect : TRect;
I : integer;
begin
if (CTL3D and (BorderStyle=bsSingle)) or
(AutoSize and (CTL3D or (BorderStyle=bsNone))) then Exit; DC := GetWindowDC(Handle);
Try
GetWindowRect(Handle, ARect);
OffsetRect(ARect, -ARect.Left, -ARect.Top); if (Not AutoSize) and ((Not CTL3D ) or (BorderStyle=bsNone)) then
For i:=0 to 1 do
FrameRect(DC,Rect(Arect.Left I,Arect.Top I,Arect.Right-I,Arect.Bottom-I)
,Brush.handle); if (Not CTL3D ) and (BorderStyle=bsSingle) then
begin
Brush.Color :=BorderColor;
FrameRect(DC,Arect,Brush.handle);
Brush.Color :=Color;
end;
finally
ReleaseDC (Handle, DC);
end;
end; procedure TEvEdit.WMPaint(var Message: TWMPaint);
begin
inherited;
DrawBorder;
end; end.
|
BorlandUser
中階會員 發表:148 回覆:217 積分:73 註冊:2004-02-19 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |