Delphi控件制作技巧[一]

2016-02-19 18:50 36 1 收藏

下面图老师小编跟大家分享一个简单易学的Delphi控件制作技巧[一]教程,get新技能是需要行动的,喜欢的朋友赶紧收藏起来学习下吧!

【 tulaoshi.com - 编程语言 】

 

  unit USWLMSelectDa;

  {$S-,W-,R-}                                            
  {$C PRELOAD}

  interface

  uses
    Windows,Messages,SysUtils, Types, Classes, Graphics, Controls,StdCtrls,Forms,
    StrUtils,Math,ADODB,TFlatButtonUnit,USWLMStyleEdit;

  type
    TEditDataType = (sdString, sdInteger,sdFloat,sdMoney);
    TVAlignment = (tvaTopJustify, tvaCenter, tvaBottomJustify);
    TDataStyle = (dsBm, dsZj, dsMc);
  type
    TSelectDa = class(TCustomControl)
    private
      FPen: TPen;
      FBrush:TBrush;
      FFont:TFont;
      FCaption:string;
      FBmText:string;
      FZjText:string;
      FMcText:string;
      FDataType: TEditDataType;
      FPrecision: Integer;
      FReadOnly:Boolean;
      FEditFont:TFont;
      FHAlignment : TAlignment;
      FVAlignment : TVAlignment;
      FEdit:TStyleEdit;
      FButton:TFlatButton;
      FTitleName:string;
      FTableName:string;
      FDataStyle:TDataStyle;
      FBmField:string;
      FZjField:string;
      FMcField:string;
      FOnClick: TNotifyEvent;
      FOnEnter: TNotifyEvent;
      FOnExit: TNotifyEvent;
      FOnKeyPress: TKeyPressEvent;
      procedure SetPen(const Value:TPen);
      procedure SetBrush(const Value:TBrush);
      procedure SetFont(const Value:TFont);
      procedure SetCaption(const Value:string);
      procedure SetBmText(const Value:string);
      procedure SetZjText(const Value:string);
      procedure SetMcText(const Value:string);
      procedure SetDataType(const Value: TEditDataType);
      procedure SetPrecision(const Value: Integer);
      procedure SetReadOnly(const Value:Boolean);
      procedure SetEditFont(const Value:TFont);
      procedure SetHAlignment(const Value:TAlignment);
      procedure SetVAlignment(const Value:TVAlignment);
      procedure SetTitleName(const Value:string);
      procedure SetTableName(const Value:string);
      procedure SetDataStyle(const Value:TDataStyle);
      procedure SetBmField(const Value:string);
      procedure SetZjField(const Value:string);
      procedure SetMcField(const Value:string);
      function  GetAsFloat(): string;
      function  GetAsMoney(): string;
      function  GetAsInteger(): string;
      function  GetAsText(): string;
      procedure SetAsFloat(const Value: string);
      procedure SetAsMoney(const Value: string);
      procedure SetAsInteger(const Value: string);
      procedure SetAsText(const Value: string);
      procedure StyleChanged(Sender: TObject);
      procedure SetBackColor(const Value : TColor);
      procedure SetColorOnEnter(const Value : TColor);
      procedure DoClick(Sender: TObject);
      procedure DoEnter(Sender: TObject);
      procedure DoExit(Sender: TObject);
      procedure DoKeyPress(Sender: TObject; var Key: Char);
    protected
      procedure Paint; override;
    public
      constructor Create(AOwner: TComponent); override;
      destructor  Destroy; override;
    published
      property Pen: TPen read FPen write SetPen;
      property Brush: TBrush read FBrush write SetBrush;
      property Font: TFont read FFont write SetFont;
      property Caption:string read FCaption write SetCaption;
      property Bm:string read FBmText write SetBmText ;
      property Zjf:string read FZjText write SetZjText ;
      property Mc:string read FMcText write SetMcText ;
      property Text:string read FMcText write SetMcText;
      property DataType: TEditDataType read FDataType write SetDataType default SdString;
      property Precision: Integer read Fprecision write SetPrecision default 2;
      property ReadOnly: Boolean read FReadOnly write SetReadOnly default False;
      property EditFont: TFont read FEditFont write SetEditFont;
      property HAlignment:TAlignment read FHAlignment write SetHAlignment default taLeftJustify;
      property VAlignment:TVAlignment read FVAlignment write SetVAlignment default tvaBottomJustify;
      property TitleName:string read FTitleName write SetTitleName ;
      property TableName:string read FTableName write SetTableName ;
      property DataStyle:TDataStyle read FDataStyle write SetDataStyle default dsBm;
      property BmField:string read FBmField write SetBmField ;
      property ZjField:string read FZjField write SetZjField ;
      property McField:string read FMcField write SetMcField ;
      property AsFloat:string read GetAsFloat {write SetAsFloat};
      property AsMoney:string read GetAsMoney {write SetAsMoney};
      property AsInt: string read GetAsInteger {write SetAsInteger};
      property AsStr: string read GetAsText write SetAsText;
      property OnClick: TNotifyEvent read FOnClick write FOnClick;
      property OnKeyPress: TKeyPressEvent read FOnKeyPress write FOnKeyPress;
      property OnEnter: TNotifyEvent read FOnEnter write FOnEnter;
      property OnExit: TNotifyEvent read FOnExit write FOnExit;
      property BackColor : TColor write SetBackColor;
      property ColorOnEnter : TColor write SetColorOnEnter;
      property AlignDisabled;
      property VisibleDockClientCount;
      property ControlCount;
      property ParentWindow;
      property Showing;
      property TabOrder;
      property TabStop;
    end;

  procedure Register;

  implementation

  uses Consts;

  procedure TSelectDa.SetPen(const Value: TPen);
  begin
    FPen.Assign(Value);
    Invalidate;
  end;

  procedure TSelectDa.SetBrush(const Value:TBrush);
  begin
    FBrush.Assign(Value);
    Invalidate;
  end;

  procedure TSelectDa.SetFont(const Value:TFont);
  begin
    FFont.Assign(Value);
    Invalidate;
  end;

  procedure TSelectDa.SetCaption(const Value:string);
  begin
    if FCaption Value then
    begin
      FCaption:=Value;
      Invalidate;
    end;
  end;

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  procedure TSelectDa.SetBmText(const Value:string);
  begin
    if FBmText Value then
    begin
      FBmText:=Value;
      Invalidate;
    end;
  end;

  procedure TSelectDa.SetZjText(const Value:string);
  begin
    if FZjText Value then
    begin
      FZjText:=Value;
    end;
  end;

  procedure TSelectDa.SetMcText(const Value:string);
  begin
    if FMcText Value then
    begin
      FMcText:=Value;
      Invalidate;
    end;
  end;

  procedure TSelectDa.SetReadOnly(const Value:Boolean);
  begin
    if FReadOnlyValue then
    begin
      FReadOnly:=Value;
      Invalidate;
    end;
  end;

  procedure TSelectDa.SetEditFont(const Value:TFont);
  begin
    FEditFont.Assign(Value);
    Invalidate;
  end;

  procedure TSelectDa.SetPrecision(const Value: Integer);
  begin
    if FprecisionValue then
    begin
      case Value of
      1..6:FPrecision:=Value;
      else FPrecision:=2;
      end;
      Invalidate;
    end;
  end;

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  procedure TSelectDa.SetDataType(const Value: TEditDataType);
  begin
    if FDataType Value then
    begin
      FDataType:=Value;
      case FDataType of
        SdString:FEdit.InputStyle:=IsString;
        SdInteger:FEdit.InputStyle:=IsInteger;
        SdFloat:FEdit.InputStyle:=IsFloat;
        SdMoney:FEdit.InputStyle:=IsMoney;
        else FEdit.InputStyle:=IsString;
      end;
      Invalidate;
    end;
  end;

  procedure TSelectDa.SetHAlignment(const Value:TAlignment);
  begin
    if FHAlignment Value then
    begin
        FHAlignment:=Value;
        Invalidate;
    end;
  end;

  procedure TSelectDa.SetVAlignment(const Value:TVAlignment);
  begin
    if FVAlignment Value then
    begin
        FVAlignment:=Value;
        Invalidate;
    end;
  end;

  procedure TSelectDa.SetTitleName(const Value:string);
  begin
    if FTitleNameValue then FTitleName:=Value;
  end;

  procedure TSelectDa.SetTableName(const Value:string);
  begin
    if FTableNameValue then
    begin
      FTableName:=Value;
      Invalidate;
    end;
  end;

  procedure TSelectDa.SetDataStyle(const Value:TDataStyle);
  begin
    if FDataStyleValue then FDataStyle:=Value;
  end;

  procedure TSelectDa.SetBmField(const Value:string);
  begin
    if FBmFieldValue then
    begin
        FBmField:=Value;
        Invalidate;
    end;
  end;

  procedure TSelectDa.SetZjField(const Value:string);
  begin
    if FZjFieldValue then  FZjField:=Value;
  end;

  procedure TSelectDa.SetMcField(const Value:string);
  begin
    if FMcFieldValue then
    begin
        FMcField:=Value;
        Invalidate;
    end;
  end;

  function  TSelectDa.GetAsFloat: string;
    function StrToDouble(S:string):Double;
    begin
      if not trystrToFloat(s,Result) then Result:=0;
    end;
  begin
    case FPrecision of
    1..6:  Result:=FormatFloat('###0.'+DupeString('0',FPrecision),StrToDouble(FMcText));
    else  Result:=FormatFloat('###0.00',StrToDouble(FMcText));
    end;
  end;

  function  TSelectDa.GetAsMoney: string;
    function StrToDouble(S:string):Double;
    begin
      if not trystrToFloat(s,Result) then Result:=0;
    end;
  begin
    Result:=FormatFloat('###0.00',StrToDouble(FMcText));
  end;

  function  TSelectDa.GetAsInteger: string;
    Function StrToInteger(S:string):integer;
    begin
      if not trystrToInt(s,Result) then Result:=0;
    end;
  begin
    Result:=IntToStr(StrToInteger(FMcText));
  end;

  function  TSelectDa.GetAsText: string;
  begin
    Result:=FMcText;
  end;

  procedure TSelectDa.SetAsFloat(const Value: string);
    function StrToDouble(S:string):Double;
    begin
      if not trystrToFloat(s,Result) then Result:=0;
    end;
  var
    f:Double;
  begin
    f:=StrToDouble(Value);
    case FPrecision of
    1..6:
    begin
      f:=RoundTo(f,-FPrecision);
      SetMcText(FormatFloat('###0.'+DupeString('0',FPrecision),f));
    end
    else
    begin
      f:=RoundTo(f,-2);
      SetMcText(FormatFloat('###0.00',f));
    end;
    end;
  end;

  procedure TSelectDa.SetAsMoney(const Value: string);
    function StrToDouble(S:string):Double;
    begin
      if not trystrToFloat(s,Result) then Result:=0;
    end;
  var
    f:Double;
  begin
    f:=StrToDouble(Value);
    f:=RoundTo(f,-2);
    SetMcText(FormatFloat('###0.00',f));
  end;

  procedure TSelectDa.SetAsInteger(const Value: string);
    Function StrToInteger(S:string):integer;
    begin
      if not trystrToInt(s,Result) then Result:=0;
    end;
  var
    i:Integer;
  begin
    i:=StrToInteger(Value);
    SetMcText(IntToStr(i));
  end;

  procedure TSelectDa.SetAsText(const Value: string);
  begin
    SetMcText(Value);
  end;

  procedure TSelectDa.StyleChanged(Sender: TObject);
  begin
    Invalidate;
  end;

  procedure TSelectDa.SetBackColor(const Value : TColor);
  begin
    FEdit.BackColor:=Value;
  end;

  procedure TSelectDa.SetColorOnEnter(const Value : TColor);
  begin
    FEdit.ColorOnEnter:=Value;
  end;

  constructor TSelectDa.Create(AOwner: TComponent);
  begin
    inherited Create(AOwner);
    Width:=188;
    Height:=20;
    FCaption:='未命名';
    FBmText:='';
    FZjText:='';
    FMcText:='';
    FReadOnly:=False;
    FHAlignment:=taLeftJustify;
    FVAlignment:=tvaBottomJustify;
    FDataType:=SdString;
    FPrecision:=2;
    FTitleName:='';
    FTableName:='';
    FDataStyle:=dsBm;
    FBmField:='';
    FZjField:='';
    FMcField:='';
    FPen := TPen.Create;
    FPen.OnChange:=StyleChanged;
    FBrush := TBrush.Create;
    FBrush.OnChange:=StyleChanged;
    FFont := TFont.Create;
    FFont.OnChange:=StyleChanged;
    FFont.Charset:=GB2312_CHARSET;
    FFont.Name:='宋体';
    FFont.Size:=9;
    FEditFont := TFont.Create;
    FEditFont.OnChange:=StyleChanged;
    FEditFont.Charset:=GB2312_CHARSET;
    FEditFont.Name:='宋体';
    FEditFont.Size:=9;
    FEdit:=TStyleEdit.Create(Self);
    FEdit.Parent:=Self;
    FEdit.BorderStyle:=bsNone;
    FEdit.InputStyle:=isString;
    FEdit.OnKeyPress:=DoKeyPress;
    FEdit.OnEnter:=DoEnter;
    FEdit.OnExit:=DoExit;
    FButton:=TFlatButton.Create(Self);
    FButton.Parent:=Self;
    FButton.Font:=FFont;
    FButton.ColorBorder:=FBrush.Color;
    FButton.Color:=FBrush.Color;
    FButton.ColorDown:=FBrush.Color;
    FButton.ColorShadow:=FBrush.Color;
    FButton.ColorFocused:=FBrush.Color;
    FButton.Width:=19;
    FButton.Caption:='…';
    FButton.OnClick:=DoClick;
  end;

  procedure TSelectDa.Paint;
  var
    aText:Pchar;
    aRect:TRect;
    Flag:DWORD;
  begin
    with Canvas do
    begin
      Font:=FFont;
      Pen:=FPen;
      Brush:=FBrush;
      FillRect(ClientRect);
      if FBmText'' then aText:=Pchar(FCaption+'['+FBmText+']') else aText:=Pchar(FCaption);
      aRect:=Rect(ClientRect.Left+FPen.Width, ClientRect.Top+FPen.Width, ClientRect.Right-FPen.Width, ClientRect.Bottom-FPen.Width);
      DrawText(Handle, aText, StrLen(aText), aRect, (DT_SINGLELINE or DT_VCENTER) or DT_LEFT);
      Inc(aRect.Left,TextWidth(aText));
      Dec(aRect.Right,FButton.Width);
      MoveTo(aRect.Left,aRect.Bottom);
      LineTo(aRect.Right,aRect.Bottom);
      Inc(aRect.Left,FPen.Width);
      if FReadOnly then
      begin
        FEdit.Visible:=False;
        FButton.Visible:=False;
        Flag:=DT_SINGLELINE;
        case FHAlignment of
          taLeftJustify:Flag:=Flag or DT_LEFT;
          taRightJustify:Flag:=Flag or DT_RIGHT;
          taCenter:Flag:=Flag or DT_CENTER;
          else Flag:=Flag or DT_LEFT;
        end;
        case FVAlignment of
          tvaTopJustify:Flag:=Flag or DT_TOP;
          tvaCenter:Flag:=Flag or DT_VCENTER;
          tvaBottomJustify:Flag:=Flag or DT_BOTTOM;
          else Flag:=Flag or DT_BOTTOM;
        end;
        Font:=FEditFont;
        case FDataType of
          SdString:DrawText(Handle, PChar(AsStr),  StrLen(PChar(AsStr)), aRect, Flag);
          SdInteger:DrawText(Handle, PChar(AsInt), StrLen(PChar(AsInt)), aRect, Flag);
          SdFloat:DrawText(Handle, PChar(AsFloat), StrLen(PChar(AsFloat)), aRect, Flag);
          SdMoney:DrawText(Handle, PChar(AsMoney), StrLen(PChar(AsMoney)), aRect, Flag);
        end;
      end
      else
      begin
        FEdit.Alignment:=FHAlignment;
        FEdit.Font:=FEditFont;
        FEdit.Text:=FMcText;
        FEdit.Width:=aRect.Right-aRect.Left;
        FEdit.Height:=Min(Max(TextHeight(FMcText),TextHeight(FCaption)),aRect.Bottom-aRect.Top);
        FEdit.Left:=aRect.Left;
        case FVAlignment of
          tvaTopJustify:FEdit.Top:=aRect.Top;
          tvaCenter:FEdit.Top:=aRect.Top+(aRect.Bottom-aRect.Top-FEdit.Height)div 2;
          tvaBottomJustify:FEdit.Top:=aRect.Top+(aRect.Bottom-aRect.Top-FEdit.Height);
          else FEdit.Top:=aRect.Top;
        end;
        FButton.Left:=aRect.Right;
        FButton.Top:=aRect.Top;
        FButton.Height:=aRect.Bottom-aRect.Top;
        if (FDataType=SdString) and (FBmField'') and (FMcField'') and (FTableName'') then FButton.Visible:=True
        else FButton.Visible:=False;
      end;
    end;
  end;

  destructor TSelectDa.Destroy;
  begin
    FPen.Free;
    FBrush.Free;
    FFont.Free;
    FEditFont.Free;
    if Assigned(FEdit) then FreeAndNil(FEdit);
    if Assigned(FButton) then FreeAndNil(FButton);
    inherited Destroy;
  end;

  procedure TSelectDa.DoClick(Sender: TObject);
  begin
    if Assigned(FOnClick) then FOnClick(Self);
  end;

  procedure TSelectDa.DoEnter(Sender: TObject);
  begin
    if Assigned(FOnEnter) then FOnEnter(Self);
  end;

  procedure TSelectDa.DoExit(Sender: TObject);
  begin
    if Assigned(FOnExit) then FOnExit(Self);
  end;

  procedure TSelectDa.DoKeyPress(Sender: TObject; var Key: Char);
  begin
    if Assigned(FOnKeyPress) then FOnKeyPress(Self,Key);
  end;

  procedure Register;
  begin
    RegisterComponents('swlmsoft', [TSelectDa]);
  end;

  end.

