求助,这个程序怎么实现呀(win32记事本升级版,我之前文章里提到了)

这是作业,目前效果是这样的,但要求助一下

代码是这样的,谁来帮我改一下

#include <richedit.h> 
#define IDM_FONT 101 
#define IDM_COLOR 102 
#define IDM_SAVE 103 
#define IDM_COPY 104 
#define IDM_OPEN 105 
#define IDM_PASTE 106 
#define IDM_CUT 107 
#define IDM_UNDO 108 
#define IDM_REDO 109 
// 全局变量 
HWND g_hWndRichEdit; // RichEdit控件句柄 
#ifdef _UNICODE 
#if defined _M_IX86 
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") 
#elif defined _M_X64 
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"") 
#else 
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") 
#endif 
#endif 
// 函数声明 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void OpenFontDialog();
void OpenColorDialog();
void SaveToFile();
void OpenFile();
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // 注册窗口类 
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = L"RichEditExample";
    wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL, L"窗口类注册失败!", L"错误", MB_OK | MB_ICONERROR);
        return 0;
    }
    // 创建主窗口 
    HWND hWnd = CreateWindow(L"RichEditExample", L"RichEdit Example", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
    HMODULE hModule = LoadLibrary(L"msftedit.dll");
    if (hModule == NULL)
    {
        // 加载库失败 
        MessageBoxW(hWnd, L"库加载失败,failed to load library,开始退出程序", L"error", MB_ICONERROR);
    }
    HMENU hMenu = CreateMenu();
    HMENU hSubMenu = CreatePopupMenu();
    AppendMenu(hSubMenu, MF_STRING, IDM_FONT, L"字体");
    AppendMenu(hSubMenu, MF_STRING, IDM_COLOR, L"颜色");
    AppendMenu(hSubMenu, MF_STRING, IDM_SAVE, L"保存");
    AppendMenu(hSubMenu, MF_STRING, IDM_OPEN, L"打开");
    AppendMenu(hSubMenu, MF_SEPARATOR, 0, NULL);
    AppendMenu(hSubMenu, MF_STRING, IDM_COPY, L"复制");
    AppendMenu(hSubMenu, MF_STRING, IDM_PASTE, L"粘贴");
    AppendMenu(hSubMenu, MF_STRING, IDM_CUT, L"剪切");
    AppendMenu(hSubMenu, MF_SEPARATOR, 0, NULL);
    AppendMenu(hSubMenu, MF_STRING, IDM_UNDO, L"撤销");
    AppendMenu(hSubMenu, MF_STRING, IDM_REDO, L"重做");
    AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hSubMenu, L"编辑");
    SetMenu(hWnd, hMenu);
    if (!hWnd)
    {
        MessageBox(NULL, L"窗口创建失败!", L"错误", MB_OK | MB_ICONERROR);
        return 0;
    }
    // 创建RichEdit控件 
    g_hWndRichEdit = CreateWindowEx(0, MSFTEDIT_CLASS, L"", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
        0, 0, 800, 600, hWnd, NULL, hInstance, NULL);
    if (!g_hWndRichEdit)
    {
        MessageBox(NULL, L"RichEdit控件创建失败!", L"错误", MB_OK | MB_ICONERROR);
        return 0;
    }
    // 显示窗口 
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    // 消息循环 
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
    {
        int wmId = LOWORD(wParam);
        // 分析菜单选择: 
        switch (wmId)
        {
        case IDM_FONT:
            OpenFontDialog();
            break;
        case IDM_COLOR:
            OpenColorDialog();
            break;
        case IDM_SAVE:
            SaveToFile();
            break;
        case IDM_OPEN:
            OpenFile();
            break;
        case IDM_COPY:
            SendMessage(g_hWndRichEdit, WM_COPY, 0, 0);
            break;
        case IDM_PASTE:
            SendMessage(g_hWndRichEdit, WM_PASTE, 0, 0);
            break;
        case IDM_CUT:
            SendMessage(g_hWndRichEdit, WM_CUT, 0, 0);
            break;
        case IDM_UNDO:
            SendMessage(g_hWndRichEdit, EM_UNDO, 0, 0);
            break;
        case IDM_REDO:
            SendMessage(g_hWndRichEdit, EM_REDO, 0, 0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
    break;
    case WM_SIZE:
    {
        // 调整RichEdit控件的大小以适应窗口 
        RECT rcClient;
        GetClientRect(hWnd, &rcClient);
        MoveWindow(g_hWndRichEdit, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, TRUE);
    }
    break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
void OpenFontDialog()
{
    CHOOSEFONT cf;
    LOGFONT lf;
    ZeroMemory(&cf, sizeof(CHOOSEFONT));
    cf.lStructSize = sizeof(CHOOSEFONT);
    cf.hwndOwner = g_hWndRichEdit;
    cf.lpLogFont = &lf;
    cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | CF_EFFECTS;
    if (ChooseFont(&cf))
    {
        CHARFORMAT2 cf2;
        ZeroMemory(&cf2, sizeof(CHARFORMAT2));
        cf2.cbSize = sizeof(CHARFORMAT2);
        cf2.dwMask = CFM_FACE | CFM_SIZE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_COLOR;
        cf2.dwEffects = 0;
        cf2.yHeight = cf.iPointSize * 20;
        wcscpy_s(cf2.szFaceName, LF_FACESIZE, lf.lfFaceName);
        cf2.crTextColor = cf.rgbColors;
        SendMessage(g_hWndRichEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf2);
    }
}
void OpenColorDialog()
{
    CHOOSECOLOR cc;
    COLORREF acrCustClr[16];
    ZeroMemory(&cc, sizeof(CHOOSECOLOR));
    cc.lStructSize = sizeof(CHOOSECOLOR);
    cc.hwndOwner = g_hWndRichEdit;
    cc.lpCustColors = (LPDWORD)acrCustClr;
    cc.Flags = CC_FULLOPEN | CC_RGBINIT;
    if (ChooseColor(&cc))
    {
        CHARFORMAT2 cf2;
        ZeroMemory(&cf2, sizeof(CHARFORMAT2));
        cf2.cbSize = sizeof(CHARFORMAT2);
        cf2.dwMask = CFM_COLOR;
        cf2.crTextColor = cc.rgbResult;
        SendMessage(g_hWndRichEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf2);
    }
}
void SaveToFile()
{
    OPENFILENAME ofn;
    WCHAR szFileName[MAX_PATH];
    ZeroMemory(&ofn, sizeof(OPENFILENAME));
    ZeroMemory(szFileName, sizeof(szFileName));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = g_hWndRichEdit;
    ofn.lpstrFilter = L"Note Files (*.not)\0*.not\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT;
    if (GetSaveFileName(&ofn))
    {
        // 获取RichEdit控件的内容 
        int nTextLength = GetWindowTextLength(g_hWndRichEdit);
        LPWSTR pszText = new WCHAR[nTextLength + 1];
        GetWindowText(g_hWndRichEdit, pszText, nTextLength + 1);
        // 保存到文件 
        HANDLE hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile != INVALID_HANDLE_VALUE)
        {
            DWORD dwBytesWritten;
            WriteFile(hFile, pszText, nTextLength * sizeof(WCHAR), &dwBytesWritten, NULL);
            CloseHandle(hFile);
        }
        delete[] pszText;
    }
}
void OpenFile()
{
    OPENFILENAME ofn;
    WCHAR szFileName[MAX_PATH];
    ZeroMemory(&ofn, sizeof(OPENFILENAME));
    ZeroMemory(szFileName, sizeof(szFileName));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = g_hWndRichEdit;
    ofn.lpstrFilter = L"Note Files (*.not)\0*.not\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST;
    if (GetOpenFileName(&ofn))
    {
        // 打开文件 
        HANDLE hFile = CreateFile(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile != INVALID_HANDLE_VALUE)
        {
            // 获取文件大小 
            DWORD dwFileSize = GetFileSize(hFile, NULL);
            if (dwFileSize > 0)
            {
                // 读取文件内容 
                LPWSTR pszText = new WCHAR[dwFileSize / sizeof(WCHAR) + 1];
                DWORD dwBytesRead;
                ReadFile(hFile, pszText, dwFileSize, &dwBytesRead, NULL);
                // 设置RichEdit控件的内容 
                SetWindowText(g_hWndRichEdit, pszText);
                delete[] pszText;
            }
            CloseHandle(hFile);
        }
    }
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值