Skip to content

Instantly share code, notes, and snippets.

@pabloko
Last active May 16, 2020 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pabloko/c4752a41b7f6696c007b3b8a27fb6b8a to your computer and use it in GitHub Desktop.
Save pabloko/c4752a41b7f6696c007b3b8a27fb6b8a to your computer and use it in GitHub Desktop.
(C) ChooseFont color picker with extended color dialog

The ChooseFont dialog color picker extended

Using ChooseFont you will notice it only support 15 predefined an ugly colors. The color palette is very limited and theres no way to use more colors without subclassing it. Here is a good start point to subclass it, this mod will:

  • Display the color in HTML format
  • Display a full featured ChooseColor dialog when color ComboBox is clicked

#pragma comment(lib, "Comctl32.lib")
long IDM_COLORSEL = 0x473;
LRESULT CALLBACK ChooseFontComboProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubClass, DWORD_PTR udata){
switch (msg) {
case WM_LBUTTONDOWN: {
CHOOSECOLOR cc;
static COLORREF acrCustClr[16];
ZeroMemory(&cc, sizeof(cc));
cc.lStructSize = sizeof(cc);
cc.hwndOwner = g_webWindow->hWndWebWindow;
cc.lpCustColors = (LPDWORD)acrCustClr;
cc.rgbResult = (COLORREF)SendMessage(hwnd, CB_GETITEMDATA, 16, 0);
cc.Flags = CC_FULLOPEN | CC_RGBINIT | CC_ANYCOLOR;
if (ChooseColor(&cc) == TRUE) {
SendMessage(hwnd, CB_SETITEMDATA, 16, cc.rgbResult);
SendMessage(hwnd, CB_SETCURSEL, 16, cc.rgbResult);
RECT rc;
GetClientRect(GetParent(hwnd), &rc);
InvalidateRect(GetParent(hwnd), &rc, TRUE);
}
return false;
} break;
case CB_GETLBTEXTLEN: {
return 7;
} break;
case CB_GETLBTEXT: {
COLORREF co = (COLORREF)SendMessage(hwnd, CB_GETITEMDATA, 16, 0);
_swprintf((TCHAR*)lParam, L"#%02X%02X%02X", GetRValue(co), GetGValue(co), GetBValue(co));
return 7;
} break;
case WM_NCDESTROY: {
RemoveWindowSubclass(hwnd, ChooseFontComboProc, uIdSubClass);
} break;
}
return DefSubclassProc(hwnd, msg, wParam, lParam);
}
UINT_PTR CALLBACK ChooseFontProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg) {
case WM_INITDIALOG: {
HWND hCbo = GetDlgItem(hwnd, IDM_COLORSEL);
RECT rc;
GetClientRect(hCbo, &rc);
SetWindowSubclass(hCbo, ChooseFontComboProc, 0, 0);
} break;
}
return 0;
};
void DoChooseFont() {
CHOOSEFONT cc;
COLORREF rgbColors = 0x000000; //Use any startup color
LOGFONT lf;
ZeroMemory(&cc, sizeof(cc));
cc.lStructSize = sizeof(cc);
cc.hwndOwner = g_webWindow->hWndWebWindow;
cc.rgbColors = rgbColors;
cc.lpLogFont = &lf;
cc.lpfnHook = ChooseFontProc;
cc.Flags = CF_ENABLEHOOK | CF_EFFECTS;
if (ChooseFont(&cc) == TRUE) {
//do whatever with data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment