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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
|
//------------------------------------------------------------------------------
(* SoLuTions is an Versatile Library for Delphi -
* -
* Version originale publiée par Rodrigo sur "The Road to Delphi a Blog about programming"
* Post : "Fixing a VCL Style bug in the TButton component" -
* Post URL = "https://theroadtodelphi.wordpress.com/2012/02/26/fixing-a-vcl-style-bug-in-the-tbutton-component/"
* Another Source = "http://code.google.com/p/vcl-styles-utils/source/browse/trunk/Common/Vcl.Styles.Fixes.pas"
* -
* Copyright ou © ou Copr. "SLT Solutions", (2006) -
* contributeur : ShaiLeTroll (2013) - Reprise dans la SLT<2012> sous C++Builder du Patch TButtonStyleHookFix écrit par Rodrigo Ruz V. & Leonardo Cechet (2012) - "http://qc.embarcadero.com/wc/qcmain.aspx?d=103708"
* contributeur : ShaiLeTroll (2014) - Traduction en Delphi XE2 dans la SLT<2013> de TSLTButtonStyleHookFix et simuation du StyleElements de C++Builder XE3 n'existant pas encore en Delphi XE2
* contributeur : ShaiLeTroll (2015) - Suppression du thème d'une TDBCheckBox placé dans un TDBCtrlGrid
* -
* -
* Ce logiciel est un programme informatique servant à aider les développeurs -
* Delphi avec une bibliothèque polyvalente, adaptable et fragmentable. -
* -
* Ce logiciel est régi par la licence CeCILL-C soumise au droit français et -
* respectant les principes de diffusion des logiciels libres. Vous pouvez -
* utiliser, modifier et/ou redistribuer ce programme sous les conditions -
* de la licence CeCILL-C telle que diffusée par le CEA, le CNRS et l'INRIA -
* sur le site "http://www.cecill.info". -
* -
* En contrepartie de l'accessibilité au code source et des droits de copie, -
* de modification et de redistribution accordés par cette licence, il n'est -
* offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, -
* seule une responsabilité restreinte pèse sur l'auteur du programme, le -
* titulaire des droits patrimoniaux et les concédants successifs. -
* -
* A cet égard l'attention de l'utilisateur est attirée sur les risques -
* associés au chargement, à l'utilisation, à la modification et/ou au -
* développement et à la reproduction du logiciel par l'utilisateur étant -
* donné sa spécificité de logiciel libre, qui peut le rendre complexe à -
* manipuler et qui le réserve donc à des développeurs et des professionnels -
* avertis possédant des connaissances informatiques approfondies. Les -
* utilisateurs sont donc invités à charger et tester l'adéquation du -
* logiciel à leurs besoins dans des conditions permettant d'assurer la -
* sécurité de leurs systèmes et ou de leurs données et, plus généralement, -
* à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. -
* -
* Le fait que vous puissiez accéder à cet en-tête signifie que vous avez -
* pris connaissance de la licence CeCILL-C, et que vous en avez accepté les -
* termes. -
* -
*----------------------------------------------------------------------------*)
unit SLT.Controls.VCL.StdCtrlsEx;
interface
uses System.Types, System.Classes, System.UITypes,
Vcl.StdCtrls, Vcl.Graphics, Vcl.Themes, Vcl.Styles, Vcl.Controls,
Vcl.DBCtrls, Vcl.DBCGrids,
Winapi.Windows, Winapi.CommCtrl, Winapi.UxTheme, Winapi.Messages,
SLT.Common.SystemEx;
{$IF CompilerVersion = CompilerVersionXE2}
type
//to avoid writting a lot of extra code we are to use TButtonStyleHook class and override the paint method
/// <seealso href="https://theroadtodelphi.wordpress.com/2012/02/26/fixing-a-vcl-style-bug-in-the-tbutton-component/">Rodrigo : Fixing a VCL Style bug in the TButton component</seealso>
TSLTButtonStyleHookFix = class(TButtonStyleHook)
protected
procedure Paint(Canvas: TCanvas); override;
end;
{$IFEND}
{$IF CompilerVersion = CompilerVersionXE2}
type
/// <seealso href="http://qc.embarcadero.com/wc/qcmain.aspx?d=102590">Embarcadero Quality Central : No wordwrap for TCheckBox </seealso>
TSLTCheckBoxStyleHookFix = class(TCheckBoxStyleHook)
private
function InDBCtrlGrid(): Boolean;
protected
procedure Paint(Canvas: TCanvas); override;
public
constructor Create(AControl: TWinControl); override;
end;
{$IFEND}
{$IF CompilerVersion = CompilerVersionXE2}
type
/// <seealso href="http://qc.embarcadero.com/wc/qcmain.aspx?d=100645">Embarcadero Quality Central : Style does not honor specific color of a control (TEdit, TMemo etc) </seealso>
/// <seealso href="http://qc.embarcadero.com/wc/qcmain.aspx?d=101822">Embarcadero Quality Central : VCL Style is active, the edit.color does not change </seealso>
/// <seealso href="http://docwiki.embarcadero.com/Libraries/fr/Vcl.StdCtrls.TEdit.StyleElements">Embarcadero Wiki : Vcl.StdCtrls.TEdit.StyleElements</seealso>
TSLTEditStyleHookFix = class(TEditStyleHook)
private
procedure UpdateColors;
protected
procedure WndProc(var Message: TMessage); override;
public
constructor Create(AControl: TWinControl); override;
end;
{$IFEND}
{$IF CompilerVersion = CompilerVersionXE2}
type
TSLTMemoStyleHookFix = class(TMemoStyleHook)
private
procedure UpdateColors;
protected
procedure WndProc(var Message: TMessage); override;
public
constructor Create(AControl: TWinControl); override;
end;
{$IFEND}
{$IF CompilerVersion = CompilerVersionXE2}
type
TSLTStaticTextStyleHookFix = class(TStaticTextStyleHook)
strict protected
procedure Paint(Canvas: TCanvas); override;
end;
{$IFEND}
{$IF CompilerVersion = CompilerVersionXE2}
type
TSLTComboBoxStyleHookFix = class(TComboBoxStyleHook)
private
procedure UpdateColorsFixed();
strict protected
procedure WndProc(var Message: TMessage); override;
public
constructor Create(AControl: TWinControl); override;
end;
{$IFEND}
implementation
uses SLT.Controls.VCL.GraphicsEx;
{$IF CompilerVersion = CompilerVersionXE2}
type
// we need this hack to access some protectd fields
TCustomButtonHack = class(TCustomButton);
// we need this hack to access some protectd fields
TControlHack = class(TControl);
// we need this hack to access some protectd fields
TWinControlHack = class(TWinControl);
// we need this hack helper to access some strict private fields
TButtonStyleHookHackHelper = class Helper for TButtonStyleHook
protected
function Pressed : Boolean;
function DropDown: Boolean;
end;
// we need this hack to access some protectd fields
TCustomStaticTextHack = class(TCustomStaticText);
{$IFEND}
{$IF CompilerVersion = CompilerVersionXE2}
type
// we need this hack helper to access some strict private fields
TCheckBoxStyleHookHackHelper = class Helper for TCheckBoxStyleHook
protected
function IsRightAlignment(): Boolean;
end;
{$IFEND}
{ TSLTButtonStyleHookFix }
{$IF CompilerVersion = CompilerVersionXE2}
//------------------------------------------------------------------------------
procedure TSLTButtonStyleHookFix.Paint(Canvas: TCanvas);
// SLT - Constantes reprises de TCustomButton.UpdateImageList et BCM_SETIMAGELIST - enum PUSHBUTTONSTATES (vssym32.h)
const
PBS_NORMAL = Winapi.UxTheme.PBS_NORMAL - 1; // ImageIndex
PBS_HOT = Winapi.UxTheme.PBS_HOT - 1; // HotImageIndex
PBS_PRESSED = Winapi.UxTheme.PBS_PRESSED - 1; // PressedImageIndex
PBS_DISABLED = Winapi.UxTheme.PBS_DISABLED - 1; // DisabledImageIndex
PBS_DEFAULTED = Winapi.UxTheme.PBS_DEFAULTED - 1; // SelectedImageIndex
var
LDetails: TThemedElementDetails;
DrawRect, TextRect: TRect;
pbuttonImagelist: Winapi.CommCtrl.BUTTON_IMAGELIST;
IW, IH, IX, IY: Integer;
LTextFormatFlags: TTextFormatFlags;
ThemeTextColor: TColor;
Buffer: string;
BufferLength: Integer;
SaveIndex: Integer;
X, Y, I: Integer;
BCaption: String;
LImageIndex: Integer;
TextFlags: Cardinal;
begin
if StyleServices.Available then
begin
BCaption := Text;
if Pressed then
begin
LDetails := StyleServices.GetElementDetails(tbPushButtonPressed);
LImageIndex := PBS_PRESSED;
end
else if MouseInControl then
begin
LDetails := StyleServices.GetElementDetails(tbPushButtonHot);
LImageIndex := PBS_HOT;
end
// SLT, 2014-07-01 : Bug dans TButtonStyleHook.Paint, un bouton ne peut pas être focusé ET désactivé (cas d'une désactivation du bouton durant le OnClick du bouton)
else if Focused and Control.Enabled then
begin
LDetails := StyleServices.GetElementDetails(tbPushButtonDefaulted);
LImageIndex := PBS_DEFAULTED;
end
else if Control.Enabled then
begin
LDetails := StyleServices.GetElementDetails(tbPushButtonNormal);
LImageIndex := PBS_NORMAL;
end
else
begin
LDetails := StyleServices.GetElementDetails(tbPushButtonDisabled);
LImageIndex := PBS_DISABLED;
end;
DrawRect := Control.ClientRect;
StyleServices.DrawElement(Canvas.Handle, LDetails, DrawRect);
if Button_GetImageList(Handle, pbuttonImagelist) and
(pbuttonImagelist.himl <> 0) and
ImageList_GetIconSize(pbuttonImagelist.himl, IW, IH) then
begin
// SLT, gestion du ImageMargins (au moins le décalage de l'image)
if (GetWindowLong(Handle, GWL_STYLE) and BS_COMMANDLINK) = BS_COMMANDLINK
then
IY := DrawRect.Top + 15
else
IY := DrawRect.Top + (DrawRect.Height - IH) div 2; // Image centré verticalement !
// SLT, si il n'y a pas l'image demandé (par exemple en cas du ElevationRequired)
if LImageIndex >= ImageList_GetImageCount(pbuttonImagelist.himl) then
LImageIndex := PBS_NORMAL;
// SLT : A partir d'ici, code spécifique gérant ImageAlignment et ImageMargins
// here the image is drawn properly according to the ImageAlignment value
case TCustomButton(Control).ImageAlignment of
TImageAlignment.iaLeft:
begin
IX := DrawRect.Left + TCustomButton(Control).ImageMargins.Left;
IY := IY + TCustomButton(Control).ImageMargins.Top;
ImageList_Draw(pbuttonImagelist.himl, LImageIndex, Canvas.Handle,
IX + 3, IY, ILD_NORMAL);
Inc(DrawRect.Left, IW + 3 + TCustomButton(Control).ImageMargins.Left + TCustomButton(Control).ImageMargins.Right);
end;
TImageAlignment.iaRight:
begin
IX := DrawRect.Right - TCustomButton(Control).ImageMargins.Right;
IY := IY + TCustomButton(Control).ImageMargins.Top;
ImageList_Draw(pbuttonImagelist.himl, LImageIndex, Canvas.Handle,
IX - IW - 3, IY, ILD_NORMAL);
Dec(DrawRect.Right, IW + 3 + TCustomButton(Control).ImageMargins.Left + TCustomButton(Control).ImageMargins.Right);
end;
TImageAlignment.iaCenter:
begin
IX := DrawRect.Left + (DrawRect.Width - IW) div 2; // Image centré horizontalement !
IX := IX + (TCustomButton(Control).ImageMargins.Left - TCustomButton(Control).ImageMargins.Right) div 2;
IY := IY + (TCustomButton(Control).ImageMargins.Top - TCustomButton(Control).ImageMargins.Bottom) div 2;
ImageList_Draw(pbuttonImagelist.himl, LImageIndex, Canvas.Handle,
IX, IY, ILD_NORMAL);
end;
TImageAlignment.iaTop:
begin
IX := DrawRect.Left + (DrawRect.Width - IW) div 2; // Image centré horizontalement !
IY := DrawRect.Top + TCustomButton(Control).ImageMargins.Top;
ImageList_Draw(pbuttonImagelist.himl, LImageIndex, Canvas.Handle,
IX, IY + 3, ILD_NORMAL);
Inc(DrawRect.Top, IH + 3 + TCustomButton(Control).ImageMargins.Top + TCustomButton(Control).ImageMargins.Bottom);
end;
TImageAlignment.iaBottom:
begin
IX := DrawRect.Left + (DrawRect.Width - IW) div 2; // Image centré horizontalement !
IY := DrawRect.Bottom - TCustomButton(Control).ImageMargins.Bottom;
ImageList_Draw(pbuttonImagelist.himl, LImageIndex, Canvas.Handle,
IX, IY - IH - 3, ILD_NORMAL);
Dec(DrawRect.Bottom, IH + 3 + TCustomButton(Control).ImageMargins.Top + TCustomButton(Control).ImageMargins.Bottom);
end;
end;
end;
// SLT : A partir d'ici, le code est une copie de TButtonStyleHook.Paint
if (GetWindowLong(Handle, GWL_STYLE) and BS_COMMANDLINK) = BS_COMMANDLINK then
begin
if pbuttonImagelist.himl = 0 then
Inc(DrawRect.Left, 35);
Inc(DrawRect.Top, 15);
Inc(DrawRect.Left, 5);
Canvas.Font := TCustomButtonHack(Control).Font;
Canvas.Font.Style := [];
Canvas.Font.Size := 12;
LTextFormatFlags := TTextFormatFlags(DT_LEFT);
if StyleServices.GetElementColor(LDetails, ecTextColor, ThemeTextColor) then
Canvas.Font.Color := ThemeTextColor;
StyleServices.DrawText(Canvas.Handle, LDetails, BCaption, DrawRect,
LTextFormatFlags, Canvas.Font.Color);
SetLength(Buffer, Button_GetNoteLength(Handle) + 1);
if Length(Buffer) <> 0 then
begin
BufferLength := Length(Buffer);
if Button_GetNote(Handle, PChar(Buffer), BufferLength) then
begin
LTextFormatFlags := TTextFormatFlags(DT_LEFT or DT_WORDBREAK);
Inc(DrawRect.Top, Canvas.TextHeight('Wq') + 2);
Canvas.Font.Size := 8;
StyleServices.DrawText(Canvas.Handle, LDetails, Buffer, DrawRect,
LTextFormatFlags, Canvas.Font.Color);
end;
end;
if pbuttonImagelist.himl = 0 then
begin
if Pressed then
LDetails := StyleServices.GetElementDetails(tbCommandLinkGlyphPressed)
else if MouseInControl then
LDetails := StyleServices.GetElementDetails(tbCommandLinkGlyphHot)
else if Control.Enabled then
LDetails := StyleServices.GetElementDetails(tbCommandLinkGlyphNormal)
else
LDetails := StyleServices.GetElementDetails(tbCommandLinkGlyphDisabled);
DrawRect.Right := 35;
DrawRect.Left := 3;
DrawRect.Top := 10;
DrawRect.Bottom := DrawRect.Top + 32;
StyleServices.DrawElement(Canvas.Handle, LDetails, DrawRect);
end;
end
else if (GetWindowLong(Handle, GWL_STYLE) and BS_SPLITBUTTON) = BS_SPLITBUTTON then
begin
Dec(DrawRect.Right, 15);
DrawControlText(Canvas, LDetails, Text, DrawRect, DT_VCENTER or
DT_CENTER);
if DropDown then
begin
LDetails := StyleServices.GetElementDetails(tbPushButtonPressed);
SaveIndex := SaveDC(Canvas.Handle);
try
IntersectClipRect(Canvas.Handle, Control.Width - 15, 0, Control.Width,
Control.Height);
DrawRect := Rect(Control.Width - 30, 0, Control.Width,
Control.Height);
StyleServices.DrawElement(Canvas.Handle, LDetails, DrawRect);
finally
RestoreDC(Canvas.Handle, SaveIndex);
end;
end;
with Canvas do
begin
Pen.Color := StyleServices.GetSystemColor(clBtnShadow);
MoveTo(Control.Width - 15, 3);
LineTo(Control.Width - 15, Control.Height - 3);
if Control.Enabled then
Pen.Color := StyleServices.GetSystemColor(clBtnHighLight)
else
Pen.Color := Font.Color;
MoveTo(Control.Width - 14, 3);
LineTo(Control.Width - 14, Control.Height - 3);
Pen.Color := Font.Color;
X := Control.Width - 8;
Y := Control.Height div 2 + 1;
for I := 3 downto 0 do
begin
MoveTo(X - I, Y - I);
LineTo(X + I + 1, Y - I);
end;
end;
end
else
begin
// finally the text is aligned and drawn depending of the value of the ImageAlignment property
case TCustomButton(Control).ImageAlignment of
TImageAlignment.iaLeft,
TImageAlignment.iaRight,
TImageAlignment.iaCenter:
TextFlags := DT_VCENTER or DT_CENTER;
TImageAlignment.iaBottom:
TextFlags := DT_TOP or DT_CENTER;
TImageAlignment.iaTop:
TextFlags := DT_BOTTOM or DT_CENTER;
else
TextFlags := 0;
end;
// SLT, gestion du ImageAlignment et du WordWrap
// Il semble que si l'on ne mets pas DT_WORDBREAK cela considère cela comme si il y avait DT_SINGLELINE
// DT_VCENTER Centers text vertically. This value is used only with the DT_SINGLELINE value.
// DT_BOTTOM Justifies the text to the bottom of the rectangle. This value is used only with the DT_SINGLELINE value.
if (Control is TCustomButton) and TCustomButtonHack(Control).WordWrap then
begin
TextFlags := TextFlags or DT_WORDBREAK;
InflateRect(DrawRect, -1, -1); // Evite que cela empiète de trop sur les bordures
end;
// Le centrage horizontal du texte géré par les styles est vilain en mode WordWrap
// TseCustomStyle.DrawStyleText fait un centrage de l'image texte obtenue par le WordWrap mais le texte lui même reste aligné à gauche puis il centre le texte dans le bouton
// le DrawText suivant est inspiré du TseCustomStyle.DrawStyleText mais il fait correctement le centrage du texte puis centre le texte dans le bouton
if TextFlags and DT_WORDBREAK = DT_WORDBREAK then
begin
Canvas.Font := TControlHack(Control).Font;
TextRect := DrawRect;
Winapi.Windows.DrawText(Canvas.Handle, PChar(BCaption), Length(BCaption), TextRect, TextFlags or DT_CALCRECT);
TextRect := CenteredRect(DrawRect, TextRect);
Winapi.Windows.SetBkMode(Canvas.Handle, TRANSPARENT);
Winapi.Windows.DrawText(Canvas.Handle, PChar(BCaption), Length(BCaption), TextRect, TextFlags);
end
else
DrawControlText(Canvas, LDetails, BCaption, DrawRect, TextFlags);
end;
end;
end;
{$IFEND}
{ TSLTCheckBoxStyleHookFix }
{$IF CompilerVersion = CompilerVersionXE2}
//------------------------------------------------------------------------------
constructor TSLTCheckBoxStyleHookFix.Create(AControl: TWinControl);
begin
inherited Create(AControl);
// En XE2, le TDBCtrlGrid n'affiche pas correctement la case à cocher,
// Passer OverridePaint à False permet d'inhiber le thème avec son dessin spécifique laissant ici la responsable du dessin au contrôle.
// En XE3, il suffirait de modifier la propriété StyleElements !!!
OverridePaint := not InDBCtrlGrid();
end;
//------------------------------------------------------------------------------
procedure TSLTCheckBoxStyleHookFix.Paint(Canvas: TCanvas);
//--------------------------------------------------------------------------
function RectVCenter(var R: TRect; Bounds: TRect): TRect;
begin
OffsetRect(R, -R.Left, -R.Top);
OffsetRect(R, 0, (Bounds.Height - R.Height) div 2);
OffsetRect(R, Bounds.Left, Bounds.Top);
Result := R;
end;
var
State: TCheckBoxState;
Details: TThemedElementDetails;
R: TRect;
Spacing: Integer;
BoxSize: TPoint;
LCaption: string;
DrawTextFlags: UINT;
begin
// SLT : le code est une copie de TCheckBoxStyleHook.Paint de Vcl.StdCtrls avec une correction des DrawText !
if StyleServices.Available and not InDBCtrlGrid() then
begin
State := TCheckBoxState(SendMessage(Handle, BM_GETCHECK, 0, 0));
Details := StyleServices.GetElementDetails(GetDrawState(State));
Spacing := 5;
R := Control.ClientRect;
BoxSize := Point(13, 13);
// SLT : IsRightAlignment est Hack par helper
if not IsRightAlignment() then
begin
R := Rect(0, 0, BoxSize.X, BoxSize.Y);
RectVCenter(R, Rect(0, 0, Control.Width, Control.Height));
end
else
begin
R := Rect(Control.Width - BoxSize.X - 1, 0, Control.Width, Control.Height);
RectVCenter(R, Rect(Control.Width - BoxSize.X - 1, 0, Control.Width, Control.Height));
end;
StyleServices.DrawElement(Canvas.Handle, Details, R);
// SLT : Hack par transtypage en TCheckBox
Canvas.Font := TCheckBox(Control).Font;
R := Rect(0, 0, Control.Width - BoxSize.X - 10, Control.Height);
LCaption := Text;
// SLT : ajout du WordWrap
DrawTextFlags := Control.DrawTextBiDiModeFlags(DT_CALCRECT or DT_EXPANDTABS);
// SLT : Hack par transtypage en TCheckBox
if TCheckBox(Control).WordWrap then
DrawTextFlags := DrawTextFlags or DT_WORDBREAK;
DrawText(Canvas.Handle, PWideChar(LCaption), Length(LCaption), R, DrawTextFlags);
// SLT : IsRightAlignment est Hack par helper
if not IsRightAlignment() then
RectVCenter(R, Rect(BoxSize.X + Spacing, 0, Control.Width, Control.Height))
else
begin
if Control.BiDiMode <> bdRightToLeft then
RectVCenter(R, Rect(3, 0,
Control.Width - BoxSize.X - Spacing, Control.Height))
else
RectVCenter(R, Rect(Control.Width - BoxSize.X - Spacing - R.Right, 0,
Control.Width - BoxSize.X - Spacing, Control.Height));
end;
// SLT : ajout du WordWrap
DrawTextFlags := Control.DrawTextBiDiModeFlags(DT_LEFT or DT_VCENTER or DT_EXPANDTABS);
// SLT : Hack par transtypage en TCheckBox
if TCheckBox(Control).WordWrap then
DrawTextFlags := DrawTextFlags or DT_WORDBREAK;
DrawControlText(Canvas, Details, LCaption, R, DrawTextFlags);
if Focused then
begin
InflateRect(R, 2, 1);
if R.Top < 0 then
R.Top := 0;
if R.Bottom > Control.Height then
R.Bottom := Control.Height;
Canvas.Brush.Color := StyleServices.GetSystemColor(clBtnFace);
Canvas.DrawFocusRect(R);
end;
end;
end;
//------------------------------------------------------------------------------
function TSLTCheckBoxStyleHookFix.InDBCtrlGrid(): Boolean;
var
Level: TWinControl;
begin
Result := False;
Level := Control.Parent;
while Assigned(Level) and not Result do
begin
Result := Level is TDBCtrlGrid;
Level := Level.Parent;
end;
end;
{$IFEND}
{ TButtonStyleHookHelper }
{$IF CompilerVersion = CompilerVersionXE2}
//------------------------------------------------------------------------------
function TButtonStyleHookHackHelper.DropDown: Boolean;
begin
Result := Self.FDropDown;
end;
//------------------------------------------------------------------------------
function TButtonStyleHookHackHelper.Pressed: Boolean;
begin
Result := Self.FPressed;
end;
{$IFEND}
{$IF CompilerVersion = CompilerVersionXE2}
//------------------------------------------------------------------------------
function TCheckBoxStyleHookHackHelper.IsRightAlignment(): Boolean;
begin
Result := Self.RightAlignment;
end;
{$IFEND}
{ TSLTEditStyleHookFix }
{$IF CompilerVersion = CompilerVersionXE2}
//------------------------------------------------------------------------------
constructor TSLTEditStyleHookFix.Create(AControl: TWinControl);
begin
inherited;
// Vérifie les couleurs à utiliser
UpdateColors();
end;
//------------------------------------------------------------------------------
procedure TSLTEditStyleHookFix.UpdateColors();
begin
// Here you set the colors of the style hook
if Control.Enabled then
begin
// utilise la couleur du controle uniquement si ce n'est pas la couleur par défaut
// Il manque en XE2, la propriété StyleElements seClient
Brush.Color := TWinControlHack(Control).Color;
if Brush.Color = clWindow then
Brush.Color := StyleServices.GetStyleColor(scEdit);
// utilise la couleur de fonte du controle uniquement si ce n'est pas la couleur par défaut
// Il manque en XE2, la propriété StyleElements seFont
FontColor := TWinControlHack(Control).Font.Color;
if FontColor = clWindowText then
FontColor := StyleServices.GetStyleFontColor(sfEditBoxTextNormal);
// Cas particulier, le champ Edit de la même couleur que le fond neutre d'une fenêtre
if Brush.Color = clBtnFace then
begin
Brush.Color := StyleServices.GetStyleColor(scPanel);
FontColor := StyleServices.GetStyleFontColor(sfPanelTextNormal);
end;
end
else
begin
// Le thème est conservé si le champ est désactivé !
Brush.Color := StyleServices.GetStyleColor(scEditDisabled);
FontColor := StyleServices.GetStyleFontColor(sfEditBoxTextDisabled);
end;
end;
//------------------------------------------------------------------------------
procedure TSLTEditStyleHookFix.WndProc(var Message: TMessage);
begin
// Handle the messages of the control
case Message.Msg of
CN_CTLCOLORMSGBOX..CN_CTLCOLORSTATIC:
begin
// Vérifie les couleurs à utiliser
UpdateColors();
SetTextColor(Message.WParam, ColorToRGB(FontColor));
SetBkColor(Message.WParam, ColorToRGB(Brush.Color));
Message.Result := LRESULT(Brush.Handle);
Handled := True;
end;
CM_COLORCHANGED, CM_ENABLEDCHANGED:
begin
// Vérifie les couleurs à utiliser
UpdateColors();
Handled := False;
end
else
inherited WndProc(Message);
end;
end;
{$IFEND}
{ TSLTMemoStyleHookFix }
{$IF CompilerVersion = CompilerVersionXE2}
//------------------------------------------------------------------------------
constructor TSLTMemoStyleHookFix.Create(AControl: TWinControl);
begin
inherited;
// Vérifie les couleurs à utiliser
UpdateColors();
end;
//------------------------------------------------------------------------------
procedure TSLTMemoStyleHookFix.UpdateColors();
begin
// Here you set the colors of the style hook
if Control.Enabled then
begin
// utilise la couleur du controle uniquement si ce n'est pas la couleur par défaut
// Il manque en XE2, la propriété StyleElements seClient
Brush.Color := TWinControlHack(Control).Color;
if Brush.Color = clWindow then
Brush.Color := StyleServices.GetStyleColor(scEdit);
// utilise la couleur de fonte du controle uniquement si ce n'est pas la couleur par défaut
// Il manque en XE2, la propriété StyleElements seFont
FontColor := TWinControlHack(Control).Font.Color;
if FontColor = clWindowText then
FontColor := StyleServices.GetStyleFontColor(sfEditBoxTextNormal);
// Cas particulier, le champ Memo de la même couleur que le fond neutre d'une fenêtre
if Brush.Color = clBtnFace then
begin
Brush.Color := StyleServices.GetStyleColor(scPanel);
FontColor := StyleServices.GetStyleFontColor(sfPanelTextNormal);
end;
end
else
begin
// Le thème est conservé si le champ est désactivé !
Brush.Color := StyleServices.GetStyleColor(scEditDisabled);
FontColor := StyleServices.GetStyleFontColor(sfEditBoxTextDisabled);
end;
end;
//------------------------------------------------------------------------------
procedure TSLTMemoStyleHookFix.WndProc(var Message: TMessage);
begin
// Handle the messages of the control
case Message.Msg of
CN_CTLCOLORMSGBOX..CN_CTLCOLORSTATIC:
begin
// Vérifie les couleurs à utiliser
UpdateColors();
SetTextColor(Message.WParam, ColorToRGB(FontColor));
SetBkColor(Message.WParam, ColorToRGB(Brush.Color));
Message.Result := LRESULT(Brush.Handle);
Handled := True;
end;
CM_COLORCHANGED, CM_ENABLEDCHANGED:
begin
// Vérifie les couleurs à utiliser
UpdateColors();
Handled := False;
end
else
inherited WndProc(Message);
end;
end;
{$IFEND}
{ TSLTStaticTextStyleHookFix }
{$IF CompilerVersion = CompilerVersionXE2}
//------------------------------------------------------------------------------
procedure TSLTStaticTextStyleHookFix.Paint(Canvas: TCanvas);
procedure DrawControlText(Canvas: TCanvas; Details: TThemedElementDetails; const S: string; var R: TRect; Flags: Cardinal);
var
ThemeTextColor: TColor;
TextFormat: TTextFormatFlags;
vText: string;
vRect: TRect;
begin
Canvas.Font := TWinControlHack(Control).Font;
TextFormat := TTextFormatFlags(Flags);
// utilise la couleur de fonte du controle uniquement si ce n'est pas la couleur par défaut
// Il manque en XE2, la propriété StyleElements seFont
ThemeTextColor := TWinControlHack(Control).Font.Color;
if ThemeTextColor = clWindowText then
begin
if StyleServices.GetElementColor(Details, ecTextColor, ThemeTextColor) then
begin
Canvas.Font.Color := ThemeTextColor;
StyleServices.DrawText(Canvas.Handle, Details, S, R, TextFormat, ThemeTextColor);
end
else
begin
Canvas.Refresh();
StyleServices.DrawText(Canvas.Handle, Details, S, R, TextFormat);
end;
end
else
begin
vText := S;
vRect := R;
Canvas.TextRect(vRect, vText, TextFormat);
end;
end;
const
Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
States: array[Boolean] of TThemedTextLabel = (ttlTextLabelDisabled, ttlTextLabelNormal);
var
Details: TThemedElementDetails;
R: TRect;
begin
if StyleServices.Available then
begin
R := Control.ClientRect;
if TCustomStaticTextHack(Control).Transparent then
begin
// Etrange cela utilise l'élément CheckBox pour un TStaticText dans Vcl.StdCtrls !
Details := StyleServices.GetElementDetails(tbCheckBoxUncheckedNormal);
StyleServices.DrawParentBackground(Handle, Canvas.Handle, Details, False);
Canvas.Brush.Style := bsClear;
end
else
begin
// utilise la couleur du controle uniquement si ce n'est pas la couleur par défaut
// Il manque en XE2, la propriété StyleElements seClient
Canvas.Brush.Color := TWinControlHack(Control).Color;
if Canvas.Brush.Color = clBtnFace then
Canvas.Brush.Color := StyleServices.GetStyleColor(scWindow);
Canvas.FillRect(R);
end;
Details := StyleServices.GetElementDetails(States[Control.Enabled]);
DrawControlText(Canvas, Details, Text, R,
Alignments[TCustomStaticTextHack(Control).Alignment] or DT_WORDBREAK);
end;
end;
{$IFEND}
{ TSLTComboBoxStyleHookFix }
{$IF CompilerVersion = CompilerVersionXE2}
//------------------------------------------------------------------------------
constructor TSLTComboBoxStyleHookFix.Create(AControl: TWinControl);
begin
inherited Create(AControl);
UpdateColorsFixed();
end;
//------------------------------------------------------------------------------
procedure TSLTComboBoxStyleHookFix.UpdateColorsFixed();
const
ColorStates: array[Boolean] of TStyleColor = (scComboBoxDisabled, scComboBox);
FontColorStates: array[Boolean] of TStyleFont = (sfComboBoxItemDisabled, sfComboBoxItemNormal);
var
LStyle: TCustomStyleServices;
begin
if Control is TComboBox then
begin
if TComboBox(Control).Enabled and (TComboBox(Control).Style = csDropDown) and (TComboBox(Control).Color <> clWindow) then
begin
Brush.Color := TComboBox(Control).Color;
FontColor := TCanvasSLTAssistant.GetConstratedColor(Brush.Color); // Couleur de luminosité inverse : Contraste garanti pour les couleurs claires ou foncées
end
else
begin
LStyle := StyleServices;
Brush.Color := LStyle.GetStyleColor(ColorStates[Control.Enabled]);
FontColor := LStyle.GetStyleFontColor(FontColorStates[Control.Enabled]);
end;
end;
end;
//------------------------------------------------------------------------------
procedure TSLTComboBoxStyleHookFix.WndProc(var Message: TMessage);
const
States: array[Boolean] of TStyleColor = (scEditDisabled, scComboBox);
begin
case Message.Msg of
WM_CTLCOLORMSGBOX..WM_CTLCOLORSTATIC,
CN_CTLCOLORMSGBOX..CN_CTLCOLORSTATIC:
begin
if TComboBox(Control).Enabled and (TComboBox(Control).Style = csDropDown) and (TComboBox(Control).Color <> clWindow) then
begin
FontColor := TCanvasSLTAssistant.GetConstratedColor(TComboBox(Control).Color); // Couleur de luminosité inverse : Contraste garanti pour les couleurs claires ou foncées
SetTextColor(Message.WParam, ColorToRGB(FontColor));
// La couleur du fond du controle
Brush.Color := TComboBox(Control).Color;
// La couleur du fond de texte
SetBkColor(Message.WParam, ColorToRGB(TComboBox(Control).Color));
Message.Result := LRESULT(Brush.Handle);
Handled := True;
end
else
begin
SetTextColor(Message.WParam, ColorToRGB(FontColor));
// La couleur du fond du controle
Brush.Color := StyleServices.GetStyleColor(States[Control.Enabled]);
// La couleur du fond de texte
SetBkColor(Message.WParam, ColorToRGB(Brush.Color));
Message.Result := LRESULT(Brush.Handle);
Handled := True;
end;
end;
CM_ENABLEDCHANGED:
begin
UpdateColorsFixed();
Handled := False; // Allow control to handle message
end;
CM_COLORCHANGED, CM_FONTCHANGED:
begin
UpdateColorsFixed();
Invalidate();
Handled := True;
end;
else
inherited WndProc(Message);
end;
end;
{$IFEND}
{$IF CompilerVersion = CompilerVersionXE2}
initialization
TStyleManager.Engine.RegisterStyleHook(TButton, TSLTButtonStyleHookFix);
TStyleManager.Engine.RegisterStyleHook(TCheckBox, TSLTCheckBoxStyleHookFix);
TStyleManager.Engine.RegisterStyleHook(TDBCheckBox, TSLTCheckBoxStyleHookFix);
TStyleManager.Engine.RegisterStyleHook(TCustomEdit, TSLTEditStyleHookFix);
TStyleManager.Engine.RegisterStyleHook(TEdit, TSLTEditStyleHookFix);
TStyleManager.Engine.RegisterStyleHook(TCustomMemo, TSLTMemoStyleHookFix);
TStyleManager.Engine.RegisterStyleHook(TMemo, TSLTMemoStyleHookFix);
TStyleManager.Engine.RegisterStyleHook(TStaticText, TSLTStaticTextStyleHookFix);
TStyleManager.Engine.RegisterStyleHook(TComboBox, TSLTComboBoxStyleHookFix);
{$IFEND}
end. |
Partager