Mega Code Archive

 
Categories / Delphi / System
 

Xp Button

Title: xp Button Question: This Component Is Not Complete Only The stxpBlue Style Of It Completed I Try For New Style So Son HbtTundar@Gmail.com Answer: unit HbtXpButtons; interface uses Messages, Windows, SysUtils, Classes, Dialogs, Controls, Forms, Menus, Graphics, StdCtrls, ExtCtrls, Buttons; Const HbtXPButtonVersion=$0100;// ** 1.00 ** type TXpStyle =(StxpBlue ,StxpSilver ,StxpOliveGreen,stCoughDropLicorice, stCoughDropBerry,stCoughDropCherry,stCoughDropCinnamon, stCoughDropGrape,stCoughDropLime,stCoughDropOrange,stGucciBlue, stGucciGreen,StPearl); HbtRGBArray = array[0..2] of Byte; HbtColor = array of TColor; //---------------------------------------------------------------------------- THbtXpButton = Class(TButton) Private FParentForm : TForm; FCanvas: TCanvas; FRect : TRect; IsFocused: Boolean; FMouseInControl :Boolean; FBorderColor:TColor; FMouseInColor :TColor; FFocusHighlightColor :Tcolor; FPushHighLightColor: Tcolor; FLeftTopColor:TColor; FMiddleColor:Tcolor; FRightDownColor:TColor; FMixColor:TColor; FVersion :Integer; FStyle: TXpStyle; FCancel: Boolean; FDefault: Boolean; FModalResult: TModalResult; procedure SetStyle(const Value: TXpStyle); Procedure SetVersion( Value: String ); Function GetVersion: String; procedure SetBorderColor(const Value: TColor); procedure SetMouseInColor(const Value: TColor); procedure SetFocuseHighlightColor(const Value: Tcolor); procedure SetPushHighLightColor(const Value: Tcolor); procedure SetDefault(const Value: Boolean); Protected procedure CreateParams(var Params: TCreateParams); override; procedure WndProc(var Message : TMessage); override; procedure SetButtonStyle(Value: Boolean); override; procedure DrawButton(thisRect: TRect; State: UINT); Procedure SetGradiantDefualt(Var thisCanvas:TCanvas;Var thisRect:Trect); Procedure SetGradiantDisabled(Var thisCanvas:TCanvas;Var thisRect:Trect); Procedure SetGradiantPush(Var thisCanvas:TCanvas;Var thisRect:Trect); procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED; procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED; procedure CNMeasureItem(var Message: TWMMeasureItem); message CN_MEASUREITEM; procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER; procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE; procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM; procedure CreateHandle; override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published Property Style : TXpStyle Read FStyle Write SetStyle default stxpBlue; Property Version:String Read getVersion Write SetVersion; Property BorderColor : TColor Read FBorderColor Write SetBorderColor default clNavy; property MouseInColor : TColor Read FMouseInColor Write SetMouseInColor Default $000097E5; Property FocusHighlightColor : Tcolor Read FFocusHighlightColor Write SetFocuseHighlightColor Default $00EE8269; Property PushHighLightColor : Tcolor Read FPushHighLightColor Write SetPushHighLightColor Default ClWhite; property Action; property Anchors; property BiDiMode; property Cancel: Boolean read FCancel write FCancel default False; property Caption; property Constraints; property Default: Boolean read FDefault write SetDefault default False; property DragCursor; property DragKind; property DragMode; property Enabled; property Font; property ModalResult: TModalResult read FModalResult write FModalResult default 0; property ParentBiDiMode; property ParentFont; property ParentShowHint; property PopupMenu; property ShowHint; property TabOrder; property TabStop default True; property Visible; property WordWrap; property OnClick; property OnContextPopup; property OnDragDrop; property OnDragOver; property OnEndDock; property OnEndDrag; property OnEnter; property OnExit; property OnKeyDown; property OnKeyPress; property OnKeyUp; property OnMouseDown; property OnMouseMove; property OnMouseUp; property OnStartDock; property OnStartDrag; end; Procedure Register; //------------------------------------------------------------------------------ implementation uses Math, Types, StrUtils, Consts; procedure THbtXpButton.CMEnabledChanged(var Message: TMessage); begin Invalidate; inherited; Invalidate; end; procedure THbtXpButton.CMFontChanged(var Message: TMessage); begin inherited; Invalidate; end; procedure THbtXpButton.CMMouseEnter(var Message: TMessage); begin inherited; FMouseInControl := True; Invalidate; end; procedure THbtXpButton.CMMouseLeave(var Message: TMessage); begin inherited; FMouseInControl := False; Invalidate; end; procedure THbtXpButton.CNDrawItem(var Message: TWMDrawItem); var SaveIndex: Integer; begin with Message.DrawItemStruct^ do begin SaveIndex := SaveDC(hDC); FCanvas.Lock; try FCanvas.Handle := hDC; FCanvas.Font := Font; FCanvas.Brush := Brush; DrawButton(rcItem, itemState); finally FCanvas.Handle := 0; FCanvas.Unlock; RestoreDC(hDC, SaveIndex); end; end; Message.Result := 1; end; procedure THbtXpButton.CNMeasureItem(var Message: TWMMeasureItem); begin with Message.MeasureItemStruct^ do begin itemWidth := Width; itemHeight := Height; end; end; constructor THbtXpButton.Create(AOwner: TComponent); begin FParentForm := TForm(AOwner); inherited Create(AOwner); FCanvas := TCanvas.Create; FStyle := StxpBlue; FBorderColor := ClNavy; FMouseInColor := $000097E5; FFocusHighlightColor := $00EE8269; FVersion := HbtXPButtonVersion; FPushHighLightColor := Clwhite; ControlStyle := ControlStyle + [csReflector]; Height:= 23; Width := 80; DoubleBuffered := True; end; procedure THbtXpButton.CreateHandle; var State: TButtonState; begin if Enabled then State := bsUp else State := bsDisabled; inherited CreateHandle; end; procedure THbtXpButton.CreateParams(var Params: TCreateParams); const ButtonStyles: array[Boolean] of DWORD = (BS_PUSHBUTTON, BS_DEFPUSHBUTTON); begin inherited CreateParams(Params); with Params do Style := Style or BS_OWNERDRAW ; end; destructor THbtXpButton.Destroy; begin FCanvas.Free; inherited Destroy; end; procedure THbtXpButton.DrawButton(thisRect: TRect; State: UINT); //---------------------------------------------------------------------------- Function CaptionRect(Totalwidth,TotalHeight,TextWidth,TextHeight:Integer):TRect; Var Left,Top,Right,Bottom:Integer; begin Left := (TotalWidth - TextWidth) Div 2; Top := (TotalHeight - TextHeight)Div 2; Right := Left+TextWidth; Bottom := Top+TextHeight; Result := Rect(Left,Top,Right,Bottom); end; //---------------------------------------------------------------------------- Procedure DrawFocus(Var thisCanvas: Tcanvas ;FocusColor:Tcolor); Var FocusRect:TRect; begin FocusRect := Rect(FRect.Left+2,FRect.Top+2,FRect.Right-2,FRect.Bottom-2); with FocusRect do begin thisCanvas.Pen.Color := clWindowFrame; thisCanvas.Brush.Color := FocusColor; Windows.DrawFocusRect(thisCanvas.Handle,FocusRect); end; end; //--------------------------------------------------------------------------- Procedure DrawHighLight(Var thisCanvas: Tcanvas ;HighLightColor:Tcolor); Var HighLightRect:TRect; begin HighLightRect := Rect(FRect.Left+1,FRect.Top+1,FRect.Right-1,FRect.Bottom-1); with HighLightRect do begin ExcludeClipRect(thisCanvas.Handle,FRect.Left,FRect.Top,FRect.Right,FRect.Bottom); SelectClipRgn(thisCanvas.Handle, 0); thisCanvas.Brush.Style := bsClear; thisCanvas.Pen.Width := 1; thisCanvas.Pen.Color := HighLightColor; thisCanvas.RoundRect(FRect.Left+1,FRect.Top+1,FRect.Right-1,FRect.Bottom-1,3,3); end; end; //---------------------------------------------------------------------------- Procedure DrawCaption(var thisCanvas:Tcanvas;thisCaption:Tcaption;thisFlags:Integer;UseOffset:Boolean); Var CaptionFalg: Longint; H,W:Integer; //W As Caption Width H As Text height CRect: TRect; begin CaptionFalg :=DrawTextBiDiModeFlags(DT_SINGLELINE); H:= thisCanvas.TextHeight('0'); W:= thisCanvas.TextWidth(Caption); CRect := CaptionRect(ClientWidth,ClientHeight,w,h); with CRect do begin if UseOffset Then OffsetRect(CRect,1,1); ExcludeClipRect(thisCanvas.Handle,FRect.Left,FRect.Top,FRect.Right,FRect.Bottom); thisCanvas.Brush.Style := bsClear; SelectClipRgn(thisCanvas.Handle, 0); DrawText(thisCanvas.Handle, PChar(Text), Length(Text), CRect, thisFlags or DT_CALCRECT); DrawText(thisCanvas.Handle, PChar(Caption), Length(Caption), CRect, thisFlags); end; end; //---------------------------------------------------------------------------- Procedure DrawDisabledCaption(var thisCanvas:Tcanvas;thisCaption:Tcaption;thisFlags:Integer); Var CaptionFalg: Longint; H,W:Integer; //W As Caption Width H As Text height CRect: TRect; begin CaptionFalg :=DrawTextBiDiModeFlags(DT_SINGLELINE); H:= thisCanvas.TextHeight('0'); W:= thisCanvas.TextWidth(Caption); CRect := CaptionRect(ClientWidth,ClientHeight,w,h); with CRect do begin ExcludeClipRect(thisCanvas.Handle,FRect.Left,FRect.Top,FRect.Right,FRect.Bottom); case FStyle of StxpBlue : thisCanvas.Pen.Color := $0092A1A1; end; thisCanvas.Brush.Style := bsClear; SelectClipRgn(thisCanvas.Handle, 0); DrawText(thisCanvas.Handle, PChar(Text), Length(Text), CRect, thisFlags or DT_CALCRECT); SetTextColor(thisCanvas.Handle,$0092A1A1); DrawText(thisCanvas.Handle, PChar(Caption), Length(Caption), CRect, thisFlags); end; end; //---------------------------------------------------------------------------- var Flags: Longint; IsDown,IsDefault,IsDisabled: Boolean; SaveIndex:Integer; GradiantRect,FocusRoundRect:TRect; Size: TSize; Offset: TPoint; begin Flags := DFCS_BUTTONPUSH or DFCS_ADJUSTRECT; IsDown := State and ODS_SELECTED 0; IsDefault := State and ODS_FOCUS 0; IsDisabled := State and ODS_DISABLED 0; if IsDown then Flags := Flags or DFCS_PUSHED; if IsDisabled then Flags := Flags or DFCS_INACTIVE; //--------------------------------------------------------------------- //--------------------------------------------------------------------- //-------------------------- NorMal Button ---------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- FRect := ClientRect; if not IsDisabled Then begin case FStyle of StxpBlue : FCanvas.pen.Color := FBorderColor; end; end; FCanvas.Brush.Style := bsSolid; FCanvas.Pen.Style := psSolid; FCanvas.Font.Color := Font.Color; RoundRect(FCanvas.Handle,FRect.Left,FRect.Top,FRect.Right,FRect.Bottom,3,3); GradiantRect := Rect(FRect.Left+2,FRect.Top+2,FRect.Right-2,FRect.Bottom-2); //------------------------------------------------------------------------ if not IsDisabled Then begin FCanvas.Lock; try SetGradiantDefualt(Fcanvas,GradiantRect); if Caption '' Then DrawCaption(Fcanvas,Caption,Flags,false); finally FCanvas.Unlock; end; end; if IsDisabled Then begin case FStyle Of StxpBlue : FCanvas.Pen.Color := $0092A1A1; end;//end of Case FCanvas.Pen.Style := psSolid; FCanvas.Lock; try SetGradiantDisabled(Fcanvas,GradiantRect); if Caption '' Then DrawDisabledCaption(Fcanvas,Caption,Flags); finally FCanvas.Unlock; end; end; //------------------------------------------------------------------------ if not (csDesigning In ComponentState) Then begin //--------------------------------------------------------------------- //--------------------------------------------------------------------- //------------------------ {Disabled Button } ------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- if IsDisabled Then begin case FStyle Of StxpBlue : FCanvas.Pen.Color := $0092A1A1; end;//end of Case FCanvas.Pen.Style := psSolid; FCanvas.Lock; try SetGradiantDisabled(Fcanvas,GradiantRect); if Caption '' Then DrawDisabledCaption(Fcanvas,Caption,Flags); finally FCanvas.Unlock; end; end; //--------------------------------------------------------------------- //--------------------------------------------------------------------- //-------------------------{Down Button }------------------------------ //--------------------------------------------------------------------- //--------------------------------------------------------------------- if IsDown Then begin FCanvas.Lock; Try SetGradiantPush(Fcanvas,GradiantRect); DrawFocus(Fcanvas,clBtnFace); if Caption '' Then DrawCaption(Fcanvas,Caption,Flags,True); Finally FCanvas.Unlock; end;//end of Try end; //--------------------------------------------------------------------- //------------------------ Focus Button ------------------------------ //--------------------------------------------------------------------- //--------------------------------------------------------------------- if IsFocused then begin SetGradiantDefualt(Fcanvas,GradiantRect); DrawFocus(Fcanvas,clBtnface); DrawHighLight(fcanvas,FFocusHighlightColor); if Caption '' Then DrawCaption(Fcanvas,Caption,Flags,False); end; //--------------------------------------------------------------------- //--------------------------------------------------------------------- //--------------------- Focus and Pudh Button ------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- if IsDown and IsFocused Then begin FCanvas.Lock; Try SetGradiantPush(Fcanvas,GradiantRect); DrawFocus(FCanvas,clBtnFace); DrawHighLight(Fcanvas,FPushHighLightColor); if Caption '' Then DrawCaption(Fcanvas,Caption,Flags,True); Finally FCanvas.Unlock; end;//end of Try end; //--------------------------------------------------------------------- //--------------------------------------------------------------------- //-------------------------- MouseIn Button --------------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- if FMouseInControl Then begin SetGradiantDefualt(Fcanvas,GradiantRect); DrawHighLight(fcanvas,FMouseInColor); if Caption '' Then DrawCaption(Fcanvas,Caption,Flags,False); end; //--------------------------------------------------------------------- //--------------------------------------------------------------------- //--------------------- Focus and MouseIn Button ---------------------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- if IsFocused and FMouseInControl Then begin SetGradiantDefualt(Fcanvas,GradiantRect); DrawFocus(Fcanvas,clBtnFace); DrawHighLight(fcanvas,FMouseInColor); if Caption '' Then DrawCaption(Fcanvas,Caption,Flags,false); end; //--------------------------------------------------------------------- //--------------------------------------------------------------------- //--------------------- Focus and MouseIn and ISDown Button ----------- //--------------------------------------------------------------------- //--------------------------------------------------------------------- if IsFocused and IsDown and FMouseInControl Then begin FCanvas.Lock; Try SetGradiantPush(Fcanvas,GradiantRect); DrawFocus(FCanvas,clBtnFace); DrawHighLight(fcanvas,FPushHighLightColor); if Caption '' Then DrawCaption(Fcanvas,Caption,Flags,True); Finally FCanvas.Unlock; end;//end of Try end; end;//end of if not CsDesigning .... end; function THbtXpButton.GetVersion: String; begin Result := Format( '%d.%d', [ Hi( FVersion ), Lo( FVersion ) ] ); end; procedure THbtXpButton.SetBorderColor(const Value: TColor); begin FBorderColor := Value; end; procedure THbtXpButton.SetButtonStyle(Value: Boolean); begin if Value IsFocused then begin IsFocused := Value; Invalidate; end; end; procedure THbtXpButton.SetDefault(const Value: Boolean); begin FDefault := Value; end; procedure THbtXpButton.SetFocuseHighlightColor(const Value: Tcolor); begin FFocusHighlightColor := Value; end; procedure THbtXpButton.SetGradiantDefualt(var thisCanvas: TCanvas;Var thisRect:Trect); var x, y, z, stelle, mx, bis, faColorsh, mass: Integer; Faktor: double; A:HbtRGBArray; B: array of HBtRGBArray; merkw: integer; merks: TPenStyle; merkp: TColor; FColor:HbtColor; begin mass:=0; SetLength(FColor,4); case FStyle Of StxpBlue : begin FLeftTopColor := $00fdfdfd; FMiddleColor := $00fdf7f6; FRightDownColor := $00ecdfde; FMixColor := $00fdf5f4; end; end; FColor[0]:=FLeftTopColor; FColor[1]:=FMixColor; FColor[2]:=FRightDownColor; FColor[3]:=FMiddleColor; If thiscanvasNil Then begin mx := High(FColor); if mx 0 then begin mass := (thisRect.Bottom) - (thisRect.Top); SetLength(b, mx + 1); for x := 0 to mx do begin FColor[x] := ColorToRGB(FColor[x]); b[x][0] := GetRValue(FColor[x]); b[x][1] := GetGValue(FColor[x]); b[x][2] := GetBValue(FColor[x]); end; merkw := thisCanvas.Pen.Width; merks := thisCanvas.Pen.Style; merkp := thisCanvas.Pen.Color; thisCanvas.Pen.Width := 1; thisCanvas.Pen.Style := psSolid; faColorsh := Round(mass / mx); for y := 0 to mx - 1 do begin if y = mx - 1 then bis := mass - y * faColorsh - 1 else bis := faColorsh; for x := 0 to bis do begin Stelle := x + y * faColorsh; faktor := x / bis; for z := 0 to 2 do a[z] := Trunc(b[y][z] + ((b[y + 1][z] - b[y][z]) * Faktor)); thisCanvas.Pen.Color := RGB(a[0], a[1], a[2]); thisCanvas.MoveTo(thisRect.Left , thisRect.Top + Stelle); thisCanvas.LineTo(thisRect.Right, thisRect.Top + Stelle); end; end; b := nil; thisCanvas.Pen.Width := merkw; thisCanvas.Pen.Style := merks; thisCanvas.Pen.Color := merkp; end end; end; procedure THbtXpButton.SetGradiantDisabled(var thisCanvas: TCanvas; var thisRect: Trect); var x, y, z, stelle, mx, bis, faColorsh, mass: Integer; Faktor: double; A:HbtRGBArray; B: array of HBtRGBArray; merkw: integer; merks: TPenStyle; merkp: TColor; FColor:HbtColor; begin mass:=0; SetLength(FColor,4); case FStyle Of StxpBlue : begin FLeftTopColor := $00EdF1F1; FMiddleColor := $00EdF1F1; FRightDownColor := $00EdF1F1; FMixColor := $00EdF1F1; end; end; FColor[0]:=FLeftTopColor; FColor[1]:=FMixColor; FColor[2]:=FRightDownColor; FColor[3]:=FMiddleColor; If thiscanvasNil Then begin mx := High(FColor); if mx 0 then begin mass := (thisRect.Bottom) - (thisRect.Top); SetLength(b, mx + 1); for x := 0 to mx do begin FColor[x] := ColorToRGB(FColor[x]); b[x][0] := GetRValue(FColor[x]); b[x][1] := GetGValue(FColor[x]); b[x][2] := GetBValue(FColor[x]); end; merkw := thisCanvas.Pen.Width; merks := thisCanvas.Pen.Style; merkp := thisCanvas.Pen.Color; thisCanvas.Pen.Width := 1; thisCanvas.Pen.Style := psSolid; faColorsh := Round(mass / mx); for y := 0 to mx - 1 do begin if y = mx - 1 then bis := mass - y * faColorsh - 1 else bis := faColorsh; for x := 0 to bis do begin Stelle := x + y * faColorsh; faktor := x / bis; for z := 0 to 2 do a[z] := Trunc(b[y][z] + ((b[y + 1][z] - b[y][z]) * Faktor)); thisCanvas.Pen.Color := RGB(a[0], a[1], a[2]); thisCanvas.MoveTo(thisRect.Left , thisRect.Top + Stelle); thisCanvas.LineTo(thisRect.Right, thisRect.Top + Stelle); end; end; b := nil; thisCanvas.Pen.Width := merkw; thisCanvas.Pen.Style := merks; thisCanvas.Pen.Color := merkp; end end; invalidate; end; procedure THbtXpButton.SetGradiantPush(var thisCanvas: TCanvas; var thisRect: Trect); var x, y, z, stelle, mx, bis, faColorsh, mass: Integer; Faktor: double; A:HbtRGBArray; B: array of HBtRGBArray; merkw: integer; merks: TPenStyle; merkp: TColor; FColor:HbtColor; begin mass:=0; SetLength(FColor,4); case FStyle Of StxpBlue : begin FLeftTopColor := $00BFA6A2; FMiddleColor := $00ecdfde; FRightDownColor := $00fdfdfd; FMixColor := $00ecdfde; end; end; FColor[0]:=FLeftTopColor; FColor[1]:=FMixColor; FColor[2]:=FRightDownColor; FColor[3]:=FMiddleColor; If thiscanvasNil Then begin mx := High(FColor); if mx 0 then begin mass := (thisRect.Bottom) - (thisRect.Top); SetLength(b, mx + 1); for x := 0 to mx do begin FColor[x] := ColorToRGB(FColor[x]); b[x][0] := GetRValue(FColor[x]); b[x][1] := GetGValue(FColor[x]); b[x][2] := GetBValue(FColor[x]); end; merkw := thisCanvas.Pen.Width; merks := thisCanvas.Pen.Style; merkp := thisCanvas.Pen.Color; thisCanvas.Pen.Width := 1; thisCanvas.Pen.Style := psSolid; faColorsh := Round(mass / mx); for y := 0 to mx - 1 do begin if y = mx - 1 then bis := mass - y * faColorsh - 1 else bis := faColorsh; for x := 0 to bis do begin Stelle := x + y * faColorsh; faktor := x / bis; for z := 0 to 2 do a[z] := Trunc(b[y][z] + ((b[y + 1][z] - b[y][z]) * Faktor)); thisCanvas.Pen.Color := RGB(a[0], a[1], a[2]); thisCanvas.MoveTo(thisRect.Left , thisRect.Top + Stelle); thisCanvas.LineTo(thisRect.Right, thisRect.Top + Stelle); end; end; b := nil; thisCanvas.Pen.Width := merkw; thisCanvas.Pen.Style := merks; thisCanvas.Pen.Color := merkp; end end; end; procedure THbtXpButton.SetMouseInColor(const Value: TColor); begin FMouseInColor := Value; end; procedure THbtXpButton.SetPushHighLightColor(const Value: Tcolor); begin FPushHighLightColor := Value; end; procedure THbtXpButton.SetStyle(const Value: TXpStyle); begin FStyle := Value; end; procedure THbtXpButton.SetVersion(Value: String); begin // do nothing; end; procedure THbtXpButton.WndProc(var Message: TMessage); var SaveIndex : Integer; begin Inherited; if (Message.Msg = CM_MOUSELEAVE) then begin invalidate; end; if (Message.Msg = CM_MOUSEENTER) then begin invalidate; end; end; Procedure Register ; begin RegisterComponents('Hbt xp pack',[THbtXpButton]); end; end.