01 / 59Creating Single Document Interface Applications
1 / 17
Télécharger la présentation
Creating Single Document Interface Applications
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
The Array Classes • CArray:A template class for creating your own array class • CByteArray:An array of Byte data types • CDWordArray:An array of DWORD data types. • CObArray:An array of classes derived from the Cobject base class • CPtrArray:An array of pointers(to any object or data type) • CStringArray:An array of Cstring objects • CUIntArray:An array of UNIT data types(unsigned integers) • CWordArray:An array of WORD data types
在文件類別中加入相關的成員函式 • 函式功能 • Getting the from and to points • Creating a new line object • Adding it to the object array • 函式定義內容 • 名稱:AddLine • 傳回值型態:Cline * • 傳入參數:CPoint ptFrom, CPoint ptTo
AddLine 函式內容 CLine *pLine=NULL; try { pLine = new CLine(ptFrom, ptTo); //Create a CLine object m_oaLines.Add(pLine); //Add the new line to the object array SetModifiedFlag(); //Mark the document as dirty } catch(CMemoryException* perr) { AfxMessageBox("Out of Memory", MB_ICONSTOP|MB_OK); if (pLine) { delete pLine; //delete it pLine = NULL; } perr->Delete(); //Delete the exception object } return pLine;
用法: Place a try section around the code that might have problem The try section should always be followed by one or more catch sections If a problem occurs during the code in the try section, the program immediately jumps to the catch sections 捕捉程式執行時的錯誤並加以處理 try { … } catch { … }
在Doc類別中加入「Getting the Line Count」功能 • 在Doc類別中加入函式 • 名稱為GetLineCount • 存取模式為public • 傳回值型態為int,沒有輸入參數 • 在函式中加入以下程式碼: • return (int)m_oaLines.GetCount() ;
在Doc類別中加入「Retrieving a Specific Line」功能 • 在Doc類別中加入函式 • 名稱為GetLine • 存取模式為public • 傳回值型態為Cline* • 一個輸入參數 • int iIndex • 在函式中加入以下程式碼: • return (CLine*)m_oaLines[iIndex];
Showing the Users • We have already built the capability into the document class to hold the drawing. • Now we need to add the functionality to the view object to read the user’s drawing input and to draw the image.
在CSDISquigView::LButtonDown()中加入以下程式碼 在CSDISquigView::LButtonUp中加入以下程式碼 WM_LBUTTONDOWN及WM_LBUTTONUP事件程序 if (GetCapture()==this) ReleaseCapture(); SetCapture(); m_ptPrevPos=point;
WM_MOUSEMOVE事件程序 • 在CSDISquigView::OnMouseMove中加入以下程式碼: if ((nFlags & MK_LBUTTON)==MK_LBUTTON) { if (GetCapture()==this) { CClientDC dc(this); CLine *pLine=GetDocument()->AddLine(m_ptPrevPos, point); pLine->Draw(&dc); m_ptPrevPos=point; } }
在View class的OnDraw函式加入以下程式碼 • The function OnDraw is called whenever the image presented to users needs to be redrawn • 加入以下程式碼到OnDraw中 int iCount = pDoc->GetLineCount(); if (iCount) { int iPos; CLine *pLine = NULL; for (iPos=0; iPos < iCount ; iPos++) { pLine = pDoc->GetLine(iPos); pLine->Draw(pDC); } }
Deleting the Current Drawing • SDI應用程式的DOC類別的基底類別為CDocument,此類別中有一個DeleteContens成員函式,在需要清除目前視窗內容時(如使用者開新檔案)將自動呼叫該函式。 • 在SDI的Doc類別中,覆寫上述函式 • 對Doc類別按右鍵並選取屬性 • 在屬性視窗中按覆寫鈕 • 選取DeleteContents • 將下一張投影片的程式碼加到DeleteContents函式中
Deleting the Current Drawing //Get the number of lines in the object array int iCount = (int)m_oaLines.GetCount(); int iPos; try { if (iCount) { //Loop through the array , deleting each object for (iPos=0; iPos < iCount ; iPos++) delete (CLine*)m_oaLines.GetAt(iPos); //Reset the array m_oaLines.RemoveAll(); } } catch (CMemoryException* perr) { //Display a message for the user, giving him or her the bad news perr->ReportError(); //Delete the exception object perr->Delete(); }
Saving and Restoring • There are two parts of serialization • When application data is stored on the system drive in the form of a file, it’s called serialization. • When the application state is restored from the file , it’s called deserialization. • Serialization in Visual C++ is accomplished through the Archive class
The serialization function • When an application is reading or writing a file , the document object’s Serialize function is called .
Saving and Restoring the Drawing • Find the serialization function in the doc class, 加入以下程式碼: //Pass the serialization on to the object array; m_oaLines.Serialize(ar);
Saving and Restoring the Drawing • Add a new function to the Line class(存取模式為public), 先加到.cpp • 加函式原型定義到.h中 void Serialize(CArchive &ar); void CLine::Serialize(CArchive &ar) { CObject::Serialize(ar); if (ar.IsStoring()) //writing ar << m_ptFrom << m_ptTo; else ar >> m_ptFrom >> m_ptTo; //reading }
Saving and Restoring the Drawing • VC++ must be told that a class should be serializable.To do this: • Open the Cline header file • Add DECLARE_SERIAL(CLine) just after the first line of the class definition. • (Line.h) class CLine : • public CObject • Open the source code file • Add IMPLEMENT_SERIAL (CLine, CObject, 1) just before the class constructor • (Line.cpp) #include "line.h" • 保留預設的建構函式(沒有輸入參數)
ID_COLOR_BLACK ID_COLOR_BLUE ID_COLOR_GREEN ID_COLOR_CYAN ID_COLOR_RED ID_COLOR_MAGENTA ID_COLOR_YELLOW ID_COLOR_WHITE Caption:&Black Modifying the Menu
Adding Color the the Cline Class • Add another member variable to the Cline class to hold the color of each line private: COLORREF m_crColor; • Modify the class constructor(.cpp及.h) CLine::CLine(CPoint ptFrom, CPoint ptTo, COLORREF crColor) { //設定兩個成員資料的初值 m_ptFrom = ptFrom; m_ptTo=ptTo; m_crColor=crColor; }
Adding color the Cline Class • Modify the Draw function to use the specified color void CLine::Draw(CDC* pDC) { //Create a pen CPen lpen(PS_SOLID, 1, m_crColor); //Set the new pen as the drawing object CPen* pOldPen = pDC->SelectObject(&lpen); //Draw the line pDC->MoveTo(m_ptFrom); pDC->LineTo(m_ptTo); //Reset the previous pen pDC->SelectObject(pOldPen); }
Adding color the Cline Class • Modify the Serialize function to save and restore the color information void CLine::Serialize(CArchive &ar) { CObject::Serialize(ar); if (ar.IsStoring()) //writing ar << m_ptFrom << m_ptTo << m_crColor; else ar >> m_ptFrom >> m_ptTo>>m_crColor; //reading }
Adding color to the Document • Add a member variable to hold the current color,存取模式為private. UINT m_nColor; • Add a color table to convert color IDs into RGB values,存取模式為public static const COLORREF m_crColors[8];
Adding color to the Document • Add the Color Table specification to the source code ,加在END_MESSAGE_MAP()之後 const COLORREF CSDISquigDoc::m_crColors[8]={ RGB(0, 0, 0), //Black RGB(0, 0, 255), //Blue RGB(0, 255, 0), //Green RGB(0, 255, 255), //Cyan RGB(255, 0, 0), //Red RGB(255, 0, 255), //Magenta RGB(255, 255, 0), //Yellow RGB(255, 255, 255), //White };
Adding color to the Document • 在OnNewDocument()中加入以下指令, 以設定顏色初始值 m_nColor=0; • Modify the AddLine function pLine = new CLine(ptFrom, ptTo, m_crColors[m_nColor] );
Adding color to the Document • Adding a function to return the current color,存取模式為public UINT CSDISquigDoc::GetColor(void) { //Return the current color return ID_COLOR_BLACK+m_nColor; }
為Color 的相關指令加事件處理程式 void CSDISquigDoc::OnColorBlack() { // TODO: 在此加入您的命令處理常式程式碼 //Set the current color to black m_nColor = ID_COLOR_BLACK - ID_COLOR_BLACK; } void CSDISquigDoc::OnUpdateColorBlack(CCmdUI *pCmdUI) { // TODO: 在此加入您的命令更新 UI 處理常式程式碼 //Determine if the Black menu entry should be checked pCmdUI->Enable(); pCmdUI->SetCheck(GetColor()==ID_COLOR_BLACK?1:0); }
Adding width to the line class • Add another member variable to the Cline class to hold the width of each line private: UINT m_lnWidth; • Modify the class constructor(.cpp及.h) CLine::CLine(CPoint ptFrom, CPoint ptTo,COLORREF crColor, UINT lnWidth) { //設定兩個成員資料的初值 m_ptFrom = ptFrom; m_ptTo=ptTo; m_crColor=crColor; m_lnWidth=lnWidth; }
Adding width to the line class • Modify the Draw function to use the specified width void CLine::Draw(CDC* pDC) { //Create a pen CPen lpen(PS_SOLID,m_lnWidth, m_crColor); //Set the new pen as the drawing object CPen* pOldPen = pDC->SelectObject(&lpen); //Draw the line pDC->MoveTo(m_ptFrom); pDC->LineTo(m_ptTo); //Reset the previous pen pDC->SelectObject(pOldPen); }
Adding width to the line class • Modify the Serialize function to save and restore the width information CObject::Serialize(ar); if (ar.IsStoring()) //writing ar << m_ptFrom << m_ptTo << m_crColor << m_lnWidth; else //reading ar >> m_ptFrom >> m_ptTo >> m_crColor >> m_lnWidth;
Adding width to the Document • Add a member variable to hold the current width,存取模式為private. UINT m_nWidth; • Add a width table to convert width IDs into width values,存取模式為public static const UINT m_lnWidths[5];
Adding width to the Document • Add the WidthTable specification to the source code ,加在END_MESSAGE_MAP()之後 Const UINT CSDISquigDoc::m_lnWidths[5]={ 1, //VeryThin 8, //Thin 16, //Medium 24, //Thick 32, //Very Thick };
Adding Width to the Document • 在OnNewDocument()中加入以下指令, 以設定線條粗細初始值 m_nWidth=0; • Modify the AddLine function pLine = new CLine(ptFrom, ptTo, m_crColors[m_nColor], m_lnWidths[m_nWidth]);
Adding width to the Document • Adding a function to return the current width,存取模式為public UINT CSDISquigDoc::GetWidth(void) { //Return the current width return ID_WIDTH_VERYTHIN+m_nWidth; }
Modifying the Menu • ID_WIDTH_VERYTHIN • ID_WIDTH_THIN • ID_WIDTH_MEDIUM • ID_WIDTH_THICK • ID_WIDTH_VERYTHICK
為Width 的相關指令加事件處理程式 void CSDISquigDoc::OnWidthVerythin() { // TODO: 在此加入您的命令處理常式程式碼 //Set the current width to very thin m_nWidth = ID_WIDTH_VERYTHIN - ID_WIDTH_VERYTHIN; } void CSDISquigDoc::OnUpdateWidthVerythin(CCmdUI *pCmdUI) { // TODO: 在此加入您的命令更新 UI 處理常式程式碼 //Determine if the Very Thin menu entry should be checked pCmdUI->Enable(); pCmdUI->SetCheck(GetWidth()==ID_WIDTH_VERYTHIN?1:0); }
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.