_BORLAND C++ 4.5 AND OLE 2.0 PROGRAMMING_ by Ted Faison Listing One: Creating a window (a) CMyWnd* myWnd = new CMyWnd; myWnd->Create(...); (b) TMyWindow* w = new TMyWindow(...); Listing Two: Creating an MDI frame window (a) class CMyWnd : public CMDIFrameWnd {...}; class CMyApp : public CWinApp { public: // ... virtual BOOL InitInstance() { CMyWnd* w = new CMyWnd; if (!w)->LoadFrame(IDRES) ) return FALSE; w->ShowWindow(m_nCmdShow); w->UpdateWindow(); m_pMainWnd = w; return TRUE; } }; (b) class TMyWnd : public TMDIFrame {...}; class TMDIFileApp : public TApplication { public: void InitMainWindow() { Frame = new TMDIFrame(..); Frame->Attr.AccelTable = IDRES; Frame->SetMenuDescr(...); } }; Listing Three: Creating an SDI frame window (a) class CMyWnd : public CFrameWnd {...}; class CMyApp : public CWinApp { public: // ... virtual BOOL InitInstance(){ CMyWnd* w = new CMyWnd; if (!w->LoadFrame(IDRES) ) return FALSE; w->ShowWindow(m_nCmdShow); w->UpdateWindow(); m_pMainWnd = w; return TRUE; } }; (b) class TMyWnd : public TFrameWindow {...}; class TSDIFileApp : public TApplication { public: void InitMainWindow() { Frame = new TFrameWindow(...); Frame->Attr.AccelTable = IDRES; Frame->SetMenuDescr(...); } }; Listing Four: Creating Doc/View templates (a) class CMyWnd : public CMDIChildWnd {..}; class CMyApp : public CWinApp { public: // ... virtual BOOL InitInstance() { AddDocTemplate(new CMultiDocTemplate(IDRES, RUNTIME_CLASS(CMyDoc), RUNTIME_CLASS(CMyWnd), RUNTIME_CLASS(CMyView))); CMyWnd* w = new CMyWnd; if (!w->LoadFrame(IDRES) ) return FALSE; w->ShowWindow(m_nCmdShow); w->UpdateWindow(); m_pMainWnd = w; } }; (b) DEFINE_DOC_TEMPLATE_CLASS( TMyDocument, TMyView, MyTemplate); MyTemplate btpl("My files", "*.txt", 0, "TXT", dtAutoDelete); class TMyApp : public TApplication { public: // ... void InitMainWindow() { DocManager = new TDocManager(dmSDI | dmMenu); } }; Listing Five: Iterating over child windows (a) void CMyWnd::Iterate() { for (CWnd* w = GetTopWindow(); w != NULL; w = w->GetNextWindow()) { // use child window 'w' } } (b) static void f(TWindow* w, void*) {...do something with 'w'} void TMyWindow::g() { ForEach(f); } Listing Six: Locating a child window (a) CWnd* CMyWnd::FindChild() { for (CWnd* w = GetTopWindow(); w != NULL; w = w->GetNextWindow()) { // see if child window found if (w is the right window) return w; } return 0; } (b) static BOOL f(TWindow* win, void*) { if (win satisfies some condition) return TRUE; else return FALSE; } void TMyWindow::g() { TWindow* first = FirstThat(f); TWindow* last = LastThat(f); } Listing Seven: Finding active MDI child window (a) class CMyWnd: public CMDIFrameWnd { // ... public: void f() { CMDIChildWnd* w = MDIGetActive(); if (!w) return; // use w ... } }; (b) class TMDIFileApp : public TApplication { public: // ... MDIClient* Client; protected: void f() { TMDIChild* w = Client->GetActiveMDIChild(); if (!w) return; // use w.. } }; Listing Eight: Initializing controls in a dialog box (a) class CMyDlg : public CDialog { public: // ... //{{AFX_DATA(CMyDlg) int m_Value1; int m_Value2; //}}AFX_DATA protected: DECLARE_MESSAGE_MAP() }; void CMyDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT1, m_Value1); DDV_MinMaxInt(pDX, m_Value1, -10, 20); DDX_Text(pDX, IDC_EDIT2, m_Value2); DDV_MinMaxInt(pDX, m_Value2, 0, 100); } (b) struct { // transfer buffer // ... } Buffer class TMyDlg : public TDialog { public: // ... TMyDlg(...) { // .. create controls TransferBuffer = &Buffer; } }; Listing Nine: Bitmapped buttons (a) class CMyDlg : public CDialog { public: enum {IDD = IDD_BITMAPDLG}; CMyDlg(); // ... protected: CBitmapButton button1; }; CMyDlg::CMyDlg() : CDialog(CMyDlg::IDD) { if (!button1.LoadBitmaps("Up","Down","Focus")) { TRACE("Problem!"); AfxThrowResourceException(); } } (b) no code necessary Listing Ten: Creating a pen (a) CPen pen; pen.CreatePen(PS_SOLID, 1, RGB(0,0,0)); (b) TPen pen(TColor(0, 0, 0) ); Listing Eleven: Drawing a line (a) void CMyWnd::Line(CDC& dc) { CPen pen; pen.CreatePen(PS_SOLID, 1, RGB(0,0,0) ); CPen* pOldPen = dc.SelectObject(&pen); dc.MoveTo(10, 10); dc.LineTo(20, 30); dc.SelectObject(pOldPen); } (b) void TMyWnd::Line(TDC& dc) { TPen pen(TColor(0, 0, 0) ); dc.SelectObject(pen); dc.MoveTo(0, 100); dc.LineTo(100, 20); dc.RestorePen(); } Listing Twelve: Painting with a brush (a) void CMyWnd::Box(CDC& dc) { CBrush brush(RGB(0, 0, 0) ); CBrush* pOldBrush = pDC->SelectObject(&brush); dc.Rectangle(30, 30, 100, 100); dc.SelectObject(pOldBrush); } (b) void TMyWnd::Box(TDC& dc) { dc.SelectObject( TBrush(Color(0,0,0))); dc.Rectangle(0,20,30,400); dc.RestoreBrush(); } Listing Thirteen: Creating fonts (a) void CMyWnd::Font(CDC& dc) { LOGFONT lf; memset(&lf, 0, sizeof(lf)); lf.lfHeight = 20; lf.lfWeight = FW_BOLD; strcpy(lf.lfFaceName, "Arial"); CFont font; font.CreateFontIndirect(&lf)); } (b) void TMyWnd::Font(TDC& dc) { LOGFONT lf; memset(&lf, 0, sizeof(lf)); lf.lfHeight = 20; lf.lfWeight = FW_BOLD; strcpy(lf.lfFaceName, "Arial"); TFont font(&lf); } Listing Fourteen: Displaying bitmaps (a) void CMyWnd::DrawBM(CDC& dc) { CBitmap bm; bm.Create("MYBITMAP"); CBitmap* pbmOld; CDC dcMem; dcMem.CreateCompatibleDC(&dc); pbmOld = dcMem.SelectObject(&bm); dc.BitBlt(100, 100, 50, 50, &dcMem, 0, 0, SRCCOPY); dcMem.SelectObject(pbmOld); dcMem.DeleteDC(); } (b) void TMyWnd::Draw(TDC& dc) { TBitmap* bm = new TBitmap(*GetModule(), "ID"); TMemoryDC memoryDC(dc); memoryDC.SelectObject(*Bitmap); TRect rect(0, 0, 40, 40); dc.BitBlt(rect, memoryDC, TPoint(0,0), SRCCOPY); } Listing Fifteen: Creating an array (a) CByteArray myArray; (b) TIArrayAsVectormyArray(5,0,5);; Listing Sixteen: Copying an array (a) CByteArray myArray; // array to be copied CByteArray copyArray; // array copied into for (int i=0; i < myArray.GetSize(); i++) copyArray [i] = myArray [i]; (b) TVectorImp myArray; TVectorImp copyArray; for (int i = 0; i < myArray.Count(); i++) copyArray [i] = myArray [i]; Listing Seventeen: Adding elements to an array (a) CByteArray myA; BYTE value = 2; myA.Add(value); (b) TIArrayAsVector myA(5,0,5); int value = 5; myA.AddAt(&value, 5); Listing Eighteen: Removing elements from an array (a) CByteArray myArray; myArray.RemoveAt(10); (b) TIArrayAsVector myArray(5,0,5); myArray.Detach(3); Listing Nineteen: Searching an array for an item (a) CByteArray myArray; int FindItem(BYTE value) { for (int i=0; i < myArray.GetSize(); i++) { if (myArray [i] == value) return i; } return -1; } (b) TIArrayAsVector myArray(5,0,5); int value = 5; int index = myArray.Find(&value); Listing Twenty: Deleting the items in an array (a) CStringArray myArray; void DeleteArray() { for (int i=0; i < myArray.GetSize(); i++) delete myArray [i]; myArray.RemoveAll(); } (b) TIArrayAsVector myArray(5,0,5); myArray.Flush(); Listing Twenty-one: Creating a list (a) CStringList myList; (b) TListImp myList(); Listing Twenty-two: Copying a list (a) CStringList myList; // list to copy CStringList copyList; // list copied to void CopyList() { POSITION pos = myList.GetHeadPosition(); while (pos) copyList.AddTail(myList.GetNext(pos) ); } (b) TListImp myList; TListImp copyList; static void DoCopy(string& s, void*) {copyList.Add(s);} void f() { myList.ForEach(DoCopy, 0); } Listing Twenty-three: Adding items to a list (a) CStringList myList; myList.AddTail("Hello"); myList.AddHead("Good-bye"); (b) TListImp myList; string s("Test"); myList.Add(s); Listing Twenty-four: Removing items from a list (a) CStringList myList; void RemoveItem(CString& target) { POSITION pos = myList.GetHeadPosition(); while (pos) { CString& str = myList.GetNext(pos); if (str == target) myList.RemoveAt(pos); delete str; } } (b) TListImp myList; string s("Test"); myList.Detach(s); Listing Twenty-five: Searching a list for an item (a) CStringList myList; BOOL HasString(CString& target) { POSITION pos = myList.GetHeadPosition(); while (pos) { CString& str = myList.GetNext(pos); if (str == target) return TRUE; } return FALSE; } (b) TListImp myList; string s("Test"); if (myList.Find(s) ) { // the item was found... } Listing Twenty-six: Deleting all the items in a list (a) CStringList myList; void DeleteList() { POSITION pos = myList.GetHeadPosition(); while (pos) delete myList.GetNext(pos); myList.RemoveAll(); } (b) TListImp myList; myList.Flush(); Listing Twenty-seven: Creating a dictionary (a) CMapStringToOb myMap; (b) // create a hashable class class HashString : public string { public: HashString() : string() {} HashString(const char* s) : string(s) {} unsigned HashValue() const { return hash(); } }; void f() { typedef TDDAssociation symbol; TDictionaryAsHashTable Dictionary; Listing Twenty-eight: Copying a dictionary (a) CMapStringToOb myMap; // map to copy CMapStringToOb myCopy; // map copied to POSITION pos = myMap.GetStartPosition(); while (pos) { CString string; CObject* pObject; myMap.GetNextAssoc(pos, string, pObject); copyMap.SetAt(string, pObject); } (b) typedef TDDAssociation symbol; typedef TDictionaryAsHashTable dictionary; dictionary myTable; dictionary copyTable; static void DoCopy(symbol& s, void*) { copyTable.Add(s); } void f() { myTable.ForEach(DoCopy, 0); } Listing Twenty-nine: Adding items to a dictionary (a) CMapStringToOb myMap; CString string; CObject* pObject; myMap.SetAt(string, pObject); (b) symbol s(HashString("K"), HashString("V")); myTable.Add(s); Listing Thirty: Removing items from a dictionary (a) CMapStringToOb myMap; void RemoveItem(CString& str) { CObject* pObject; if (!myMap.Lookup(str,&pObject)) return; myMap.RemoveKey(str); delete str; delete *pObject; } (b) symbol s(HashString("K"), HashString("V") ); myTable.Detach(s); Listing Thirty-one: Searching a dictionary for an item (a) CMapStringToOb myMap; BOOL HasItem(CString& str) { CObject* pObj; return myMap.Lookup(str,&pObj) ; } (b) symbol s(HashString("K"), HashString("V") ); symbol* r = myTable.Find(s); if (r) { // found... } Listing Thirty-two: Deleting all the items in a dictionary (a) CMapStringToOb myMap; POSITION pos = myMap.GetStartPosition(); while (pos) { CString string; CObject* pObject; myMap.GetNextAssoc(pos, string, pObject); delete pObject; } myMap.RemoveAll(); (b) typedef TDDAssociation symbol; typedef TDictionaryAsHashTable dictionary; dictionary myTable; myTable.Flush(); Listing Thirty-three // Paint routine for Window, Printer, and PrintPreview for a TOleView client. void mytestOleView::Paint (TDC& dc, bool erase, TRect& rect) { mytestApp *theApp = TYPESAFE_DOWNCAST(GetApplication(), mytestApp); if (theApp) { // Only paint if we're printing and we have something // to paint, otherwise do nothing. if (theApp->Printing && theApp->Printer && !rect.IsEmpty()) { // Use pageSize to get the size of the window to render into. // For a Window it's the client area; for a printer, it's the // printer DC dimensions; for print preview, it's layout window. TSize pageSize(rect.right - rect.left, rect.bottom - rect.top); TPrintDialog::TData &printerData = theApp->Printer->GetSetup(); // Compute the number of pages to print. printerData.MinPage = 1; printerData.MaxPage = 1; TOcView* ocView = GetOcView(); // Default TOcPart painting TRect clientRect = GetClientRect(); TRect logicalRect = clientRect + (Tsize&) ocView->GetOrigin(); for (TOcPartCollectionIter i(GetOcDoc()->GetParts()); i; i++) { TOcPart& p = *i.Current(); if (p.IsVisible(logicalRect)) { TRect r = p.GetRect(); r -= ocView->GetOrigin(); // Draw the embedded object p.Draw(dc, r, clientRect); if (p.IsSelected()) { TUIHandle handle(r, TUIHandle::HandlesIn | TUIHandle::Grapples | TUIHandle::HatchBorder, 5); handle.Paint(dc); } else { TUIHandle handle(r, TUIHandle::HatchBorder, 5); handle.Paint(dc); } } } // INSERT>> Special printing code goes here. } else { TOleView::Paint(dc, erase, rect); // INSERT>> Normal painting code goes here. } dc.TextOut(0, 30, "mytest OLE Server"); } } Example 1: (a) Creating a pen using C and the Windows API; (b) MFC does not take advantage of default arguments; (c) OWL's use of default arguments allows for simpler notation (a) HPEN pen; pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0) ); (b) Cpen pen; pen.Create(PS_SOLID, 1, RGB(0, 0, 0) ); (c) TPen pen; Example 2: (a) Making Windows API calls from C to Draw a line; (b) drawing a line using MFC; (c) drawing a line using OWL (a) void DrawLine(HDC dc) { HPEN pen; pen.CreatePen(PS_SOLID, 1, RGB(0,0,0) ); HPEN pOldPen = SelectObject(dc, pen); MoveTo(dc, 10, 10); LineTo(dc, 20, 30); SelectObject(dc, oldPen); } (b) void CMyWnd::DrawLine(CDC& dc) { CPen pen; pen.CreatePen(PS_SOLID, 1, RGB(0,0,0) ); CPen* pOldPen = dc.SelectObject(&pen); dc.MoveTo(10, 10); dc.LineTo(20, 30); dc.SelectObject(pOldPen); } (c) void TMyWnd::Line(TDC& dc) { TPen pen; dc.SelectObject(pen); dc.MoveTo(0, 100); dc.LineTo(100, 20); }