I am a beginner in win32 API and I made
HWND button = CreateWindowEx(0, "BUTTON", NULL, WS_CHILD | WS_VISIBLE | BS_BITMAP, 150, 100, 150, 30, hwnd, (HMENU)ID_BUTTON, hInstance, NULL);
and
HWND check = CreateWindowEx(0, "BUTTON", "text", WS_CHILD | WS_VISIBLE | BS_CHECKBOX, 150, 100, 300, 20, hwnd, (HMENU)ID_CHECK, hInstance, NULL);
I want to remove white border from button control to which I inserted a bitmap and set black background and white text on BS_CHECKBOX
. I read about WM_CTLCOLORBTN
but I don't know how to do it correctly.
WM_CTLCOLORBTN should be used with BS_OWNERDRAW .
That is to say, only when BS_OWNERDRAW
is added, the WM_CTLCOLORBTN
will be triggered and the button can be redrawn.
But when you use BS_OWNERDRAW
, your original checkbox style will be replaced, like this.
check = CreateWindowEx(0,L"BUTTON",L"text", WS_CHILD | WS_VISIBLE | BS_CHECKBOX | BS_OWNERDRAW, 150, 100, 60, 20, hWnd, (HMENU)ID_CHECK, (HINSTANCE)GetWindowLong(hWnd, GWLP_HINSTANCE), NULL);
case WM_CTLCOLORBTN:
{
return (INT_PTR)CreateSolidBrush(RGB(0, 0, 0));
break;
}
case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT pDIS = (LPDRAWITEMSTRUCT)lParam;
if (pDIS->hwndItem == check)
{
SetTextColor(pDIS->hDC, RGB(255, 255, 255));
SetBkColor(pDIS->hDC, RGB(0, 0, 0));
WCHAR Text[99];
int len = SendMessage(check, WM_GETTEXT,
ARRAYSIZE(Text), (LPARAM)Text);
TextOut(pDIS->hDC, pDIS->rcItem.left, pDIS->rcItem.top, Text, len);
}
return TRUE;
}
Debugging results:
You'll find that it's different from what you think.
According to MSDN,creates an owner-drawn button. The owner window receives a WM_DRAWITEM message when a visual aspect of the button has changed. Do not combine the BS_OWNERDRAW style with any other button styles.
So, I suggest you do that.
check = CreateWindowEx(0,L"BUTTON",L"text123", WS_CHILD | WS_VISIBLE | BS_CHECKBOX ,150, 100, 60, 20, hWnd, (HMENU)ID_CHECK, (HINSTANCE)GetWindowLong(hWnd, GWLP_HINSTANCE), NULL);
case WM_CTLCOLORSTATIC:
{
HDC hdc = (HDC)wParam;
HWND hWnd = (HWND)lParam;
if (hWnd == check)
{
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(255, 255, 255));
return (INT_PTR)CreateSolidBrush(RGB(0, 0, 0));
}
break;
}
By triggering WM_CTLCOLORSTATIC
, the text and background colors are self-drawn.
Strive answer doesn't work with buttons. Seems like it works only with text boxes and checkboxes. Btw here still border's on it.
You should create buttons in this way:
#include <CommCtrl.h>
#pragma comment(lib,"comctl32.lib")
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
CreateWindowEx(WS_EX_TRANSPARENT, "Button", NULL, WS_CHILD | WS_VISIBLE | BS_FLAT | BS_OWNERDRAW, start_x, start_y, size_y, size_x, hWnd, (HMENU)(i + 2000), NULL, NULL);
.
case WM_DRAWITEM: {
auto id = LOWORD(wParam);
if (id >= 2000) {
id -= 2000;
drawButton(dis, id);
return true;
}
case WM_INITDIALOG: {
INITCOMMONCONTROLSEX icx;
icx.dwSize = sizeof(INITCOMMONCONTROLSEX);
icx.dwICC = ICC_STANDARD_CLASSES;
InitCommonControlsEx(&icx);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.