当前位置: 首页 > article >正文

Visual studio code编写简单记事本exe笔记

安装扩展cmake tools
c/c++
c/c++ Extension pack

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(NotepadApp)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Windows specific settings
if(WIN32)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
endif()

# Include Windows SDK
if(WIN32)
    include_directories($ENV{WindowsSdkDir}Include/10.0.26100.0/um)
    include_directories($ENV{WindowsSdkDir}Include/10.0.26100.0/shared)
    link_directories($ENV{WindowsSdkDir}Lib/10.0.26100.0/um/x64)
endif()

# Add executable
add_executable(NotepadApp WIN32 src/main.cpp)

# Windows specific libraries
if(WIN32)
    target_link_libraries(NotepadApp
        Comctl32.lib
        Comdlg32.lib
        Gdi32.lib
        User32.lib
        Shell32.lib
    )
endif()

src/main.cpp

#include <windows.h>
#include <commdlg.h> // 用于打开文件对话框

const char g_szClassName[] = "notepadWindowClass";

// 保存文件函数
void SaveFile(HWND hwnd) {
    HWND hEdit = GetWindow(hwnd, GW_CHILD);
    int len = GetWindowTextLength(hEdit) + 1;
    char* text = new char[len];
    GetWindowText(hEdit, text, len);

    OPENFILENAME ofn;
    char szFileName[MAX_PATH] = "";

    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
    ofn.lpstrDefExt = "txt";

    if (GetSaveFileName(&ofn)) {
        HANDLE hFile = CreateFile(ofn.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile != INVALID_HANDLE_VALUE) {
            DWORD dwBytesWritten;
            WriteFile(hFile, text, len - 1, &dwBytesWritten, NULL);
            CloseHandle(hFile);
        }
    }

    delete[] text;
}

// 打开文件函数
void OpenFile(HWND hwnd) {
    HWND hEdit = GetWindow(hwnd, GW_CHILD);

    OPENFILENAMEW ofn;
    wchar_t szFileName[MAX_PATH] = L"";

    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFilter = L"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
    ofn.lpstrDefExt = L"txt";

    if (GetOpenFileNameW(&ofn)) {
        HANDLE hFile = CreateFileW(ofn.lpstrFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile != INVALID_HANDLE_VALUE) {
            DWORD dwFileSize = GetFileSize(hFile, NULL);
            char* text = new char[dwFileSize + 1];
            DWORD dwBytesRead;
            ReadFile(hFile, text, dwFileSize, &dwBytesRead, NULL);
            text[dwFileSize] = '\0'; // 添加字符串结束符
            
            // 将UTF-8转换为宽字符
            int wideLen = MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0);
            wchar_t* wideText = new wchar_t[wideLen];
            MultiByteToWideChar(CP_UTF8, 0, text, -1, wideText, wideLen);
            
            SetWindowTextW(hEdit, wideText);
            CloseHandle(hFile);
            delete[] text;
            delete[] wideText;
        }
    }
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE: {
            // 创建字体
            HFONT hFont = CreateFont(
                16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
                DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
                DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
                "微软雅黑");

            // 创建编辑框
            HWND hEdit = CreateWindow("EDIT", "", 
                WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
                0, 0, 100, 100, hwnd, NULL, NULL, NULL);
            
            // 设置字体
            SendMessage(hEdit, WM_SETFONT, (WPARAM)hFont, TRUE);

            // 创建菜单栏
            HMENU hMenu = CreateMenu();
            HMENU hFileMenu = CreatePopupMenu();
            AppendMenu(hFileMenu, MF_STRING, 1, "Open");
            AppendMenu(hFileMenu, MF_STRING, 2, "Save");
            AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, "File");
            SetMenu(hwnd, hMenu); // 将菜单栏附加到窗口
            break;
        }
        case WM_COMMAND: {
            switch (LOWORD(wParam)) {
                case 1: // Open
                    OpenFile(hwnd);
                    break;
                case 2: // Save
                    SaveFile(hwnd);
                    break;
            }
            break;
        }
        case WM_SIZE: {
            HWND hEdit = GetWindow(hwnd, GW_CHILD);
            SetWindowPos(hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam), SWP_NOZORDER);
            break;
        }
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Notepad",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

终端命令
1

cd build; cmake .. -G "Visual Studio 17 2022" -A x64 

2

cmake --build . --clean-first --config Release  

3

 bin/Release/NotepadApp.exe

在这里插入图片描述


http://www.kler.cn/a/470567.html

相关文章:

  • 均值聚类算法
  • Android Telephony | 协议测试针对 test SIM attach network 的问题解决(3GPP TS 36523-1-i60)
  • 三维卷积( 3D CNN)
  • JAVA创建绘图板JAVA构建主窗口鼠标拖动来绘制线条
  • 小白学Pytorch
  • LabVIEW瞬变电磁接收系统
  • 【Uniapp-Vue3】image媒体组件属性
  • 【Logstash01】企业级日志分析系统ELK之Logstash 安装与介绍
  • 【通识安全】煤气中毒急救的处置
  • 数据库系统安全
  • 数据结构:时间复杂度和空间复杂度
  • 如何恢复依赖的go自定义SDK的GoZero项目
  • MCGS学习记录
  • JuiceFS 详解:一款为云原生设计的高性能分布式文件系统
  • @Cacheable 注解爆红(不兼容的类型。实际为 java. lang. String‘,需要 ‘boolean‘)
  • SQL Server 中 STRING_AGG 函数使用拼接字符串超长处理方法
  • JVM实战—10.MAT的使用和JVM优化总结
  • 机器学习周报-ModernTCN文献阅读
  • 回归预测 | MATLAB实现CNN-BiLSTM-Attention多输入单输出回归预测
  • 解决linux自启程序无法在终端输出问题
  • 低空经济应用探索,无人机个性需求组装技术详解
  • 云计算基础,虚拟化原理
  • Java测试开发平台搭建(八) Jenkins
  • 【Linux】RPMSG通讯协议介绍
  • w140体育馆使用预约平台的设计与实现
  • CV-MLLM经典论文解读|OneLLM: One Framework to Align All Modalities with Language