对应的下载地址:
http://download.csdn.net/detail/zengraoli/5311361
// Win32Project1.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "Win32Project1.h" #include "PopFile.h" #define EDITID 1 #define MAX_LOADSTRING 100 #define UNTITLED TEXT ("(untitled)") #pragma comment(lib, "PopFile.lib") PopFile *pop_file; // 全局变量: HINSTANCE hInst; // 当前实例 // 此代码模块中包含的函数的前向声明: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void ShowMessage(HWND hwnd, TCHAR *szMessage, TCHAR *szTitleName); short AskAboutSave(HWND hwnd, TCHAR *szTitleName); int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。 MSG msg = {0}; // 初始化全局字符串 MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } // 主消息循环: while (msg.message != WM_QUIT) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { Sleep(100); } } return (int) msg.wParam; } // // 函数: MyRegisterClass() // // 目的: 注册窗口类。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT1); wcex.lpszClassName = L"test"; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } // // 函数: InitInstance(HINSTANCE, int) // // 目的: 保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // 将实例句柄存储在全局变量中 hWnd = CreateWindow(L"test", L"test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目的: 处理主窗口的消息。 // // WM_COMMAND - 处理应用程序菜单 // WM_PAINT - 绘制主窗口 // WM_DESTROY - 发送退出消息并返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static BOOL bNeedSave = FALSE ; static HINSTANCE hInst ; static HWND hWndEdit; int wmId, wmEvent; static TCHAR szFileName[MAX_PATH], szTitleName[MAX_PATH] ; switch (message) { case WM_CREATE: hInst = ((LPCREATESTRUCT)lParam)->hInstance; hWndEdit = CreateWindow (TEXT ("edit"), NULL, WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0, 0, 0, 0, hWnd, (HMENU) EDITID, hInst, NULL) ; SendMessage(hWndEdit, EM_LIMITTEXT, 32000, 0L) ; // create and Initialize pop file pop_file = new PopFile(hWnd, hWndEdit); break; case WM_SETFOCUS: SetFocus(hWndEdit) ; return 0 ; case WM_SIZE: MoveWindow(hWndEdit, 0, 0, LOWORD (lParam), HIWORD (lParam), TRUE); return 0 ; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 分析菜单选择: switch (wmId) { case ID_NEW: if (bNeedSave && IDCANCEL == AskAboutSave (hWnd, szTitleName)) return 0 ; SetWindowText(hWndEdit, TEXT ("\0")) ; szFileName[0] = '\0'; szTitleName[0] = '\0'; bNeedSave = FALSE ; return 0 ; case ID_OPEN: if (bNeedSave && IDCANCEL == AskAboutSave(hWnd, szTitleName)) return 0 ; // 这里做一下初始化的判断 if (pop_file->PopFileOpenDlg(szFileName, szTitleName)) { if (!pop_file->PopFileRead(szFileName)) { ShowMessage(hWnd, TEXT ("Could not read file %s!"), szTitleName) ; szFileName[0] = '\0' ; szTitleName[0] = '\0' ; } } bNeedSave = FALSE ; return 0 ; case ID_SAVE: if (szFileName[0]) { if (pop_file->PopFileWrite(szFileName)) { bNeedSave = FALSE ; return 1 ; } else { ShowMessage(hWnd, TEXT ("Could not write file %s"), szTitleName) ; return 0 ; } } // fall through case ID_SAVEAS: if (pop_file->PopFileSaveDlg(szFileName, szTitleName)) { if (pop_file->PopFileWrite(szFileName)) { bNeedSave = FALSE ; return 1 ; } else { ShowMessage(hWnd, TEXT ("Could not write file %s"), szTitleName) ; return 0 ; } } return 0 ; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_KEYDOWN: if (wParam == VK_ESCAPE) { SendMessage(hWnd, WM_CLOSE, 0, 0); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } short AskAboutSave(HWND hwnd, TCHAR * szTitleName) { TCHAR szBuffer[64 + MAX_PATH] ; int iReturn ; wsprintf (szBuffer, TEXT ("Save current changes in %s?"), szTitleName[0] ? szTitleName : UNTITLED) ; iReturn = MessageBox (hwnd, szBuffer, L"test", MB_YESNOCANCEL | MB_ICONQUESTION) ; if (iReturn == IDYES) if (!SendMessage (hwnd, WM_COMMAND, ID_SAVE, 0)) iReturn = IDCANCEL ; return iReturn ; } void ShowMessage(HWND hwnd, TCHAR * szMessage, TCHAR * szTitleName) { TCHAR szBuffer[64 + MAX_PATH] ; wsprintf(szBuffer, szMessage, szTitleName[0] ? szTitleName : UNTITLED) ; MessageBox(hwnd, szBuffer, L"test", MB_OK | MB_ICONEXCLAMATION) ; }
下载地址:
http://download.csdn.net/detail/zengraoli/5311397
.h:
#include <windows.h> #include <commdlg.h> #ifdef MYLIBAPI #else #define MYLIBAPI __declspec(dllimport) #endif #define RELEASE(p) if (p) \ { \ delete p; \ p = NULL; \ } #define FILTER TEXT ("Text Files (*.TXT)\0*.txt\0") \ TEXT ("All Files (*.*)\0*.*\0\0") // default file extend #define DEFEXT_FILE_POSTFIX TEXT ("txt") class MYLIBAPI PopFile { public: PopFile(HWND hWnd, HWND hWndEdit = NULL); BOOL PopFileOpenDlg(PTSTR pstrFileName, PTSTR pstrTitleName); BOOL PopFileSaveDlg(PTSTR pstrFileName, PTSTR pstrTitleName); BOOL PopFileRead(PTSTR pstrFileName); BOOL PopFileWrite(PTSTR petrFileName); private: void PopFileInitialize(TCHAR Filter[] = FILTER); private: OPENFILENAME ofn_; HWND hWnd_; HWND hWndEdit_; };
// PopFile.cpp : 定义 DLL 应用程序的导出函数。 // #define MYLIBAPI __declspec(dllexport) #include "PopFile.h" /// <summary> /// create a new instance of the class, and Initializes. /// </summary> /// <param name="hWnd">The Pop parent hwnd.</param> /// <param name="hWndEdit">The read or write purpose hwnd.</param> PopFile::PopFile(HWND hWnd, HWND hWndEdit) { hWnd_ = hWnd; hWndEdit_ = hWndEdit; PopFileInitialize(); } void PopFile::PopFileInitialize(TCHAR Filter[]) { ofn_.lStructSize = sizeof (OPENFILENAME) ; ofn_.hwndOwner = hWnd_ ; ofn_.hInstance = NULL ; ofn_.lpstrFilter = Filter ; ofn_.lpstrCustomFilter = NULL ; ofn_.nMaxCustFilter = 0 ; ofn_.nFilterIndex = 0 ; ofn_.lpstrFile = NULL ; // Set in Open and Close functions ofn_.nMaxFile = MAX_PATH ; ofn_.lpstrFileTitle = NULL ; // Set in Open and Close functions ofn_.nMaxFileTitle = MAX_PATH ; ofn_.lpstrInitialDir = NULL ; ofn_.lpstrTitle = NULL ; ofn_.Flags = 0 ; // Set in Open and Close functions ofn_.nFileOffset = 0 ; ofn_.nFileExtension = 0 ; ofn_.lpstrDefExt = DEFEXT_FILE_POSTFIX; ofn_.lCustData = 0L ; ofn_.lpfnHook = NULL ; ofn_.lpTemplateName = NULL ; } /// <summary> /// Pops the file open DLG. /// </summary> /// <param name="pstrFileName">指向包含初始化文件名编辑控件使用的文件名的缓冲.</param> /// <param name="pstrTitleName">指向接收选择的文件的文件名和扩展名的缓冲.</param> /// <returns>BOOL.</returns> BOOL PopFile::PopFileOpenDlg(PTSTR pstrFileName, PTSTR pstrTitleName) { ofn_.hwndOwner = hWnd_ ; ofn_.lpstrFile = pstrFileName ; ofn_.lpstrFileTitle = pstrTitleName ; ofn_.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT ; return GetOpenFileName (&ofn_) ; } BOOL PopFile::PopFileSaveDlg(PTSTR pstrFileName, PTSTR pstrTitleName) { ofn_.hwndOwner = hWnd_ ; ofn_.lpstrFile = pstrFileName ; ofn_.lpstrFileTitle = pstrTitleName ; ofn_.Flags = OFN_OVERWRITEPROMPT ; return GetSaveFileName (&ofn_) ; } /// <summary> /// Pops the file read. /// </summary> /// <param name="pstrFileName">file name and path to read.</param> /// <returns>BOOL.</returns> BOOL PopFile::PopFileRead(PTSTR pstrFileName) { if (NULL != hWndEdit_) { BYTE bySwap ; DWORD dwBytesRead ; HANDLE hFile ; int i, iFileLength, iUniTest ; PBYTE pBuffer, pText, pConv ; // Open the file. if (INVALID_HANDLE_VALUE == (hFile = CreateFile(pstrFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL))) return FALSE ; // Get file size in bytes and allocate memory for read. // Add an extra two bytes for zero termination. iFileLength = GetFileSize (hFile, NULL) ; pBuffer = new BYTE[iFileLength + 2]; // Read file and put terminating zeros at end. ReadFile(hFile, pBuffer, iFileLength, &dwBytesRead, NULL) ; CloseHandle(hFile) ; pBuffer[iFileLength] = '\0' ; pBuffer[iFileLength + 1] = '\0' ; // Test to see if the text is Unicode iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE ; if (IsTextUnicode(pBuffer, iFileLength, &iUniTest)) { pText = pBuffer + 2 ; iFileLength -= 2 ; if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE) { for (i = 0 ; i < iFileLength / 2 ; i++) { bySwap = ((BYTE *) pText) [2 * i] ; ((BYTE *) pText) [2 * i] = ((BYTE *) pText) [2 * i + 1] ; ((BYTE *) pText) [2 * i + 1] = bySwap ; } } // Allocate memory for possibly converted string pConv = new BYTE[iFileLength + 2]; // If the edit control is not Unicode, convert Unicode text to // non-Unicode (ie, in general, wide character). #ifndef UNICODE WideCharToMultiByte(CP_ACP, 0, (PWSTR) pText, -1, pConv, iFileLength + 2, NULL, NULL) ; // If the edit control is Unicode, just copy the string #else lstrcpy((PTSTR) pConv, (PTSTR) pText) ; #endif } else // the file is not Unicode { pText = pBuffer ; // Allocate memory for possibly converted string. pConv = new BYTE[2 * iFileLength + 2]; // If the edit control is Unicode, convert ASCII text. #ifdef UNICODE MultiByteToWideChar(CP_ACP, 0, (LPCCH)pText, -1, (PTSTR) pConv, iFileLength + 1) ; // If not, just copy buffer #else lstrcpy((PTSTR) pConv, (PTSTR) pText) ; #endif } SetWindowText(hWndEdit_, (PTSTR) pConv) ; RELEASE(pBuffer); RELEASE(pConv); } return TRUE ; } /// <summary> /// Pops the file read. /// </summary> /// <param name="pstrFileName">file name and path to write.</param> /// <returns>BOOL.</returns> BOOL PopFile::PopFileWrite(PTSTR pstrFileName) { if (NULL != hWndEdit_) { DWORD dwBytesWritten ; HANDLE hFile ; int iLength ; PTSTR pstrBuffer ; WORD wByteOrderMark = 0xFEFF ; // Open the file, creating it if necessary if (INVALID_HANDLE_VALUE == (hFile = CreateFile(pstrFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL))) return FALSE ; // Get the number of characters in the edit control and allocate // memory for them. iLength = GetWindowTextLength(hWndEdit_) ; pstrBuffer = new TCHAR[iLength + 1]; if (!pstrBuffer) { CloseHandle(hFile) ; return FALSE ; } // If the edit control will return Unicode text, write the // byte order mark to the file. #ifdef UNICODE WriteFile(hFile, &wByteOrderMark, 2, &dwBytesWritten, NULL) ; #endif // Get the edit buffer and write that out to the file. GetWindowText(hWndEdit_, pstrBuffer, iLength + 1) ; WriteFile(hFile, pstrBuffer, iLength * sizeof (TCHAR), &dwBytesWritten, NULL) ; if ((iLength * sizeof (TCHAR)) != (int) dwBytesWritten) { CloseHandle(hFile) ; RELEASE(pstrBuffer); return FALSE ; } CloseHandle(hFile) ; RELEASE(pstrBuffer); } return TRUE ; }