简体   繁体   English

是否可以在不影响其他行的情况下更改编辑控件的字体?

[英]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.在我的Edit control我有一个文本,但我想要一些大字体和粗体的标题和标题,而其他行的字体较小。

I tried SendMessage(hEdit, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(0, true));我试过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);我认为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.您正在寻找的是RichEdit 控件(想想写字板),特别是它的EM_SETCHARFORMAT消息,它可用于将不同的格式(包括字体、颜色等)应用于文本的不同部分。

This is not working with the default Editcontrol, but you can use a Richeditcontrol这不适用于默认的 Editcontrol,但您可以使用 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:现在将字体设置为您的 Richeditcontrol 使用:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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