8000 Incorrect colors retrieved from a style in design mode · Issue #102 · digao-dalpiaz/DzHTMLText · GitHub
[go: up one dir, main page]

Skip to content

Incorrect colors retrieved from a style in design mode #102

Open

Description

@FranmoFR

In design mode, it still doesn't pick up the color from the style.

On the left, the colors are correctly picked (different component), on the right, the colors are picked incorrectly (TDzHTMLText 6.11)

Image

Activity

  1. added
    bug
    on Nov 26, 2025
  2. digao-dalpiaz commented on Dec 6, 2025

    @digao-dalpiaz
    Owner

    Please place here the HTML code of the text you showed in the image.

    Also enter which Application Theme you are using.

    Thanks.

  3. added and removed
    bug
    on Dec 6, 2025
  4. FranmoFR commented on Dec 6, 2025

    @FranmoFR
    Author
  5. digao-dalpiaz commented on Jan 18, 2026

    @digao-dalpiaz
    Owner

    Hello,

    I have checked this issue now.

    At design time, when trying to obtain the correct theme color, Delphi is not returning the color according to the selected Theme.

    I don't know how the other components are doing, but here, using the same Themes method, it doesn't return the color.

    In other words, for now, it only works at runtime.

  6. added
    wontfix
    and removed on Jan 18, 2026
  7. FranmoFR commented on Jan 19, 2026

    @FranmoFR
    Author

    You need to use StyleServices.

    Why doesn't clHighlight work in DesignTime?
    At designtime, clHighlight is simply a system constant. Standard components, such as TLabel, use internal VCL mechanisms that check if a style is enabled in the project options and use the appropriate "Style Hook".

    Solution: Use StyleServices

    In the component code, where the color is retrieved or used for drawing (usually in the Paint method or a helper function that retrieves the color), the following logic should be used:

    uses
      Vcl.Themes;
    
    function GetCorrectColor(AColor: TColor): TColor;
    var
      LDetails: TThemedElementDetails;
    begin
      Result := AColor;
    
    // Check if styles are enabled (works in both Runtime and DesignTime)
    if StyleServices.Enabled then
      begin
       // Get the color from the style for a specific system color
       if not StyleServices.GetElementColor(StyleServices.GetElementDetails(teWindow), AColor, Result) then
         begin
          // Optional: if GetElementColor doesn't support a specific color,
          // you can use dedicated methods for system colors
          Result := StyleServices.GetSystemColor(AColor);
         end;
      end;
    end;
    

    Steps:

    1. Color Retrieval Location: Locate the place in the TDzHTMLText code where it parses color tags or sets the default text/background color.
    2. DesignTime Condition: You can use the csDesigning property in ComponentState to force a refresh, but StyleServices.Enabled is usually sufficient in newer versions of Delphi (12.1).
    3. Paint Method: Make sure that the component's Paint method resolves colors just before drawing. If the component caches colors, add a mechanism to clear the cache when changing the style.

    Supporting Style Changes in the IDE

    For a component to react immediately when you change the style in Project Options, it should handle the CM_STYLECHANGED message:

    procedure TCMStyleChanged(var Message: TMessage); message CM_STYLECHANGED;
    
    // In the implementation:
    procedure TDzHTMLText.TCMStyleChanged(var Message: TMessage);
    begin
      inherited;
      Invalidate; // Forces a redraw of the component with the new style colors
    end;
    
    • StyleServices in Delphi 12.1 is aware of the styles selected in the project settings (even if the IDE itself looks different).
    • Using GetSystemColor maps constants like clHighlight to their equivalents in the currently selected .vsf file.
  8. added and removed
    wontfix
    on Jan 24, 2026
  9. digao-dalpiaz commented on Jan 24, 2026

    @digao-dalpiaz
    Owner

    The code you provided doesn't work.

    StyleServices.GetElementDetails does not exist.

    Also, StyleServices.GetSystemColor I'm already using, but it only works at runtime.

  10. FranmoFR commented on Jan 24, 2026

    @FranmoFR
    Author

    The following GetStyleColor function correctly retrieves the clHighlight color from the tmMenuItemSelected element
    when the style is disabled: $00D77800 (blue color)

    When the style is enabled, it retrieves a different color, meaning the function correctly returns the color of the active style.

    function GetStyleColor: TColor;
    var
     LColor: TColor;
    begin
     // Default color
     Result := ColorToRGB(clHighlight);
    
     if StyleServices.Enabled then
       begin
        // Method 1: Getting the system color directly from a style
        // This is most effective in Delphi 12.1 for clHighlight
        Result := StyleServices.GetSystemColor(clHighlight);
    
        // Method 2: If you need the color from a specific element (as in StyleControls)
        // Get the details for the selected menu item (classic "Highlight")
        var LDetails: TThemedElementDetails := StyleServices.GetElementDetails(tmMenuItemSelected);
    
        if StyleServices.GetElementColor(LDetails, ecFillColor, LColor) then Result := LColor;
       end;
    end;
    

    Below is the original function from the StyleControls component, where the color is taken from the style:

    procedure TscSysStyleHook.DrawControlText(Canvas: TCanvas;
      Details: TThemedElementDetails; const S: string; var R: TRect;
      Flags: Cardinal);
    var
      ThemeTextColor: TColor;
      TextFormat: TTextFormatFlags;
    begin
      Canvas.Font := SysControl.Font;
      TextFormat := TTextFormatFlags(Flags);
      if StyleServices.GetElementColor(Details, ecTextColor, ThemeTextColor) then
        begin
          Canvas.Font.Color := ThemeTextColor;
          StyleServices.DrawText(Canvas.Handle, Details, S, R, TextFormat,
            Canvas.Font.Color);
        end
      else
        begin
          Canvas.Refresh;
          StyleServices.DrawText(Canvas.Handle, Details, S, R, TextFormat);
        end;
    end;
    
  11. digao-dalpiaz commented on Jan 24, 2026

    @digao-dalpiaz
    Owner

    I tested all these ways and unfortunately it doesn't work. The correct color is only obtained at runtime!

    In designtime, you always end up getting the color according to the default theme.

  12. FranmoFR commented on Nov 28, 2026

    @FranmoFR
    Author

    FIXED!

    preview

    I can still reproduce this with DzHTMLText 6.13 in Delphi 12.2 using the attached TestHTML project and the Material Oxford Blue VCL style.

    In Delphi 12, StyleServices without an argument returns TStyleManager.ActiveStyle. The VCL form designer uses TStyleManager.ActiveDesigningStyle. Passing the control as StyleServices(Self) lets VCL select the designer's style when the control is in csDesigning, then resolve the existing system color through GetSystemColor.

    The patch updates TDzHTMLText.GetRealColor for text and CanvasProcess for the background of non-transparent controls. It keeps the seFont and seClient checks and the parameterless call on Delphi versions older than 10.4.

    I verified the original and patched Win32 runtime packages in a test that loads the actual TestHTML form and sets csDesigning. With Material Oxford Blue the patched package resolved both clWindowText and the custom HTML style's clHighlight to the form's design-time palette. Both builds returned the same colors in normal runtime tests.

    Rebuild DzHTMLText_VCL and DzHTMLTextDesign_VCL together when testing in the IDE. The color lookup lives in the runtime package used by the designer.

    diff --git a/Source/Vcl.DzHTMLText.pas b
    535E
    /Source/Vcl.DzHTMLText.pas
    --- a/Source/Vcl.DzHTMLText.pas
    +++ b/Source/Vcl.DzHTMLText.pas
    @@ -1267,7 +1267,7 @@
     begin
       {$IF Defined(DCC) and Defined(VCL)}
       if (Color<>clNone) and (seFont in StyleElements) then
    -    Result := StyleServices.GetSystemColor(Color)
    +    Result := StyleServices{$IF CompilerVersion >= 34}(Self){$IFEND}.GetSystemColor(Color)
       else
       {$ENDIF}
       Result := Color;
    @@ -1340,8 +1340,8 @@
           C.Brush.Color := GetColorresolvingParent
         else
         {$ELSE}
    -    if (seClient in StyleElements) {and not (csDesigning in ComponentState)} then
    -      C.Brush.Color := StyleServices.GetSystemColor(Color)
    +    if seClient in StyleElements then
    +      C.Brush.Color := StyleServices{$IF CompilerVersion >= 34}(Self){$IFEND}.GetSystemColor(Color)
         else
         {$ENDIF}
           C.Brush.Color := Color;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0