1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476:
| unit ClButton;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, StdCtrls, Buttons, ExtCtrls;
type TGradientOrientation = (goVertical, goHorizontal);
TPixelRec = packed record case Boolean of true: (Color: TColor); false: (r, g, b, Reserved: Byte); end;
TClButton = class(TButton) private FCanvas: TCanvas; IsFocused: Boolean; FBackColor: TColor; FForeColor: TColor; FHoverColor: TColor; FBackColor2: TColor; FHoverColor2: TColor; FClickColor,FClickColor2: TColor; FPic,Fpic2,FPic3:TPicture; FRoundH,FRoundW,FGradientFact,FGradientFact2,FGradientFact3:Integer; FHover:Boolean; procedure SetBackColor(const Value: TColor); procedure SetForeColor(const Value: TColor); procedure SetHoverColor(const Value: TColor); procedure SetBackColor2(const Value: TColor); procedure SetHoverColor2(const Value: TColor); procedure SetRoundW(const Value: Integer); procedure SetRoundH(const Value: Integer); procedure SetGradientFact(const Value: Integer); procedure SetGradientFact2(const Value: Integer); procedure SetGradientFact3(const Value: Integer); procedure SetPicture(const Value: TPicture); procedure SetPictureHover(const Value: TPicture); procedure SetPictureDown(const Value: TPicture); procedure SetClickColor(const Value: TColor); procedure SetClickColor2(const Value: TColor); procedure DrawPicture(Pic:TPicture);
protected procedure CreateParams(var Params: TCreateParams); override; procedure WndProc(var Message: TMessage); override;
procedure SetButtonStyle(Value: Boolean); override; procedure DrawButton(Rect: TRect; State: UINT);
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 CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM; public constructor Create(AOwner: TComponent); override; destructor Destroy; override;
published property BackColor: TColor read FBackColor write SetBackColor default clBtnFace; property ForeColor: TColor read FForeColor write SetForeColor default clBtnText; property HoverColor: TColor read FHoverColor write SetHoverColor default clBtnFace; property BackColor2: TColor read FBackColor2 write SetBackColor2 default clBtnFace; property HoverColor2: TColor read FHoverColor2 write SetHoverColor2 default clBtnFace; property ClickColor: TColor read FClickColor write SetClickColor default clBtnFace; property ClickColor2: TColor read FClickColor2 write SetClickColor2 default clBtnFace; property RoundH: Integer read FRoundH write SetRoundH default 10; property RoundW: Integer read FRoundW write SetRoundW default 10; property GradientFact: Integer read FGradientFact write SetGradientFact default 0; property GradientFactHover: Integer read FGradientFact2 write SetGradientFact2 default 0; property GradientFactDown: Integer read FGradientFact3 write SetGradientFact3 default 0; property Picture: TPicture read FPic write SetPicture; property PictureHover: TPicture read FPic2 write SetPictureHover; property PictureDown: TPicture read FPic3 write SetPictureDown;
end;
procedure Register;
implementation
procedure DrawGradient(const Canvas: TCanvas; Color1, Color2: TColor; ARect: TRect; GradientOrientation: TGradientOrientation); var c1, c2, c: TPixelRec; x, y: Integer; OldPenWidth: Integer; OldPenStyle: TPenStyle;begin c1.Color := ColorToRGB(Color1); c2.Color := ColorToRGB(Color2); OldPenWidth := Canvas.Pen.Width; OldPenStyle := Canvas.Pen.Style; Canvas.Pen.Width:=1; Canvas.Pen.Style:=psInsideFrame;
case GradientOrientation of goVertical: begin for y := 0 to ARect.Bottom - ARect.Top do begin c.r := Round(c1.r + (c2.r - c1.r) * y / (ARect.Bottom - ARect.Top)); c.g := Round(c1.g + (c2.g - c1.g) * y / (ARect.Bottom - ARect.Top)); c.b := Round(c1.b + (c2.b - c1.b) * y / (ARect.Bottom - ARect.Top)); Canvas.Brush.Color := c.Color; Canvas.FillRect(Classes.Rect(ARect.Left, ARect.Top + y,ARect.Right, ARect.Top + y + 1)); end; end; goHorizontal: begin for x := 0 to ARect.Right - ARect.Left do begin c.r := Round(c1.r + (c2.r - c1.r) * x / (ARect.Right - ARect.Left)); c.g := Round(c1.g + (c2.g - c1.g) * x / (ARect.Right - ARect.Left)); c.b := Round(c1.b + (c2.b - c1.b) * x / (ARect.Right - ARect.Left)); Canvas.Brush.Color := c.Color; Canvas.FillRect(Rect(ARect.Left + x, ARect.Top,ARect.Left + x + 1, ARect.Bottom)); end; end; end; Canvas.Pen.Width := OldPenWidth; Canvas.Pen.Style := OldPenStyle; end;
constructor TClButton.Create(AOwner: TComponent); begin inherited Create(AOwner); FCanvas := TCanvas.Create; BackColor := clBtnFace; BackColor2 := clBtnFace; ForeColor := clBtnText; HoverColor := clBtnFace; HoverColor2 := clBtnFace; FClickColor := clBtnFace; FClickColor2 := clBtnFace; FRoundH:=10; FRoundW:=10; FHover:=false; FPic:=TPicture.Create; FPic2:=TPicture.Create; FPic3:=TPicture.Create; end;
destructor TClButton.Destroy; begin FCanvas.Free; FPic.Free; FPic2.Free; FPic3.Free; inherited Destroy; end;
procedure TClButton.WndProc(var Message: TMessage); begin if (Message.Msg = CM_MOUSELEAVE) then begin FHover:=false; Invalidate; end else if (Message.Msg = CM_MOUSEENTER) then begin FHover:=true; Invalidate; end; inherited; end;
procedure TClButton.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); with Params do Style := Style or BS_OWNERDRAW; end;
procedure TClButton.SetButtonStyle(Value: Boolean); begin if Value <> IsFocused then begin IsFocused := Value; Invalidate; end; end;
procedure TClButton.CNMeasureItem(var Message: TWMMeasureItem); begin with Message.MeasureItemStruct^ do begin itemWidth := Width; itemHeight := Height; end; end;
procedure TClButton.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 TClButton.CMEnabledChanged(var Message: TMessage); begin inherited; Invalidate; end;
procedure TClButton.CMFontChanged(var Message: TMessage); begin inherited; Invalidate; end;
procedure TClButton.SetBackColor(const Value: TColor); begin if FBackColor <> Value then begin FBackColor := Value; Invalidate; end; end;
procedure TClButton.SetBackColor2(const Value: TColor); begin if FBackColor2 <> Value then begin FBackColor2 := Value; Invalidate; end; end;
procedure TClButton.SetForeColor(const Value: TColor); begin if FForeColor <> Value then begin FForeColor := Value; Invalidate; end; end;
procedure TClButton.SetHoverColor(const Value: TColor); begin if FHoverColor <> Value then begin FHoverColor := Value; Invalidate; end; end;
procedure TClButton.SetHoverColor2(const Value: TColor); begin if FHoverColor2 <> Value then begin FHoverColor2 := Value; Invalidate; end; end;
procedure TClButton.SetClickColor(const Value: TColor); begin if FClickColor <> Value then begin FClickColor := Value; Invalidate; end; end;
procedure TClButton.SetClickColor2(const Value: TColor); begin if FClickColor2 <> Value then begin FClickColor2 := Value; Invalidate; end; end;
procedure TClButton.SetRoundW(const Value: Integer); begin if FRoundW <> Value then begin FRoundW := Value; Invalidate; end; end;
procedure TClButton.SetRoundH(const Value: Integer); begin if FRoundH <> Value then begin FRoundH := Value; Invalidate; end; end;
procedure TClButton.SetGradientFact(const Value: Integer); begin if FGradientFact <> Value then begin FGradientFact := Value; Invalidate; end; end;
procedure TClButton.SetGradientFact2(const Value: Integer); begin if FGradientFact2 <> Value then begin FGradientFact2 := Value; Invalidate; end; end;
procedure TClButton.SetGradientFact3(const Value: Integer); begin if FGradientFact3 <> Value then begin FGradientFact3 := Value; Invalidate; end; end;
procedure TClButton.DrawButton(Rect: TRect; State: UINT); var Flags, OldMode: Longint; IsDown, IsDefault, IsDisabled: Boolean; OldColor: TColor; OrgRect: TRect; rgn: HRGN; tmp:Integer; aktColor,aktColor2:TColor; aktGradient:Integer; tmpImage:TPicture; begin OrgRect := Rect;
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;
if IsFocused or IsDefault then begin FCanvas.Pen.Color := clWindowFrame; FCanvas.Pen.Width := 1; FCanvas.Brush.Style := bsClear; FCanvas.Rectangle(Rect.Left, Rect.Top, Rect.Right, Rect.Bottom); InflateRect(Rect, - 1, - 1); end;
if IsDown then begin FCanvas.Pen.Color := clBtnShadow; FCanvas.Pen.Width := 1; FCanvas.Brush.Color := clBtnFace; FCanvas.Rectangle(Rect.Left, Rect.Top, Rect.Right, Rect.Bottom); InflateRect(Rect, - 1, - 1); end else begin end;
if IsFocused then OffsetRect(Rect, 1, 1); OldColor := FCanvas.Brush.Color;
If(IsDown) then begin aktColor := FClickColor; aktColor2 := FClickColor2; aktGradient:=FGradientFact3; tmpImage:=FPic3; end else if(FHover) then begin aktColor := HoverColor; aktColor2 := HoverColor2; aktGradient:=FGradientFact2; tmpImage:=FPic2; end else begin aktColor := FBackColor; aktColor2 := FBackColor2; aktGradient:=FGradientFact; tmpImage:=FPic; end; if(tmpImage.Graphic<>nil) then DrawPicture(tmpImage) else begin If(aktGradient>Rect.Bottom-Rect.Top) then aktGradient:=0; if(aktGradient>0) then begin tmp:=Rect.Bottom; Rect.Bottom:=Rect.Top+aktGradient; DrawGradient(FCanvas,aktColor,aktColor2,Rect,goVertical); Rect.Bottom:=tmp; tmp:=Rect.Top; Rect.Top:=Rect.Top+aktGradient; DrawGradient(FCanvas,aktColor2,aktColor,Rect,goVertical); Rect.Top:=tmp; end else DrawGradient(FCanvas,aktColor,aktColor2,Rect,goVertical); end; FCanvas.Brush.Color := OldColor; OldMode := SetBkMode(FCanvas.Handle, TRANSPARENT); FCanvas.Font.Color := ForeColor; If(IsDown) then begin Rect.Top:=Rect.Top+2; Rect.Left:=Rect.Left+2; end; DrawText(FCanvas.Handle, PChar(Caption), -1, Rect, DT_SINGLELINE or DT_CENTER or DT_VCENTER);
SetBkMode(FCanvas.Handle, OldMode);
if IsFocused and IsDefault then begin Font.Style := Font.Style + [fsBold]; end else begin Font.Style := Font.Style - [fsBold]; end;
rgn := CreateRoundRectRgn(3,3,Width - 2,Height - 2,FRoundW,FRoundH); SetWindowRgn(Handle, rgn, True); end;
procedure TClButton.DrawPicture(Pic:TPicture); begin if (Pic <> nil) then FCanvas.Draw((Width - Pic.Width) div 2,(Height - Pic.Height) div 2 ,Pic.Graphic); end;
procedure TClButton.SetPicture(const Value: TPicture); begin if (FPic<> Value) then begin FPic.Assign(Value); Invalidate; end; end;
procedure TClButton.SetPictureHover(const Value: TPicture); begin if (FPic2<> Value) then begin FPic2.Assign(Value); Invalidate; end; end;
procedure TClButton.SetPictureDown(const Value: TPicture); begin if (FPic3<> Value) then begin FPic3.Assign(Value); Invalidate; end; end;
procedure Register; begin RegisterComponents('Beispiele', [TClButton]); end;
end. |