用C编写的记事本
作者:yueguanghaidao 发布时间:[ 2016/12/1 10:55:38 ] 推荐标签:记事本 语言
近想好好研究C,于是便拿记事本这个小程序练练手。
自己从网上找了一个挺不错的,从这个开始学习吧。。。。
一:代码
先建立一文件夹->方便管理与查看
拿我来说,在d盘根目录下新建note文件夹
1.新建一note.rc资源文件,代码如下。
#include "sample.h"
A ICON MOVEABLE PURE LOADONCALL DISCARDABLE "sample.ico"
MAINMENU MENU
{
POPUP "文件(&F)"
{
MENUITEM "打开(&O)...", CM_FILE_OPEN
MENUITEM "另存为(&S)...", CM_FILE_SAVEAS
MENUITEM SEPARATOR
MENUITEM "关闭", CM_FILE_EXIT
}
POPUP "帮助(&H)"
{
MENUITEM "关于(&A)", CM_ABOUT
}
}
2.新建sample.h,代码如下
#define CM_FILE_SAVEAS 9072
#define CM_FILE_EXIT 9071
#define CM_FILE_OPEN 9070
#define CM_ABOUT 9069
3.新建源程序note.c ,代码如下
#include <windows.h>
#include "sample.h"
static char g_szClassName[] = "MyWindowClass";
static HINSTANCE g_hInst = NULL;
#define IDC_MAIN_TEXT 1001
BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
LPSTR pszFileText;
pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
if(pszFileText != NULL)
{
DWORD dwRead;
if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
{
pszFileText[dwFileSize] = 0;
if(SetWindowText(hEdit, pszFileText))
bSuccess = TRUE;
}
GlobalFree(pszFileText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwTextLength;
dwTextLength = GetWindowTextLength(hEdit);
if(dwTextLength > 0)
{
LPSTR pszText;
pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
if(pszText != NULL)
{
if(GetWindowText(hEdit, pszText, dwTextLength + 1))
{
DWORD dwWritten;
if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
bSuccess = TRUE;
}
GlobalFree(pszText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL DoFileOpenSave(HWND hwnd, BOOL bSave)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
szFileName[0] = 0;
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "文本文件 (*.txt) *.txt 所有文件 (*.*) *.* ";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
if(bSave)
{
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT;
if(GetSaveFileName(&ofn))
{
if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
{
MessageBox(hwnd, "保存文件失败.", "错误信息",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
else
{
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if(GetOpenFileName(&ofn))
{
if(!LoadFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
{
MessageBox(hwnd, "打开文件失败.", "错误信息",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
return TRUE;
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11