WEB开发网      濠电姷鏁告繛鈧繛浣冲洤纾瑰┑鐘宠壘閻ょ偓銇勯幇鍫曟闁稿鍠愰妵鍕冀閵娧佲偓鎺楁⒒閸曨偄顏柡宀嬬畱铻e〒姘煎灡绗戦梻浣筋嚙濮橈箓顢氳濠€浣糕攽閻樿宸ュΔ鐘叉啞缁傚秹宕滆绾惧ジ寮堕崼娑樺缂佹宀搁弻鐔风暋閻楀牆娈楅梺璇″枓閺呯姴鐣疯ぐ鎺濇晝闁靛牆妫欓蹇旂節閻㈤潧浠﹂柛銊ョ埣楠炴劙骞橀鑲╋紱闂佽宕樼粔顔裤亹閹烘挸浜归梺缁樺灦閿曗晛螞閸曨垱鈷戦柟鑲╁仜婵″ジ鎮楀☉鎺撴珖缂侇喖顑呴鍏煎緞濡粯娅囬梻浣瑰缁诲倿寮绘繝鍥ㄦ櫇闁稿本绋撻崢鐢告煟鎼淬垻鈯曢柨姘舵煟韫囥儳绋荤紒缁樼箖缁绘繈宕橀妸褌绱濋梻浣筋嚃閸ㄤ即宕弶鎴犳殾闁绘梻鈷堥弫鍌炴煕閳锯偓閺呮瑧妲愬Ο琛℃斀闁绘劕妯婇崵鐔封攽椤旇棄鍔ら摶鐐烘煕閺囥劌澧柛娆忕箻閺屽秹宕崟顒€娅g紓浣插亾濠㈣泛顑囩粻楣冩煙鐎涙ḿ绠橀柨娑樼У椤ㄣ儵鎮欓鍕紙闂佽鍠栫紞濠傜暦閹偊妲诲┑鈩冨絻椤兘寮诲☉銏犖╅柕澶堝労閸斿绱撴担绋库偓鍝ョ矓瑜版帒鏋侀柟鍓х帛閺呮悂鏌ㄩ悤鍌涘 ---闂傚倸鍊烽悞锔锯偓绗涘厾娲煛閸涱厾顔嗛梺璺ㄥ櫐閹凤拷
开发学院软件开发Delphi 可以左右居中对齐并可设置DisplayFormat的Edit控件... 阅读

可以左右居中对齐并可设置DisplayFormat的Edit控件

 2006-02-04 13:32:22 来源:WEB开发网 闂傚倸鍊风欢姘缚瑜嶈灋闁圭虎鍠栫粻顖炴煥閻曞倹瀚�闂傚倸鍊风粈渚€骞夐敓鐘插瀭闁汇垹鐏氬畷鏌ユ煙閹殿喖顣奸柛搴$У閵囧嫰骞掗幋婵冨亾閻㈢ǹ纾婚柟鐐灱濡插牊绻涢崱妤冃℃繛宀婁簽缁辨捇宕掑鎵佹瀸闂佺懓鍤栭幏锟�濠电姷鏁告慨顓㈠箯閸愵喖宸濇い鎾寸箘閹规洟姊绘笟鈧ḿ褍煤閵堝悿娲Ω閳轰胶鍔﹀銈嗗笂閼冲爼鍩婇弴銏$厪闁搞儮鏅涙禒褏绱掓潏鈺佷槐闁轰焦鎹囬弫鎾绘晸閿燂拷闂傚倸鍊风欢姘缚瑜嶈灋闁圭虎鍠栫粻顖炴煥閻曞倹瀚�  闂傚倸鍊烽懗鑸电仚缂備胶绮〃鍛村煝瀹ュ鍗抽柕蹇曞У閻庮剟姊虹紒妯哄闁圭⒈鍋嗛惀顏囶樄闁哄本娲樼换婵婄疀閺囩姷鐛ラ梻浣哄帶婢瑰﹥绂嶅⿰鍫氣偓鏃堝礃椤忎礁浜鹃柨婵嗛婢ь喖霉閻樻瑥瀚粻楣冩煕椤愩倕鏋庨柣蹇嬪劜閵囧嫰寮村Ο鍝勫Е濡炪們鍨洪悷鈺呭箖閳╁啯鍎熼柕鍥у簻閹凤拷
