http://www.netgocn.com Original
All buttons have no color in standard Windows programs. Therefore, all button components provided by Delphi also have no color attributes. Sometimes you may make a colorful program interface, and the button colors may be very disproportionate.
Here I provide a method to make ammonium buttons with color attributes using custom components. It follows the component definition rules of Delphi. After completion, the button has an additional Color attribute compared to the normal button (Button). You can Use it forever, change the color at will during the design stage, just like the components provided by Delphi itself (the code in this article is done under Delphi 4.0).
first step
Open Delphi, select the Component/New Component option of the menu, and manually fill in or drop-down in the Ancestor type drop-down box of the pop-up dialog box. This is the ancestor class that selected our custom component. We will complete it based on this. Define the next step of code writing for the component (this is also the first step in custom component writing). The rest of the writing content in the dialog box will be filled in as you please, but you must note that the Class Name cannot be the same as the existing (including your customized) class name, and you should also remember that it is Define the installation location of the component (the content in the Palette Page drop-down box) and the storage location of the unit files on the disk (the content of the Unit file name edit box), otherwise where will you find it in the future? This article takes the default value of Delphi TButton1 as Class name.
Step 2
After finishing the above work, press the OK button below and Delphi will automatically generate a basic component code for you. There is generally no need to modify such a code framework. If you have to modify it, please pay attention to Delphi's component definition rules (this article has only been deleted. All automatically generated comments) should be saved.
Step 3
Add our code in the above code framework, of course this is the main work we want to do.
1. Modify the data type definition part of the unit file automatically generated by Delphi to:
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;
illustrate:
a. We only added one property, so there is only one Color property under the code of the published segment, and set the default color to white (clWhite, of course you can change it at will).
b. Overload the constructor and the destructor, both of which should be called externally, so they should be placed in the public section.
c. Read the private data domain of the attribute and the write method SetColor of the attribute should be placed in the private segment (private). Other non-independent variables and procedures/functions related to this should also be placed in the private segment to make the class They cannot be accessed outside.
2. Delphi's automatic process Register can ignore it. After its process body, we manually add the following code before end. (note the symbol ".") to complete the process body writing of all the processes we defined above (we do not define a function prototype here):
//*** Constructor****************************************** ************
constructor TButton1.Create(AOwner: TComponent);
Begin
inherited Create(AOwner);
FCanvas := TCanvas.Create;
FColor:=clWhite;//Default color
end;
//*** Destructor********************************************* *******
destructor TButton1.Destroy;
Begin
FCanvas.Free;
inherited Destroy;
end;
//*** To define the button style, the button must be redefined as a self-drawn button*********************
procedure TButton1.CreateParams(var Params: TCreateParams);
Begin
inherited CreateParams(Params);
with Params do Style := Style or BS_OWNERDRAW;
end;
//*** Attribute writing method*************************************************** *******
procedure TButton1.SetColor(Value:TColor);
Begin
FColor:=Value;
Invalidate;
end;
//*** Set button status************************************************ ****
procedure TButton1.SetButtonStyle(ADefault: Boolean);
Begin
if ADefault <> IsFocused then
Begin
IsFocused := ADefault;
Refresh;
end;
end;
//*** Draw button****************************************** *******
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
//Draw the state when the button gets focus
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
//Draw the status when the button is pressed
FCanvas.Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
InflateRect(ARect, -1, -1);
end else
//Draw an unpressed button
DrawFrameControl(DrawItemStruct.hDC, 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
begin // When the button fails, you should draw Caption text once more
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 dotted line of the inner frame when you get the 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;
//** The End ********************************************* ***************
end.
Step 4: After checking, confirming that the error is correct, select the Component/Install Component option of the Delphi menu. Confirm your file path and name in the Unite file name edit box and press the OK button. Delphi will compile and install the component.
If you follow this article completely, after compiling and installing correctly, you can find a button with the same icon as TButton in the Samples tab of the Delphi component label. Create a new project and place this button we use on the Form. Its default name is Button11, and you will see a white button. How about it? Through its Color property you set its color at will.
Finally, the button drawing method in this article adopts a similar drawing method as Delphi's TButton to achieve a similar action appearance as Delphi button. However, if you want to play, you can draw any text and graphics on the FCanvas canvas that you want to express your personality.
If you have any questions, please send a letter or leave a message.
Welcome to try out the software of this site: NetGoCN (network)