// 【MoreWindows工作筆記9】OleGetClipboard 訪問剪下板的常值內容// http://blog.csdn.net/morewindows/article/details/17655053// By MoreWindows( http://blog.csdn.net/MoreWindows )#include <Windows.h>#include <ObjBase.h>#include <iostream>using namespace std;void DisplayDataObject(IDataObject *pDataObject){ FORMATETC fmtetc = {CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL}; STGMEDIUM stgmedium; if (pDataObject->GetData(&fmtetc, &stgmedium) == S_OK) { cout<<" 剪下板資料格式上為CF_TEXT, 常值內容如下:"<<endl; char* data = (char*)GlobalLock(stgmedium.hGlobal); cout<<data<<endl; GlobalUnlock(stgmedium.hGlobal); ReleaseStgMedium(&stgmedium); } else { cout<<" 剪下板資料格式不為CF_TEXT"<<endl; }} int main() { printf(" 【MoreWindows工作筆記9】OleGetClipboard 訪問剪下板的常值內容\n"); printf(" - http://blog.csdn.net/morewindows/article/details/17655053 -\n"); printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n"); if (OleInitialize(NULL) != S_OK) return 0; IDataObject *pDataObject; // Retrieves a data object that you can use to access the contents of the clipboard. if (OleGetClipboard(&pDataObject) == S_OK) { DisplayDataObject(pDataObject); pDataObject->Release(); } OleUninitialize(); return 0;}
HRESULT OleGetClipboard( _Out_ LPDATAOBJECT *ppDataObj);
typedef struct tagFORMATETC { CLIPFORMAT cfFormat; // 剪貼簿資料格式DVTARGETDEVICE *ptd; // 裝置有關的資訊DWORD dwAspect; // 圖形資料的表現方式LONG lindex; // dwAspect 成員的補充DWORD tymed; // 資料的儲存介質} FORMATETC;
FORMATETC cFmt = {(CLIPFORMAT) CF_TEXT, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
// winuser.h#ifndef NOCLIPBOARD/* * Predefined Clipboard Formats */#define CF_TEXT 1#define CF_BITMAP 2#define CF_METAFILEPICT 3#define CF_SYLK 4#define CF_DIF 5#define CF_TIFF 6#define CF_OEMTEXT 7#define CF_DIB 8#define CF_PALETTE 9#define CF_PENDATA 10#define CF_RIFF 11#define CF_WAVE 12#define CF_UNICODETEXT 13#define CF_ENHMETAFILE 14#if(WINVER >= 0x0400)#define CF_HDROP 15#define CF_LOCALE 16#endif /* WINVER >= 0x0400 */#if(WINVER >= 0x0500)#define CF_DIBV5 17#endif /* WINVER >= 0x0500 */#if(WINVER >= 0x0500)#define CF_MAX 18#elif(WINVER >= 0x0400)#define CF_MAX 17#else#define CF_MAX 15#endif#define CF_OWNERDISPLAY 0x0080#define CF_DSPTEXT 0x0081#define CF_DSPBITMAP 0x0082#define CF_DSPMETAFILEPICT 0x0083#define CF_DSPENHMETAFILE 0x008E/* * "Private" formats don't get GlobalFree()'d */#define CF_PRIVATEFIRST 0x0200#define CF_PRIVATELAST 0x02FF/* * "GDIOBJ" formats do get DeleteObject()'d */#define CF_GDIOBJFIRST 0x0300#define CF_GDIOBJLAST 0x03FF#endif /* !NOCLIPBOARD */
typedef struct tagSTGMEDIUM { DWORD tymed; union { HBITMAP hBitmap; HMETAFILEPICT hMetaFilePict; HENHMETAFILE hEnhMetaFile; HGLOBAL hGlobal; LPOLESTR lpszFileName; IStream *pstm; IStorage *pstg; }; IUnknown *pUnkForRelease;} STGMEDIUM, *LPSTGMEDIUM;