![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Changing the Views Default CursorTo change the default cursor in the view to an I-beam cursor, we simply add the following line, customizing the window class style of the view as its being created and loading in a new cursor:
BOOL CEditorView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS, AfxGetApp()->LoadStandardCursor(IDC_IBEAM), (HBRUSH)(COLOR_WINDOW + 1), AfxGetApp()->LoadIcon(IDR_MAINFRAME)); ⇐ return CView::PreCreateWindow(cs); } Now the default mouse cursor in our view is an I-beam, as shown in Figure 5.2. Now that weve added a caret to our view, theres one more point to handle. When our program loses the input focus (that is, another window is clicked), we should hide the caret and show it again when our program regains the focus. Gaining and Losing the FocusThe OnSetFocus() and OnKillFocus() functions let us handle gaining and losing the focus, so add them to Editor with ClassWizard. void CEditorView::OnKillFocus(CWnd* pNewWnd) { CView::OnKillFocus(pNewWnd); // TODO: Add your message handler code here } void CEditorView::OnSetFocus(CWnd* pOldWnd) { CView::OnSetFocus(pOldWnd); // TODO: Add your message handler code here } All we do here is hide the caret when we lose the focus and show it again when we regain the focus. void CEditorView::OnKillFocus(CWnd* pNewWnd) { HideCaret(); ⇐ CView::OnKillFocus(pNewWnd); } void CEditorView::OnSetFocus(CWnd* pOldWnd) { ShowCaret(); ⇐ CView::OnSetFocus(pOldWnd); } Now were ready to start working with the clipboard. Using the ClipboardUsing the clipboard in a text-oriented program usually requires enabling four operations:
We see all those operations here, starting with selecting text. Selecting TextFor brevity, we enable only one way of selecting text in Editorselecting all the text at once. (Its fairly easy, if tedious, to enable character-by-character text selection with the mouse if you want to add that feature to the program.) Following the Windows convention, we allow the user to select all the text in the document by pressing Ctrl+A, and we also add a new item to the Edit menu: Select All. Add the Select All item to the Edit menu now and connect an accelerator key to that item: Ctrl+A. In addition, we set up a new flag in the view class, m_SelectFlag, which we toggle when the user presses Ctrl+A or chooses Select All in the Edit menu in a handler function connected to that menu item. void CEditorView::OnEditSelectall() { m_SelectFlag = !m_SelectFlag; ⇐ Invalidate(); ⇐ } Now we can draw the text in classic selected stylethat is, as white text on a black backgroundin OnDraw() if its selected. We do that by setting the background color with the divide context function SetBkColor() and setting the text color with SetTextColor(). void CEditorView::OnDraw(CDC* pDC) { if(!m_CaretFlag){ TEXTMETRIC textmetric; pDC->GetTextMetrics(&textmetric); CreateSolidCaret(textmetric.tmAveCharWidth/4, textmetric.tmHeight); caret.x = caret.y = 0; SetCaretPos(caret); ShowCaret(); m_CaretFlag = true; } CEditorDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CString out_string(pDoc->m_data.GetText()); if(m_SelectFlag){ ⇐ pDC->SetBkColor(RGB(0, 0, 0)); ⇐ pDC->SetTextColor(RGB(255, 255, 255,)); ⇐ } ⇐ else{ ⇐ pDC->SetBkColor(RGB(255, 255, 255)); ⇐ pDC->SetTextColor(RGB(0, 0, 0,)); ⇐ } ⇐ pDC->TextOut(pDoc->m_data.GetX(), pDoc->m_data.GetY(), out_string); HideCaret(); caret.x = pDoc->m_data.GetX() + (pDC->GetTextExtent(pDoc->m_data.GetText())).cx; caret.y = pDoc->m_data.GetY(); SetCaretPos(caret); ShowCaret(); } Now when the user presses Ctrl+A or chooses the Select All item from the Edit menu, the text in the Editor program is selected, as shown in Figure 5.3. Now that were able to select text, we can copy it to the clipboard. Copying TextCopying text to the clipboard involves a little more work than you might think. The text we want to place into the clipboard is currently in the document, so we start by adding code to the Copy item in the Edit menu and getting a pointer to the document. void CEditorView::OnEditCopy() { CEditorDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); . . .
Next, we set up a block of global memory, which we can share between processes, to hold the text and get a handle to that block of memory. void CEditorView::OnEditCopy() { CEditorDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); HGLOBAL MemoryHandle; ⇐ MemoryHandle = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, pDoc->m_data.GetText().GetLength()); ⇐ . . . We get a char* pointer to that memory location, pMemory, with the GlobalLock() function, locking that memory for our use. 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); ⇐ . . . Now that we have a pointer to global memory, we can copy the text from the documents m_data object to that memory location if we cast the data in the m_data object as a constant character text string and use the framework function ::lstrcpy(). Then we unlock the memory weve locked. 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); ⇐ . . .
|
![]() |
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. |