|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 无符号整形 于 2016-8-7 18:11 编辑
大家好!我是无符号整形。以前是自学去的,后来自学去关了,就到这来玩。
哈哈哈!废话不多说,上代码和图片!
上不了了,我是新手……自己看看吧。。。
代码(无rc文件):
- #define _CRT_SECURE_NO_WARNINGS
- #include <windows.h>
- #include<stdio.h>
- #include<tchar.h>
- #include"resource.h"
- HINSTANCE g_hInstance;
- static LOGFONT logfont;
- static HFONT hFont;
- BOOL bUserAbort;
- static HWND hDlgModeless;
- HWND hDlgPrint;
- //窗口过程函数
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- BOOL CALLBACK PrintDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
- BOOL CALLBACK AbortProc(HDC hPrinterDC, int iCode);
- BOOL CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);
- void PopFontInitialize(HWND);
- BOOL PopPrntPrintFile(HINSTANCE, HWND, HWND, PTSTR);
- BOOL PopFontChooseFont(HWND);
- void PopFontSetFont(HWND);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
- {
- //定义窗口类结构体变量
- WNDCLASS wc;
- //窗口类名称
- static TCHAR* szAppName = TEXT("mymenu");
- //窗口句柄
- HWND hwnd = NULL;
- //消息结构
- MSG msg;
-
- /*下面的代码填充窗口类信息,如图标,鼠标样式,背景,过程函数等
- */
- wc.style = CS_HREDRAW | CS_VREDRAW; //窗口样式
- wc.lpfnWndProc = WndProc; //过程函数
- wc.cbClsExtra = 0; //扩展字段
- wc.cbWndExtra = 0; //扩展字段
- wc.hInstance = hInstance; //当前实例句柄
- wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDC)); //设置程序图标
- wc.hCursor = LoadCursor(NULL, IDC_ARROW); //设置鼠标
- //用白色画刷填充背景
- wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- //菜单
- wc.lpszMenuName = NULL;
- //类名
- wc.lpszClassName = szAppName;
- if (!RegisterClass(&wc))
- {
- MessageBox(NULL, TEXT("程序只能在windowsNT下运行"),
- szAppName, MB_ICONERROR);
- return 0;
- }
-
- HMENU hMenu, hMenuPopup;
- hMenu = CreateMenu();
- hMenuPopup = CreateMenu();
- AppendMenu(hMenuPopup, MF_STRING, 101, "新建");
- AppendMenu(hMenuPopup, MF_STRING, 102, "打开...");
- AppendMenu(hMenuPopup, MF_STRING, 103, "保存");
- AppendMenu(hMenuPopup, MF_STRING, 104, "另存为...");
- AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
- AppendMenu(hMenuPopup, MF_STRING, 200, "打印...");
- AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
- AppendMenu(hMenuPopup, MF_STRING, 112, "退出");
- AppendMenu(hMenu, MF_POPUP, (ULONG_PTR)hMenuPopup, "文件");
- hMenuPopup = CreateMenu();
- AppendMenu(hMenuPopup, MF_STRING, 105, "调整字体...");
- AppendMenu(hMenu, MF_POPUP, (ULONG_PTR)hMenuPopup, "格式");
- hMenuPopup = CreateMenu();
- AppendMenu(hMenuPopup, MF_STRING, 400, "撤销\tCtrl+Z");
- AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
- AppendMenu(hMenuPopup, MF_STRING, 401, "剪切\tCtrl+X");
- AppendMenu(hMenuPopup, MF_STRING, 402, "复制\tCtrl+C");
- AppendMenu(hMenuPopup, MF_STRING, 403, "粘贴\tCtrl+V");
- AppendMenu(hMenuPopup, MF_STRING, 404, "清除");
- AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
- AppendMenu(hMenuPopup, MF_STRING, 405, "时间");
- AppendMenu(hMenu, MF_POPUP, (ULONG_PTR)hMenuPopup, "编辑");
- hMenuPopup = CreateMenu();
- AppendMenu(hMenuPopup, MF_STRING, 500, "关于...");
- AppendMenu(hMenu, MF_POPUP, (ULONG_PTR)hMenuPopup, "更多");
- //创建窗口
- hwnd = CreateWindow(szAppName, //要注册的窗口类名
- TEXT("文档编辑器"), //窗口标题
- WS_OVERLAPPEDWINDOW, //窗口样式
- CW_USEDEFAULT, //窗口距离屏幕左上角都横坐标
- CW_USEDEFAULT, //窗口距离屏幕左上角都纵坐标
- 1005, //窗口宽度
- 511, //窗口高度
- NULL, //父窗口句柄
- //NULL,
- hMenu, //菜单句柄
- hInstance, //当前实例句柄
- NULL); //指向一个值的指针,该值传递给窗口 WM_CREATE消息。一般为NULL
- g_hInstance = hInstance;
- //显示窗口
- ShowWindow(hwnd, iCmdShow);
- //更新窗口
- UpdateWindow(hwnd);
-
-
- //像操作系统注册窗口类
- //消息循环,一直停在这里,退出消息循环就表示程序结束了。
- while (GetMessage(&msg, NULL, 0, 0))
- {
- //翻译消息
- TranslateMessage(&msg);
- //分发消息
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- //消息处理函数
- LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- static HWND hBtn;
- static BOOL bFile = FALSE;
- static TCHAR szBuffer[1048576];//主缓冲区
- static TCHAR szBuffer1[1048576];
- static TCHAR szBufferpath[MAX_PATH];
- static OPENFILENAME ofn;
- static HANDLE hFile;
- static DWORD dFileSize;
- static LPDWORD pdwNumBytesRead;
- static TCHAR szData[1048576];
- static LPDWORD pdwNumBytesWrite;
- static TCHAR chErr[1000];
- static DWORD dErr;
- static SYSTEMTIME st;
- static TCHAR szSystemTime[100];
- static LPFINDREPLACE pfr;
- switch (message)
- {
- case WM_CREATE:
- hBtn = CreateWindow("edit", "",
- WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL | WS_HSCROLL |
- ES_MULTILINE | ES_WANTRETURN | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
- 0, 0, 985, 451, hwnd, (HMENU)1, g_hInstance, NULL);
- return 0;
- case WM_QUERYENDSESSION:
- return 0;
- break;
- case WM_COMMAND:
- switch(LOWORD(wParam))
- {
- case 112:
- SendMessage(hwnd,WM_CLOSE,0,0);
- return 0;
- case 101:
- GetWindowText(hBtn, szBuffer, 1048576);
- if (strcmp(szBuffer, szBuffer1) != 0)
- {
- int ret = MessageBox(hwnd, "有未保存的工作,是否保存?", "", MB_YESNO | MB_ICONQUESTION);
- if (ret == IDYES)
- {
- ofn = { sizeof(OPENFILENAME) };
- ofn.lpstrFilter = "";
- ofn.lpstrInitialDir = NULL;
- ofn.lpstrFile = szBufferpath;
- ofn.nMaxFile = sizeof(szBufferpath) / sizeof(*szBufferpath);
- ofn.nFilterIndex = 0;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
- if (!GetSaveFileName(&ofn))
- {
- return 0;
- }
- hFile = CreateFile(szBufferpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
- CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- dErr = GetLastError();
- sprintf_s(chErr, _countof(chErr), "文件操作失败!错误代码:%d", dErr);
- MessageBox(hwnd, chErr, "错误", MB_OK | MB_ICONERROR);
- return 0;
- }
- CloseHandle(hFile);
- }
- }
- SetWindowText(hBtn, "");
- bFile = FALSE;
- break;
- case 102:
- ofn = { sizeof(OPENFILENAME) };
- ofn.lpstrFilter = "";
- ofn.lpstrInitialDir = NULL;
- ofn.lpstrFile = szBufferpath;
- ofn.nMaxFile = sizeof(szBufferpath) / sizeof(*szBufferpath);
- ofn.nFilterIndex = 0;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
- if (!GetOpenFileName(&ofn))
- {
- return 0;
- }
- hFile = CreateFile(szBufferpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == (HANDLE)-1)
- {
- dErr = GetLastError();
- sprintf_s(chErr, _countof(chErr), "文件操作失败!错误代码:%d", dErr);
- MessageBox(hwnd, chErr, "错误", MB_OK | MB_ICONERROR);
- return 0;
- }
- dFileSize = GetFileSize(hFile, &dFileSize);
- ReadFile(hFile, szBuffer, dFileSize, pdwNumBytesRead, NULL);
- SetWindowText(hBtn, szBuffer);
- CloseHandle(hFile);
- break;
- case 103:
- if (bFile)
- {
- strcpy(szBuffer1, szBuffer);
- hFile = CreateFile(szBufferpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- dErr = GetLastError();
- sprintf_s(chErr, _countof(chErr), "文件操作失败!错误代码:%d", dErr);
- MessageBox(hwnd, chErr, "错误", MB_OK | MB_ICONERROR);
- return 0;
- }
- GetWindowText(hBtn, szData, 1048576);
- dFileSize = GetFileSize(hFile, &dFileSize);
- WriteFile(hFile, szData, dFileSize, pdwNumBytesWrite, NULL);
- CloseHandle(hFile);
- }
- else
- {
- bFile = TRUE;
- ofn = { sizeof(OPENFILENAME) };
- ofn.lpstrFilter = "";
- ofn.lpstrInitialDir = NULL;
- ofn.lpstrFile = szBufferpath;
- ofn.nMaxFile = sizeof(szBufferpath) / sizeof(*szBufferpath);
- ofn.nFilterIndex = 0;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
- if (!GetSaveFileName(&ofn))
- {
- bFile = FALSE;
- return 0;
- }
- goto saveas;
- }
- break;
- case 104:
- ofn = { sizeof(OPENFILENAME) };
- ofn.lpstrFilter = "";
- ofn.lpstrInitialDir = NULL;
- ofn.lpstrFile = szBufferpath;
- ofn.nMaxFile = sizeof(szBufferpath) / sizeof(*szBufferpath);
- ofn.nFilterIndex = 0;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
- if (!GetSaveFileName(&ofn))
- {
- return 0;
- }
- saveas:
- hFile = CreateFile(szBufferpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
- CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- dErr = GetLastError();
- sprintf_s(chErr, _countof(chErr), "文件操作失败!错误代码:%d", dErr);
- MessageBox(hwnd, chErr, "错误", MB_OK | MB_ICONERROR);
- CloseHandle(hFile);
- return 0;
- }
- GetWindowText(hBtn, szData, 1048576);
- WriteFile(hFile, szData, strlen(szData), NULL, NULL);
- dFileSize = GetFileSize(hFile, &dFileSize);
- CloseHandle(hFile);
- break;
- case 105:
- MessageBox(hwnd, "请注意!选择字体时不能选择以“@”号开头的字体!否则字体全部会翻转270度!", "请注意", MB_OK | MB_ICONEXCLAMATION);
- if (PopFontChooseFont(hwnd))
- PopFontSetFont(hBtn);
- break;
- case 200:
- PopPrntPrintFile(g_hInstance, hwnd, hBtn, szBufferpath);
- break;
- case 400:
- SendMessage(hBtn, WM_UNDO, 0, 0);
- return 0;
- case 401:
- SendMessage(hBtn, WM_CUT, 0, 0);
- return 0;
- case 402:
- SendMessage(hBtn, WM_COPY, 0, 0);
- return 0;
- case 403:
- SendMessage(hBtn, WM_PASTE, 0, 0);
- return 0;
- case 404:
- SendMessage(hBtn, WM_CLEAR, 0, 0);
- return 0;
- case 405:
- GetLocalTime(&st);
- sprintf_s(szSystemTime, _countof(szSystemTime), "%d/%d/%d %d:%d:%d",st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
- GetWindowText(hBtn, szData, 1048576);
- strcat(szData, szSystemTime);
- SetWindowText(hBtn, szData);
- return 0;
- case 500:
- DialogBox(g_hInstance, "NOTEPAD", hwnd, AboutDlgProc);
- return 0;
- }
- return 0;
- case WM_SIZE:
- MoveWindow(hBtn, 0, 0, LOWORD(lParam), HIWORD(lParam),FALSE);
- return 0;
- case WM_CLOSE:
- GetWindowText(hBtn, szBuffer, 1048576);
- if (strcmp(szBuffer, szBuffer1) != 0)
- {
- int ret = MessageBox(hwnd, "有未保存的工作,是否保存?", "", MB_YESNO | MB_ICONQUESTION);
- if (ret == IDYES)
- {
- ofn = { sizeof(OPENFILENAME) };
- ofn.lpstrFilter = "";
- ofn.lpstrInitialDir = NULL;
- ofn.lpstrFile = szBufferpath;
- ofn.nMaxFile = sizeof(szBufferpath) / sizeof(*szBufferpath);
- ofn.nFilterIndex = 0;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
- GetSaveFileName(&ofn);
- hFile = CreateFile(szBufferpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
- CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- dErr = GetLastError();
- sprintf_s(chErr, _countof(chErr), "文件操作失败!错误代码:%d", dErr);
- MessageBox(hwnd, chErr, "错误", MB_OK | MB_ICONERROR);
- return 0;
- }
- CloseHandle(hFile);
- }
- else
- {
- DeleteObject(hFont);
- PostQuitMessage(0);
- }
- return 0;
- }
- else
- {
- DeleteObject(hFont);
- PostQuitMessage(0);
- }
- return 0;
- }
- //调用默认的过程函数
- return DefWindowProc(hwnd, message, wParam, lParam);
- }
- BOOL PopFontChooseFont(HWND hwnd)
- {
- CHOOSEFONT cf;
- cf.lStructSize = sizeof (CHOOSEFONT);
- cf.hwndOwner = hwnd;
- cf.hDC = NULL;
- cf.lpLogFont = &logfont;
- cf.iPointSize = 0;
- cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_SCREENFONTS;
- cf.rgbColors = 0;
- cf.lCustData = 0;
- cf.lpfnHook = NULL;
- cf.lpTemplateName = NULL;
- cf.hInstance = NULL;
- cf.lpszStyle = NULL;
- cf.nFontType = 0; // Returned from ChooseFont
- cf.nSizeMin = 0;
- cf.nSizeMax = 0;
- return ChooseFont(&cf);
- }
- void PopFontInitialize(HWND hwndEdit)
- {
- GetObject(GetStockObject(SYSTEM_FONT), sizeof (LOGFONT),
- (PTSTR)&logfont);
- hFont = CreateFontIndirect(&logfont);
- SendMessage(hwndEdit, WM_SETFONT, (WPARAM)hFont, 0);
- }
- void PopFontSetFont(HWND hwndEdit)
- {
- HFONT hFontNew;
- RECT rect;
- hFontNew = CreateFontIndirect(&logfont);
- SendMessage(hwndEdit, WM_SETFONT, (WPARAM)hFontNew, 0);
- DeleteObject(hFont);
- hFont = hFontNew;
- GetClientRect(hwndEdit, &rect);
- InvalidateRect(hwndEdit, &rect, TRUE);
- }
- void PopFontDeinitialize(void)
- {
- DeleteObject(hFont);
- }
- BOOL CALLBACK PrintDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch (msg)
- {
- case WM_INITDIALOG:
- EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
- return TRUE;
- case WM_COMMAND:
- bUserAbort = TRUE;
- EnableWindow(GetParent(hDlg), TRUE);
- DestroyWindow(hDlg);
- hDlgPrint = NULL;
- return TRUE;
- }
- return FALSE;
- }
- BOOL CALLBACK AbortProc(HDC hPrinterDC, int iCode)
- {
- MSG msg;
- while (!bUserAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return !bUserAbort;
- }
- BOOL PopPrntPrintFile(HINSTANCE hInst, HWND hwnd, HWND hwndEdit,
- PTSTR szTitleName)
- {
- static DOCINFO di = { sizeof (DOCINFO) };
- static PRINTDLG pd;
- BOOL bSuccess;
- int yChar, iCharsPerLine, iLinesPerPage, iTotalLines,
- iTotalPages, iPage, iLine, iLineNum;
- PTSTR pstrBuffer;
- TCHAR szJobName[64 + MAX_PATH];
- TEXTMETRIC tm;
- WORD iColCopy, iNoiColCopy;
- pd.lStructSize = sizeof (PRINTDLG);
- pd.hwndOwner = hwnd;
- pd.hDevMode = NULL;
- pd.hDevNames = NULL;
- pd.hDC = NULL;
- pd.Flags = PD_ALLPAGES | PD_COLLATE |
- PD_RETURNDC | PD_NOSELECTION;
- pd.nFromPage = 0;
- pd.nToPage = 0;
- pd.nMinPage = 0;
- pd.nMaxPage = 0;
- pd.nCopies = 1;
- pd.hInstance = NULL;
- pd.lCustData = 0L;
- pd.lpfnPrintHook = NULL;
- pd.lpfnSetupHook = NULL;
- pd.lpPrintTemplateName = NULL;
- pd.lpSetupTemplateName = NULL;
- pd.hPrintTemplate = NULL;
- pd.hSetupTemplate = NULL;
- if (!PrintDlg(&pd))
- return TRUE;
- if (0 == (iTotalLines = SendMessage(hwndEdit, EM_GETLINECOUNT, 0, 0)))
- return TRUE;
- GetTextMetrics(pd.hDC, &tm);
- yChar = tm.tmHeight + tm.tmExternalLeading;
- iCharsPerLine = GetDeviceCaps(pd.hDC, HORZRES) / tm.tmAveCharWidth;
- iLinesPerPage = GetDeviceCaps(pd.hDC, VERTRES) / yChar;
- iTotalPages = (iTotalLines + iLinesPerPage - 1) / iLinesPerPage;
- pstrBuffer = (PTSTR)malloc(sizeof (TCHAR)* (iCharsPerLine + 1));
- EnableWindow(hwnd, FALSE);
- bSuccess = TRUE;
- bUserAbort = FALSE;
- SetAbortProc(pd.hDC, AbortProc);
- GetWindowText(hwnd, szJobName, sizeof (szJobName));
- di.lpszDocName = szJobName;
- if (StartDoc(pd.hDC, &di) > 0)
- {
- for (iColCopy = 0;
- iColCopy < ((WORD)pd.Flags & PD_COLLATE ? pd.nCopies : 1);
- iColCopy++)
- {
- for (iPage = 0; iPage < iTotalPages; iPage++)
- {
- for (iNoiColCopy = 0;
- iNoiColCopy < (pd.Flags & PD_COLLATE ? 1 : pd.nCopies);
- iNoiColCopy++)
- {
-
- if (StartPage(pd.hDC) < 0)
- {
- bSuccess = FALSE;
- break;
- }
- for (iLine = 0; iLine < iLinesPerPage; iLine++)
- {
- iLineNum = iLinesPerPage * iPage + iLine;
- if (iLineNum > iTotalLines)
- break;
- *(int *)pstrBuffer = iCharsPerLine;
- TextOut(pd.hDC, 0, yChar * iLine, pstrBuffer,
- (int)SendMessage(hwndEdit, EM_GETLINE,
- (WPARAM)iLineNum, (LPARAM)pstrBuffer));
- }
- if (EndPage(pd.hDC) < 0)
- {
- bSuccess = FALSE;
- break;
- }
- if (bUserAbort)
- break;
- }
- if (!bSuccess || bUserAbort)
- break;
- }
- if (!bSuccess || bUserAbort)
- break;
- }
- }
- else
- bSuccess = FALSE;
- if (bSuccess)
- EndDoc(pd.hDC);
- if (!bUserAbort)
- {
- EnableWindow(hwnd, TRUE);
- DestroyWindow(hDlgPrint);
- }
- free(pstrBuffer);
- DeleteDC(pd.hDC);
- return bSuccess && !bUserAbort;
- }
- BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- return TRUE;
- case WM_COMMAND:
- EndDialog(hDlg, 0);
- return TRUE;
- }
- return FALSE;
- }
复制代码
OKOKOK附件来也!
百度云:http://pan.baidu.com/s/1skK7GWP 无密码
360云:https://yunpan.cn/cRM9StGWKSXEU 访问密码 0db4
如果失效了 请回复通知。
顺便说说
1.运行uninst.exe的时候360会报毒,请退出360或添加信任
2.为了让软件内的数字签名生效,请安装安全证书!
哈哈888 |
|