每次到用的時候就各種查資料,我這人記性又不好,遂記錄下來:
普通的編輯控件:
- 創建: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