博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个简单的文本编辑器。(是在DEV C++下写的)
阅读量:5887 次
发布时间:2019-06-19

本文共 10426 字,大约阅读时间需要 34 分钟。

//头文件//
  main.h
  #define CM_FILE_SAVEAS 9072
  #define CM_FILE_EXIT 9071
  #define CM_FILE_OPEN 9070
  #define CM_ABOUT 9069
  //主程序文件//
  main.c
  /*****************************************************/
  #include <windows.h>
  #pragma hdrstop
  #include "Main.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; // Null terminator
   if(SetWindowText(hEdit, pszFileText))
   bSuccess = TRUE; // It worked!
   }
   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)// No need to bother if there's no text.
   {
   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 = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
   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, "Save file failed.", "Error",
   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, "Load of file failed.", "Error",
   MB_OK | MB_ICONEXCLAMATION);
   return FALSE;
   }
   }
   }
   return TRUE;
  }
  LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  {
   switch(Message)
   {
   case WM_CREATE:
   CreateWindow("EDIT", "",
   WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE |
   ES_WANTRETURN,
   CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
   hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL);
   SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT,
   (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
   break;
   case WM_SIZE:
   if(wParam != SIZE_MINIMIZED)
   MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam),
   HIWORD(lParam), TRUE);
   break;
   case WM_SETFOCUS:
   SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT));
   break;
   case WM_COMMAND:
   switch(LOWORD(wParam))
   {
   case CM_FILE_OPEN:
   DoFileOpenSave(hwnd, FALSE);
   break;
   case CM_FILE_SAVEAS:
   DoFileOpenSave(hwnd, TRUE);
   break;
   case CM_FILE_EXIT:
   PostMessage(hwnd, WM_CLOSE, 0, 0);
   break;
   case CM_ABOUT:
   MessageBox (NULL, "File Editor for Windows !\n Using the Win32 API" , "About...", 0);
   }
   break;
   case WM_CLOSE:
   DestroyWindow(hwnd);
   break;
   case WM_DESTROY:
   PostQuitMessage(0);
   break;
   default:
   return DefWindowProc(hwnd, Message, wParam, lParam);
   }
   return 0;
  }
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
   LPSTR lpCmdLine, int nCmdShow)
  {
   WNDCLASSEX WndClass;
   HWND hwnd;
   MSG Msg;
   g_hInst = hInstance;
   WndClass.cbSize = sizeof(WNDCLASSEX);
   WndClass.style = 0;
   WndClass.lpfnWndProc = WndProc;
   WndClass.cbClsExtra = 0;
   WndClass.cbWndExtra = 0;
   WndClass.hInstance = g_hInst;
   WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
   WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
   WndClass.lpszMenuName = "MAINMENU";
   WndClass.lpszClassName = g_szClassName;
   WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
   if(!RegisterClassEx(&WndClass))
   {
   MessageBox(0, "Window Registration Failed!", "Error!",
   MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
   return 0;
   }
   hwnd = CreateWindowEx(
   WS_EX_CLIENTEDGE,
   g_szClassName,
   "A File Program",
   WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
   NULL, NULL, g_hInst, NULL);
   if(hwnd == NULL)
   {
   MessageBox(0, "Window Creation Failed!", "Error!",
   MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
   return 0;
   }
   ShowWindow(hwnd, nCmdShow);
   UpdateWindow(hwnd);
   while(GetMessage(&Msg, NULL, 0, 0))
   {
   TranslateMessage(&Msg);
   DispatchMessage(&Msg);
   }
   return Msg.wParam;
  }
//头文件// main.h #define CM_FILE_SAVEAS 9072 #define CM_FILE_EXIT 9071 #define CM_FILE_OPEN 9070 #define CM_ABOUT 9069 //主程序文件// main.c /*****************************************************/ #include <windows.h> #pragma hdrstop #include "Main.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; // Null terminator if(SetWindowText(hEdit, pszFileText)) bSuccess = TRUE; // It worked! } 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)// No need to bother if there's no text. { 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 = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0"; 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, "Save file failed.", "Error", 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, "Load of file failed.", "Error", MB_OK | MB_ICONEXCLAMATION); return FALSE; } } } return TRUE; } LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_CREATE: CreateWindow("EDIT", "", WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_WANTRETURN, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL); SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0)); break; case WM_SIZE: if(wParam != SIZE_MINIMIZED) MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE); break; case WM_SETFOCUS: SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT)); break; case WM_COMMAND: switch(LOWORD(wParam)) { case CM_FILE_OPEN: DoFileOpenSave(hwnd, FALSE); break; case CM_FILE_SAVEAS: DoFileOpenSave(hwnd, TRUE); break; case CM_FILE_EXIT: PostMessage(hwnd, WM_CLOSE, 0, 0); break; case CM_ABOUT: MessageBox (NULL, "File Editor for Windows !\n Using the Win32 API" , "About...", 0); } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, Message, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX WndClass; HWND hwnd; MSG Msg; g_hInst = hInstance; WndClass.cbSize = sizeof(WNDCLASSEX); WndClass.style = 0; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = g_hInst; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); WndClass.lpszMenuName = "MAINMENU"; WndClass.lpszClassName = g_szClassName; WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&WndClass)) { MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL); return 0; } hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "A File Program", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 320, 240, NULL, NULL, g_hInst, NULL); if(hwnd == NULL) { MessageBox(0, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }
 

转载地址:http://nqoix.baihongyu.com/

你可能感兴趣的文章
CSS魔法堂:display:none与visibility:hidden的恩怨情仇
查看>>
git 放弃本地修改(转)
查看>>
.NET获取服务器信息,如服务器版本、IIS等
查看>>
你能熟练使用Dictionary字典和List列表吗?
查看>>
读取Json
查看>>
关于DLL文件和EXE文件不在同一目录下的设置
查看>>
C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 密码强化、网络安全强化...
查看>>
web 开发之js---ajax 中的两种返回状态 xmlhttp.status和 xmlhttp.readyState
查看>>
TeX
查看>>
【Machine Learning in Action --2】K-最近邻分类
查看>>
cocos2dx3.1.1+cocosstudio+lua问题总结
查看>>
漫游Kafka设计篇之性能优化(7)
查看>>
MVC在添加控制器的时候发现没有添加的选项了?肿么办?
查看>>
Android AndroidManifest.xml配置文件
查看>>
[原创]自定义BaseAcitivity的实现,统一activity的UI风格样式
查看>>
C# LDAP认证登录
查看>>
spark源码 hashpartitioner
查看>>
Vue文件跳转$router传参数
查看>>
gitlab简单使用教程【转】
查看>>
《荣枯鉴》闻达卷二
查看>>