【SDK編程】仿微軟記事本V2.0

來源:互聯網
上載者:User

更新:
更換了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+3INT_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;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.