简体   繁体   中英

Is it possible to change font for an edit control without affecting the other lines?

Hello I want to know if it is possible to change the font of an edit control for some lines only without affecting the remaining:

In my Edit control I have a text but I want some headlines and titles in bigger font and bold while the other lines are with smaller font.

I tried SendMessage(hEdit, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(0, true)); But it sets the whole text in the passed in font.

I thought some messing up with SelectObject(hDcEdit, hFont); But I don't know if it is correct and how.

A standard Edit Control (think, Notepad) does not support what you are looking for. It only supports one Font for the entire text.

What you are looking for is a RichEdit Control instead (think, Wordpad), and in particular its EM_SETCHARFORMAT message, which can be used to apply different formatting (including fonts, colors, etc) to different sections of text.

This is not working with the default Editcontrol, but you can use a Richeditcontrol

#include <Windows.h>
#include <CommCtrl.h>

HINSTANCE relib = LoadLibrary("riched32.dll");
if (relib == NULL) {
    MessageBox(NULL, "couldn't load richedit32.dll", "", MB_ICONEXCLAMATION);

hEdit = CreateWindow(RICHEDIT_CLASS, "", WS_VISIBLE | WS_CHILD | ES_MULTILINE | 
ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_VSCROLL | WS_HSCROLL, 0, 0, 200, 200, hWnd, NULL, 
NULL, NULL);

Now to set the font to your Richeditcontrol use:

CHARFORMAT2 cf;
memset(&cf, 0, sizeof cf);
cf.cbSize = sizeof cf;
cf.dwMask = CFM_FACE;
wsprintf(cf.szFaceName, "Arial"); //Here you can set the fontname you wont (C:/Windows/Fonts)
SendMessage(hEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM