如何为在 MFC 中获取当前的打印机设置 mfc 如何拿到当前打印机的DC csdn.net

作者&投稿:象申 (若有异议请与网页底部的电邮联系)
  获取对当前的打印机设置访问权限的唯一办法是通过存储在 CWinApp 中的 m_hDevMode 和 m_hDevNames 结构。通过 PRINTDLG 结构的指针传递给从 CWinApp 类派生的类的 GetPrinterDeviceDefaults() 成员函数访问这些成员。由于返回值、 hDevMode 和 hDevNames,HGLOBAL 变量:: GlobalLock() 必须调用以返回指向结构的指针。使用该指针,您可以从该结构以确定打印机的当前状态中提取信息。请这些结构的内容,参阅 DEVMODE 并且 DEVNAMES 在 SDK 联机帮助以获取更多信息。

下面的代码示例将返回当前打印机的页面大小创建与打印机设置 CDC 对象上使用 GetDeviceCaps() 的 CView 派生类的成员函数︰

/* Compile options needed: none
*/

BOOL CMyView::GetPageSize(CSize &nRetVal)
{
PRINTDLG FAR * pPrintDlg = new PRINTDLG;
BOOL bRet = FALSE;

// Get the current printer's settings.

if(AfxGetApp()->GetPrinterDeviceDefaults(pPrintDlg))
{

// Get pointers to the two setting structures.

DEVNAMES FAR *lpDevNames =
(DEVNAMES FAR *)::GlobalLock(pPrintDlg->hDevNames);

DEVMODE FAR *lpDevMode =
(DEVMODE FAR *)::GlobalLock(pPrintDlg->hDevMode);

// Get the specific driver information.

CString szDriver((LPTSTR)lpDevNames +
lpDevNames->wDriverOffset);
CString szDevice((LPTSTR)lpDevNames +
lpDevNames->wDeviceOffset);
CString szOutput((LPTSTR)lpDevNames +
lpDevNames->wOutputOffset);

// Create a CDC object according to the current settings.

CDC pDC;
pDC.CreateDC(szDriver, szDevice, szOutput, lpDevMode);

// Query this CDC object for the width and height of the current
// page.

nRetVal.cx = pDC.GetDeviceCaps(HORZSIZE);
nRetVal.cy = pDC.GetDeviceCaps(VERTSIZE);

// Get rid of the CDC object.

pDC.DeleteDC();

// Unlock the pointers to the setting structures.

::GlobalUnlock(pPrintDlg->hDevNames);
::GlobalUnlock(pPrintDlg->hDevMode);

bRet = TRUE;
}
delete pPrintDlg;
return bRet;
}

MFC中怎么获得默认打印机名称~

获取对当前的打印机设置访问权限的唯一办法是通过存储在 CWinApp 中的 m_hDevMode 和 m_hDevNames 结构。通过 PRINTDLG 结构的指针传递给从 CWinApp 类派生的类的 GetPrinterDeviceDefaults() 成员函数访问这些成员。由于返回值、 hDevMode 和 hDevNames,HGLOBAL 变量:: GlobalLock() 必须调用以返回指向结构的指针。使用该指针,您可以从该结构以确定打印机的当前状态中提取信息。请这些结构的内容,参阅 DEVMODE 并且 DEVNAMES 在 SDK 联机帮助以获取更多信息。

下面的代码示例将返回当前打印机的页面大小创建与打印机设置 CDC 对象上使用 GetDeviceCaps() 的 CView 派生类的成员函数︰

/* Compile options needed: none
*/

BOOL CMyView::GetPageSize(CSize &nRetVal)
{
PRINTDLG FAR * pPrintDlg = new PRINTDLG;
BOOL bRet = FALSE;

// Get the current printer's settings.

if(AfxGetApp()->GetPrinterDeviceDefaults(pPrintDlg))
{

// Get pointers to the two setting structures.

DEVNAMES FAR *lpDevNames =
(DEVNAMES FAR *)::GlobalLock(pPrintDlg->hDevNames);

DEVMODE FAR *lpDevMode =
(DEVMODE FAR *)::GlobalLock(pPrintDlg->hDevMode);

// Get the specific driver information.

CString szDriver((LPTSTR)lpDevNames +
lpDevNames->wDriverOffset);
CString szDevice((LPTSTR)lpDevNames +
lpDevNames->wDeviceOffset);
CString szOutput((LPTSTR)lpDevNames +
lpDevNames->wOutputOffset);

// Create a CDC object according to the current settings.

CDC pDC;
pDC.CreateDC(szDriver, szDevice, szOutput, lpDevMode);

// Query this CDC object for the width and height of the current
// page.

nRetVal.cx = pDC.GetDeviceCaps(HORZSIZE);
nRetVal.cy = pDC.GetDeviceCaps(VERTSIZE);

// Get rid of the CDC object.

pDC.DeleteDC();

// Unlock the pointers to the setting structures.

::GlobalUnlock(pPrintDlg->hDevNames);
::GlobalUnlock(pPrintDlg->hDevMode);

bRet = TRUE;
}
delete pPrintDlg;
return bRet;
}

CPrintDialog你看下这个的MSDN解释。
// Display the Windows Print dialog box with "All" radio button
// initially selected. All other radio buttons are disabled.
CPrintDialog dlg(FALSE);
if (dlg.DoModal() == IDOK)
{
// Create a printer device context (DC) based on the information
// selected from the Print dialog.
HDC hdc = dlg.CreatePrinterDC();
ASSERT(hdc);
}