转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
可以左右居中对齐并可设置DisplayFormat的Edit控件         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1728 更新时间:2009/4/23 18:43:16

 

 

欢迎测试!

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.Bott

[1] [2]  下一页


[Delphi程序]Sender 的應用:所有Edit共用一個過濾格式  [Delphi程序]偶写的第一个控件,一个用选择代替输入的Edit控件
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台