Calling QueryGetData and GetData

When you select QueryGetData, DataUser generates five calls to IDataObject::QueryGetData, passing FORMATETC structures containing CF_TEXT, CF_BITMAP, CF_DIB, CF_METAFILEPICT, and CF_WAVE. DataUser displays the result (and whether that was the expected result) in its window. Each call to QueryGetData itself is isolated in the function CApp::TryQueryGetData.

Those calls to QueryGetData specifying the CF_DIB and CF_WAVE formats should fail, while the others succeed. Remember that QueryGetData will return either S_OK or S_FALSE success codes depending on the format's availability. An error code means that the query operation itself failed and the answer is indeterminate.

When you select one of the GetData And Display menu items, DataUser will retrieve a rendering of the specified format from the current object and display it in the client area. DataUser holds on to the resulting STGMEDIUM in CApp::m_stm until it calls GetData again or until you close the program entirely.


//In the WM_COMMAND message case of DataUserWndProc

case IDM_OBJECTGETDATATEXT:
case IDM_OBJECTGETDATABITMAP:
case IDM_OBJECTGETDATAMETAFILEPICT:
if (NULL==pApp->m_pIDataObject)
break;

//Clean up whatever we currently have.
pApp->m_cf=0;
ReleaseStgMedium(&pApp->m_stm);

if (IDM_OBJECTGETDATATEXT==wID)
SETDefFormatEtc(fe, CF_TEXT, TYMED_HGLOBAL);

if (IDM_OBJECTGETDATABITMAP==wID)
SETDefFormatEtc(fe, CF_BITMAP, TYMED_GDI);

if (IDM_OBJECTGETDATAMETAFILEPICT==wID)
{
SETDefFormatEtc(fe, CF_METAFILEPICT
, TYMED_MFPICT);
}

hr=pApp->m_pIDataObject->GetData(&fe
, &(pApp->m_stm));

if (SUCCEEDED(hr))
pApp->m_cf=fe.cfFormat;

InvalidateRect(hWnd, NULL, TRUE);
UpdateWindow(hWnd);
break;

Repaints performed by DataUser happen in the function CApp::Paint, which calls DrawText for CF_TEXT data, BitBlt for CF_BITMAP data, and PlayMetaFile for CF_METAFILEPICT data. Because DataUser holds the data until it calls GetData again, you can resize the window as necessary and the data will still be visible.