控件–RichEdit-WindowsAPI论坛-WindowsAPI-津桥芝士平台

控件–RichEdit

使用WindowsAPI,创建RichEdit:

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


#pragma comment(lib, "comctl32.lib")
//#pragma comment(lib, "Msftedit.lib") // 确保链接 RichEdit 控件库
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#define ID_RICHEDIT 101
HINSTANCE hInst;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
    case WM_CREATE: {
        
        LoadLibrary(TEXT("Msftedit.dll"));
        HWND hwndRichEdit = CreateWindowEx(0, MSFTEDIT_CLASS, TEXT("Type here"),
            ES_MULTILINE | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP,
            10, 10, 100, 100,
            hwnd, NULL, hInst, NULL);
        // 设置一些文本
        SendMessage(hwndRichEdit, EM_SETLIMITTEXT, 1000, 0); // 限制输入字符数
        SendMessage(hwndRichEdit, WM_SETTEXT, 0, (LPARAM)L"Hello, RichEdit Control!");

        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    hInst = hInstance;
    // 初始化 Common Controls
    INITCOMMONCONTROLSEX icex;
    icex.dwSize = sizeof(icex);
    icex.dwICC = ICC_STANDARD_CLASSES; // 初始化标准控件类
    InitCommonControlsEx(&icex);

    // 初始化 RichEdit 控件
    LoadLibrary(TEXT("Msftedit.dll"));

    // 创建窗口类
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = L"RichEditExample";
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

    RegisterClass(&wc);

    // 创建窗口
    HWND hwnd = CreateWindowEx(
        0, L"RichEditExample", L"RichEdit Control Example",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        NULL, NULL, hInstance, NULL);

    // 消息循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

效果图:

image

 

请登录后发表评论

    没有回复内容