Delphi控件制作技巧[二]

2016-02-19 19:34 20 1 收藏

在这个颜值当道,屌丝闪边的时代,拼不过颜值拼内涵,只有知识丰富才能提升一个人的内在气质和修养,所谓人丑就要多学习,今天图老师给大家分享Delphi控件制作技巧[二],希望可以对大家能有小小的帮助。

【 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,sdDate);
    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;
      function  GetAsDate(): 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 AsDate: string read GetAsDate ;
      property AsStr: string read GetAsText write SetAsText;
      property OnClick: TNotifyEvent read FOnClick
      property OnKeyPress: TKeyPressEvent read FOnKeyPress
      property OnEnter: TNotifyEvent read FOnEnter
      property OnExit: TNotifyEvent read 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;

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

  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;

  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;

  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;
        SdDate:FEdit.InputStyle:=IsDate;
        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.GetAsDate(): string;
  var
    TempDate:TDateTime;
  begin
    if TryStrToDate(FMcText,TempDate) then Result:=FormatDateTime('YYYY-MM-DD',TempDate)
    else Result:='';
  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;

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

  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);
          SdDate:DrawText(Handle, PChar(AsDate), StrLen(PChar(AsDate)), 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''))
           or (FDataType=SdDate) 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 TSelectDa.SetOnClick(const Value:TNotifyEvent);
  begin
    if @FOnClick@Value then
    begin
      FOnClick:=Value;
      FButton.OnClick:=FOnClick;
    end;
  end;

  procedure TSelectDa.SetOnKeyPress(const Value:TKeyPressEvent);
  begin
    if @FOnKeyPress@Value then
    begin
      FOnKeyPress:=Value;
      FEdit.OnKeyPress:=FOnKeyPress;
    end;
  end;

  procedure TSelectDa.SetOnEnter(const Value:TNotifyEvent);
  begin
    if @FOnEnter@Value then
    begin
      FOnEnter:=Value;
      FEdit.OnEnter:=FOnEnter;
    end;
  end;

  procedure TSelectDa.SetOnExit(const Value:TNotifyEvent);
  begin
    if @FOnExit@Value then
    begin
      FOnExit:=Value;
      FEdit.OnExit:=FOnExit;
    end;
  end;

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

  end.

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

延伸阅读
在Delphi中实现将Font.Style写入INI文件 前不久我编写一个小程序在INI文件中记录字体的属性(颜色值/color,大小/size,字体名/name,样式/style),其中color值和size值可以用数值方式写入INI文件,name是用字符方式写入,但Font.style不是数值型、字符型,也不是布尔型,而是TfontStyles类,无法直接写入INI文件中去,我找了好多相关书...
  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...
6.4 文件管理综合举例:文件管理器的实现 在本章的最后,我们利用Delphi提供的文件控件和文件管理函数开发一个简单的文件管理器。虽然这一文件管理器还无法和Windows提供的文件管理器相比拟,但它也为一般的文件操作提供了足够多的功能,而且如果读者感兴趣,还可以对它做进一步的扩充。在后边的拖放操作一章中,我们就为它提供了拖...
标签: Delphi
  1.2.3 设计简单的用户界面 首先从空白窗体开始工作。我们将设计一个简单的程序 : 在屏幕上开一个窗口,窗口中有一个图框,用三个按钮来改变图框的形状;再用一个图标按钮来进行图框的颜色设置,通过颜色编辑对话框来选择变成哪一种颜色。 在菜单上选用 File|New Application 菜单项来生成新的工程文件。这时,屏幕上出现图...
创建Northwind的OLEDB数据链接 访问数据的一个重要步骤是为想要访问的每个数据库都创建一个OLEDB数据源。下面的步骤为VisualBasic所提供的Nwind.mdb(Northwind)数据库创建这样一个对象。这个数据源被用于VisualBasic文档所提供的一些示例过程。在一个计算机上只需要创建一次OLEDB数据源。 要创建Northwind的OLEDB数据源 ...

经验教程

625

收藏

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