Kendi bileşen setimden kullanıp hatasını görmediklerimi burada yayınlamaya karar verdim. TEdit'in ufak değişiklikler geçirmiş hali olan TucEdit ile başlıyorum.
Eklenen özellikler:
-Focus olduğunda font ve renk seçebilme.
-Değer girilmediyse ve focused değilse font, renk ve yazı değiştirebilme. Bu özelliği açıklayayım: EmptyText property'sini boş bırakın. InsteadOfEmptyText property'sine Bu alanı boş bırakmayın yazarsanız, imleç edit üzerinde değil iken Edit.Text = '' ise edit'de Bu alanı boş bırakmayın yazacaktır. FontOnEmpty ve ColorOnEmpty property'leri ile de bu yazı yazarken geçerli olacak font ve arkaplan renklerini de seçebilirsiniz.
Hayırlı olması dileğiyle:
Kod: Tümünü seç
unit ucEdits;
interface
uses
Classes, Controls, Graphics, ucControls, Types, Forms, Windows, SysUtils,
Messages, StdCtrls, Mask;
type
TInputType = (itAll, itAlphabet, itFloat, itInteger);
type TucCustomEdit = class(TCustomMaskEdit)
private
{ Private Declarations }
FAlignment: TAlignment;
FEmptyText: String;
FInsteadOfEmptyText: String;
FIsEmptyText: Boolean;
FFontOnStandBy: TFont;
FFontOnEmpty: TFont;
FFontOnFocus: TFont;
FColorOnStandBy: TColor;
FColorOnEmpty: TColor;
FColorOnFocus: TColor;
FCheckEmptyText: Boolean;
// FInputType: TInputType;
procedure SetEmptyText(const Value: String);
procedure SetFontOnEmpty(const Value: TFont);
procedure SetInsteadOfEmptyText(const Value: String);
procedure SetText(const Value: String);
procedure SetColorOnEmpty(const Value: TColor);
procedure SetColorOnFocus(const Value: TColor);
procedure SetColorOnStandBy(const Value: TColor);
function GetText: String;
procedure SetFontOnFocus(const Value: TFont);
procedure SetFontOnStandBy(const Value: TFont);
procedure SetAlignment(const Value: TAlignment);
// procedure SetInputType(const Value: TInputType);
public
{ Public Declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetControlsAlignment: TAlignment; override;
protected
{ Protected Declarations }
procedure LoadColor; virtual;
procedure LoadFont; virtual;
procedure DoEnter; override;
procedure DoExit; override;
procedure CreateParams(var Params: TCreateParams); override;
procedure Loaded; override;
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
property CheckEmptyText: Boolean read FCheckEmptyText write FCheckEmptyText default True;
property IsEmptyText: Boolean read FIsEmptyText;
property Text: String read GetText write SetText;
property EmptyText: String read FEmptyText write SetEmptyText;
property InsteadOfEmptyText: String read FInsteadOfEmptyText
write SetInsteadOfEmptyText;
property FontOnEmpty: TFont read FFontOnEmpty write SetFontOnEmpty;
property FontOnFocus: TFont read FFontOnFocus write SetFontOnFocus;
property FontOnStandBy: TFont read FFontOnStandBy write SetFontOnStandBy;
property ColorOnEmpty: TColor read FColorOnEmpty write SetColorOnEmpty default clWindow;
property ColorOnFocus: TColor read FColorOnFocus write SetColorOnFocus default clWindow;
property ColorOnStandBy: TColor read FColorOnStandBy write SetColorOnStandBy default clWindow;
// property InputType: TInputType read FInputType write SetInputType default itAll;
published
{ Published Declarations }
end;
type
TucEdit = class(TucCustomEdit)
private
{ Private Declarations }
public
{ Public Declarations }
protected
{ Protected Declarations }
published
{ Published Declarations }
property Align;
property Alignment;
property Text;
property CheckEmptyText;
property EmptyText;
property InsteadOfEmptyText;
property FontOnEmpty;
property FontOnFocus;
property FontOnStandBy;
property ColorOnEmpty;
property ColorOnFocus;
property ColorOnStandBy;
property Anchors;
property AutoSelect;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind;
property BevelOuter;
property BiDiMode;
property BorderStyle;
property CharCase;
property Color;
property Constraints;
property Ctl3D default False;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property EditMask;
property ImeMode;
property ImeName;
property MaxLength;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PasswordChar;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
implementation
{ TucCustomEdit }
constructor TucCustomEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Parent := TWinControl(AOwner);
// FInputType := itAll;
FColorOnEmpty := clWindow;
FColorOnStandBy := clWindow;
FColorOnFocus := clWindow;
FCheckEmptyText := True;
FFontOnEmpty := TFont.Create;
FFontOnStandBy := TFont.Create;
FFontOnFocus := TFont.Create;
FFontOnStandBy.Assign(GetParentForm(Self).Font);
FFontOnFocus.Assign(FFontOnStandBy);
FFontOnEmpty.Assign(FFontOnStandBy);
FFontOnEmpty.Color := clGrayText;
FIsEmptyText := False;
Ctl3D := False;
end;//Create
destructor TucCustomEdit.Destroy;
begin
FFontOnStandBy.Free;
FFontOnEmpty.Free;
FFontOnFocus.Free;
inherited;
end;//Destroy
procedure TucCustomEdit.DoEnter;
begin
if IsEmptyText then
inherited Text := FEmptyText;
FIsEmptyText := False;
LoadColor;
LoadFont;
inherited;
end;//DoEnter
procedure TucCustomEdit.DoExit;
begin
if (Text = FEmptyText) then
begin
FIsEmptyText := True;
inherited Text := FInsteadOfEmptyText;
end;
LoadColor;
LoadFont;
inherited;
end;//DoExit
function TucCustomEdit.GetText: String;
begin
if FIsEmptyText then
Result := FEmptyText
else
Result := inherited Text;
end;//GetText
procedure TucCustomEdit.LoadColor;
begin
if Focused then
Color := FColorOnFocus
else if (FIsEmptyText) and (FCheckEmptyText) then
Color := FColorOnEmpty
else
Color :=FColorOnStandBy;
end;//LoadColor
procedure TucCustomEdit.SetColorOnEmpty(const Value: TColor);
begin
FColorOnEmpty := Value;
Refresh;
end;
procedure TucCustomEdit.SetEmptyText(const Value: String);
begin
FEmptyText := Value;
Refresh;
end;
procedure TucCustomEdit.SetColorOnFocus(const Value: TColor);
begin
FColorOnFocus := Value;
Refresh;
end;
procedure TucCustomEdit.SetFontOnEmpty(const Value: TFont);
begin
FFontOnEmpty.Assign(Value);
Refresh;
end;
procedure TucCustomEdit.SetInsteadOfEmptyText(const Value: String);
begin
FInsteadOfEmptyText := Value;
Refresh;
end;
procedure TucCustomEdit.SetColorOnStandBy(const Value: TColor);
begin
FColorOnStandBy := Value;
Refresh;
end;
procedure TucCustomEdit.SetText(const Value: String);
begin
if SameText(Value, EmptyText) then
begin
FIsEmptyText := True;
inherited Text := InsteadOfEmptyText;
end
else begin
FIsEmptyText := False;
inherited Text := Value;
end;//if
LoadFont;
end;//SetText
procedure TucCustomEdit.LoadFont;
begin
if Focused then
Font := FFontOnFocus
else if (FIsEmptyText) and (FCheckEmptyText) then
Font := FFontOnEmpty
else
Font := FFontOnStandBy;
end;//LoadFont
procedure TucCustomEdit.SetFontOnFocus(const Value: TFont);
begin
FFontOnFocus := Value;
end;
procedure TucCustomEdit.SetFontOnStandBy(const Value: TFont);
begin
FFontOnStandBy := Value;
end;
procedure TucCustomEdit.SetAlignment(const Value: TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
RecreateWnd;
end;//if
end;
function TucCustomEdit.GetControlsAlignment: TAlignment;
begin
Result := FAlignment;
end;
procedure TucCustomEdit.CreateParams(var Params: TCreateParams);
var
AEditStyle: DWORD;
begin
inherited CreateParams(Params);
case FAlignment of
taLeftJustify: AEditStyle := ES_LEFT;
taRightJustify: AEditStyle := ES_RIGHT;
else
AEditStyle := ES_CENTER;
end;//case-of
Params.Style := Params.Style or AEditStyle;
end;//CreateParams
procedure TucCustomEdit.Loaded;
begin
inherited Loaded;
DoExit;
end;//Loaded
end.