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

特色按钮

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

每当用到DELPHI自带的控件都感到少了一点什么,形状也好,颜色也好,变化的方式也好,都与自已的项目所需要的标准相差了一些,查阅了一些书籍后发现下面的控件很有可用之处!!!

以下是它的源代码:

unit DsFancyButton;

interface

uses
  SysUtils,Windows, Messages, Classes, Graphics, Controls, Forms;

type
  TTextStyle = (txNone, txLowered, txRaised, txShadowed);
  TShape = (shCapsule, shOval, shRectangle, shRoundRect);
  TDsFancyButton = class(TGraphicControl)
  private
    FButtonColor: TColor;
    FIsDown: Boolean;
    FFrameColor: TColor;
    FFrameWidth: Integer;
    FCornerRadius: Integer;
    FRgn, MRgn: HRgn;
    FShape: TShape;
    FTextColor: TColor;
    FTextStyle: TTextStyle;

    procedure SetButtonColor(Value: TColor);
    procedure CMEnabledChanged(var message: TMessage);
              message CM_ENABLEDCHANGED;
    procedure CMTextChanged(var message: TMessage);
              message CM_TEXTCHANGED;
    procedure CMDialogChar(var message: TCMDialogChar);
              message CM_DIALOGCHAR;
    procedure WMSize(var message: TWMSize); message WM_PAINT;
  protected
    procedure Click; override;
    procedure DrawShape;
    procedure Paint; override;
    procedure SetFrameColor(Value: TColor);
    procedure SetFrameWidth(Value: Integer);
    procedure SetCornerRadius(Value: Integer);
    procedure SetShape(Value: TShape);
    procedure SetTextStyle(Value: TTextStyle);
    procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
    procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
    procedure WriteCaption;
  public
    constructor Create(Aowner: TComponent); override;
    destructor Destroy; override;
  published
    property ButtonColor: TColor
             read FButtonColor write SetButtonColor;
    property Caption;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    property FrameColor: TColor
             read FFrameColor write SetFrameColor;
    property FrameWidth: Integer
             read FFrameWidth write SetFrameWidth;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property CornerRadius: Integer
             read FCornerRadius write SetCornerRadius;
    property Shape: TShape
             read FShape write SetShape default shRoundRect;
    property ShowHint;
    property TextStyle: TTextStyle
             read FTextStyle write SetTExtStyle;
    property Visible;

    property OnClick;   property OnDragDrop;
    property OnDragOver;  property OnEndDrag;
    property OnMouseDown; Property OnMouseUp;
    Property OnMouseMove;
  end;

procedure Register;

implementation

constructor TDsFancyButton.Create(AOwner: TComponent);
begin
  inherited Create(Aowner);
  ControlStyle := [csClickEvents,  csCaptureMouse,  csSetCaption];
  Enabled := True;
  FButtonColor := clBtnFace;
  FIsDown := False;
  FFrameColor := clGray;
  FFrameWidth := 6;
  FCornerRadius := 10;
  FRgn := 0;
  FShape := shRoundRect;
  FTextStyle := txRaised;
  Height := 25;
  Visible := True;
  Width := 97;
end;

destructor TDsFancyButton.Destroy;
begin
  DeleteObject(FRgn);
  DeleteObject(MRgn);
  inherited Destroy;
end;

procedure TDsFancyButton.Paint;
var Dia: integer;
    ClrUp,  ClrDown: TColor;
begin
  Canvas.Brush.Style := bsClear;

  if FIsDown then
    begin ClrUp := clBtnShadow; ClrDown := clBtnHighlight; end
  else
    begin ClrUp := clBtnHighlight; ClrDown := clBtnShadow; end;

  with Canvas do
    begin
      case Shape of
        shRoundRect:
          begin
            Dia := 2*CornerRadius;
            Mrgn := CreateRoundRectRgn(0, 0, Width, Height, Dia, Dia);
          end;
        shCapsule:
          begin
            if Width < Height then Dia := Width else Dia := Height;
            Mrgn := CreateRoundRectRgn(0, 0, Width ,  Height, Dia, Dia);
          end;
        shRectangle: MRgn := CreateRectRgn(0, 0, Width - 1, Height - 1);
        shOval: MRgn := CreateEllipticRgn(0, 0, Width, Height);
      end;//case
      Canvas.Brush.Color := FButtonColor;
      FillRgn(Handle, MRgn, Brush.Handle);
      Brush.Color :=ClrUp;
      FrameRgn(Handle, MRgn, Brush.Handle, 1,1);
      OffsetRgn(MRgn, 1, 1);
      Brush.Color := ClrDown;
      FrameRgn(Handle, MRgn, Brush.Handle, 1, 1);
    end;//canvas
    DrawShape;
    WriteCaption;
end;

procedure TDsFancyButton.DrawShape;
var
  FC, Warna: TColor;
  R, G, B: Byte;
  AwalR, AwalG, AwalB, AkhirR, AkhirG, AkhirB, n, t, Dia: Integer;
begin
  if FFrameWidth mod 2=0 then t := FFrameWidth
  else t := FFrameWidth + 1;

  Warna := ColorToRGB(ButtonColor);
  FC := ColorToRGB(FrameColor);
  Canvas.Brush.Color := Warna;

  AwalR := GetRValue(FC); AkhirR := GetRValue(Warna);
  AwalG := GetGValue(FC); AkhirG := GetGValue(Warna);
  AwalB := GetBValue(FC); AkhirB := GetBValue(Warna);
  FRgn := 0;
  with Canvas do
  for n := 0 to t - 1 do
  begin
    R := AwalR + Trunc(Sqrt(t*t - Sqr(t-n))*(AkhirR - AwalR)/t);
    G := AwalG + Trunc(Sqrt(t*t - Sqr(t-n))*(AkhirG - AwalG)/t);
    B := AwalB + Trunc(Sqrt(t*t - Sqr(t-n))*(AkhirB - AwalB)/t);
    Brush.Color := RGB(R, G, B);

    Case Shape of
      shOval: FRgn := CreateEllipticRgn(1 + n, 1 + n, Width - n, Height - n);
      shRoundRect:
        begin
          Dia := CornerRadius;
          if (Dia - n) >0 then
            FRgn :=
              CreateRoundRectRgn(1 + n, 1 + n ,Width - n, Height - n, 2*(Dia - n), 2*(Dia - n))
          else FRgn := CreateRectRgn( 1 + n, 1 + n, Width - n - 1, Height - n - 1);
        end;
       shCapsule:
         begin
           if Width < Height then Dia := Width div 2 else Dia := Height div 2;
             if (Dia - n) > 0 then
               FRgn:=
                 CreateRoundRectRgn(1 + n, 1 + n, Width - n, Height - n, 2*(Dia - n), 2*(Dia - n))
             else FRgn := CreateRectRgn(1 + n, 1 + n ,Width - n - 1, Height - n - 1);

[1] [2]  下一页


没有相关教程
教程录入: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……
    咸宁网络警察报警平台