分享
 
 
 

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

王朝delphi·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

欢迎测试!

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有