分享
 
 
 

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

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

//头文件//

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有