11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9. Font<br />

We find out if the left button is held down when the mouse is moving by checking MK_LBUTTON bit of<br />

parameter nFlags. If so, function CGDIDoc::SetCaret(…) is called to set the selection and update the client<br />

window. Function CGDIView::OnLButtonUp(…) is implemented exactly the same except that we don’t need<br />

to check the status of left button here:<br />

void CGDIView::OnLButtonUp(UINT nFlags, CPoint point)<br />

{<br />

CGDIDoc *ptrDoc;<br />

}<br />

ptrDoc=(CGDIDoc *)GetDocument();<br />

if(::GetCursor() == m_hCur)ptrDoc->SetCaret(point);<br />

CScrollView::OnLButtonUp(nFlags, point);<br />

With the above knowledge, it is very easy for us to implement selection by using left/right ARROW<br />

key when SHIFT key is held down. To implement this, we need to check SHIFT key’s status when either<br />

left or right ARROW key is pressed. If it is held, we can update the selection indices and redraw the client<br />

window.<br />

9.9 One Line Text Editor, Step 6: Cut, Copy and Paste<br />

Global Memory<br />

Cut, copy and paste are supported almost by every application. They provide a way to exchange data<br />

among different applications. We must use globally shared data to implement clipboard data transfer. Once<br />

we send some data to the clipboard, it becomes public and can be accessed by all the programs. Any<br />

process in the system can clear the clipboard.<br />

Because of this, we must allocate global memory to store our data in the clipboard. In Windows<br />

programming, the following API functions can be used to manage global memory:<br />

HGLOBAL ::GlobalAlloc(UINT uFlags, DWORD dwBytes);<br />

LPVOID ::GlobalLock(HGLOBAL hMem);<br />

HGLOBAL ::GlobalReAlloc(HGLOBAL hMem, DWORD dwBytes, UINT uFlags);<br />

BOOL ::GlobalUnlock(HGLOBAL hMem);<br />

HGLOBAL ::GlobalFree(HGLOBAL hMem);<br />

Global memory is also managed through using handle. Unlike memory allocated using new key word,<br />

function ::GlobalAlloc(…) returns an HGLOBAL type handle to the allocated memory block if we allocate<br />

non-fixed global memory.<br />

Generally, before accessing a non-fixed block of global memory, we must lock it by calling function<br />

::GlobalLock(…), which will return the address of the memory block. After reading from or writing to this<br />

memory block, we need to call function ::GlobalUnlock(…) to unlock the memory again. We can free a<br />

block of global memory by calling function ::GlobalFree(…) (We can not free a block of memory when it<br />

is being locked).<br />

We can also allocate fixed global memory, in which case the address of the memory will be returned<br />

by function ::GlobalAlloc(…) directly, and we do not need lock or unlock operation in order to access the<br />

memory.<br />

Parameter nFlags of function ::GlobalAlloc(…) specifies how the memory will be allocated. There<br />

are many possible choices. For example, we can make the memory movable or fixed, and fill all the buffers<br />

with zero. The following is a list of some commonly used flags:<br />

Flag<br />

Meaning<br />

GMEM_FIXED Allocate fixed global memory<br />

GMEM_MOVEABLE Allocate movable global memory<br />

GMEM_ZEROINIT Initialize all the buffers to zero<br />

GMEM_SHARE Used for clipboard implementation or DDE<br />

268

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!