问题
In order to print some data I have to print an OCX as well. To do so I create a printer-dc from the GUI, print on it and so on. When it comes to printing the OCX I want to use its existing Draw-Method but to the printer-devicecontext I have. The code shown seems to work but look kind of hacked to me and I'm not sure if it works just "by accident". So the question is: Is this the right way to give a device-Context via Com-call to the OCX and use it?
This is in an Visual-C++-MFC-Project targeting windows
///////////////////////////////////// OCX
//IDL
[id(79)] void DrawBeamerToDC(OLE_HANDLE printerDC,
LONG drawRectLeft, LONG drawRectTop,
LONG drawRectRight, LONG drawRectBottom);
//the OCX-.cpp
void CSPCListViewCtrl::DrawBeamerToDC(OLE_HANDLE printerDC,
LONG drawRectLeft, LONG drawRectTop,
LONG drawRectRight, LONG drawRectBottom)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CRect rectDraw(drawRectLeft, drawRectTop, drawRectRight, drawRectBottom);
CDC* pDc = CDC::FromHandle((HDC)printerDC);
DrawMe(pDc, rectDraw);
}
////////////////////////////////// client
//the OCX-Wrapper (generated)
//note that OLE_HANDLE gets translated to a plain long
void CSPCListView::DrawBeamerToDC(long printerDC,
long drawRectLeft, long drawRectTop,
long drawRectRight, long drawRectBottom)
{
static BYTE parms[] = VTS_I4 VTS_I4 VTS_I4 VTS_I4 VTS_I4;
InvokeHelper(0x4f, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
printerDC,
drawRectLeft, drawRectTop,
drawRectRight, drawRectBottom);
}
//the callers .cpp
CRect rectBeamer = GetRectoPrintInColumnRectFromPrintRect();
CSPCListView* ptestctrl = GetOCXToPrint();
ptestctrl->DrawBeamerToDC((long)m_hPrintDC,
rectBeamer.left, rectBeamer.top,
rectBeamer.right, rectBeamer.bottom);
来源:https://stackoverflow.com/questions/56337654/drawing-ocx-to-given-printer-devicecontext