Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Now that we’ve set things up, placing this memory into the clipboard is not hard. Here, we open the clipboard, empty it, place our data in it with SetClipboardData(), and close the clipboard:

void CEditorView::OnEditCopy()
{
    CEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    HGLOBAL MemoryHandle;

    MemoryHandle = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
pDoc->m_data.GetText().GetLength());

    char* pMemory;

    pMemory = (char*)::GlobalLock(MemoryHandle);

    ::lstrcpy(pMemory, (LPCTSTR)pDoc->m_data.GetText());

    ::GlobalUnlock(MemoryHandle);

    OpenClipboard();                                    ⇐

    EmptyClipboard();                                   ⇐

    SetClipboardData(CF_TEXT, MemoryHandle);            ⇐

    CloseClipboard();                                   ⇐
}

Finally, we enable the Copy menu item only if the user has selected some text, and we can determine that with the m_SelectFlag flag, which we put to use in OnUpdateCopy().

void CEditorView::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(m_SelectFlag);                     ⇐

}

Once the Editor program supports copying text to the clipboard, we can support cutting text.

Cutting Text

Cutting the text is basically the same function as copying it, except that we also clear the text in the document, and that looks like this in OnEditCut():

void CEditorView::OnEditCut()
{
    CEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    HGLOBAL MemoryHandle;

    MemoryHandle = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
pDoc->m_data.GetText().GetLength());

    char* pMemory;

    pMemory = (char*)::GlobalLock(MemoryHandle);

    ::lstrcpy(pMemory, (LPCTSTR)pDoc->m_data.GetText());

    ::GlobalUnlock(MemoryHandle);

    OpenClipboard();

    EmptyClipboard();

    SetClipboardData(CF_TEXT, MemoryHandle);

    CloseClipboard();

    pDoc->m_data.SetText(””);                 ⇐
    Invalidate();
}

In addition, we enable the Cut item in the Edit menu by checking m_SelectFlag.

void CEditorView::OnUpdateEditCut(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(m_SelectFlag);             ⇐

}

Finally, we enable pasting of text in our application.

Pasting Text

Pasting text is the opposite of copying it. In this case, we start by opening the clipboard in OnEditPaste().

void CEditorView::OnEditPaste()
{
    OpenClipboard();                    ⇐
     .
     .
     .

Next, we get a handle to the memory set aside for the clipboard with ::GetClipboardData() and a character pointer to that memory with GlobalLock().

void CEditorView::OnEditPaste()
{
    OpenClipboard();

    HANDLE MemoryHandle;                                ⇐

    MemoryHandle = ::GetClipboardData(CF_TEXT);         ⇐

    char* pMemory;                                      ⇐

    pMemory = (char*)::GlobalLock(MemoryHandle);        ⇐
     .
     .
     .

The next step is to copy the data from the clipboard to the CString object in m_data.

void CEditorView::OnEditPaste()
{
    OpenClipboard();

    HANDLE MemoryHandle;

    MemoryHandle = ::GetClipboardData(CF_TEXT);

    char* pMemory;

    pMemory = (char*)::GlobalLock(MemoryHandle);

    CEditorDoc* pDoc = GetDocument();                   ⇐
    ASSERT_VALID(pDoc);                                 ⇐

    pDoc->m_data.SetText(pMemory);                      ⇐
     .
     .
     .

All that’s left is to unlock the memory, close the clipboard, and invalidate the view to redraw it.

void CEditorView::OnEditPaste()
{
    OpenClipboard();

    HANDLE MemoryHandle;

    MemoryHandle = ::GetClipboardData(CF_TEXT);

    char* pMemory;

    pMemory = (char*)::GlobalLock(MemoryHandle);

    CEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    pDoc->m_data.SetText(pMemory);

    ::GlobalUnlock(MemoryHandle);       ⇐

    ::CloseClipboard();                 ⇐

    Invalidate();                       ⇐
}

Finally, we enable the Paste menu item only if there is data in the clipboard to paste. We determine that with the ::IsClipboardFormatAvailable() framework function.

void CEditorView::OnUpdateEditPaste(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(::IsClipboardFormatAvailable(CF_TEXT));⇐
}

We’ve now enabled copying, cutting, pasting, and selecting text. Now we’re using the clipboard in Visual C++. Next, we turn to printing and advanced printing topics.

Printing from the Editor Program

AppWizard has already prepared our program for printing—the Print, Print Preview, and Print Setup items are active. You can see how Print Preview looks in our program in Figure 5.4.

However, there’s certainly more to consider here. For example, what if we had a multiple page document? We can set the number of pages to print in the OnBeginPrinting() function with the SetMinPage() and SetMaxPage() functions of the CPrintInfo object passed to us (note that here, pDC is a pointer to the printer’s device context).

void CEditorView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
    pInfo->SetMinPage(1);
    pInfo->SetMaxPage(2);
}


How Many Pages Will Your Document Be?

You can determine how many pages your document will be when printed with the device context function GetDeviceCaps().


In this case, we’ve made our document two pages long. In fact, the program prints two pages with the same text in it. If we really did have a two-page document, how could we switch to the second page when it’s time to do so? We can do that in the OnPrepareDC() function, which is called for each page that is printed, just before it is printed.


Figure 5.4  Using Print Preview in the Editor program.

You can determine which page is currently being printed with the m_CurPage data member of the CPrintInfo object passed to us in OnPrepareDC. For example, if we wanted to print the second page in a new font, say 240-point Roman, we could put this code into OnPrepareDC(). Here, we create a new font and load it into the printer device context (note that you pass the point size to CreatePointFont() in tenths of a font, so we pass a value of 2400 for a 240-point font):

void CEditorView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
    if(!pInfo)                                          ⇐
     return;                                            ⇐

    CFont* font;                                        ⇐

    font = new CFont();                                 ⇐

    font->CreatePointFont(2400, CString(”Roman”));      ⇐

    if(pInfo->m_nCurPage == 2){                         ⇐
     pDC->SelectObject(font);                           ⇐
    }

    CView::OnPrepareDC(pDC, pInfo);                     ⇐
}


Previous Table of Contents Next


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.