Source Code Get color of active window title bar in Delphi

emailx45

Social Engineer
Joined
May 5, 2008
Messages
2,387
Reaction score
2,149
Get color of active window title bar in Delphi
[SHOWTOGROUPS=4,20]
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

Code:
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.
[/SHOWTOGROUPS]
 

sergiobadi

New member
Joined
Oct 30, 2010
Messages
3
Reaction score
0
"Hey guys, I managed to get this working by messing around with Windows API calls. Specifically, you can use GetWindowRect and GetDC to get the title bar's rectangle and color. I'll share the code snippet if anyone's interested!"
 

merih

New member
Joined
Apr 9, 2006
Messages
1
Reaction score
0
"Just thought I'd chime in, I used `GetWindowLong` with the `GWL_EXSTYLE` value to get the title bar color in the past. Here's a quick code snippet to get you started: `result := GetWindowLong(hwnd, GWL_EXSTYLE)`;".
 

Svl

New member
Joined
May 14, 2006
Messages
3
Reaction score
0
"Lol, still trying to get to the bottom of this Delphi question. From what I recall, you can use the `TScreen.Monitors` component to get the current screen resolution and then get the color of the active window's title bar. Any luck with that, OP?"
 
Top