Get color of active window title bar in Delphi

This post shows you how to get the color of the title bar for the active window.  The method to get the color of the title bar of an active window in Delphi has changed over time.

In Windows 7 could get the color by using clActiveCaption but on WIndows 10 that no longer works but you can use the following code instead.

The title bar can have two colours fading from left to right.

I have provided function to get the left and right color and to determine if the right color is used or not

unit zzColorU;

interface

uses Vcl.Graphics;

function zzGetWindowTitleBarPrimaryColor : TColor;
  // return the primary color of the title bar in active windows

function zzGetWindowTitleBarSecondaryColor : TColor;
  // return the secondary (right hand side) gradiated color of the title bar in active windows

function zzGetWindowTitleBarIsGradiated : boolean;
  // return TRUE if the color of title bars for active windows is gradiated

implementation

uses WinApi.Windows;

// =================================

function zzGetWindowTitleBarPrimaryColor : TColor;
  // return the primary color of the title bar in active windows
begin
          // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsyscolor
  result := WinApi.Windows.GetSysColor (WinApi.Windows.COLOR_ACTIVECAPTION)
end;

// =================================

function zzGetWindowTitleBarSecondaryColor : TColor;
  // return the secondary (right hand side) gradiated color of the title bar in active windows
begin
          // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsyscolor
  result := WinApi.Windows.GetSysColor (WinApi.Windows.COLOR_GRADIENTACTIVECAPTION);
end

// =================================

function zzGetWindowTitleBarIsGradiated : boolean;
  // return TRUE if the color of title bars for active windows is gradiated
var
  vUseGradient : boolean;
begin
  result := FALSE;
  if (SystemParametersInfo(SPI_GETGRADIENTCAPTIONS, 0, @vUseGradient, 0)) then
      if (vUseGradient) then
          result := TRUE;
end;

// =================================

end.

 

About Me

scott_hollows_201611

  • Oracle & Delphi software developer based in Perth, Western Australia
  • Australian Delphi User Group – President and WA meeting organizer
  • Australian Oracle User Group – WA State Committee Member
  • Available to do remote presentations to user groups on Delphi and Oracle topics
blog email linkedinlogo

 

 

Published by

3 responses to “Get color of active window title bar in Delphi”

  1. Sergio Bonfiglio Avatar
    Sergio Bonfiglio

    Dear Scott, thanks for this useful piece of code!
    It would be nice if you may explain how to set the color of the background of the title bar, and possibly the font color, if this possibility exists outside the setup of windows (10)

  2. Sergio

    You should be able to do that using a custom title bar.
    I think that was introduced in Delphi 10.4

    The form has a CustomTitleBar property and you link it to a TTitleBarPanel control that you place on the form

    That is mainly intended to be used to add custom buttons to the title bar, but I think you can customize the title bar area as well to change the color or add a background image

    Documentation
    https://docwiki.embarcadero.com/RADStudio/Sydney/en/Custom_Title_Bar_for_VCL_Forms

    Demo by Ray Konopka

    I hope that helps

  3. That Youtube link is not working for me

    Search Youtube for “Creating Custom Title Bars in VCL Apps – with Ray Konopka”

Leave a comment