mfc剪下板總結

來源:互聯網
上載者:User

 

HANDLE SetClipboardData(

UINT uFormat,

HANDLE hMem);

Parameters

格式:

[in] Unsigned integer that specifies a clipboard format. This parameter can be a registered format or any of the standard clipboard formats. Windows CE supports the following standard clipboard:

Value

Description

CF_BITMAP

A handle to a bitmap (HBITMAP).

CF_DIB

A memory object containing a BITMAPINFO structure followed by the bitmap bits.

CF_DIF

Software Arts' Data Interchange Format.

CF_PALETTE

[參見MSDN]

CF_PENDATA

Data for the pen extensions to the Microsoft Windows for Pen Computing.

CF_RIFF

Represents audio data more complex than can be represented in a CF_WAVE standard wave format.

CF_SYLK

Microsoft Symbolic Link (SYLK) format.

CF_TEXT

Text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the data. Use this format for ANSI text.

CF_WAVE

Represents audio data in one of the standard wave formats, such as 11 kHz or 22 kHz pulse code modulation (PCM).

CF_TIFF

Tagged-image file format.

CF_UNICODETEXT

Unicode text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the data.

 

舉例1:文本的複製與粘貼

/*
將資料COPY到剪下板原理:
1:OpenClipboard() 開啟剪下板
2:EmptyClipboard() 清空剪下板,讓當前進程擁有剪下板
3:GlobalAlloc() 分配全域堆記憶體
4:GlobalLock() 鎖定記憶體並返回記憶體首地址
5:strcpy() 給記憶體賦值
6:GlobalUnlock() 解鎖記憶體
7:SetClipboardData() 設定到剪下板
8:CloseClipboard() 關閉剪下板

*/
void CClipboardDlg::OnBnClickedButton2()
{
if(OpenClipboard())//開啟剪下板
{
   EmptyClipboard();//清空剪下板,讓當前視窗進程擁有剪下板
   CString Str;
   GetDlgItemText(IDC_EDIT_SEND,Str);//擷取資料
   HANDLE hClip;
   hClip=GlobalAlloc(GMEM_MOVEABLE,Str.GetLength()+1);//分配記憶體對象返回地址(該函數是系統從全域堆中分配記憶體)
   char *pBuf;
   pBuf=(char *)GlobalLock(hClip);//鎖定全域記憶體中指定的記憶體塊,並返回一個地址值,令其指向記憶體塊的起始處
   strcpy(pBuf,Str);//將Str對象中的資料Copy到記憶體空間中
   GlobalUnlock(hClip);//解鎖全域記憶體塊
   SetClipboardData(CF_TEXT,hClip);//設定剪貼簿資料
   CloseClipboard();//關閉
}
}
/*
從剪下板取回資料的原理:
1:IsClipboardFormatAvailable() 判斷剪下板是否有要取回的資料
2:OpenClipboard() 開啟剪下板
3:GetClipboardData() 取回指定格式的物件控點
4:GlobalLock() 鎖定記憶體,並取回對象的資料
5:GlobalUnlock 解鎖
6:CloseClipboard() 關閉剪下板
*/
void CClipboardDlg::OnBnClickedButton1()
{
// TODO: 在此添加控制項通知處理常式代碼
if(IsClipboardFormatAvailable(CF_TEXT))//擷取剪貼簿裡的格式是否可以處理
{
   if(OpenClipboard())
   { HANDLE hClip;
    char *pBuf;
    hClip=GetClipboardData(CF_TEXT); //檢索從指定格式剪貼簿資料,並返回一個剪貼簿物件控點
    pBuf=(char *)GlobalLock(hClip);//鎖定全域記憶體中指定的記憶體塊 並返回一個地址值,並返回資料指標頭地址
    GlobalUnlock(hClip);           //解鎖
    SetDlgItemText(IDC_EDIT_RECV,pBuf);
    CloseClipboard();
   }
}
}

舉例2.BITMAP的複製與粘貼

位元影像的複製

