c++ - WIN32API : owner-drawn button creates white background around the text when clicked

标签 c++ c windows winapi user-interface

我创建了一个 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 )。

按钮呈现得很好,但按下它会使文本背景变为白色。 我在网上搜索原因和解决方案,但没有找到具体的解决方案。 提前致谢!

最佳答案

您需要在实际绘制文本之前调用这些行

SetTextColor((HDC)wParam, RGB(0,0,0));
SetBkColor((HDC)wParam, RGB(229,255,229));
TextOut((HDC)wParam,30,15,buttonText,textLength);

关于c++ - WIN32API : owner-drawn button creates white background around the text when clicked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25698805/

相关文章:

c++ - Libpurple 无法连接 - Purple_presence_is_online 失败

windows - WSAEventSelect,零事件

c++ - 为什么我们不将 C++ main 方法放在一个类中?

c++ - IAMVideoProcAmp GetRange 仅在延迟后有效(C++)?

c++ - "structural binding"上的提案在哪里?

c++ - 关闭功能未被识别 - C++

c - c中函数的声明和使用

c - 这个 "#define CONCAT_3(p1, p2, p3) p1##p2##p3"宏定义是什么意思?

windows - 如何在 Windows Azure 中安装和使用 COM 对象?

c++ - c++11 lambda 真的支持闭包吗?函数变量中存在语义冲突