|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Supporting copy is usually straightforward for most applications. It usually requires 10-20 lines of code. But like many features of MFC, the implementation is simple. However, finding the information needed to create the implementation is tedious and time consuming. To save you many hours of reading documentation, I have included several common methods for placing data on the clipboard. Copying a Bitmap of the Client AreaThe following bit of code renders the client area using void CXStitchView::OnEditCopy() { CRect rect; CClientDC dc(this); CDC memDC; CBitmap bitmap; GetClientRect(&rect); // Create memDC memDC.CreateCompatibleDC(&dc); bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()); CBitmap* pOldBitmap = memDC.SelectObject(&bitmap); // Fill in memDC memDC.FillSolidRect(rect, dc.GetBkColor()); OnPrepareDC(&memDC); OnDraw(&memDC); // Copy contents of memDC to clipboard OpenClipboard(); EmptyClipboard(); SetClipboardData(CF_BITMAP, bitmap.GetSafeHandle()); CloseClipboard(); // Clean up memDC.SelectObject(pOldBitmap); bitmap.Detach(); } Copying a Table of Data to the ClipboardPlacing a table of data on the clipboard is easy. It is simply a string of text. Tabs separate columns, new lines separate rows. Here's some example source. #include <afxpriv.h> void CClipExamView::OnEditCopy() { // Create a shared memory file CSharedFile sf(GMEM_MOVEABLE | GMEM_SHARE | GMEM_ZEROINIT); // Place clipboard data in the shared memory file RenderTableData(sf); if (sf.GetLength() > 0) { // Put the data on the clipboard OpenClipboard(); EmptyClipboard(); SetClipboardData(CF_TEXT, sf.Detach()); CloseClipboard(); } } void CClipExamView::RenderTableData(CFile &file) { // Columns are separated by tabs ('\t') // Rows are separated by new lines ('\n') CString buffer = "1\t2\t3\n4\t5\t6\n"; file.Write(buffer, buffer.GetLength()); } Copying Formatted Data to the ClipboardFormatted text can support bold, italic or any other formatting that can be done in a word processor. Formatted text is usually placed on the clipboard as RTF (Rich Text Format). The Rich Text Format is intended as an interchange format for Word-processing applications. Because of that, it is a rather large and feature rich file format. Fortunately, it is possible to describe a minimal RTF command set for creating simple formatted documents. Basic RTF commands:\par - Starts a new paragraph.
\tab - A tab.
\b - Enable Bold (scoped within a group)
\i - Enable Italics (scoped within a group)
\ul - Enable Underline (scoped within a group)
For example, the RTF string: {\rtf1 {1 \tab 2 \tab 3 \par 4 \tab {\b\i 5} \tab 6}}
Source #include <afxpriv.h> void CClipExamView::OnEditCopy() { // Create a shared memory file CSharedFile sf(GMEM_MOVEABLE | GMEM_SHARE | GMEM_ZEROINIT); // Place clipboard data in the shared memory file RenderFormattedData(sf); if (sf.GetLength() > 0) { // Put the data on the clipboard OpenClipboard(); EmptyClipboard(); SetClipboardData(::RegisterClipboardFormat(CF_RTF), sf.Detach()); CloseClipboard(); } } void CClipExamView::RenderFormattedData(CFile &file) { // RTF (Rich Text Format) - Don't forget to escape // the \ character in your C strings! CString buffer = "{\\rtf1 {1 \\tab 2 \\tab 3" " \\par 4 \\tab {\\b\\i 5} \\tab 6}}"; file.Write(buffer, buffer.GetLength()); } Getting More Info on RTFMost books on file format address RTF in some form. You can find the RTF specification here. Virtually all word processors can save RTF files. This is usually a good place to start when building an RTF template.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||