MMEdit.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:11k
- {========================================================================}
- {= (c) 1995-98 SwiftSoft Ronald Dittrich =}
- {========================================================================}
- {= All Rights Reserved =}
- {========================================================================}
- {= D 01099 Dresden = Fax.: +49(0)351-8037944 =}
- {= Loewenstr.7a = info@swiftsoft.de =}
- {========================================================================}
- {= Actual versions on http://www.swiftsoft.de/index.html =}
- {========================================================================}
- {= This code is for reference purposes only and may not be copied or =}
- {= distributed in any format electronic or otherwise except one copy =}
- {= for backup purposes. =}
- {= =}
- {= No Delphi Component Kit or Component individually or in a collection=}
- {= subclassed or otherwise from the code in this unit, or associated =}
- {= .pas, .dfm, .dcu, .asm or .obj files may be sold or distributed =}
- {= without express permission from SwiftSoft. =}
- {= =}
- {= For more licence informations please refer to the associated =}
- {= HelpFile. =}
- {========================================================================}
- {= $Date: 16.11.98 - 12:58:27 $ =}
- {========================================================================}
- unit MMEdit;
- {$I COMPILER.INC}
- interface
- uses
- {$IFDEF WIN32}
- Windows,
- {$ELSE}
- WinTypes,
- WinProcs,
- {$ENDIF}
- SysUtils,
- Messages,
- Classes,
- Controls,
- StdCtrls,
- MMObj,
- MMUtils;
- type
- {-- TMMNumberEdit ---------------------------------------------------------}
- TMMNumberEdit = class(TEdit)
- private
- FMinValue : LongInt;
- FMaxValue : LongInt;
- FAlignment : TAlignment;
- function GetValue: LongInt;
- function CheckValue (NewValue: LongInt): LongInt;
- procedure SetValue (NewValue: LongInt);
- procedure SetAlignment(Value: TAlignment);
- procedure CMEnter(var Message: TCMGotFocus); message CM_ENTER;
- procedure CMExit(var Message: TCMExit); message CM_EXIT;
- procedure WMPaste(var Message: TWMPaste); message WM_PASTE;
- procedure WMCut(var Message: TWMCut); message WM_CUT;
- protected
- function IsValidChar(Key: Char): Boolean; virtual;
- procedure KeyPress(var Key: Char); override;
- procedure CreateParams(var Params: TCreateParams); override;
- public
- constructor Create(AOwner: TComponent); override;
- published
- property MaxValue: LongInt read FMaxValue write FMaxValue default 1000;
- property MinValue: LongInt read FMinValue write FMinValue default 0;
- property Value: LongInt read GetValue write SetValue default 0;
- property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
- end;
- {-- TMMFloatNumberEdit ----------------------------------------------------}
- TMMFloatNumberEdit = class(TEdit)
- private
- FMinValue : Double;
- FMaxValue : Double;
- FAlignment : TAlignment;
- function GetValue: Double;
- function CheckValue(NewValue: Double): Double;
- procedure SetValue (NewValue: Double);
- procedure SetAlignment(Value: TAlignment);
- procedure CMEnter(var Message: TCMGotFocus); message CM_ENTER;
- procedure CMExit(var Message: TCMExit); message CM_EXIT;
- procedure WMPaste(var Message: TWMPaste); message WM_PASTE;
- procedure WMCut(var Message: TWMCut); message WM_CUT;
- protected
- function IsValidChar(Key: Char): Boolean; virtual;
- procedure KeyPress(var Key: Char); override;
- procedure CreateParams(var Params: TCreateParams); override;
- public
- constructor Create(AOwner: TComponent); override;
- published
- property MaxValue: Double read FMaxValue write FMaxValue;
- property MinValue: Double read FMinValue write FMinValue;
- property Value: Double read GetValue write SetValue;
- property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
- end;
- implementation
- {== TMMNumberEdit =============================================================}
- constructor TMMNumberEdit.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- Text := '0';
- FMaxValue := 1000;
- FMinValue := 0;
- FAlignment:= taLeftJustify;
- ControlStyle := ControlStyle - [csSetCaption];
- ErrorCode := ComponentRegistered(InitCode, Self, ClassName);
- if (ErrorCode <> 0) then RegisterFailed(InitCode, Self , ClassName);
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- procedure TMMNumberEdit.KeyPress(var Key: Char);
- begin
- if not IsValidChar(Key) then
- begin
- Key := #0;
- MessageBeep(0)
- end;
- if Key <> #0 then inherited KeyPress(Key);
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- function TMMNumberEdit.IsValidChar(Key: Char): Boolean;
- begin
- Result := (Key in ['+', '-', '0'..'9']) or
- ((Key < #32) and (Key <> Chr(VK_RETURN)));
- if not Enabled and Result and ((Key >= #32) or
- (Key = Char(VK_BACK)) or (Key = Char(VK_DELETE))) then
- Result := False;
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- procedure TMMNumberEdit.CreateParams(var Params: TCreateParams);
- const
- Alignments: array[TAlignment] of integer = (ES_LEFT, ES_RIGHT, ES_CENTER);
- begin
- inherited CreateParams(Params);
- { Params.Style := Params.Style and not WS_BORDER; }
- Params.Style := Params.Style or ES_MULTILINE or WS_CLIPCHILDREN or Alignments[FAlignment];
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- procedure TMMNumberEdit.SetAlignment(Value: TAlignment);
- begin
- if FAlignment <> Value then
- begin
- FAlignment := Value;
- RecreateWnd;
- end;
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- procedure TMMNumberEdit.WMPaste(var Message: TWMPaste);
- begin
- if not Enabled or ReadOnly then Exit;
- inherited;
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- procedure TMMNumberEdit.WMCut(var Message: TWMPaste);
- begin
- if not Enabled or ReadOnly then Exit;
- inherited;
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- procedure TMMnumberEdit.CMExit(var Message: TCMExit);
- begin
- inherited;
- SetValue(Value);
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- function TMMNumberEdit.GetValue: LongInt;
- begin
- try
- Result := StrToInt(Text);
- except
- Result := FMinValue;
- end;
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- procedure TMMNumberEdit.SetValue (NewValue: LongInt);
- begin
- Text := IntToStr(CheckValue(NewValue));
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- function TMMNumberEdit.CheckValue(NewValue: LongInt): LongInt;
- begin
- Result := NewValue;
- if (FMaxValue <> FMinValue) then
- begin
- if NewValue < FMinValue then
- Result := FMinValue
- else if NewValue > FMaxValue then
- Result := FMaxValue;
- end;
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- procedure TMMNumberEdit.CMEnter(var Message: TCMGotFocus);
- begin
- if AutoSelect and not (csLButtonDown in ControlState) then
- SelectAll;
- inherited;
- end;
- {== TMMFloatNumberEdit ========================================================}
- constructor TMMFloatNumberEdit.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- Text := '0.00';
- FMaxValue := 1000;
- FMinValue := 0;
- FAlignment:= taLeftJustify;
- ControlStyle := ControlStyle - [csSetCaption];
- ErrorCode := ComponentRegistered(InitCode, Self, ClassName);
- if (ErrorCode <> 0) then RegisterFailed(InitCode, Self , ClassName);
- end;
- {-- TMMFloatNumberEdit --------------------------------------------------------}
- procedure TMMFloatNumberEdit.KeyPress(var Key: Char);
- begin
- if not IsValidChar(Key) then
- begin
- Key := #0;
- MessageBeep(0)
- end;
- if Key <> #0 then inherited KeyPress(Key);
- end;
- {-- TMMFloatNumberEdit --------------------------------------------------------}
- function TMMFloatNumberEdit.IsValidChar(Key: Char): Boolean;
- begin
- Result := (Key in [DecimalSeparator, '+', '-', '0'..'9']) or
- ((Key < #32) and (Key <> Chr(VK_RETURN)));
- if not Enabled and Result and ((Key >= #32) or
- (Key = Char(VK_BACK)) or (Key = Char(VK_DELETE))) then
- Result := False;
- end;
- {-- TMMFloatNumberEdit --------------------------------------------------------}
- procedure TMMFloatNumberEdit.CreateParams(var Params: TCreateParams);
- const
- Alignments: array[TAlignment] of Longint = (ES_LEFT, ES_RIGHT, ES_CENTER);
- begin
- inherited CreateParams(Params);
- { Params.Style := Params.Style and not WS_BORDER; }
- Params.Style := Params.Style or ES_MULTILINE or WS_CLIPCHILDREN or Alignments[FAlignment];
- end;
- {-- TMMFloatNumberEdit --------------------------------------------------------}
- procedure TMMFloatNumberEdit.SetAlignment(Value: TAlignment);
- begin
- if FAlignment <> Value then
- begin
- FAlignment := Value;
- RecreateWnd;
- end;
- end;
- {-- TMMFloatNumberEdit --------------------------------------------------------}
- procedure TMMFloatNumberEdit.WMPaste(var Message: TWMPaste);
- begin
- if not Enabled or ReadOnly then Exit;
- inherited;
- end;
- {-- TMMFloatNumberEdit --------------------------------------------------------}
- procedure TMMFloatNumberEdit.WMCut(var Message: TWMPaste);
- begin
- if not Enabled or ReadOnly then Exit;
- inherited;
- end;
- {-- TMMFloatNumberEdit --------------------------------------------------------}
- procedure TMMFloatNumberEdit.CMExit(var Message: TCMExit);
- begin
- inherited;
- SetValue(Value);
- end;
- {-- TMMFloatNumberEdit --------------------------------------------------------}
- function TMMFloatNumberEdit.GetValue: Double;
- begin
- try
- Result := StrToFloat(Text);
- except
- Result := FMinValue;
- end;
- end;
- {-- TMMFloatNumberEdit --------------------------------------------------------}
- procedure TMMFloatNumberEdit.SetValue(NewValue: Double);
- begin
- Text := Format('%f',[CheckValue(NewValue)]);
- end;
- {-- TMMNumberEdit -------------------------------------------------------------}
- function TMMFloatNumberEdit.CheckValue(NewValue: Double): Double;
- begin
- Result := NewValue;
- if (FMaxValue <> FMinValue) then
- begin
- if NewValue < FMinValue then
- Result := FMinValue
- else if NewValue > FMaxValue then
- Result := FMaxValue;
- end;
- end;
- {-- TMMFloatNumberEdit --------------------------------------------------------}
- procedure TMMFloatNumberEdit.CMEnter(var Message: TCMGotFocus);
- begin
- if AutoSelect and not (csLButtonDown in ControlState) then
- SelectAll;
- inherited;
- end;
- end.