可以左右居中對齊並可設定DisplayFormat的Edit控制元件 (轉)
歡迎測試!
to:liang_z@163">liang_z@163.net
unit OWEdit;
interface
uses
, 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 : ;
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); overr;
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.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-990038/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- exttoolbar裡的button預設都是靠左對齊的,請問能設定靠右或者居中對齊嗎?
- css居中對齊大全CSS
- 常見的div居中對齊方式
- CSS居中對齊終極指南CSS
- CSS文字水平居中對齊CSS
- 線上直播原始碼,自定義導航欄並固定居中對齊原始碼
- 文字如何做到垂直居中對齊
- 視窗和對話方塊居中對齊
- Android ImageView對齊方式設定AndroidView
- 如何讓文字垂直居中對齊文字框
- BCB:TEdit控制元件右對齊的簡單實現 (轉)控制元件
- ·UILable屬性詳解,設定居上對齊,居中對齊,居下對齊,獲取斜體字,字型、大小、單位、顏色UI
- Word 2007表格中文字設定左右和上下居中(水平和垂直居中)教程
- table上下對齊滾動條設定
- CSS Tips——未知寬度高度居中對齊CSS
- Qt中Widget如何保證居中對齊QT
- SwiftUI 佈局之元件對齊實現上下對齊和水平居中 (教程含原始碼)SwiftUI元件原始碼
- 如何讓圖片在div中垂直水平居中對齊
- leetcode 68. 文字左右對齊 模擬LeetCode
- css 元素左右居中總結CSS
- QToolBar上的控制元件靠右側對齊的方法QT控制元件
- checkbox和後面文字無法居中對齊的解決方案
- 實現文字標題和input文字框垂直居中對齊
- word怎麼設定表格數字水平居中 word設定表格數字居中的方法
- 設定oracle sqlplus中的Edit編輯模式OracleSQL模式
- 設定圖片和文字的垂直居中
- idea設定 執行 按鈕在右上角,設定toolbar兩端對齊Idea
- css垂直居中怎麼設定?文字上下居中和圖片垂直居中CSS
- 設定圖片水平垂直居中
- <摘錄>位元組對齊(強制對齊以及自然對齊)
- Qt 設定視窗居中顯示QT
- CSS 技巧篇(七):設定元素居中CSS
- 遊戲設計製作中對鎖定設定的應用(轉)遊戲設計
- 封裝自定義圓角方向並且可設定投影的View封裝View
- 具有edit功能的combobox (轉)
- css-flex:在不確定div高的情況下讓圖片文字上下左右居中CSSFlex
- App跳轉到許可權設定介面APP
- input文字框和img圖片能夠垂直居中對齊程式碼例項