Dalam pemrograman berbasis rad, component adalah hal yang mutlak yang harus diketahui. Borland delphi sangat kaya akan component yang bisa dipakai untuk mengembangkan aplikasi. Hal yang unik dari delphi adalah component delphi dibuat dengan delphi, sehingga delphi sangat flexible.
Dari sisi sudut pandang, pemrograman component sangatlah beda dengan pemrograman aplikasi secara umum. Kalau pemrograman aplikasi, orientasinya dititik beratkan pada pemakai akhir (end user) yang akan menggunakan aplikasi tersebut, yaitu bagaimana caranya agar user mudah mengoperasikan aplikasi, fleksibel, dan sebagainya. Sedangkan sudut pandang component writer/programmer adalah bagaimana caranya agar programmer yang akan memakai component-nya dapat memenuhi kebutuhan sang programmer. Sehingga sang programmer pemakai component tersebut tidak perlu lagi menulis kode program (yang mungkin sangat kompleks) yang sama sampai berulang-ulang hingga dapat menghemat waktu, tenaga dan biaya.
Di bawah ini adalah contoh komponent sederhana turunan dari TEdit,Saya namakan TReshiEdit. Fitur yang saya contohkan disini adalah untuk mengubah warna EditBox saat terfokus, dan kembali ke warna sebelumnya saat fokus meninggalkan EditBox, Serta pilihan fungsi edit, yakni untuk input telephone, input normal, dan input angka. Disini juga dikenalkan fungsi propercase, yakni untuk mengubah semua huruf awal dari sebuah kata menjadi huruf besar.
Source Code :
{**********************************************************************
Author : I GEDE MADE RESHI MAHENDRA
Copyright © 2005-2009
E-mail: reshimhendra@gmail.com
Web: http://reshimahendra.goldeye.info
Component Name : TReshiEdit
Feature : - Edit Mode : * esNormal, just like TEdit
* esPhone, only receive Numeric and
special caracter for phone only (E.g '(',')',
'-','+',Backspace,and Return)
- Alignment implementation TEdit
- Draw color OnEnter an OnExit
- Check if the edit is blank or empty
**********************************************************************}
unit TReshiEdit;
interface
uses
SysUtils, Spin, Graphics, Buttons, Messages,Classes, Controls, ComCtrls, StdCtrls, Forms, WinProcs;
Const
AuthorName = 'Copyright(C) I Gede Made Reshi Mahendra (mahendra.reshi@gmail.com)';
{******************
** Reshi EDIT **
******************}
type
//Mode Caracter Case
TCaseMode = (cmNormal, cmProperCase, cmLowerCase, cmUpperCase);
//Mode Type Edit
TEditStyle = (esNormal,esPhone, esNumeric);
TReshiCustomEdit = class(TCustomEdit)
private
FAlignment: TAlignment;
FEnterLikeTab : boolean;
FFocusedColor: TColor;
FCaseMode: TCaseMode;
FMouseINColor: TColor;
FMouseOutColor: TColor;
FMousePos : Boolean;
FEditStyle: TEditStyle;
FUnFocusedColor: TColor;
FCanEmpty: Boolean;
FReadOnlyColor: TColor;
function GetAbout: String;
procedure setReadOnlyColor(const Value: TColor);
procedure SetEditStyle(Value: TEditStyle);
procedure setAlignment(Value: TAlignment);
procedure setFocusedColor(Value: TColor);
procedure setCaseMode(Value: TCaseMode);
procedure setUnFocusedColor(Value: TColor);
procedure setCanEmpty(Value: Boolean);
protected
procedure DoEnter;override;
procedure DoExit;override;
procedure KeyPress(Var Key:Char);override;
procedure KeyDown(var Key: Word; Shift: TShiftState);override;
//CustomProperty
property Alignment : TAlignment
read FAlignment write setAlignment default taLeftJustify;
property FocusedColor : TColor
read FFocusedColor write setFocusedColor default clSkyBlue;
property UnFocusedColor : TColor
read FUnFocusedColor write setUnFocusedColor default clWhite;
property CharCase : TCaseMode
read FCaseMode write setCaseMode default cmNormal;
property EnterLikeTab : boolean
read FEnterLikeTab write FEnterLikeTab;
property EditStyle : TEditStyle
read FEditStyle write setEditStyle default esNormal;
property ReadOnlyColor : TColor
read FReadOnlyColor write setReadOnlyColor default clSkyBlue;
public
procedure CreateParams (var Params: TCreateParams);Override;
constructor Create (AOwner : TComponent);override;
destructor Destroy;override;
//Additional procedure/ function
function IsBlank:boolean;
published
property About : String
read GetAbout;
property CanEmpty : Boolean
read FCanEmpty write setCanEmpty default True;
End;
TReshiEdit = Class(TReshiCustomEdit)
published
Property About; property Alignment;
property FocusedColor; property UnFocusedColor;
property EnterLikeTab; property CharCase;
property EditStyle; property PasswordChar;
property Text; property Color;
property DragCursor; property DragMode;
property Enabled; property Font;
property HideSelection; property ParentColor;
property ParentCtl3D; property ParentFont;
property ParentShowHint; property PopupMenu;
property ReadOnly; property ShowHint;
property TabOrder; property TabStop;
property Visible; property MaxLength;
property ReadOnlyColor;
property OnChange; property OnClick;
property OnDblClick; property OnDragDrop;
property OnDragOver; property OnEndDrag;
property OnEnter; property OnExit;
property OnKeyDown; property OnKeyPress;
property OnKeyUp; property OnMouseDown;
property OnMouseMove; property OnMouseUp;
end;
Procedure Register;
implementation
{ TReshiEdit }
procedure Register;
begin
RegisterComponents('ReshiMahendra', [TReshiEdit]);
end;
constructor TReshiCustomEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCanEmpty := True;
BevelKind := bkFlat;
BorderStyle := bsNone;
FFocusedColor := clSkyBlue;
FUnFocusedColor := clWhite;
FReadOnlyColor := clSkyBlue;
FMousePos := true;
FMouseINColor := clInfoBk;
FMouseOutColor := clWhite;
ControlStyle := ControlStyle - [csSetCaption];
Height := 21;
FAlignment := taLeftJustify;
FEnterLikeTab := true;
//Disable popup
PopupMenu := nil;
end;
procedure TReshiCustomEdit.setCanEmpty(Value: Boolean);
begin
if FCanEmpty<>value then begin
FCanEmpty := Value;
Invalidate;
end;
end;
procedure TReshiCustomEdit.doExit;
begin
Color := FUnFocusedColor;
Text := Trim(Text);
Invalidate;
inherited doExit;
end;
procedure TReshiCustomEdit.KeyDown(var Key: Word; Shift: TShiftState);
var
FEditTemp : TCustomForm;
begin
inherited KeyDown(Key,Shift);
if (Key = 13) and FEnterLikeTab then
begin
FEditTemp := GetParentForm(Self);
SendMessage(FEditTemp.Handle, WM_NEXTDLGCTL, 0, 0);
Key := 0;
exit;
end;
end;
///////////////////////////////////
// OVERRIDING THE KEYPRESS EVENT //
///////////////////////////////////
procedure TReshiCustomEdit.keyPress(Var Key:Char);
Function IsNumeric : boolean;
begin
Result := (Key in ['0'..'9']) or (key =#13) or (Key = #8) //numeric,CariageReturn & BackSpace
end;
begin
inherited keyPress(key);
if (FEditStyle = esPhone) then begin
If not((IsNumeric)
Or (Key = #32) //space
or (Key = #40) // '('
or (Key = #41) // ')'
or (Key = #43) // '+' for country code
or (Key = #45)) then // '-'
Key := #0;
end else if (FEditStyle = esNumeric) then begin
if not IsNumeric then Key := #0;
end;
/////////////////////////////////////////////////////
// SET THE CASE MODE AFTER USER CLICK THE KEYBOARD //
/////////////////////////////////////////////////////
case FCaseMode of
cmProperCase : begin
if (SelStart = 0) or (Text[SelStart] = ' ') then
Key := AnsiUpperCase(Key)[1];
end;
cmUpperCase : begin
Key := AnsiUpperCase(Key)[1];
end;
cmLowerCase : begin
Key := AnsiLowerCase(Key)[1];
end;
end;
Invalidate;
end;
////////////////////////////
// CHANGE THE EDIT STYLE //
////////////////////////////
procedure TReshiCustomEdit.SetEditStyle(Value: TEditStyle);
begin
if (FEditStyle <> value) then begin
FEditStyle := Value;
if (FEditStyle = esPhone) then begin
Alignment := taLeftJustify;
Text := '(999)-99999999';
end
else if (FEditStyle = esNormal) then begin
Alignment := taLeftJustify;
Clear;
end else if (FEditStyle = esNumeric) then begin
Alignment := taRightJustify;
Clear;
end;
end;
end;
destructor TReshiCustomEdit.Destroy;
begin
inherited Destroy;
end;
procedure TReshiCustomEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[TAlignment] of Word = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or Alignments[FAlignment];
end;
procedure TReshiCustomEdit.setAlignment(Value: TAlignment);
var
theSelStart, theSelLength : Byte;
begin
if FAlignment <> Value then
begin
theSelStart := SelStart;
theSelLength := SelLength;
FAlignment := Value;
RecreateWnd;
SelStart := theSelStart;
SelLength := theSelLength;
end;
end;
procedure TReshiCustomEdit.setUnFocusedColor(Value: TColor);
begin
if FUnFocusedColor <> value then begin
FUnFocusedColor := Value;
invalidate;
end;
end;
procedure TReshiCustomEdit.doEnter;
begin
Color := FFocusedColor;
Text := Trim(Text);
Invalidate;
inherited doEnter;
end;
procedure TReshiCustomEdit.setFocusedColor(Value: TColor);
begin
if (FFocusedColor <> value) then
begin
FFocusedColor := Value;
Invalidate;
end;
end;
procedure TReshiCustomEdit.setCaseMode(Value: TCaseMode);
begin
if (FCaseMode <> Value) then
begin
FCaseMode := Value;
Invalidate;
end;
end;
function TReshiCustomEdit.IsBlank: boolean;
begin
Result := Text = '';
end;
procedure TReshiCustomEdit.setReadOnlyColor(const Value: TColor);
begin
if ReadOnly then begin
FUnFocusedColor := FReadOnlyColor;
FFocusedColor := FReadOnlyColor;
Color := FReadOnlyColor;
end else begin
FUnFocusedColor := clWhite;
FFocusedColor := clSkyBlue;
Color := clWhite;
end;
Invalidate;
end;
function TReshiCustomEdit.GetAbout: String;
begin
Result := AuthorName;
end;
end.
Semoga Membantu,
Reshi Mahendra
0 komentar:
Posting Komentar