//头文件//

  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; }

一个简单的文本编辑器。(是在DEV C++下写的)的更多相关文章

  1. python使用wxPython创建一个简单的文本编辑器。

    ubuntu下通过'sudo apt-get install python-wxtools'下载wxPython.load和save函数用于加载和保存文件内容,button通过Bind函数绑定这两个函 ...

  2. 学习vi和vim编辑(3):一个简单的文本编辑器(2)

    然后文章,继续评论vi编辑简单的文本编辑命令. 本文主要是删除的文字.复制,运动命令. 删除文本: 正如上一篇文章中讲过的,对于删除命令("d")也具有"(command ...

  3. C++ 容器的综合应用的一个简单实例——文本查询程序

    C++ 容器的综合应用的一个简单实例——文本查询程序 [0. 需求] 最近在粗略学习<C++ Primer 4th>的容器内容,关联容器的章节末尾有个很不错的实例.通过实现一个简单的文本查 ...

  4. 原生JS实现简单富文本编辑器2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 原生JS实现简单富文本编辑器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 实现一个最简单的VIM文本编辑器(可能有bug,随便写了一个)

    简单的写了一个文本编辑器,功能很简单,但足以把文件IO相关的操作熟悉了,可能功能或者分配的大小还不够完善.请参考参考: #include <stdio.h> #include <co ...

  7. JS 的execCommand 方法 做的一个简单富文本

    execCommand 当一个 HTML 文档切换到设计模式(designMode)时,文档对象暴露 execCommand 方法,该方法允许运行命令来操纵可编辑区域的内容.大多数命令影响文档的选择( ...

  8. C语言实例解析精粹学习笔记——39(简单的文本编辑器)

    实例说明: 编辑一个简单的单行文本编辑器,编辑命令有以下几种:(E.Q.R.I.D) 只有自己在完全空白的情况下编写出来的程序,才是真正自己会的程序,现在所做的,不过是程序的搬运工,把书上的程序搬到网 ...

  9. Win7下安装一个装逼文本编辑器Atom + activate-power-mode插件

    Atom是Github推出的一个文本编辑器,搜索一下大概是给Web前端用的,最近比较火的是他的一个插件activate-power-mode,可以实现打字屏振效果. 用来装装逼还是挺适合的,本来想试试 ...

随机推荐

  1. cookie简介

    上例子 1.首先要用php创建cookie发送给客户端,利用setcookie()方法即可 <?php /* * * @Authors peng--jun * @Email 1098325951 ...

  2. php数据类型有哪些?

    php数据类型有哪些?有三大类1.基本数据类型 1.1整型 $a = 0123; // 八进制数(是以0开头) 83 $a = 0x1A; // 十六进制数              26 1.2小数 ...

  3. ubuntu远程windows服务器

    ubuntu端: sudo apt-get install rdesktop windows端: 需要允许此windows远程访问.我的windows是windows server2012,基本操作: ...

  4. c语言中 %p的含义

    格式控制符“%p”中的p是pointer(指针)的缩写.指针的值是语言实现(编译程序)相关的,但几乎所有实现中,指针的值都是一个表示地址空间中某个存储器单元的整数.printf函数族中对于%p一般以十 ...

  5. Android下pm 命令详解

    Sam在看相关PackageManager代码时,无意中发现Android 下提供一个pm命令,通常放在/system/bin/下.这个命令与Package有关,且非常实用.所以研究之.0. Usag ...

  6. 浅谈Web Api配合SignalR的跨域支持

    最近接手的一个项目中,涉及到一个简单的消息模块,由于之前有简单了解过SignalR,所以打算尝试着摸索摸索~! 首先,通过Nuget管理器添加Microsoft ASP.NET SignalR引用~目 ...

  7. 编译时出现clock skew detected, your build may be incompeleted

    错误原因为文件修改时间大于系统时间,这时候如果date输出系统时间,会发现这个时间是错误的.在nachos实习时多次出现这个错误,简单的方法尝试make多次直到有一次出现'nachos' is up ...

  8. collection系列用法-deque双向队列

    deque双向队列 Deque可以从两端添加和删除元素.常用的结构,是它的简化版本. Deque支持序列的常用操作,现在举一个简单例子,你会发现其实跟平成的list没啥区别: import colle ...

  9. 07.15 first与first-child的区别

    举例: $("ul li:first");  //选取第一个 <ul> 元素的第一个 <li> 元素 $("ul li:first-child&q ...

  10. 《Linux命令行与shell脚本编程大全》 第十四章 学习笔记

    第十四章:呈现数据 理解输入与输出 标准文件描述符 文件描述符 缩写 描述 0 STDIN 标准输入 1 STDOUT 标准输出 2 STDERR 标准错误 1.STDIN 代表标准输入.对于终端界面 ...