Trackbar control
How can I ensure that the background colour of my trackbar control is the same
colour as the background of the dialog box containing it ?
You need to handle
WM_CTLCOLORSTATIC to update background color:
https://docs.microsoft.com/en-us/windows/win32/controls/wm-ctlcolorstatic
How to do this?
Following code sets trackbar background color to green:
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
|
#include <d2d1.h>
#include <Windows.h>
// Brush handle used by system to paint trackbar background
HBRUSH hCtlBrush = NULL;
// Color conversion function
ToColorRef(const D2D1::ColorF& color) noexcept
{
const BYTE red = static_cast<BYTE>(255 * color.r);
const BYTE green = static_cast<BYTE>(255 * color.g);
const BYTE blue = static_cast<BYTE>(255 * color.b);
return RGB(red, green, blue);
}
// Handler for WM_CTLCOLORSTATIC
LRESULT OnCtlColorStatic(WPARAM wParam, LPARAM lParam) noexcept
{
HDC hDCStatic = reinterpret_cast<HDC>(wParam);
const COLORREF background = ToColorRef(D2D1::ColorF::Green);
SetTextColor(hDCStatic, RGB(0, 0, 0)); // black text
SetBkColor(hDCStatic, background); // green background
if (hCtlBrush == NULL)
{
hCtlBrush = CreateSolidBrush(background);
}
return reinterpret_cast<LRESULT>(hCtlBrush );
}
// Don't forget to destroy brush at some point in your code
DeleteObject(hCtlBrush);
| |
EDIT:
Btw. if your next issue is to learn current background color of a dialog then:
1 2 3 4
|
HWND hDlg = /* dialog handle */
HDC dialogDC = GetDC(hDlg);
COLORREF dialog_background = GetBkColor(dialogDC);
| |
You then simply update above handler (line 21) as follows:
const COLORREF background = dialog_background;
And your trackbar should be of same color as dialog background.
Last edited on
Topic archived. No new replies allowed.