首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网页制作-> Discuz!专栏
用delphi实现特色按钮_delphi教程
作者:网友供稿 点击:0
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 

特色按钮    

每当用到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);
         end;
       else FRgn := CreateRectRgn(1 + n, 1 + n, Width - n - 1,

Height - n - 1);
    end;//case
    FrameRgn(Handle, FRgn, Brush.Handle, 1, 1);
  end;
end;

procedure TDsFancyButton.WriteCaption;
var
  Flags: Word;
  BtnL, BtnT, BtnR, BtnB: Integer;
  R, TR: TRect;
begin
  R := ClientREct; TR := ClientRect;
  Canvas.Font := Self.Font;
  Canvas.Brush.Style := bsClear;
  Flags := DT_CENTER or DT_SINGLELINE;
  Canvas.Font := Font;

  if FIsDown then FTextColor := FrameColor
  else FTextColor := Self.Font.Color;

  with canvas do
    begin
      BtnT := (Height - TextHeight(Caption)) div 2;
      BtnB := BtnT + TextHeight(Caption);
      BtnL := (Width - TextWidth(Caption)) div 2;
      BtnR := BtnL + TextWidth(Caption);
      TR := Rect(BtnL, BtnT, BtnR, BtnB);
      R := TR;
      if ((TextStyle = txLowered) and FIsDown ) or
         ((TextStyle = txRaised) and not FIsDown) then
      begin
        Font.Color := clBtnHighLight;
        OffsetRect(TR, -1 + 1, -1 + 1);
        DrawText(Handle, PChar(Caption), Length(Caption), TR,

Flags);
      end
      else if ((TextStyle = txLowered) and not FIsDown) or
              ((TextStyle = txRaised) and FIsDown) then
           begin
             Font.Color := clBtnHighLight;
             OffsetRect(TR, + 2, + 2);
             DrawText(Handle, PChar(Caption), Length(Caption), TR,

Flags);
           end
           else if (TextStyle = txShadowed) and FIsDown then
                begin
                  Font.Color := clBtnShadow;
                  OffsetREct(TR, 3 + 1, 3 + 1);
                  DrawText(Handle, PChar(Caption),

Length(Caption), TR, Flags);
                end
                else if (TextStyle = txShadowed) and not FIsDown

then
                begin
                  Font.Color := clBtnShadow;
                  OffsetRect(TR, 2 + 1, 2 + 1);
                  DrawText(Handle, PChar(Caption),

Length(Caption), TR, Flags);
                end;

      if Enabled then Font.Color := FTextColor//self.Font.Color
      else if (TextStyle = txShadowed) and not Enabled then
        Font.Color := clBtnFace
      else Font.Color := clBtnShadow;
      if FIsDown then OffsetRect(R, 1, 1)
      else OffsetRect(R, -1, -1);
      DrawText(Handle, PChar(Caption), Length(Caption), R, Flags);
    end;
end;

procedure TDsFancyButton.SetButtonColor(value: TColor);
begin
  if value <> FButtonColor then
    begin FButtonColor := value ; Invalidate; end;
end;

procedure TDsFancyButton.WMLButtonDown(var message:

TWMLButtonDown);
begin
  if not PtInRegion(MRgn, message.xPos, message.yPos) then Exit;
  FIsDown := True;
  Paint;
  inherited;
end;

procedure TDsFancyButton.WMLButtonUp(var message: TWMLButtonUp);
begin
  if not FIsDown then Exit;
  FIsDown := False;
  paint;
  inherited;
end;

procedure TDsFancyButton.SetShape(value: TShape);
begin
  if value <> FShape then
    begin FShape := value; Invalidate; end;
end;

procedure TDsFancyButton.SetTextStyle(value: TTextStyle);
begin
  if value<>FTextStyle then
    begin  FTextStyle := value; Invalidate; end;
end;

procedure TDsFancyButton.SetFrameColor(value: TColor);
begin
  if Value<>FFrameColor then
    begin FFrameColor := Value; Invalidate;end;
end;

procedure TDsFancyButton.SetFrameWidth(Value: Integer);
var
  w: integer;
begin
  if Width<height then w := Width else w := Height;
  if Value<>FFrameWidth then FFrameWidth := value;
  if FFrameWidth < 4 then FFrameWidth := 4;
  if FFrameWidth >(w div 2) then FFrameWidth := (w div 2);
  Invalidate;
end;

procedure TDsFancyButton.SetCornerRadius(Value: integer);
var
  w: integer;
begin
  if Width<Height then w := Width else w := Height;
  if value<>FCornerRadius then FCornerRadius := value;
  if FCornerRadius<3 then FCornerRadius := 3;
  if FCornerRadius>w then FCornerRadius := w;
  Invalidate;
end;

procedure TDsFancyButton.CMEnabledChanged(var message: Tmessage);
begin
  inherited;
  invalidate;
end;

procedure TDsFancyButton.CMTextChanged(var message: TMessage);
begin
  Invalidate;
end;

procedure TDsFancyButton.CMDialogChar(var message:TCMDialogChar);
begin
  With Message do
    if IsAccel (CharCode, Caption) and Enabled then
      begin  Click; Result := 1 ;end
    else inherited;
end;

procedure TDsFancyButton.WMSize(var Message: TWMSize);
begin
  inherited;
  if width>300 then width := 300;
  if Height>300 then Height := 300;
end;

procedure TDsFancyButton.Click;
begin
  FIsDown := False;
  Invalidate;
  inherited Click;
end;

procedure Register;
begin
  RegisterComponents(WYM COMPONENT,[TDsFancyButton]);
end;

end.

耿百强。


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Eclipse + Lomboz + Tomcat JSP 开发配置-JSP教程,Jsp/Servlet
·利用Java调用可执行命令实例-JSP教程,Java技巧及代码
·彻底搞定JSP在线人数-JSP教程,Jsp/Servlet
·数据库操作类实现(C#,SqlClient)-.NET教程,C#语言
·在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)-.NET教程,C#语言
·结合PHP使用HTML表单(2)-PHP教程,PHP应用
·Java中利用JMF编写摄像头拍照程序-JSP教程,Java技巧及代码
·解析.Net框架下的XML编程技术-.NET教程,XML应用
·ASP.net Logion用户登陆验证代码-.NET教程,Asp.Net开发
·Java中精确计算的一个类用BigDecimal-JSP教程,Java技巧及代码
最新文章
·超越adsense:另类方法赚取巨额收益_网赚技巧
·google adwords优化技巧_网赚技巧
·自己误点adsense广告不用再通知google了_网赚技巧
·用fireworks滤镜轻松制作可爱gif动画_fireworks教程
·网站赚钱:google关键词广告创建的十二高招_站长心得
·提升网站使用性 打造实用性网站_站长心得
·最快速登录到google的10点主要经验_google推广
·制作主页的四十个技巧1_站长心得
·利用rss和gmail备份你的blog_站长心得
·seo终极方法_seo网站优化
相关主题
  • 用delphi实现文件下载的几种方法_delphi教程
  • 用delphi创建服务程序_delphi教程
  • 用delphi做一个有颜色属性的按钮_delphi教程
  • 用delphi开发dll来代替8581协议控制和采集华为psm—a10电源(二)_delphi教程
  • 用delphi编写系统进程监控程序_delphi教程
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