if (::OpenClipboard (m_hWnd)) {

    // Make a copy of the bitmap.

    BITMAP bm;

    CBitmap bitmap;

    CBitmap m_bitmap;

    m_bitmap.LoadBitmap(IDB_BITMAP2);

    m_bitmap.GetObject (sizeof (bm), &bm);

    bitmap.CreateBitmapIndirect (&bm);

 

    CDC dcMemSrc, dcMemDest;

    dcMemSrc.CreateCompatibleDC (NULL);

    CBitmap* pOldBitmapSrc = dcMemSrc.SelectObject (&m_bitmap);

    dcMemDest.CreateCompatibleDC (NULL);

    CBitmap* pOldBitmapDest = dcMemDest.SelectObject (&bitmap);

 

    dcMemDest.BitBlt (0, 0, bm.bmWidth, bm.bmHeight, &dcMemSrc,

        0, 0, SRCCOPY);

    HBITMAP hBitmap = (HBITMAP) bitmap.Detach ();

 

    dcMemDest.SelectObject (pOldBitmapDest);

    dcMemSrc.SelectObject (pOldBitmapSrc);

 

    // Place the copy on the clipboard.

    ::EmptyClipboard ();

    ::SetClipboardData (CF_BITMAP, hBitmap);

    ::CloseClipboard ();

}

位元影像的粘貼:

把存在於剪貼簿中的位元影像資料顯示到程式的客戶區:

HWND hWnd = GetSafeHwnd(); // 擷取安全視窗控制代碼

::OpenClipboard(hWnd); // 開啟剪貼簿

HANDLE hBitmap = ::GetClipboardData(CF_BITMAP); // 擷取剪貼簿資料控制代碼

HDC hDC = ::GetDC(hWnd); // 擷取裝置環境控制代碼

HDC hdcMem = CreateCompatibleDC(hDC); // 建立與裝置相關的記憶體環境

SelectObject(hdcMem, hBitmap); // 選擇對象

SetMapMode(hdcMem, GetMapMode(hDC)); // 設定映射模式

BITMAP bm; // 得到位元影像對象

GetObject(hBitmap, sizeof(BITMAP), &bm);

BitBlt(hDC, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); //位元影像

複製

::ReleaseDC(hWnd, hDC); // 釋放裝置環境控制代碼

DeleteDC(hdcMem); // 刪除記憶體環境

::CloseClipboard(); // 關閉剪貼簿

3. 多資料項目

要把資料放入剪貼簿,在開啟剪貼簿後一定要調用EmptyClipboard()函數清除當前

剪貼簿中的內容,而不可以在原有資料項目基礎上追加新的資料項目。但是,可以在

EmptyClipboard()和CloseClipboard()調用之間多次調用SetClipboardData()

函數來放置多個不同格式的資料項目。例如

OpenClipboard(hWnd);

EmptyClipboardData();

SetClipboardData(CF_TEXT, hGMemText);

SetClipboardData(CF_BITMAP, hBitmap);

CloseClipboard();

這時如果用CF_TEXT或CF_BITMAP等格式標記去調用IsClipboardFormatAvailable()

都將返回TRUE,表明這幾種格式的資料同時存在於剪貼簿中。以不同的格式標記去調

用GetClipboardData()函數可以得到相應的資料控制代碼。

對於多資料項目的剪貼簿資料,還可以用CountClipboardFormats()和

EnumClipboardFormats()函數得到當前剪貼簿中存在的資料格式數目和具體的資料

格式。EnumClipboardFormats()的函數原型為:

UINT EnumClipboardFormats(UINT format);

參數format指定了剪貼簿的資料格式。如果成功執行將返回format指定的格式的下一

個資料格式值,如果format為最後的資料格式值,那麼將返回0。由此不難寫出處理

剪貼簿中所有格式資料項目的程式段代碼:

UINT format = 0; // 從第一種格式值開始枚舉

OpenClipboard(hWnd);

while(format = EnumClipboardFormats(format))

{

…… // 對相關格式資料的處理

}

CloseClipboard();

4.延遲提交

http://apps.hi.baidu.com/share/detail/30391289

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.