更新:
更换了ico.....
新增选择字体和颜色的功能;
修正:
资源文件都已经打包到工程目录里了,路径也改成了相对路径;
解决了保存100KB以上的文件会丢失数据的问题。
核心代码: #include "resource.h" #include <windows.h> #include <stdio.h> #include <Richedit.h> #include <shlwapi.h> #pragma comment(lib,"shlwapi.lib") #define WM_FILENAME WM_USER+1//自定义消息,这个消息用来发送用户选择的文件全路径 #define OPEN WM_USER+2//一个Flag,标识这个文件是在打开对话框里选择的还是保存对话框 #define SAVE WM_USER+3 INT_PTR ExitCode1;//用来保存主对话框的退出代码 INT_PTR ExitCode2;//About对话框的退出代码 HINSTANCE hMain;//用来保存实例句柄 //主对话框过程函数声明 INT_PTR CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam); //About对话框过程函数声明 INT_PTR CALLBACK AboutProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam); BOOL OnInitDialog(HWND hwndDlg);//相应WM_ONINITDIALOG消息时调用的函数 BOOL OnOpen(HWND hwndDlg,DWORD Flag);//当用户点击Open菜单项时调用 //void GetTitle(char *pSourceBuf,char *pDestBuf); void ChooseFont(HWND hwndDlg,CHOOSEFONT &font,LOGFONT &log); int WINAPI WinMain(HINSTANCE hIns,HINSTANCE hPrev,LPSTR lpCmd,int nShowCmd)//入口函数 { LoadLibrary("Riched20.dll");//要用Rich Edit控件就必须要先加载这个库,否则程序无法运行 hMain=hIns; ExitCode1=DialogBox(hIns,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DialogProc);//创建主对话框 return 0; } //主对话框过程函数 INT_PTR CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch(uMsg) { case WM_INITDIALOG://初始化 OnInitDialog(hwndDlg); break; //当用户改变主对话框的大小时,Edit控件大小也应该改变。 case WM_SIZE: { RECT rect; ZeroMemory(&rect,sizeof(RECT)); GetWindowRect(hwndDlg,&rect);//获取主对话框窗口的大小,详见MSDN HWND hEdit=GetDlgItem(hwndDlg,IDC_TEXT); //改变Edit控件的大小 SetWindowPos(hEdit,HWND_TOP,0,0,rect.right-rect.left-5,rect.bottom-rect.top-50,SWP_SHOWWINDOW); CloseHandle(hEdit); } break; case WM_COMMAND: if(IDM_OPEN==wParam)//Open被用户点击 OnOpen(hwndDlg,OPEN); if(IDM_EXIT==wParam)//Exit被用户点击 SendMessage(hwndDlg,WM_CLOSE,NULL,NULL);//发送WM_CLOSE消息 if(IDM_ABOUT==wParam)//About被点击,创建ABOUT对话框 ExitCode2=DialogBox(hMain,MAKEINTRESOURCE(IDD_ABOUT),hwndDlg,AboutProc); if(IDM_SAVE==wParam)//用户点击Save { OnOpen(hwndDlg,SAVE); } if(IDM_CLOSEFILE==wParam) { SetDlgItemText(hwndDlg,IDC_TEXT,""); SetWindowText(hwndDlg,"NotePadV2.0 By 小司"); } if(IDM_FORMAT==LOWORD(wParam)) { LOGFONT log; CHOOSEFONT font; CHARFORMAT2 cm; ZeroMemory(&cm,sizeof(cm)); ChooseFont(hwndDlg,font,log); if(ChooseFont(&font)) { strcpy(cm.szFaceName,log.lfFaceName); cm.cbSize=sizeof(cm); cm.dwMask=CFM_FACE | CFM_SIZE | CFM_WEIGHT | CFM_COLOR; cm.yHeight=log.lfHeight * log.lfHeight; cm.wWeight=log.lfWeight; cm.bPitchAndFamily=log.lfPitchAndFamily; cm.crTextColor=font.rgbColors; HWND hText=GetDlgItem(hwndDlg,IDC_TEXT); SendMessage(hText,EM_SETCHARFORMAT,SCF_ALL,(LPARAM)&cm); } } break; case WM_FILENAME://自定义的消息,用来接收文件路径 if(OPEN==lParam) { //打开文件的目的是为了用GetFileSize函数获取文件大小 HANDLE hFile=CreateFile((LPCSTR)wParam,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL); if(INVALID_HANDLE_VALUE==hFile) { MessageBox(hwndDlg,"Open File Failed!","Error",0); CloseHandle(hFile); break; } DWORD dwFileSize=GetFileSize(hFile,NULL);//获取文件大小 CloseHandle(hFile); TCHAR *pszData=new TCHAR[dwFileSize]; memset(pszData,0,dwFileSize); FILE *fp=fopen((char*)wParam,"r");//wParam里保存了文件的路径 fread(pszData,dwFileSize,1,fp); fclose(fp); SetDlgItemText(hwndDlg,IDC_TEXT,pszData);//显示数据 } if(SAVE==lParam) { HWND hText=GetDlgItem(hwndDlg,IDC_TEXT); int len=GetWindowTextLength(hText); TCHAR *pBuf=new TCHAR[len]; memset(pBuf,0,len); GetDlgItemText(hwndDlg,IDC_TEXT,pBuf,len); char szFilePath[MAX_PATH+10]; sprintf_s(szFilePath,"%s.txt",(char*)wParam);//凑出来.txt FILE *fp=fopen(szFilePath,"w"); fwrite(pBuf,1,len,fp); fclose(fp); } break; //这个消息无用,调试用的.. case WM_LBUTTONDOWN: break; case WM_CLOSE: if(IDYES==MessageBox(hwndDlg,"Are you sure to exit?","Warming",MB_YESNO | MB_ICONWARNING)) { EndDialog(hwndDlg,ExitCode1); break; } break; default: break; } return FALSE; } //about窗口过程函数 INT_PTR CALLBACK AboutProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch(uMsg) { case WM_CLOSE: EndDialog(hwndDlg,ExitCode2); break; case WM_COMMAND: if(IDOK==wParam) { SendMessage(hwndDlg,WM_CLOSE,NULL,NULL); } break; default: break; } return FALSE; } BOOL OnInitDialog(HWND hwndDlg) { HICON hIcon=LoadIcon(hMain,MAKEINTRESOURCE(IDI_ICON1));//载入图标 SendMessage(hwndDlg,WM_SETICON,ICON_SMALL,(LPARAM)hIcon);//设置图标 return TRUE; } BOOL OnOpen(HWND hwndDlg,DWORD Flag) { //见MSDN中关于GetOpenFileName的说明 TCHAR szFile[MAX_PATH]; TCHAR szFileTitle[MAX_PATH]; OPENFILENAME op; memset(szFile,0,MAX_PATH); memset(szFileTitle,0,MAX_PATH); ZeroMemory(&op,sizeof(op)); op.lStructSize=sizeof(op); op.hwndOwner=hwndDlg; op.lpstrFile=szFile; op.nMaxFile=MAX_PATH; op.lpstrFilter="All\0*.*\0文本文件(*.txt)\0*.txt\0"; op.nFilterIndex=2; op.lpstrFileTitle=NULL; op.nMaxFileTitle=0; op.lpstrInitialDir=NULL; op.Flags=OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT; if(OPEN==Flag) { if(GetOpenFileName(&op)) {//把文件的路径发送给主对话框 StrCpy(szFileTitle,szFile); PathStripPath(szFileTitle); SetWindowText(hwndDlg,szFileTitle); SendMessage(hwndDlg,WM_FILENAME,(WPARAM)szFile,(LPARAM)Flag); } } if(SAVE==Flag) { if(GetSaveFileName(&op)) { SendMessage(hwndDlg,WM_FILENAME,(WPARAM)szFile,(LPARAM)Flag); } } return TRUE; } void ChooseFont(HWND hwndDlg,CHOOSEFONT &font,LOGFONT &log) { TCHAR szStyle[256]; HDC dc=GetDC(NULL); memset(szStyle,0,256); WORD wFontType=0; COLORREF color=RGB(0,0,0); ZeroMemory(&log,sizeof(log)); ZeroMemory(&font,sizeof(font)); font.lStructSize=sizeof(font); font.hwndOwner=hwndDlg; font.lpLogFont=&log; font.Flags=CF_EFFECTS | CF_FORCEFONTEXIST | CF_BOTH; font.rgbColors=color; font.lpszStyle=szStyle; font.hDC=dc; font.nFontType=wFontType; }