如何改变按钮颜色或改变按钮caption的颜色

解决方案 »

  1.   

    button1.forn.color:=clwhite;button1.color:=clblue;
      

  2.   

    可要给分阿,我可是这么晚了还给你解答,哈哈基本原理就是,在绘制按钮的时候,WM_CTLCOLORBTN消息被发送到按钮的父窗口.然后父窗口才可以改变按钮的文本和背景颜色,不过只有owner-drawn(由应用程序绘制,而不是由系统绘制的按钮)类型的按钮才会响应父窗口的消息处理. 当你要绘制一个owner-drawn按钮,系统发送给父窗口一个WM_DRAWITEM 消息,它的lParam参数是一个指向DRAWITEMSTRUCT得结构.对所有具有owner-drawn的控件使用这个结构可以为其得绘制提供应用程序的绘制信息。
      

  3.   

    unit ColorBtn;interface
      uses
        Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
        Dialogs, StdCtrls, Buttons;  type
        TColorBtn = class(TButton)
        private
          IsFocused: boolean;
          FCanvas: TCanvas;
          F3DFrame: boolean;
          FButtonColor: TColor;
          procedure Set3DFrame(Value: boolean);
          procedure SetButtonColor(Value: TColor);
          procedure CNDrawItem(var Message: TWMDrawItem);
            message CN_DRAWITEM;
          procedure WMLButtonDblClk(var Message: TWMLButtonDblClk);
            message WM_LBUTTONDBLCLK;
          procedure DrawButtonText(const Caption: string; TRC: TRect;
            State: TButtonState; BiDiFlags: Longint);
          procedure CalcuateTextPosition(const Caption: string;
            var TRC: TRect; BiDiFlags: Longint);
        protected
          procedure CreateParams(var Params: TCreateParams); override;
          procedure SetButtonStyle(ADefault: boolean); override;
        public
          constructor Create(AOwner: TComponent); override;
          destructor Destroy; override;
        published
          property ButtonColor: TColor read FButtonColor
            write SetButtonColor default clBtnFace;
          property Frame3D: boolean read F3DFrame write Set3DFrame
            default False;
      end;  procedure Register;implementation{ TColorBtn  }constructor TColorBtn.Create(AOwner: TComponent);
    begin
      Inherited Create(AOwner);
      FCanvas:= TCanvas.Create;
      FButtonColor:= clBtnFace;
      F3DFrame:= False;
    end;destructor TColorBtn.Destroy;
    begin
      FCanvas.Free;
      Inherited Destroy;
    end;procedure TColorBtn.CreateParams(var Params: TCreateParams);
    begin
      Inherited CreateParams(Params);
      with Params do Style:= Style or BS_OWNERDRAW;
    end;procedure TColorBtn.Set3DFrame(Value: boolean);
    begin
      if F3DFrame <> Value then F3DFrame:= Value;
    end;procedure TColorBtn.SetButtonColor(Value: TColor);
    begin
      if FButtonColor <> Value then begin
        FButtonColor:= Value;
        Invalidate;
      end;
    end;procedure TColorBtn.WMLButtonDblClk(var Message: TWMLButtonDblClk);
    begin
      Perform(WM_LBUTTONDOWN, Message.Keys, Longint(Message.Pos));
    end;procedure TColorBtn.SetButtonStyle(ADefault: Boolean);
    begin
      if IsFocused <> ADefault then IsFocused:= ADefault;
    end;procedure TColorBtn.CNDrawItem(var Message: TWMDrawItem);
    var
      RC: TRect;
      Flags: Longint;
      State: TButtonState;
      IsDown, IsDefault: Boolean;
      DrawItemStruct: TDrawItemStruct;
    begin
      DrawItemStruct:= Message.DrawItemStruct^;
      FCanvas.Handle:= DrawItemStruct.HDC;
      RC:= ClientRect;
      with DrawItemStruct do begin
        IsDown:= ItemState and ODS_SELECTED <> 0;
        IsDefault:= ItemState and ODS_FOCUS <> 0;
        if not Enabled then State:= bsDisabled
        else if IsDown then State:= bsDown
        else State:= bsUp;
      end;
      Flags:= DFCS_BUTTONPUSH or DFCS_ADJUSTRECT;
      if IsDown then Flags:= Flags or DFCS_PUSHED;
      if DrawItemStruct.ItemState and ODS_DISABLED <> 0 then
        Flags:= Flags or DFCS_INACTIVE;
      if IsFocused or IsDefault then begin
        FCanvas.Pen.Color:= clWindowFrame;
        FCanvas.Pen.Width:= 1;
        FCanvas.Brush.Style:= bsClear;
        FCanvas.Rectangle(RC.Left, RC.Top, RC.Right, RC.Bottom);
        InflateRect(RC, -1, -1);
      end;
      if IsDown then begin
        FCanvas.Pen.Color:= clBtnShadow;
        FCanvas.Pen.Width:= 1;
        FCanvas.Rectangle(RC.Left, RC.Top, RC.Right, RC.Bottom);
        InflateRect(RC, -1, -1);
        if F3DFrame then begin
          FCanvas.Pen.Color:= FButtonColor;
          FCanvas.Pen.Width:= 1;
          DrawFrameControl(DrawItemStruct.HDC, RC, DFC_BUTTON, Flags);
        end;
      end else
        DrawFrameControl(DrawItemStruct.HDC, RC, DFC_BUTTON, Flags);
      FCanvas.Brush.Color:= FButtonColor;
      FCanvas.FillRect(RC);
      InflateRect(RC, 1, 1);
      if IsFocused then begin
        RC:= ClientRect;
        InflateRect(RC, -1, -1);
      end;
      if IsDown then OffsetRect(RC, 1, 1);
      FCanvas.Font:= Self.Font;
      DrawButtonText(Caption, RC, State, 0);
      if IsFocused and IsDefault then begin
        RC:= ClientRect;
        InflateRect(RC, -4, -4);
        FCanvas.Pen.Color:= clWindowFrame;
        Windows.DrawFocusRect(FCanvas.Handle, RC);
      end;
      FCanvas.Handle:= 0;
    end;procedure TColorBtn.CalcuateTextPosition(const Caption: string;
          var TRC: TRect; BiDiFlags: Integer);
    var
      TB: TRect;
      TS, TP: TPoint;
    begin
      with FCanvas do begin
        TB:= Rect(0, 0, TRC.Right + TRC.Left, TRC.Top + TRC.Bottom);
        DrawText(Handle, PChar(Caption), Length(Caption), TB,
          DT_CALCRECT or BiDiFlags);
        TS := Point(TB.Right - TB.Left, TB.Bottom - TB.Top);
        TP.X := ((TRC.Right - TRC.Left) - TS.X + 1) div 2;
        TP.Y := ((TRC.Bottom - TRC.Top) - TS.Y + 1) div 2;
        OffsetRect(TB, TP.X + TRC.Left, TP.Y + TRC.Top);
        TRC:= TB;
      end;
    end;procedure TColorBtn.DrawButtonText(const Caption: string; TRC: TRect;
      State: TButtonState; BiDiFlags: Integer);
    begin
      with FCanvas do begin
        CalcuateTextPosition(Caption, TRC, BiDiFlags);
        Brush.Style:= bsClear;
        if State = bsDisabled then begin
          OffsetRect(TRC, 1, 1);
          Font.Color:= clBtnHighlight;
          DrawText(Handle, PChar(Caption), Length(Caption), TRC,
            DT_CENTER or DT_VCENTER or BiDiFlags);
          OffsetRect(TRC, -1, -1);
          Font.Color:= clBtnShadow;
          DrawText(Handle, PChar(Caption), Length(Caption), TRC,
            DT_CENTER or DT_VCENTER or BiDiFlags);
        end else
          DrawText(Handle, PChar(Caption), Length(Caption), TRC,
            DT_CENTER or DT_VCENTER or BiDiFlags);
        end;
    end;procedure Register;
    begin
      RegisterComponents('Samples', [TColorBtn]);
    end;end.
      

  4.   

    网上查到的资料:
    用Delphi做一个有颜色属性的按钮
    NetGoCN原创在标准的Windows程序中所有按钮均没有颜色。因此Delphi提供的所有按钮组件也均无颜色属性,有时你可能做了一个五颜六色的程序界面,而按钮颜色可能很不相称。
        在此本人提供一种用自定义组件制作有颜色属性的铵钮的方法,它遵循了Delphi的组件定义规则,完成后该按钮与普通按钮(Button)相比,多了一个Color属性,你可以永远地使用它,在设计阶段随意地改变颜色,就象是Delphi本身提供的组件一样(本文代码在Delphi 4.0下完成)。
    第一步
        打开Delphi,选择菜单的Component/New Component选项,在弹出对话框的Ancestor type下拉框中手工填入或下拉选择TButton,这是选择了我们自定义组件的祖先类,我们将以此为基础完成自定义组件的下一步代码编写(这也是自定义组件编写的第一步)。对话框中的其余可编写内容就随你的高兴而填写了,但是你必须注意Class Name(类名)不能和已有的(包括你自定义的)类名相同,同时还应该记住该自定义组件的安装位置(Palette Page下拉框中的内容)和单元文件在磁盘上的存放位置(Unit file name编辑框内容),不然以后你在何处去找它?本文以 Delphi的默认值TButton1为类名。
    第二步
        做完以上工作后,按下面的OK按钮,Delphi将为你自动生成一个基本的组件代码,对这样的代码框架一般没有必要修改,一定要修改的话请注意Delphi的组件定义规则(本文只是删除了所有自动生成的注释内容),接着就应该将其文件存盘。
    第三步
        在上面的代码框架中添加我们的代码,当然这是我们要做的主要工作。1.      将Delphi自动生成的单元文件的数据类型定义部份修改为:
    type
    TButton1 = class(TButton)
    private
      FColor:TColor;
      FCanvas: TCanvas;
      IsFocused: Boolean;
      procedure SetColor(Value:TColor);
      procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
    protected
      procedure CreateParams(var Params: TCreateParams); override;
      procedure SetButtonStyle(ADefault: Boolean); override;
    public
      constructor Create(AOwner: TComponent); override;
      destructor Destroy; override;
    published
      property Color:TColor read FColor write SetColor default clWhite;
    end;
    说明:
        a. 我们只添加了一个属性,因此在published段的代码下只有一个Color属性,并将默认颜色设为白色(clWhite,当然你可以随意改变)。
        b. 重载构造函数和析构函数,二者应为可以在外部调用,因此应将其放在public段。
        c. 读属性的私有数据域FColor和属性的写方法SetColor,应放在私有段(private),其它与此相关的非独立的变量和过程/函数等也应放在private段,以使在类以外不能访问它们。2.      Delphi自动生成的 procedure Register可以不理它。我们在它的过程体之后,在end.(注意符号“.”)之前手工加上以下代码,完成我们在上面定义的全部过程的过程体编写(这里我们没有定义有函数原型)://*** 构造函数 *****************************************************
    constructor TButton1.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FCanvas := TCanvas.Create;
      FColor:=clWhite;//默认颜色
    end;//*** 析构函数 *************************************************
    destructor TButton1.Destroy;
    begin
      FCanvas.Free;
      inherited Destroy;
    end;//*** 定义按钮样式,必须将该按钮重定义为自绘式按钮 *************
    procedure TButton1.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      with Params do Style := Style or BS_OWNERDRAW;
    end;//*** 属性写方法 ************************************************
    procedure TButton1.SetColor(Value:TColor);
    begin
      FColor:=Value;
      Invalidate;
    end;//*** 设置按钮状态***********************************************
    procedure TButton1.SetButtonStyle(ADefault: Boolean);
    begin
      if ADefault <> IsFocused then
      begin
        IsFocused := ADefault;
        Refresh;
      end;
    end;//*** 绘制按钮 ***************************************************
    procedure TButton1.CNDrawItem(var Message: TWMDrawItem);
    var
      IsDown, IsDefault: Boolean;
      ARect: TRect;
      Flags: Longint;
      DrawItemStruct: TDrawItemStruct;
      wh:TSize;
    begin
      DrawItemStruct:=Message.DrawItemStruct^;
      FCanvas.Handle := DrawItemStruct.hDC;
      ARect := ClientRect;
      with DrawItemStruct do
      begin
        IsDown := itemState and ODS_SELECTED <> 0;
        IsDefault := itemState and ODS_FOCUS <> 0;
      end;  Flags := DFCS_BUTTONPUSH or DFCS_ADJUSTRECT;
      if IsDown then Flags := Flags or DFCS_PUSHED;
      if DrawItemStruct.itemState and ODS_DISABLED <> 0 then
         Flags := Flags or DFCS_INACTIVE;  if IsFocused or IsDefault then
      begin
        //按钮得到焦点时的状态绘制
        FCanvas.Pen.Color := clWindowFrame;
        FCanvas.Pen.Width := 1;
        FCanvas.Brush.Style := bsClear;
        FCanvas.Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
        InflateRect(ARect, -1, -1);
      end;  FCanvas.Pen.Color := clBtnShadow;
      FCanvas.Pen.Width := 1;
      FCanvas.Brush.Color := FColor;
      if IsDown then begin
        //按钮被按下时的状态绘制
         FCanvas.Rectangle(ARect.Left , ARect.Top, ARect.Right, ARect.Bottom);
         InflateRect(ARect, -1, -1);
      end else
         //绘制一个未按下的按钮
         DrawFrameControl(DrawItemStruct.hDC, ARect, DFC_BUTTON, Flags);
      FCanvas.FillRect(ARect);  //绘制Caption文本内容
      FCanvas.Font := Self.Font;
      ARect:=ClientRect;
      wh:=FCanvas.TextExtent(Caption);
      FCanvas.Pen.Width := 1;
      FCanvas.Brush.Style := bsClear;
      if not Enabled then
      begin //按钮失效时应多绘一次Caption文本
         FCanvas.Font.Color := clBtnHighlight;
         FCanvas.TextOut((Width div 2)-(wh.cx div 2)+1,
                         (height div 2)-(wh.cy div 2)+1,
                          Caption);
         FCanvas.Font.Color := clBtnShadow;
      end;
      FCanvas.TextOut((Width div 2)-(wh.cx div 2),(height div 2)-(wh.cy div 2),Caption);  //绘制得到焦点时的内框虚线
      if IsFocused and IsDefault then
      begin
         ARect := ClientRect;
         InflateRect(ARect, -4, -4);
         FCanvas.Pen.Color := clWindowFrame;
         FCanvas.Brush.Color := FColor;
         DrawFocusRect(FCanvas.Handle, ARect);
      end;
      FCanvas.Handle := 0;
    end;
    //** The End *********************************************************
    end.第四步:检查确认无误后选择Delphi菜单的Component/Install Component选项,在Unite file name编辑框中确认你的文件路径和名称后按OK按钮,Delphi将编译、安装该组件。
          如果你完全按本文步聚进行,在编译安装无误后,你可以在Delphi组件标签的Samples标签页中找到一个图标和TButton一样的按钮。新建一个工程并将这个我们自义的按钮放置在Form上其默认的名称是Button11,你会看到一个白色的按钮。怎么样?通过其Color属性你以随意设置它的颜色。
          最后说明:本文中的按钮绘制方式采用了和Delphi本身的TButton相似的绘制方式,以达到和Delphi按钮相似的动作外观。然而你想要发挥的话你可以在FCanvas这块画布上绘制任意的、你想要的、可以表达你的个性的所有文字和图形。