Z
zhihuali
Unregistered / Unconfirmed
GUEST, unregistred user!
{******************************************************************************}
{ }
{ Email : zhihuali@163.net }
{ Email : lizhihua186@msn.com }
{ }
{ Date : 2001-12-19 }
{ }
{******************************************************************************}
{ }
{ 2002-01-19 : 添加 InputStyle , EnterTab 属性 }
{ 2002-01-22 : 添加对齐方式 Alignment }
{ }
{******************************************************************************}
unit StyleEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls;
type
TSetInputStyles = (stString,stInteger,stFloat,stMoney);
type
TStyleEdit = class(TEdit)
private
FInputStyle : TSetInputStyles;
FAlignment : TAlignment;
FEnterTab : Boolean;
FOnKeyPress : TKeyPressEvent;
procedure SetInputStyle(Value : TSetInputStyles);
procedure SetAlignment(Value : TAlignment);
procedure SetEnterTab(Value : Boolean);
{ Private declarations }
protected
procedure KeyPress(var Key: Char); OverRide;
procedure CreateParams(var Params: TCreateParams); OverRide;
{ Protected declarations }
public
constructor Create(AOwner : TComponent); OverRide;
{ Public declarations }
published
property InputStyle : TSetInputStyles Read FInputStyle Write SetInputStyle Default stString;
property Alignment : TAlignment Read FAlignment Write SetAlignment Default taLeftJustify;
property EnterTab : Boolean Read FEnterTab Write SetEnterTab Default True;
property OnKeyPress : TKeyPressEvent read FOnKeyPress write FOnKeyPress;
{ Published declarations }
end;
procedure Register;
implementation
{******************************************************************************}
{ 注册控件 }
{******************************************************************************}
procedure Register;
begin
RegisterComponents('LzhComponents', [TStyleEdit]);
end;
{ TStyleEdit }
{******************************************************************************}
{ 设置控件属性 }
{******************************************************************************}
constructor TStyleEdit.Create(AOwner: TComponent);
begin
inherited;
Text := '0';
end;
{******************************************************************************}
{ 设置控件左右对齐方式 }
{******************************************************************************}
procedure TStyleEdit.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 TStyleEdit.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
if FEnterTab and (Key = #13) then
begin
PostMessage(Handle, WM_KEYDOWN, VK_TAB, 0);
Key := #0;
Exit;
end;
case FInputStyle of
stInteger:
if not (Key in ['0'..'9', Chr(8)]) then Key := #0;
stFloat:
begin
if not (Key in ['0'..'9', '.', Chr(8)]) then Key := #0;
if (Key = '.') and (Pos(Key, Text) > 0) then Key := #0;
end;
stMoney:
begin
if not (Key in ['0'..'9', '.', Chr(8)]) then Key := #0;
if (Key = '.') and (Pos(Key, Text) > 0) then Key := #0;
if (Pos('.', Text) > 0) and (Key <> Chr(8)) and
((Length(Text) - Pos('.', Text)) >= 2) then Key := #0;
end;
end;
end;
{******************************************************************************}
{ 设置是否用回车代替 TAB 按键 }
{******************************************************************************}
procedure TStyleEdit.SetEnterTab(Value: Boolean);
begin
if FEnterTab <> Value then
FEnterTab := Value;
end;
{******************************************************************************}
{ 设置控件的 InputStyle 属性 }
{******************************************************************************}
procedure TStyleEdit.SetInputStyle(Value : TSetInputStyles);
begin
if Value <> FInputStyle then
FInputStyle := Value;
end;
{******************************************************************************}
{ 设置控件的 Alignment 属性 }
{******************************************************************************}
procedure TStyleEdit.SetAlignment(Value : TAlignment);
begin
if Value <> FAlignment then
begin
FAlignment := Value;
RecreateWnd;
end;
end;
end.
{ }
{ Email : zhihuali@163.net }
{ Email : lizhihua186@msn.com }
{ }
{ Date : 2001-12-19 }
{ }
{******************************************************************************}
{ }
{ 2002-01-19 : 添加 InputStyle , EnterTab 属性 }
{ 2002-01-22 : 添加对齐方式 Alignment }
{ }
{******************************************************************************}
unit StyleEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls;
type
TSetInputStyles = (stString,stInteger,stFloat,stMoney);
type
TStyleEdit = class(TEdit)
private
FInputStyle : TSetInputStyles;
FAlignment : TAlignment;
FEnterTab : Boolean;
FOnKeyPress : TKeyPressEvent;
procedure SetInputStyle(Value : TSetInputStyles);
procedure SetAlignment(Value : TAlignment);
procedure SetEnterTab(Value : Boolean);
{ Private declarations }
protected
procedure KeyPress(var Key: Char); OverRide;
procedure CreateParams(var Params: TCreateParams); OverRide;
{ Protected declarations }
public
constructor Create(AOwner : TComponent); OverRide;
{ Public declarations }
published
property InputStyle : TSetInputStyles Read FInputStyle Write SetInputStyle Default stString;
property Alignment : TAlignment Read FAlignment Write SetAlignment Default taLeftJustify;
property EnterTab : Boolean Read FEnterTab Write SetEnterTab Default True;
property OnKeyPress : TKeyPressEvent read FOnKeyPress write FOnKeyPress;
{ Published declarations }
end;
procedure Register;
implementation
{******************************************************************************}
{ 注册控件 }
{******************************************************************************}
procedure Register;
begin
RegisterComponents('LzhComponents', [TStyleEdit]);
end;
{ TStyleEdit }
{******************************************************************************}
{ 设置控件属性 }
{******************************************************************************}
constructor TStyleEdit.Create(AOwner: TComponent);
begin
inherited;
Text := '0';
end;
{******************************************************************************}
{ 设置控件左右对齐方式 }
{******************************************************************************}
procedure TStyleEdit.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 TStyleEdit.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
if FEnterTab and (Key = #13) then
begin
PostMessage(Handle, WM_KEYDOWN, VK_TAB, 0);
Key := #0;
Exit;
end;
case FInputStyle of
stInteger:
if not (Key in ['0'..'9', Chr(8)]) then Key := #0;
stFloat:
begin
if not (Key in ['0'..'9', '.', Chr(8)]) then Key := #0;
if (Key = '.') and (Pos(Key, Text) > 0) then Key := #0;
end;
stMoney:
begin
if not (Key in ['0'..'9', '.', Chr(8)]) then Key := #0;
if (Key = '.') and (Pos(Key, Text) > 0) then Key := #0;
if (Pos('.', Text) > 0) and (Key <> Chr(8)) and
((Length(Text) - Pos('.', Text)) >= 2) then Key := #0;
end;
end;
end;
{******************************************************************************}
{ 设置是否用回车代替 TAB 按键 }
{******************************************************************************}
procedure TStyleEdit.SetEnterTab(Value: Boolean);
begin
if FEnterTab <> Value then
FEnterTab := Value;
end;
{******************************************************************************}
{ 设置控件的 InputStyle 属性 }
{******************************************************************************}
procedure TStyleEdit.SetInputStyle(Value : TSetInputStyles);
begin
if Value <> FInputStyle then
FInputStyle := Value;
end;
{******************************************************************************}
{ 设置控件的 Alignment 属性 }
{******************************************************************************}
procedure TStyleEdit.SetAlignment(Value : TAlignment);
begin
if Value <> FAlignment then
begin
FAlignment := Value;
RecreateWnd;
end;
end;
end.