输入整数、小数、钱的控件!
unit OopsNumberEdit;
{$D-}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TOopsNumberType = (ntInteger, ntDouble, ntMoney);
TOopsNumberEdit = class(TCustomEdit)
private
FCanvas: TControlCanvas;
FFocused: Boolean;
FValue: Extended;
FisNull: Boolean;
FNumberType: TOopsNumberType;
FValidChars: set of Char;
function GetTextMargins: TPoint;
procedure SetNumberType(Value: TOopsNumberType);
procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
protected
procedure Change; override;
procedure KeyPress(var Key: Char); override;
public
constructor Create(AOwner: TComponent); override;
procedure Clear; override;
property IsNull: Boolean read FIsNull;
property Value: Extended read FValue;
published
property Anchors;
property AutoSelect;
property AutoSize;
property BiDiMode;
property BorderStyle;
property Color;
property Constraints;
property Ctl3D;
property Enabled;
property Font;
property MaxLength;
property NumberType: TOopsNumberType read FNumberType write SetNumberType;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
procedure Register;
implementation
const
TOopsNumberFmt: array[TOopsNumberType]of string =
('#,##0','#,##0.##########','#,##0.00');
constructor TOopsNumberEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
Text:='';
end;
procedure TOopsNumberEdit.Change;
var c: Extended;
begin
if Trim(Text)='' then begin
FisNull:=True;
FValue:=0; end
else
if TextToFloat(PChar(Text), FValue, fvExtended)
then begin
FisNull:=False;
c:=FValue;
case FNumberType of
ntInteger: c:=Round(FValue);
ntMoney: c:=Round(FValue)+Round(Frac(FValue)*100)/100;
end;
if c<>FValue then Text:=FloatToStr(c);
end else begin
Text:=FloatToStr(FValue);
end;
inherited;
Invalidate;
end;
procedure TOopsNumberEdit.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
if (Key in [#32..#255]) and not(Key in FValidChars) then
begin
MessageBeep(0);
Key := #0;
end;
end;
procedure TOopsNumberEdit.SetNumberType(Value: TOopsNumberType);
begin
if FNumberType<>Value then FNumberType:=Value;
Change;
if FNumberType=ntInteger
then FValidChars := ['-', '0'..'9']
else FValidChars := [DecimalSeparator, '-', '0'..'9'];
end;
procedure TOopsNumberEdit.Clear;
begin
FisNull:=True;
FValue:=0;
inherited;
end;
procedure TOopsNumberEdit.CMEnter(var Message: TCMEnter);
begin
FFocused:=True;
inherited;
end;
procedure TOopsNumberEdit.CMExit(var Message: TCMExit);
begin
FFocused:=False;
if FisNull then Text:=''
else Text:=FloatToStr(FValue);
inherited;
Invalidate;
end;
procedure TOopsNumberEdit.WMPaint(var Message: TWMPaint);
const
AlignStyle : array[Boolean, TAlignment] of DWORD =
((WS_EX_LEFT, WS_EX_RIGHT, WS_EX_LEFT),
(WS_EX_RIGHT, WS_EX_LEFT, WS_EX_LEFT));
var
Left: Integer;
Margins: TPoint;
R: TRect;
DC: HDC;
PS: TPaintStruct;
S: string;
AAlignment: TAlignment;
ExStyle: DWORD;
begin
if FFocused then AAlignment := taLeftJustify
else AAlignment := taRightJustify;
if UseRightToLeftAlignment then ChangeBiDiModeAlignment(AAlignment);
if ((AAlignment = taLeftJustify) or FFocused) and
not (csPaintCopy in ControlState) then
begin
if SysLocale.MiddleEast and HandleAllocated and (IsRightToLeft) then
begin { This keeps the right aligned text, right aligned }
ExStyle := DWORD(GetWindowLong(Handle, GWL_EXSTYLE)) and (not WS_EX_RIGHT) and
(not WS_EX_RTLREADING) and (not WS_EX_LEFTSCROLLBAR);
if UseRightToLeftReading then ExStyle := ExStyle or WS_EX_RTLREADING;
if UseRightToLeftScrollbar then ExStyle := ExStyle or WS_EX_LEFTSCROLLBAR;
ExStyle := ExStyle or
AlignStyle[UseRightToLeftAlignment, AAlignment];
if DWORD(GetWindowLong(Handle, GWL_EXSTYLE)) <> ExStyle then
SetWindowLong(Handle, GWL_EXSTYLE, ExStyle);
end;
inherited;
Exit;
end;
if FCanvas = nil then
begin
FCanvas := TControlCanvas.Create;
FCanvas.Control := Self;
end;
DC := Message.DC;
if DC = 0 then DC := BeginPaint(Handle, PS);
FCanvas.Handle := DC;
try
FCanvas.Font := Font;
with FCanvas do
begin
R := ClientRect;
if not (NewStyleControls and Ctl3D) and (BorderStyle = bsSingle) then
begin
Brush.Color := clWindowFrame;
FrameRect(R);
InflateRect(R, -1, -1);
end;
Brush.Color := Color;
if not Enabled then
Font.Color := clGrayText;
if FisNull then S:='' else begin
S := FormatFloat(TOopsNumberFmt[FNumberType],FValue);
if FNumberType=ntMoney then S:=CurrencyString+S; end;
Margins := GetTextMargins;
case AAlignment of
taLeftJustify: Left := Margins.X;
taRightJustify: Left := ClientWidth - TextWidth(S) - Margins.X - 1;
else
Left := (ClientWidth - TextWidth(S)) div 2;
end;
if SysLocale.MiddleEast then UpdateTextFlags;
TextRect(R, Left, Margins.Y, S);
end;
finally
FCanvas.Handle := 0;
if Message.DC = 0 then EndPaint(Handle, PS);
end;
end;
function TOopsNumberEdit.GetTextMargins: TPoint;
var
DC: HDC;
SaveFont: HFont;
I: Integer;
SysMetrics, Metrics: TTextMetric;
begin
if NewStyleControls then
begin
if BorderStyle = bsNone then I := 0 else
if Ctl3D then I := 1 else I := 2;
Result.X := SendMessage(Handle, EM_GETMARGINS, 0, 0) and $0000FFFF + I;
Result.Y := I;
end else
begin
if BorderStyle = bsNone then I := 0 else
begin
DC := GetDC(0);
GetTextMetrics(DC, SysMetrics);
SaveFont := SelectObject(DC, Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
I := SysMetrics.tmHeight;
if I > Metrics.tmHeight then I := Metrics.tmHeight;
I := I div 4;
end;
Result.X := I;
Result.Y := I;
end;
end;
procedure Register;
begin
RegisterComponents('OopsWare', [TOopsNumberEdit]);
end;
end.