CDC指针传递有关问题
CDC指针传递问题
STDMETHODIMP COmRonPlcMain::Print(ULONG* hDC, LONG nLeft, LONG nTop, LONG nRight, LONG nBottom)
{
TextOut(((CDC*)hDC)->m_hDC,300,300,_T("OmRonPlc"),8);
return S_OK;
}
STDMETHODIMP CPrinterInterface::OnPrint(ULONG* pData, ULONG* pParaData, ULONG lData)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: 在此添加实现代码
IOmRonPlcMain* pIOmRonPlcMain = (IOmRonPlcMain*)(*pParaData);
CPrintDialog printDlg(FALSE);
if (IDCANCEL == printDlg.DoModal())
return 0;
// 利用CPrintDialog生成打印机设备环境
CDC dc;
dc.Attach(printDlg.GetPrinterDC());
dc.m_bPrinting = TRUE;
DOCINFO di;
::ZeroMemory(&di, sizeof(DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = _T("sample");
dc.SaveDC();
// Start a print job.
int ntest = dc.StartDoc(&di);
CPrintInfo info;
// Set the number of the last page of the document.
info.SetMaxPage(1);
// Specifies the usable drawing area of the page in logical coordinates.
info.m_rectDraw.SetRect(0, 0, dc.GetDeviceCaps(HORZRES),
dc.GetDeviceCaps(VERTRES));
// dc.DPtoLP(&info.m_rectDraw);
ntest = dc.StartPage();
CRect rcClient;
GetClientRect((HWND)(*pData),&rcClient);
pIOmRonPlcMain->Print((ULONG*)&dc,rcClient.left,rcClient.top,rcClient.right,rcClient.bottom);
::TextOut(dc,0,0,_T("Test"),4);
ntest = dc.EndPage();
dc.RestoreDC(-1);
if (ntest>0)
ntest = dc.EndDoc(); // End a print job
else
dc.AbortDoc(); // Abort job.
dc.Detach(); // Detach the printer DC
return s_ok;
}
OnPrint()的TextOut函数是可以打印出来的,但是OnPrint调用接口Print()的TextOut函数不能够打印出来,这是为什么呢?
我debug发现,OnPrint和Print函数里的cdc指针地址是一样的,所以不知道问题出现在哪里?
高手指教啊
------解决方案--------------------
lz这代码写的
pIOmRonPlcMain->Print((ULONG*)&dc,rcClient.left,rcClient.top,rcClient.right,rcClient.bottom);
红色那个是啥意思?dc一般可以认为是一个数,你取地址干嘛?
------解决方案--------------------
STDMETHODIMP COmRonPlcMain::Print(HDC hDC, LONG nLeft, LONG nTop, LONG nRight, LONG nBottom)
{
TextOut( hDC, 300, 300, _T("OmRonPlc"),8);
return S_OK;
}
STDMETHODIMP COmRonPlcMain::Print(ULONG* hDC, LONG nLeft, LONG nTop, LONG nRight, LONG nBottom)
{
TextOut(((CDC*)hDC)->m_hDC,300,300,_T("OmRonPlc"),8);
return S_OK;
}
STDMETHODIMP CPrinterInterface::OnPrint(ULONG* pData, ULONG* pParaData, ULONG lData)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: 在此添加实现代码
IOmRonPlcMain* pIOmRonPlcMain = (IOmRonPlcMain*)(*pParaData);
CPrintDialog printDlg(FALSE);
if (IDCANCEL == printDlg.DoModal())
return 0;
// 利用CPrintDialog生成打印机设备环境
CDC dc;
dc.Attach(printDlg.GetPrinterDC());
dc.m_bPrinting = TRUE;
DOCINFO di;
::ZeroMemory(&di, sizeof(DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = _T("sample");
dc.SaveDC();
// Start a print job.
int ntest = dc.StartDoc(&di);
CPrintInfo info;
// Set the number of the last page of the document.
info.SetMaxPage(1);
// Specifies the usable drawing area of the page in logical coordinates.
info.m_rectDraw.SetRect(0, 0, dc.GetDeviceCaps(HORZRES),
dc.GetDeviceCaps(VERTRES));
// dc.DPtoLP(&info.m_rectDraw);
ntest = dc.StartPage();
CRect rcClient;
GetClientRect((HWND)(*pData),&rcClient);
pIOmRonPlcMain->Print((ULONG*)&dc,rcClient.left,rcClient.top,rcClient.right,rcClient.bottom);
::TextOut(dc,0,0,_T("Test"),4);
ntest = dc.EndPage();
dc.RestoreDC(-1);
if (ntest>0)
ntest = dc.EndDoc(); // End a print job
else
dc.AbortDoc(); // Abort job.
dc.Detach(); // Detach the printer DC
return s_ok;
}
OnPrint()的TextOut函数是可以打印出来的,但是OnPrint调用接口Print()的TextOut函数不能够打印出来,这是为什么呢?
我debug发现,OnPrint和Print函数里的cdc指针地址是一样的,所以不知道问题出现在哪里?
高手指教啊
------解决方案--------------------
lz这代码写的
pIOmRonPlcMain->Print((ULONG*)&dc,rcClient.left,rcClient.top,rcClient.right,rcClient.bottom);
红色那个是啥意思?dc一般可以认为是一个数,你取地址干嘛?
------解决方案--------------------
STDMETHODIMP COmRonPlcMain::Print(HDC hDC, LONG nLeft, LONG nTop, LONG nRight, LONG nBottom)
{
TextOut( hDC, 300, 300, _T("OmRonPlc"),8);
return S_OK;
}