WindowsAPI-控件:RichEdit

WindowsAPI-控件:RichEdit

来自AI助手的总结
文章介绍了如何使用Windows API在C++程序中创建并初始化一个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;
}

编译器:

MSVC

效果图:

20250501215537382-863cc964e927f203d62652ef2752161

 
 
 
 
 
 
 
 
温馨提示:本文最后更新于2025-05-01 22:39:47,某些文章具有时效性,若有错误或已失效,请在下方留言或联系 站长
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容