Delphi Self-writing components: can set the color of the button (change to Bs_ownerdraw style, and then Cn_drawitem)

Source: Internet
Author: User
Tags textout

    • Unit Colorbutton;
    • Interface
    • Uses
    • Windows, Messages, Sysutils, Classes, Graphics, Controls,
    • Stdctrls;
    • Type
    • Tcolorbutton = Class (TButton)
    • Private
    • //Add color property, default Clwhite
    • {Private declarations}
    • Fcolor:tcolor;
    • Fcanvas:tcanvas;
    • Isfocused:boolean;
    • procedure SetColor (value:tcolor);
    • procedure Cndrawitem (var message:twmdrawitem); Message Cn_drawitem;
    • protected
    • {Protected declarations}
    • procedure CreateParams (var params:tcreateparams); override;
    • procedure SetButtonStyle (Adefault:boolean); override;
    • Public
    • {public declarations}
    • Constructor Create (aowner:tcomponent); override;
    • destructor destroy;override;
    • published
    • {Published declarations}
    • Property Color:tcolor Read Fcolor write setcolor default clwhite;
    • end;
    • Procedure Register;
    • Implementation
    • //**********************************
    • Borland/delphi7/source/vcl/checklst.pas can make reference
    • //**********************************
    • Registration functions automatically added by the system
    • Procedure Register;
    • Begin
    • Registercomponents (' Additional ', [Tcolorbutton]);
    • End
    • Add Constructor ***************
    • Constructor Tcolorbutton. Create (aowner:tcomponent);
    • Begin
    • inherited Create (Aowner);
    • Fcanvas:=tcanvas. Create;
    • Fcolor:=clwhite; //Set Default color
    • End
    • Add a destructor ***************
    • destructor Tcolorbutton. Destroy;
    • Begin
    • Fcanvas. Free;
    • inherited Destroy;
    • End
    • Defines the button style, which must be redefined as a self-portrait button * * * *
    • Procedure Tcolorbutton. CreateParams (var params:tcreateparams);
    • Begin
    • inherited CreateParams (Params);
    • With Params do style:=style or Bs_ownerdraw;
    • End
    • Attribute Write Method * * * *
    • Procedure Tcolorbutton. SetColor (Value:tcolor);
    • Begin
    • Fcolor:=value;
    • Invalidate; //Full Redraw control
    • End
    • Set Button Status * * * *
    • Procedure Tcolorbutton. SetButtonStyle (Adefault:boolean);
    • Begin
    • if adefault<>isfocused Then
    • begin
    • Isfocused:=adefault;
    • Refresh;
    • end;
    • End
    • Draw button * * * *
    • Procedure Tcolorbutton. 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
    • //button to draw the state when the focus is given
    • 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
    • //The State of the button is drawn when pressed
    • Fcanvas. Rectangle (Arect. Left, Arect. Top, Arect. Right, Arect. Bottom);
    • Inflaterect (Arect,-1,-1);
    • End Else
    • //Draw a button that is not pressed
    • Drawframecontrol (drawitemstructHDC, Arect, Dfc_button, Flags);
    • Fcanvas. FillRect (arect);
    • //Draw caption text content
    • Fcanvas. Font: = Self. Font;
    • Arect:=clientrect;
    • Wh:=fcanvas. Textextent (Caption);
    • Fcanvas. Pen. Width: = 1;
    • Fcanvas. Brush. Style: = bsclear;
    • if not Enabled Then
    • The begin //button should be painted once more caption text when it fails
    • 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);
    • //Draw the inner box dashed when getting focus
    • 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
    • End.

http://blog.csdn.net/aroc_lo/article/details/3070530

Http://www.fx114.net/qa-183-149306.aspx

Delphi Self-writing components: can set the color of the button (change to Bs_ownerdraw style, and then Cn_drawitem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.