核心提示:欢迎测试!liang_z@163.netunit OWEdit;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TInputDataType = (tFloa

欢迎测试!

liang_z@163.net

unit OWEdit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TInputDataType = (tFloat,tInteger,tAll);

type
  TOWEdit = class(TEdit)
  PRivate
   { Private declarations }
   FCanvas : TCanvas;
   FDataType: TInputDataType;
   FAlignment : TAlignment;
   FDisplayFormat : String;
   FDeciNum : Word;
   FDisplayText : String;
   procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  protected
   { Protected declarations }
   procedure SetDataType(Value:TInputDataType);
   procedure SetAlignment(Value:TAlignment);
   procedure SetDisplayFormat(Value:String);
   procedure ClipPaste(var M:TMessage); Message WM_PASTE;
   procedure PaintWindow(DC: HDC); override;
   procedure Paint; virtual;
   procedure WMExit(var Message:TWMKillFocus);Message WM_KILLFOCUS;
   procedure GetDisplayText;
   procedure ShowDisplayText;
   function  GetDeciLast:integer;
  public
   { Public declarations }
   OldText : String;
   property Text;
   property Canvas: TCanvas read FCanvas;
   constructor Create(AOwner: TComponent); override;
   destructor Destroy(); override;
   procedure KeyPress(var Key: Char); override;
   procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  published
   { Published declarations }
   property DataType: TInputDataType read fDataType write SetDataType default tFloat;
   property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
   property DisplayFormat: string read FDisplayFormat write SetDisplayFormat;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Ourway', [TOWEdit]);
end;

constructor TOWEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Text := '0';
  FCanvas := TControlCanvas.Create;
  TControlCanvas(FCanvas).Control := Self;
  FDeciNum := 9999;
end;

destructor TOWEdit.Destroy();
begin
  FCanvas.Free;
  inherited Destroy();
end;

procedure TOWEdit.SetDataType(Value:TInputDataType);
begin
  If Value<>fDataType Then
  begin
   fDataType := Value;
   Case Value of
    tAll: Text := ';
    tFloat: Text:='0.0';
    tInteger: Text:='0';
   end;
   ShowDisplayText;
   Invalidate;
  end;
end;

procedure TOWEdit.SetAlignment(Value:TAlignment);
begin
  If Value<>FAlignment Then
  begin
   FAlignment := Value;
   Invalidate;
  end;
end;

procedure TOWEdit.SetDisplayFormat(Value: string);
begin
  If Value<>FDisplayFormat Then
  begin
   FDisplayFormat := Value;
   if Trim(Value)<>' then
    FDeciNum := Length(Value)-Pos('.',Value)+1
   else
    FDeciNum := 9999;
   ShowDisplayText;
   Invalidate;
  end;
end;

procedure TOWEdit.KeyDown(var Key: Word; Shift: TShiftState);
begin
  if Key = VK_DELETE then
   if Self.SelStart=pos('.',Self.Text)-1 then
    Key := 0;
  inherited KeyDown(Key,Shift);
end;
procedure TOWEdit.KeyPress(var Key: Char);
var
  kv: Integer;
begin
  kv := Ord(Key);
  case fDataType of
   tInteger:
    if (((kv>58) or (kv<48)) and (kv<>3) and (kv<>22) and (kv<>8) and (kv<>13)) then
     Key := chr(0);
   tFloat:
    begin
     if (((kv>58) or (kv<48)) and (kv<>3) and (kv<>22) and (kv<>46) and (kv<>8) and (kv<>13)) then
      Key := chr(0)
     else
     begin
      if (kv=46) and (Pos('.',self.Text)>0) then//已有小数点
       Key := chr(0)
      else
       if MaxLength<1 then//小数点前面位数不定
       begin
        if ((GetDeciLast>=FDeciNum) and (kv<>8)) then //退格键
         if ((self.SelLength=0)and(pos('.',copy(Self.Text,1,self.SelStart))>0))then
          Key := chr(0);
       end
       else//输入总长度已定
       begin
        if pos('.',copy(self.Text,1,self.selStart))<1 then
        begin//光标在小数点之前
         if ((self.SelStart>=MaxLength-FDeciNum)and(kv<>8)and(kv<>46)) then
           Key := chr(0);
        end
        else
        begin//光标在小数点之后
         if ((GetDeciLast>=FDeciNum) and (kv<>8) and (self.SelLength=0)and(pos('.',copy(Self.Text,1,self.SelStart))>0)) then
           Key := chr(0);
        end;
       end;
     end;
    end;
   else
  end;
  if (kv=8)and(Self.SelStart>0)and(Self.Text[self.SelStart]='.')and(GetDeciLast>1) then
   Key := chr(0);
  //还有一个Delete键没有截获!如果用此键删除小数点,还是有可能出错的。
  //搞定!用KeyDown override
  inherited KeyPress(Key);
end;

procedure TOWEdit.ClipPaste(var M:TMessage);
begin
  if fDataType=tAll then
   inherited;
end;

procedure TOWEdit.WMPaint(var Message: TWMPaint);
begin
  inherited;
  PaintWindow(Message.DC);
end;

procedure TOWEdit.PaintWindow(DC: HDC);
begin
  FCanvas.Lock;
  try
   FCanvas.Handle := DC;
   try
    TControlCanvas(FCanvas).UpdateTextFlags;
    Paint;
   finally
    FCanvas.Handle := 0;
   end;
  finally
   FCanvas.Unlock;
  end;
end;

procedure TOWEdit.Paint;
begin
  if not Focused then
  begin
   ShowDisplayText;
  end
  else
   inherited;
end;

procedure TOWEdit.WMExit(var Message:TWMKillFocus);
begin
  inherited;
  ShowDisplayText;
end;

procedure TOWEdit.GetDisplayText;
var
  ShowText : String;
begin
  ShowText := Text;
  if FDataType<>tAll then
  begin
   if Trim(ShowText)=' then
    ShowText := '0';
   if FDatatype=tFloat then
    ShowText := FormatFloat(FDisplayFormat,StrToFloat(ShowText))
   else
    ShowText := FormatFloat(FDisplayFormat,StrToInt(ShowText));
  end;
  FDisplayText := ShowText;
end;

procedure TOWEdit.ShowDisplayText;
var
  Rect : TRect;
  x,y : Integer;
begin
  GetDisplayText;
  Canvas.Lock;
  try
   Rect.Left := 1;
   Rect.Top := 1;
   Rect.Right := Width-1;
   Rect.Bottom:= Height-1;
   Canvas.Font := Font;
   if not Enabled then
    Canvas.Font.Color := clGrayText;
   Canvas.Brush.Color:=Self.Color;
   Canvas.FillRect(Rect);
   y := 2; x := 2;
   Case FAlignment of
    taLeftJustify:;
    taRightJustify:
     x := Width-Canvas.TextWidth(FDisplayText)-5;
   else
    x := (Width-Canvas.TextWidth(FDisplayText)-5)div 2;
   end;
   Canvas.TextOut(x,y,FDisplayText);
  finally
   Canvas.Unlock;
  end;
end;

function TOWEdit.GetDeciLast:integer;
var
  i : Integer;
begin
  Result := 0;
  if Pos('.',Text)>0 then
  begin
   for i:=1 to Length(Text) do
    if Text[i]='.' then
    begin
     Result := Length(Text)-i+1;//Length(Copy(Text,i,Length(Text)-i));
     Exit;
    end;
  end;
end;

end.

Tags:可以 左右 居中

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接