win32编辑控件字体

每次到用的时候就各种查资料,我这人记性又不好,遂记录下来:

普通的编辑控件: 

  • 创建:HWND hText = CreateWindowW(L"EDIT", L"enter some text", WS_VISIBLE | WS_CHILD | ES_RIGHT, 500, 100, 200, 100, hWnd, NULL, NULL, NULL); (仅供参考)
  • 创建笔: 
    LOGFONT logfont; 
    ZeroMemory(&logfont, sizeof(LOGFONT));
    logfont.lfCharSet = DEFAULT_CHARSET;
    logfont.lfHeight = -20; 
    HFONT hFont = CreateFontIndirect(&logfont);
  • 设置文本大小: SendMessage(hText, WM_CTLCOLOREDIT, (WPARAM)hFont, TRUE);

如果是改变文本颜色或者文本背景颜色,则在WM_CTLCOLOREDIT消息中设置

case WM_CTLCOLOREDIT:
    {
SetBkColor((HDC)wParam, RGB(40, 40, 40)); SetTextColor((HDC)wParam, RGB(
255, 0, 255)); } break;

如果是富文本控件,则复杂一点:

设置文本背景颜色: 

 SendMessage(hedit, EM_SETBKGNDCOLOR, 0, RGB(40, 40, 40));

设置文本颜色:

CHARFORMAT2 format;
            memset(&format, 0, sizeof format);

            format.cbSize = sizeof(CHARFORMAT2);
            format.dwMask = CFM_COLOR | CFM_FACE;
            format.crTextColor = RGB(255, 0, 255);
            memcpy(format.szFaceName, L"Consolas", sizeof(L"Consolas"));
            if (SendMessage(hedit, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&format) == 0) {
                mb_err("Failed to set font.");
            }

富文本控件的创建与设置具体可以参考: https://www.cnblogs.com/strive-sun/p/11778590.html

posted @ 2020-01-09 15:07  strive-sun  阅读(1276)  评论(0编辑  收藏  举报