📄 eznumed.pas
字号:
Invalidate;
End;
Procedure TEzNumEd.SetNumericValue( Value: Extended );
Begin
If Value <> FNumericValue Then
Begin
if Not FAcceptNegatives And (Value < 0) then Value := Abs( Value );
FNumericValue := Value;
FOriginalValue := FNumericValue;
If Focused Then SetCursorPos;
Invalidate;
Change;
End;
End;
Procedure TEzNumEd.DblClick;
Begin
FSelected := true;
FModified := false;
if FPartEditing <> peInteger then
begin
FPartEditing := peInteger;
SetCursorPos;
end;
Invalidate;
Inherited DblClick;
End;
Procedure TEzNumEd.MouseDown( Button: TMouseButton; Shift: TShiftState;
X, Y: Integer );
Begin
if not Focused then Windows.SetFocus(Handle);
If ( Button = mbLeft ) And ( Shift = [ssLeft] ) Then
begin
if FSelected then
begin
FSelected := false;
Invalidate;
end else
FMouseDown:= true;
end;
Inherited MouseDown( Button, Shift, X, Y );
End;
Procedure TEzNumEd.MouseMove( Shift: TShiftState; X, Y: Integer );
Begin
If FMouseDown Then
Begin
FSelected := true;
Invalidate;
End;
Inherited MouseMove( Shift, X, Y );
End;
Procedure TEzNumEd.MouseUp( Button: TMouseButton; Shift: TShiftState;
X, Y: Integer );
Begin
FMouseDown := false;
Inherited MouseUp( Button, Shift, X, Y );
End;
Procedure TEzNumEd.WMActivate( Var Message: TWMActivate );
Var
ParentForm: TCustomForm;
Begin
If IsDesigning Then
Begin
Inherited;
Exit;
End;
ParentForm := GetParentForm( Self );
If Assigned( ParentForm ) And ParentForm.HandleAllocated Then
SendMessage( ParentForm.Handle, WM_NCACTIVATE, Ord( Message.Active <> WA_INACTIVE ), 0 );
If Message.Active = WA_INACTIVE Then
EditExit
Else
EditEnter;
End;
Function TEzNumEd.CreateMask( fm: TEzEditFormat ): string;
Var
temp: string;
I, n: Integer;
begin
if UseThousandSeparator then
begin
n:= Digits div 3;
if (Digits mod 3) = 0 then Dec(n);
SetLength(Result, Digits + n);
for I:= 1 to Length(Result) do
Result[I]:= '#';
{ add a 0 to the end of formatting string }
Result[Length(Result)]:= '0';
I:= Length(Result) - 3;
while I > 0 do
begin
Result[I] := FThousandSeparator;
Dec(I, 4);
end;
end else
begin
SetLength(Result, Digits);
for I:= 1 to Length(Result) do
Result[I]:= '#';
Result[Length(Result)]:= '0';
end;
if Decimals > 0 then
begin
SetLength(temp, Decimals);
for I:= 1 to Length(temp) do
temp[I]:= '0';
Result:= Result + FDecimalSeparator + temp;
end;
end;
Function TEzNumEd.MyStrToFloat( const S: string ): Extended;
Var
TmpDecimalSeparator: Char;
TmpThousandSeparator: Char;
Modified: Boolean;
Begin
Modified:= False;
TmpDecimalSeparator:= SysUtils.DecimalSeparator;
TmpThousandSeparator:= SysUtils.ThousandSeparator;
if Not( (FThousandSeparator = SysUtils.ThousandSeparator) And
(FDecimalSeparator = SysUtils.DecimalSeparator ) ) then
begin
SysUtils.DecimalSeparator:= FDecimalSeparator;
SysUtils.ThousandSeparator:= FThousandSeparator;
Modified:= True;
end;
Result:= StrToFloat( s );
if Modified then
begin
SysUtils.DecimalSeparator:= TmpDecimalSeparator;
SysUtils.ThousandSeparator:= TmpThousandSeparator
end;
End;
function TEzNumEd.MyFormatFloat(const Format: string; const Value: Extended): string;
var
I: Integer;
TmpFmt: string;
TmpDecimalSeparator: Char;
TmpThousandSeparator: Char;
Modified: Boolean;
begin
{if ( FDecimalSeparator = '.' ) And ( FThousandSeparator = ',' ) And
( SysUtils.DecimalSeparator = '.' ) And ( SysUtils.ThousandSeparator = ',' ) then
begin
Result:= FormatFloat( Format, Value );
Exit;
end ; }
TmpFmt:= Format;
for I:= 1 to Length(TmpFmt) do
if TmpFmt[I] = FThousandSeparator then TmpFmt[I] := ','
else if TmpFmt[I] = FDecimalSeparator then TmpFmt[I] := '.';
Modified:= False;
TmpDecimalSeparator:= SysUtils.DecimalSeparator;
TmpThousandSeparator:= SysUtils.ThousandSeparator;
if Not( (FThousandSeparator = SysUtils.ThousandSeparator) And
(FDecimalSeparator = SysUtils.DecimalSeparator ) ) then
begin
SysUtils.DecimalSeparator:= FDecimalSeparator;
SysUtils.ThousandSeparator:= FThousandSeparator;
Modified:= True;
end;
Result:= FormatFloat( TmpFmt, Value );
if Modified then
begin
SysUtils.DecimalSeparator:= TmpDecimalSeparator;
SysUtils.ThousandSeparator:= TmpThousandSeparator
end;
end;
Function TEzNumEd.AsString: String;
Begin
If Focused Then
Begin
{ EditFormat }
Result:= MyFormatFloat( CreateMask( FEditFormat ), FNumericValue );
End
Else
begin
{ DisplayFormat }
if ( FNumericValue = 0 ) And ( Length( FDisplayFormat.ZeroValue ) > 0 ) then
begin
Result:= FDisplayFormat.ZeroValue;
Exit;
end;
Result:= MyFormatFloat( CreateMask( FDisplayFormat ), FNumericValue );
end;
End;
Procedure TEzNumEd.SetCursor( Position: integer );
Var
tw, X, Y: integer;
S: String;
R: TRect;
AW, AH: integer;
rw: Integer;
Begin
R := ClientRect;
InflateRect( R, -1, -1 );
S := AsString;
If ( Position < 1 ) Or ( Position > Length( S ) ) Then Exit;
AW := 0;
If FCaretBm <> Nil Then
AW := FCaretBm.Width;
if Focused then
begin
if Length( FEditFormat.RightInfo ) > 0 then
rw := Canvas.TextWidth( FEditFormat.RightInfo )
else
rw := 0;
end else
begin
if Length( FDisplayFormat.RightInfo ) > 0 then
rw := Canvas.TextWidth( FDisplayFormat.RightInfo )
else
rw := 0;
end;
X := R.Right - Canvas.TextWidth( s ) - ( AW Div 2 ) - rw;
tw := 0;
If Position <= Length( s ) Then
tw := Canvas.TextWidth( s[Position] ) Div 2;
If Position > 1 Then
Inc( X, Canvas.TextWidth( Copy( s, 1, Position - 1 ) ) + tw )
Else
Inc( X, tw );
AH := 0;
If FCaretBm <> Nil Then
AH := FCaretBm.Height;
Y := Height - AH - 1;
//[KT] - added 12/06/01, adjust caret for padding
SetCaretPos( X + WidthPad, Y );
FSelStart := Position;
End;
Procedure TEzNumEd.SetCursorPos;
Var
P, P1: integer;
s: String;
Valid: boolean;
Begin
Repeat
Valid := true;
HideCaret( Handle );
ShowCaret( Handle );
s := AsString;
If FPartEditing = peInteger Then
Begin
{ Editing integer part, place the cursor }
P := AnsiPos( FDecimalSeparator, s );
If P = 0 Then
SetCursor( Length( s ) )
Else
SetCursor( P - 1 );
End
Else
Begin
P := AnsiPos( FDecimalSeparator, s );
If P = 0 Then
Begin
FPartEditing := peInteger;
Exit;
End;
P1 := P;
If P1 < FSelStart Then
P1 := FSelStart;
If FLastKey <> #8 Then
Inc( P1 )
Else
Dec( P1 );
If P1 > Length( s ) Then
P1 := Length( s );
If P1 < P + 1 Then
Begin
If FLastKey = #8 Then
Begin
FPartEditing := peInteger;
Valid := false;
Continue;
End;
P1 := P + 1;
End;
SetCursor( P1 );
FSelStart := P1;
End;
Until Valid;
End;
Procedure TEzNumEd.WMGetDlgCode( Var Message: TWMGetDlgCode );
Begin
Inherited;
Message.Result := Message.Result Or DLGC_WANTALLKEYS Or DLGC_WANTARROWS;
End;
function TEzNumEd.IsDesigning: Boolean;
{$IFDEF ISACTIVEX}
Function IsControlInDesignMode: Boolean;
Begin
Try
Result := Not ( ( FAXCtrl.ClientSite As IAmbientDispatch ).UserMode );
Except
Result := False;
End;
End;
{$ENDIF}
Begin
{$IFDEF ISACTIVEX}
If ( FAXCtrl <> Nil ) Then
Result := IsControlInDesignMode
Else
{$ENDIF}
Result:= csDesigning in ComponentState;
end;
function TEzNumEd.ReplaceCodes( const Info: string): string;
begin
Result:= StringReplace( Info, '\c', CurrencyString, [rfReplaceAll, rfIgnoreCase])
end;
Procedure TEzNumEd.KeyDown( Var Key: Word; Shift: TShiftState );
Var
Value: Extended;
Begin
If Key = VK_INSERT Then
Begin
If Shift = [] Then
FModified := true;
If ( Shift = [] ) And FSelected Then
Begin
FSelected := false;
Invalidate;
End;
{copy}
If ssCtrl In Shift Then
Begin
Clipboard.AsText := FloatToStr( FNumericValue );
Key := 0;
End;
{paste}
If ( FReadOnly = false ) And ( ssShift In Shift ) Then
Begin
Try
Value := MyStrToFloat( Clipboard.AsText );
If Not FAcceptNegatives Then
Value:= Abs(Value);
FNumericValue := Value;
FModified := true;
Change;
FSelected := false;
Invalidate;
Except
MessageBeep( 0 );
End;
Key := 0;
End;
End;
If ( Shift = [] ) And ( Key = VK_DELETE ) And Not ( FReadOnly ) Then
Begin
FNumericValue := 0;
FModified := true;
Change;
Invalidate;
End;
If ( Shift = [] ) And ( Key = VK_UP ) Then
PostMessage( GetParentForm( Self ).Handle, WM_NEXTDLGCTL, 1, 0 );
If ( Shift = [] ) And ( Key = VK_DOWN ) Then
PostMessage( GetParentForm( Self ).Handle, WM_NEXTDLGCTL, 0, 0 );
Inherited KeyDown( Key, Shift );
End;
Procedure TEzNumEd.WMLButtonDown( Var Message: TWMLButtonDown );
Begin
If TabStop Then
Windows.SetFocus( Handle );
Inherited;
End;
Function TEzNumEd.GetUnformattedText: string;
var
temp: boolean;
begin
temp:= FUseThousandSeparator;
FUseThousandSeparator:= false;
Result:= AsString;
FUseThousandSeparator:= temp;
Result:= StringReplace(Result, #32,'',[rfReplaceAll]);
end;
Procedure TEzNumEd.KeyPress( Var Key: Char );
Var
P, P1, P2: integer;
s, sMask: String;
Changed: boolean;
Value: extended;
OneIntegerDigit: boolean;
Begin
FSelected := false;
If ( Key = #13 ) And ( FTabOnEnterKey ) Then
Begin
GetParentForm( Self ).Perform( WM_NEXTDLGCTL, 0, 0 );
Key := #0;
Exit;
End;
If ( FReadOnly = false ) And ( Key = #27 ) Then
Begin
If ( FNumericValue = FOriginalValue ) And Not FModified Then
Begin
Invalidate;
Inherited KeyPress( Key );
Exit;
End;
FNumericValue := FOriginalValue;
FModified := false;
Invalidate;
Key := #0;
Exit;
End;
If Key = ^C Then
Begin {copy}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -