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