来源:http://www.tulaoshi.com/n/20160219/1619163.html

延伸阅读
  unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, ExtCtrls, StdCtrls, jpeg; const LMouseDown=False; type   TForm1 = class(TForm)     Edit1: TEdit;     Panel2: TP...
Delphi的出现将一大批程序员带入了Windows下的可视化编程,为一些C&C++的死党程序员所羡慕、感叹。直到C++Builder的出现这些Programer才用到了梦寐以求的编程工具,也将一些不得已"投敌"到Delphi下的程序员又拉回到C++的怀抱。她正真将Windows下的可视化编程和面向对象编程语言有机的融合在一起。但是由于C++Builder面市时间...
1.如何检测Insert、Capslock、NumLock、ScrollLock状态键的状态 Delphi可以调用Win API的Getkeyboardstate()函数。 常量 按键名称 VK_INSERT znsert键 VK_NUMLOCK Num Lock键 VK_CAPITAL Caps Lock键 VK_SCROLL Scroll Lock键 键盘缓冲区每一位都有一位特定的格式,对于状态键来说,最低位是1时表示状...
1、准备 GPS(Global Positioning System),即全球定位系统,利用24颗GPS卫星的测距和测时功能进行全球定位,在许多系统中,如机场导航系统,出租车辆管理和调度系统、江河流域的灾害信息管理和预测系统中,GPS得到了广泛的应用。本文利用MSCOMM控件实现了GPS数据的采集,可为信息管理和指挥调度等提供定位数据。 本文采用GPS的...
标签: Delphi
  Delphi以其优秀的界面和简单的用法深受广大程序员的喜爱.笔者经过摸索,自做了一个具有动态显示特性的控件。只柙谥鞒绦蛑械饔酶每丶囊桓龇椒纯墒迪侄允尽T诙允镜耐?为了不影响主程序做其他的事情,笔者采用了比较流行的线程技术。  一. 方案  自做一个父类为TEdit的控件,应该有一个Text属性,能自...

经验教程

75

收藏

55
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部