![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Weve seen how to create Container programs; we turn to servers next. Creating a ServerCreating a Server program with AppWizard allows us to place OLE items into Container programs. Using AppWizard, create a new MDI program named Server. In Step 3 of AppWizard, select the Full server option, as shown in Figure 12.5. In Step 4 of AppWizard, deselect the Docking toolbar option and click Finish to create the new project. This new project has a Document class, a View class, and a Server Item class. The server items are embedded in Container programs. When you open them for editing, however, the data in them is transferred to the document, so we work with the document first. The Server DocumentWe can display a line of text in our server item; when the user embeds an item of our server type in a container, that text appears. To store that text, we set up a new CString object, text, in the document. class CServerDoc : public COleServerDoc { protected: // create from serialization only CServerDoc(); DECLARE_DYNCREATE(CServerDoc) // Attributes public: CServerSrvrItem* GetEmbeddedItem() { return (CServerSrvrItem*)COleServerDoc::GetEmbeddedItem(); } CString text; ⇐ . . . We can initialize this text to, say, Hello from the server! The message appears in our embedded server item. CServerDoc::CServerDoc() { // Use OLE compound files EnableCompoundFile(); text = Hello from the server!; ⇐ } When you open the item for editing or close it after its been edited, the data in the item is transferred using serialization, so we make sure we serialize the text object in the document. void CServerDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << text; ⇐ } else { ar >> text; ⇐ } } Now we make the Server Item class, CServerSrvrItem, active. The Server ItemTailoring the Server Item class to our data means giving the server item as it appears in the container the right size. Currently, the server item has a default size, set in the CServerSrvrItem function OnGetExtent(). BOOL CServerSrvrItem::OnGetExtent(DVASPECT dwDrawAspect, CSize& rSize) { // Most applications, like this one, only handle drawing the content // aspect of the item. If you wish to support other aspects, such // as DVASPECT_THUMBNAIL (by overriding OnDrawEx), then this // implementation of OnGetExtent should be modified to handle the // additional aspect(s). if (dwDrawAspect != DVASPECT_CONTENT) return COleServerItem::OnGetExtent(dwDrawAspect, rSize); // CServerSrvrItem::OnGetExtent is called to get the extent in // HIMETRIC units of the entire item. The default implementation // here simply returns a hard-coded number of units. CServerDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: replace this arbitrary size rSize = CSize(3000, 3000); // 3000 x 3000 HIMETRIC units ⇐ return TRUE; } We change that size to reflect the actual dimensions of the string of text in our document. To get that size, we just create a NULL device context, set the mapping mode to MM_TEXT (i.e., pixels), find the extent of the text string, and install that size. BOOL CServerSrvrItem::OnGetExtent(DVASPECT dwDrawAspect, CSize& rSize) { // Most applications, like this one, only handle drawing the content // aspect of the item. If you wish to support other aspects, such // as DVASPECT_THUMBNAIL (by overriding OnDrawEx), then this // implementation of OnGetExtent should be modified to handle the // additional aspect(s). if (dwDrawAspect != DVASPECT_CONTENT) return COleServerItem::OnGetExtent(dwDrawAspect, rSize); // CServerSrvrItem::OnGetExtent is called to get the extent in // HIMETRIC units of the entire item. The default implementation // here simply returns a hard-coded number of units. CServerDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CClientDC nullDC(NULL); ⇐ nullDC.SetMapMode(MM_TEXT); ⇐ rSize = nullDC.GetTextExtent(pDoc->text, pDoc->text.GetLength()); ⇐ nullDC.DPtoHIMETRIC(&rSize); ⇐ return TRUE; } Theres one other CServerSrvrItem function to changeOnDraw(). This function is responsible for drawing the server item in the container. Currently, that function draws an arbitrary rectangle. BOOL CServerSrvrItem::OnDraw(CDC* pDC, CSize& rSize) { // Remove this if you use rSize UNREFERENCED_PARAMETER(rSize); CServerDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: set mapping mode and extent // (The extent is usually the same as the size returned from OnGetExtent) pDC->SetMapMode(MM_ANISOTROPIC); ⇐ pDC->SetWindowOrg(0,0); ⇐ pDC->SetWindowExt(3000, 3000); ⇐ // TODO: add drawing code here. Optionally, fill in the HIMETRIC extent. // All drawing takes place in the metafile device context (pDC). return TRUE; } We change that now so the server item displays the text string from the document. BOOL CServerSrvrItem::OnDraw(CDC* pDC, CSize& rSize) { // Remove this if you use rSize UNREFERENCED_PARAMETER(rSize); CServerDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDC->TextOut(0, 0, pDoc->text); ⇐ return TRUE; } The server item is ready to go. Now lets look at the view.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |