我创建了一个Owner-Drawn按钮。我使用WM_CTLCOLORBTN消息来绘制它:
//get the text of the button
wchar_t buttonText[20];
int textLength = SendMessage((HWND)lParam,WM_GETTEXT,20,(LPARAM)buttonText);
Font FootlightMTLight(L"Footlight MT Light",20,0,false,false,false,L"Black");
SelectObject((HDC)wParam,FootlightMTLight.getWindowHandle());
TextOut((HDC)wParam,30,15,buttonText,textLength);
SetTextColor((HDC)wParam, RGB(0,0,0));
SetBkColor((HDC)wParam, RGB(229,255,229));
PatBlt((HDC)wParam,0,0,1,50,BLACKNESS); //x,y,width,height
PatBlt((HDC)wParam,269/2-1,0,1,50,BLACKNESS);
PatBlt((HDC)wParam,0,49,269/2,1,BLACKNESS);
PatBlt((HDC)wParam,0,0,269/2,1,BLACKNESS);
static HBRUSH handleToButtonBrush = CreateSolidBrush(RGB(229,255,229));
return (INT_PTR)handleToButtonBrush;
"字体"是我创建的对象(我用类等包装HFONT句柄和CreateFont函数等.getWindowHandle()基本上返回HFONT)。
按钮渲染效果很好,但按下按钮会使文本背景变为白色。 我在网上寻找原因和解决方案,但我没有找到具体的解决方案。 谢谢你提前!
答案 0 :(得分:2)
您需要在实际文本绘制发生之前调用这些行
SetTextColor((HDC)wParam, RGB(0,0,0));
SetBkColor((HDC)wParam, RGB(229,255,229));
TextOut((HDC)wParam,30,15,buttonText,textLength);