📄 eznumed.pas
字号:
Unit EzNumEd;
{***********************************************************}
{ EzGIS/CAD Components }
{ (c) 2003 EzSoft Engineering }
{ All Rights Reserved }
{***********************************************************}
{$I EZ_FLAG.PAS}
Interface
Uses
SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Menus,
Ezbase
{$IFNDEF BCB}
, DB, DBCtrls
{$ENDIF}
;
const
neEditBorderSize = 2;
neEditShadowSize = 2;
Type
{ Numeric edit control descentand of TCustomControl.
Key properties:
TEzNumEd.NumericValue.- Is the numeric value to edit.
TEzNumEd.EditFormat .- The format used when the control is in focus
TEzNumEd.Digits is the number of allowed integer digits
TEzNumEd.Decimals is the number of allowed decimals
TEzNumEd.UseThousandSeparator define if thousand separator is used
The thousand separator is taken from global var ThousandSeparator
TEzNumEd.EditFormat.LeftInfo is any string info shown to the left when editing
Example, you can add something like "$" to this property and the numeric
edit control will show a fixed currency symbol to the left.
One special code is implemented in this version: if you define this property to
"\c", then this code will be replaced by the currency symbol as is defined in global
var CurrencyString
TEzNumEd.EditFormat.RightInfo same as previous, but the string placed here will be
shown to the right of the numeric value. You can place here something like:
"Dlls." and when you edit the numeric value will be shown something like this:
$123,456.78 Dlls. Here LeftInfo = "\c", or "$" and RightInfo = " Dlls."
TEzNumEd.EditFormat.NegColor is the color when the numeric value is negative
TEzNumEd.DisplayFormat.- The format used when the control is out of focus
same as EditFormat but with three more properties:
TEzNumEd.DisplayFormat.Show defines if the numeric value is shown when not in focus
TEzNumEd.DisplayFormat.ZeroValue .-You define here the string you want to show when
the numericvalue is zero, example "Zero". If you left blank, then a "0" will be used
Default is blank
TEzNumEd.DisplayFormat.NegUseParens.- If true, then instead of showing the
"-" char for negative values, the numeric value will be enclosed in "()" pair.
TEzNumEd.TabOnEnterKey.- If set to true, when you press enter will be as if
you pressed the TAB key
TEzNumEd.Modified.- Indicates if the numeric value was edited.
TEzNumEd.ReadOnly.- If true, the numeric value cannot be edited.
TEzNumEd.AcceptNegatives.- If true, end user can type negative values
TEzNumEd.WidthPad, TEzNumEd.HeightPad.- Is a blank area to the left or right
of the numeric value inside the control
}
TEzNumEd = Class;
TEzEditFormat = class(TPersistent)
private
FNumEd: TEzNumEd;
{ the following codes applies:
- \C = the currency string
- any literal is displayed as is, like $
}
FLeftInfo: string;
FRightInfo: string;
procedure InvalidateEditor;
procedure SetLeftInfo(const Value: string);
procedure SetRightInfo(const Value: string);
public
constructor Create(NumEd: TEzNumEd);
procedure Assign(Source: TPersistent); Override;
published
property LeftInfo: string read FLeftInfo write SetLeftInfo;
property RightInfo: string read FRightInfo write SetRightInfo;
end;
TEzDisplayFormat = class(TEzEditFormat)
private
FShow: Boolean;
FZeroValue: String;
FNegUseParens: Boolean;
procedure SetShow(const Value: Boolean);
procedure SetNegUseParens(const Value: Boolean);
procedure SetZeroValue(const Value: String);
{$IFDEF BCB}
function GetNegUseParens: Boolean;
function GetShow: Boolean;
function GetZeroValue: String;
{$ENDIF}
public
constructor Create(NumEd: TEzNumEd);
procedure Assign(Source: TPersistent); Override;
published
property Show: Boolean {$IFDEF BCB} read GetShow {$ELSE} read FShow {$ENDIF} write SetShow default True;
property ZeroValue: String {$IFDEF BCB} read GetZeroValue {$ELSE} read FZeroValue {$ENDIF} write SetZeroValue;
property NegUseParens: Boolean {$IFDEF BCB} read GetNegUseParens {$ELSE} read FNegUseParens {$ENDIF} write SetNegUseParens default False;
end;
{ TEzNumEdit control }
TEzBorderStyle = (ebsNone, ebsSingle, ebsThick, ebsFlat, ebs3D);
TEzPartEditing = ( peInteger, peDecimal );
TEzNumEd = Class( TCustomControl )
Private
FDigits: Integer;
FDecimals: Integer;
FUseThousandSeparator: Boolean;
FNegColor: TColor;
FNumericValue: extended;
FOriginalValue: extended;
FAcceptNegatives: boolean;
FEditFormat: TEzEditFormat;
FDisplayFormat: TEzDisplayFormat;
FCaretBm: TBitmap;
FSelStart: integer;
FPartEditing: TEzPartEditing;
FTabOnEnterKey: boolean;
FModified: boolean;
FNegFlag: boolean;
FLastKey: char;
FOnChange: TNotifyEvent;
FReadOnly: boolean;
FSelected: boolean;
FMouseDown: boolean;
FHotTrack: Boolean;
//[KT] - added 12/06/01, FWidthPad, FHeightPad
FHeightPad: Integer;
FWidthPad: Integer;
FDecimalSeparator: Char;
FThousandSeparator: Char;
FUseWindowsSeparator: Boolean;
FMouseInControl: Boolean;
FShadowed: Boolean;
FShadowColor: TColor;
FShadowWidth: Integer;
FBorderColor: TColor;
FBorderSize: Integer;
FBorderStyle: TEzBorderStyle;
FDrawTextRect: TRect;
Procedure SetBorderStyle(Value: TEzBorderStyle);
Procedure SetBorderSize( Value: Integer );
Procedure SetBorderColor(Value: TColor);
procedure SetUseThousandSeparator(const Value: Boolean);
procedure SetNegColor(const Value: TColor);
procedure SetDecimals(const Value: Integer);
procedure SetDigits(const Value: Integer);
procedure RedrawBorder( const Clip: HRGN );
function MyFormatFloat(const Format: string; const Value: Extended): string;
Function MyStrToFloat( const S: string ): Extended;
function ReplaceCodes( const Info: string): string;
//[KT] - added 12/06/01, FWidthPad, FHeightPad
Procedure SetHeightPad( Value: Integer );
Procedure SetWidthPad( Value: Integer );
Function GetUnformattedText: string;
Procedure SetNumericValue( Value: extended );
Procedure SetCursor( Position: integer );
Procedure SetCursorPos;
Procedure EditEnter;
Procedure EditExit;
procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
//procedure WMNCCalcSize(var Message: TWMNCCalcSize); message WM_NCCALCSIZE;
procedure WMNCPaint(var Message: TMessage); message WM_NCPAINT;
Procedure WMActivateApp( Var Message: TWMActivateApp ); Message WM_ACTIVATEAPP;
Procedure WMActivate( Var Message: TWMActivate ); Message WM_ACTIVATE;
Procedure CMFontChanged( Var Message: TMessage ); Message CM_FONTCHANGED;
Procedure WMGetDlgCode( Var Message: TWMGetDlgCode ); Message WM_GETDLGCODE;
Procedure WMLButtonDown( Var Message: TWMLButtonDown ); Message WM_LBUTTONDOWN;
function IsDesigning: Boolean;
procedure SetEditFormat(Value: TEzEditFormat);
procedure SetDisplayFormat(Value: TEzDisplayFormat);
Function CreateMask( fm: TEzEditFormat ): string;
procedure SetDecimalSeparator(const Value: Char);
procedure SetThousandSeparator(const Value: Char);
procedure SetUseWindowsSeparator(Value: Boolean);
procedure SetShadowColor(const Value: TColor);
procedure SetShadowed(const Value: Boolean);
procedure SetShadowWidth(const Value: Integer);
function GetAbout: TEzAbout;
procedure SetAbout(const Value: TEzAbout);
Protected
Procedure Loaded; Override;
Procedure Change; Dynamic;
Procedure Paint; Override;
//procedure Click; override;
Procedure DblClick; Override;
Procedure MouseDown( Button: TMouseButton; Shift: TShiftState;
X, Y: Integer ); Override;
Procedure MouseMove( Shift: TShiftState; X, Y: Integer ); Override;
Procedure MouseUp( Button: TMouseButton; Shift: TShiftState;
X, Y: Integer ); Override;
Procedure SetEnabled( Value: Boolean ); Override;
Public
Constructor Create( AOwner: TComponent ); Override;
destructor Destroy; Override;
Procedure KeyDown( Var Key: Word; Shift: TShiftState ); Override;
Procedure KeyPress( Var Key: Char ); Override;
Function AsString: String;
Property Modified: boolean Read FModified Write FModified;
Published
//[KT] - added 12/06/01, FWidthPad, FHeightPad
Property About: TEzAbout read GetAbout write SetAbout;
property BorderStyle: TEzBorderStyle read FBorderStyle write SetBorderStyle default ebsSingle;
property BorderSize: Integer read FBorderSize write SetBorderSize default neEditBorderSize;
property BorderColor: TColor read FBorderColor write SetBorderColor default clHighlight;
property UseThousandSeparator: Boolean read FUseThousandSeparator write SetUseThousandSeparator default True;
property NegColor: TColor read FNegColor write SetNegColor default clRed;
Property Digits: Integer read FDigits write SetDigits default 8;
Property Decimals: Integer read FDecimals write SetDecimals default 2;
Property HotTrack: Boolean read FHotTrack write FHotTrack;
Property HeightPad: Integer Read FHeightPad Write SetHeightPad default 0;
Property WidthPad: Integer Read FWidthPad Write SetWidthPad default -15;
Property DecimalSeparator: Char read FDecimalSeparator write SetDecimalSeparator;
Property ThousandSeparator: Char read FThousandSeparator write SetThousandSeparator;
Property Shadowed: Boolean read FShadowed write SetShadowed default False;
Property ShadowColor: TColor read FShadowColor write SetShadowColor default clGray;
Property ShadowWidth: Integer read FShadowWidth write SetShadowWidth default neEditShadowSize;
Property AcceptNegatives: boolean Read FAcceptNegatives Write FAcceptNegatives Default true;
Property ReadOnly: boolean Read FReadOnly Write FReadOnly Default false;
Property TabOnEnterKey: Boolean Read FTabOnEnterKey Write FTabOnEnterKey Default true;
Property EditFormat: TEzEditFormat read FEditFormat write SetEditFormat;
Property DisplayFormat: TEzDisplayFormat read FDisplayFormat write SetDisplayFormat;
Property NumericValue: extended Read FNumericValue Write SetNumericValue;
Property UseWindowsSeparator: Boolean read FUseWindowsSeparator write SetUseWindowsSeparator default True;
Property OnChange: TNotifyEvent Read FOnChange Write FOnChange;
{publish this properties}
Property Anchors;
Property Align;
Property Color;
Property Ctl3D;
Property DragCursor;
Property DragMode;
Property Enabled;
Property Font;
Property ParentColor;
Property ParentCtl3D;
Property ParentFont;
Property ParentShowHint;
Property PopupMenu;
Property ShowHint;
Property TabOrder;
Property TabStop Default True;
Property Visible;
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;
{$IFNDEF BCB}
{ TEzDBNumEd control }
TEzDBNumEd = Class( TEzNumEd )
Private
FDataLink: TFieldDataLink;
Procedure DataChange( Sender: TObject );
Procedure EditingChange( Sender: TObject );
Procedure ActiveChange( Sender: TObject );
Procedure SetDataField( Const Value: String );
Function GetDataField: String;
Function GetDataSource: TDataSource;
Procedure SetDataSource( Value: TDataSource );
Function GetReadOnly: Boolean;
Procedure SetReadOnly( Value: Boolean );
Procedure UpdateData( Sender: TObject );
Procedure CheckFieldType( Const Value: String );
Protected
Procedure Notification( AComponent: TComponent;
Operation: TOperation ); Override;
Procedure CMEnter( Var Message: TCMEnter ); Message CM_ENTER;
Procedure CMExit( Var Message: TCMExit ); Message CM_EXIT;
Function GetField: TField;
Public
Procedure Change; Override;
Procedure KeyDown( Var Key: Word; Shift: TShiftState ); Override;
Procedure KeyPress( Var Key: Char ); Override;
Constructor Create( AOwner: TComponent ); Override;
Destructor Destroy; Override;
Property Field: TField Read GetField;
Published
Property ReadOnly: Boolean Read GetReadOnly Write SetReadOnly Default False;
Property DataField: String Read GetDataField Write SetDataField;
Property DataSource: TDataSource Read GetDataSource Write SetDataSource;
End;
{$ENDIF}
Implementation
Uses
Clipbrd, EzSystem, EzConsts ;
Type
EInvalidFieldType = Class( Exception );
procedure ShadeIt(f: TCustomForm; c: TControl; Width: Integer; Color: TColor);
var
rect: TRect;
old: TColor;
begin
if c.Visible then
begin
rect := c.BoundsRect;
rect.Left := rect.Left + Width;
rect.Top := rect.Top + Width;
rect.Right := rect.Right + Width;
rect.Bottom := rect.Bottom + Width;
old := f.Canvas.Brush.Color;
f.Canvas.Brush.Color := Color;
f.Canvas.fillrect(rect);
f.Canvas.Brush.Color := old;
end;
end;
{ TEzEditFormat }
constructor TEzEditFormat.Create(NumEd: TEzNumEd);
begin
inherited Create;
FNumEd:= NumEd;
end;
procedure TEzEditFormat.InvalidateEditor;
begin
If FNumEd.Focused Then
FNumEd.SetCursorPos;
FNumEd.Invalidate;
end;
procedure TEzEditFormat.Assign(Source: TPersistent);
Var
Src: TEzEditFormat Absolute Source;
Begin
If Source Is TEzEditFormat Then
Begin
FLeftInfo := Src.FLeftInfo;
FRightInfo := Src.FRightInfo;
End
Else
Inherited;
end;
procedure TEzEditFormat.SetLeftInfo(const Value: string);
begin
FLeftInfo := Value;
InvalidateEditor;
end;
procedure TEzEditFormat.SetRightInfo(const Value: string);
begin
FRightInfo := Value;
InvalidateEditor;
end;
{ TEzDisplayFormat }
constructor TEzDisplayFormat.Create(NumEd: TEzNumEd);
begin
inherited Create(NumEd);
FShow:= true;
end;
procedure TEzDisplayFormat.Assign(Source: TPersistent);
Var
Src: TEzDisplayFormat Absolute Source;
Begin
If Source Is TEzEditFormat Then
Begin
inherited Assign(Source);
FShow:= Src.FShow;
FZeroValue:= Src.FZeroValue;
FNegUseParens:= Src.FNegUseParens;
End
Else
Inherited;
end;
{$IFDEF BCB}
function TEzDisplayFormat.GetNegUseParens: Boolean;
begin
Result := FNegUseParens;
end;
function TEzDisplayFormat.GetShow: Boolean;
begin
Result := FShow;
end;
function TEzDisplayFormat.GetZeroValue: String;
begin
Result := FZeroValue;
end;
{$ENDIF}
procedure TEzDisplayFormat.SetZeroValue(const Value: String);
begin
if FZeroValue = Value then Exit;
FZeroValue:= Value;
InvalidateEditor;
end;
procedure TEzDisplayFormat.SetNegUseParens(const Value: Boolean);
begin
if FNegUseParens = Value then exit;
FNegUseParens := Value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -