用C++写一个windows记事本
时间: 2023-12-27 18:04:40 浏览: 95
好的,首先你需要开启一个新的项目,然后在项目中创建一个新的文本文件,保存文件名为“记事本.c”。接下来,你需要在代码中添加所需的库文件,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
然后,你需要定义一些常量,如下所示:
#define IDM_NEW 1
#define IDM_OPEN 2
#define IDM_SAVE 3
在主函数中,你需要设置窗口的大小和标题,如下所示:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
HWND hWnd;
MSG msg;
WNDCLASS wc;
// 设置窗口类
wc.style = CS_HREDRAW | CS_VREDRAW;
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 = "记事本";
// 注册窗口类
RegisterClass(&wc);
// 创建窗口
hWnd = CreateWindow(
"记事本",
"记事本",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
// 显示窗口
ShowWindow(hWnd, nCmdShow);
// 进入消息循环
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) {
static HWND hEdit;
static char szFileName[MAX_PATH];
switch (message) {
case WM_CREATE:
// 创建编辑框
hEdit = CreateWindow(
"EDIT",
"",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 0, 0,
hWnd,
(HMENU)ID_EDIT,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
// 设置菜单
{
HMENU hMenu, hSubMenu;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, IDM_NEW, "新建(&N)");
AppendMenu(hSubMenu, MF_STRING, IDM_OPEN, "打开(&O)");
AppendMenu(hSubMenu, MF_STRING, IDM_SAVE, "保存(&S)");
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hSubMenu, "文件(&F)");
SetMenu(hWnd, hMenu);
}
break;
case WM_SIZE:
// 调整编辑框大小
MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
break;
case WM_COMMAND:
// 处理菜单命令
switch (LOWORD(wParam)) {
case IDM_NEW:
SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)"");
*szFileName = '\0';
break;
case IDM_OPEN:
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = "文本文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = "打开";
ofn.Flags = OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn)) {
HANDLE hFile;
DWORD dwSize, dwRead;
hFile = CreateFile(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
MessageBox(hWnd, "无法打开文件", "错误", MB_ICONERROR);
break;
}
dwSize = GetFileSize(hFile, NULL);
if (dwSize == INVALID_FILE_SIZE) {
MessageBox(hWnd, "无法获取文件大小", "错误", MB_ICONERROR);
CloseHandle(hFile);
break;
}
char* pText = (char*)malloc(dwSize + 1);
if (!pText) {
MessageBox(hWnd, "内存不足", "错误", MB_ICONERROR);
CloseHandle(hFile);
break;
}
if (!ReadFile(hFile, pText, dwSize, &dwRead, NULL)) {
MessageBox(hWnd, "无法读取文件", "错误", MB_ICONERROR);
free(pText);
CloseHandle(hFile);
break;
}
pText[dwSize] = '\0';
SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)pText);
free(pText);
CloseHandle(hFile);
}
}
break;
case IDM_SAVE:
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = "文本文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = "保存";
ofn.Flags = OFN_OVERWRITEPROMPT;
if (GetSaveFileName(&ofn)) {
HANDLE hFile;
DWORD dwSize, dwWritten;
hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
MessageBox(hWnd, "无法创建文件", "错误", MB_ICONERROR);
break;
}
dwSize = GetWindowTextLength(hEdit);
if (dwSize == 0) {
MessageBox(hWnd, "没有内容需要保存", "提示", MB_ICONINFORMATION);
CloseHandle(hFile);
break;
}
char* pText = (char*)malloc(dwSize + 1);
if (!pText) {
MessageBox(hWnd, "内存不足", "错误", MB_ICONERROR);
CloseHandle(hFile);
break;
}
if (!GetWindowText(hEdit, pText, dwSize + 1)) {
MessageBox(hWnd, "无法获取文本内容", "错误", MB_ICONERROR);
free(pText);
CloseHandle(hFile);
break;
}
if (!WriteFile(hFile, pText, dwSize, &dwWritten, NULL)) {
MessageBox(hWnd, "无法保存数据", "错误", MB_ICONERROR);
free(pText);
CloseHandle(hFile);
break;
}
free(pText);
CloseHandle(hFile);
}
}
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
这样就完成了一个简单的 Windows 记事本程序的开发。
阅读全文