visual studio 2017 vc++ win32 API 修改 richedit control 控件字体,颜色,大小,斜体等

#include <windows.h>
#include <richedit.h>
#include <commctrl.h>

static LRESULT CALLBACK WndProc
(
    HWND hWnd,
    UINT message,
    WPARAM wParam,
    LPARAM lParam
)
{
    switch (message)
    {
    case WM_CREATE:
    {
        // Must load rich edit library
        LoadLibrary(L"riched20.dll");

        HWND hWndRichEdit;
        hWndRichEdit = CreateWindow(RICHEDIT_CLASS, NULL,
            WS_CHILD | ES_SAVESEL | ES_NOHIDESEL | WS_CHILDWINDOW | WS_BORDER\
            | WS_VISIBLE | ES_MULTILINE | WS_VSCROLL | WS_EX_STATICEDGE,
            0, 0, 300, 200, hWnd, 0, GetModuleHandle(0), 0);

        CHARFORMAT cf;
        ZeroMemory(&cf, sizeof(CHARFORMAT));
        // Get the charformat from richedit
        SendMessage(hWndRichEdit, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf);
        cf.cbSize = sizeof(cf);
        cf.dwMask = CFM_COLOR | CFM_ITALIC | CFM_SIZE | CFM_FACE; // Change color and italic
        cf.dwEffects = CFE_ITALIC; // Italic
        cf.yHeight = 18 * 18; // Size
        cf.crTextColor = RGB(0, 200, 100); // Color
        wcscpy_s(cf.szFaceName, L"宋体"); // Font

                                        // Set default char format
        SendMessage(hWndRichEdit, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf);

        // Show the text
        SendMessage(hWndRichEdit, EM_REPLACESEL, 0, (LPARAM)L"hello");
        return 0;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

int APIENTRY wWinMain
(
    _In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR lpCmdLine,
    _In_ int nCmdShow
)
{
    wchar_t szAppName[] = L"TestRicheditCharFormat";
    WNDCLASSEX wndclass;

    wndclass.cbSize = sizeof(wndclass);
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = GetModuleHandle(0);
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszClassName = szAppName;
    wndclass.lpszMenuName = NULL;
    RegisterClassEx(&wndclass);

    HWND hWnd = CreateWindow(szAppName, L"Hello, world!", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        NULL, NULL, GetModuleHandle(0), NULL);

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值