基于对话框的应用程序,调用基于文档的打印/打印预览功能,如何使得打印时候不弹出选择打印机对话框,直接打印 如题。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SendMessage(hTargetView, WM_COMMAND, (WPARAM)ID_FILE_PRINT_DIRECT, 0); void CPrintWndDlg::Print(){ CDC printDC; CPrintDialog printDlg(FALSE); DOCINFO docInfo; if(printDlg.DoModal()==IDOK) { HDC hDC; hDC=printDlg.GetPrinterDC(); if(!printDC.Attach(hDC)) return; } else { printDC.Detach(); return; } memset(&docInfo,0,sizeof(docInfo)); docInfo.cbSize=sizeof(DOCINFO); docInfo.lpszDocName="Print Window"; if(printDC.StartDoc(&docInfo)<=0) { printDC.Detach(); return; } printDC.StartPage(); //这里进行你的打印工作 printDC.EndPage(); printDC.EndDoc(); printDC.Detach();}上面的代码取得一个打印机的DC:-) 你可以使用下面的函数来获得打印机的句并HDC GetPrinterDC (void){ DWORD dwNeeded, dwReturned ; HDC hdc ; PRINTER_INFO_4 * pinfo4 ; PRINTER_INFO_5 * pinfo5 ; if (GetVersion () & 0x80000000) // Windows 98 { EnumPrinters (PRINTER_ENUM_DEFAULT, NULL, 5, NULL, 0, &dwNeeded, &dwReturned) ; pinfo5 = malloc (dwNeeded) ; EnumPrinters (PRINTER_ENUM_DEFAULT, NULL, 5, (PBYTE) pinfo5, dwNeeded, &dwNeeded, &dwReturned) ; hdc = CreateDC (NULL, pinfo5->pPrinterName, NULL, NULL) ; free (pinfo5) ; } else // Windows NT { EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, NULL, 0, &dwNeeded, &dwReturned) ; pinfo4 = malloc (dwNeeded) ; EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, (PBYTE) pinfo4, dwNeeded, &dwNeeded, &dwReturned) ; hdc = CreateDC (NULL, pinfo4->pPrinterName, NULL, NULL) ; free (pinfo4) ; } return hdc ; } 然后BOOL PrintMyPage (HWND hwnd){ static DOCINFO di = { sizeof (DOCINFO), TEXT ("Print3: Printing") } ; // BOOL bSuccess = TRUE ; HDC hdcPrn ; int xPage, yPage ; if (NULL == (hdcPrn = GetPrinterDC())) return FALSE ; xPage = GetDeviceCaps (hdcPrn, HORZRES) ; yPage = GetDeviceCaps (hdcPrn, VERTRES) ; EnableWindow (hwnd, FALSE) ; // bUserAbort = FALSE ; // hDlgPrint = CreateDialog (hInst, TEXT ("PrintDlgBox"), // hwnd, PrintDlgProc) ; // SetAbortProc (hdcPrn, AbortProc) ; if (StartDoc (hdcPrn, &di) > 0) { if (StartPage (hdcPrn) > 0) { PageGDICalls (hdcPrn, xPage, yPage) ;//draw if (EndPage (hdcPrn) > 0) EndDoc (hdcPrn) ; // else // bSuccess = FALSE ; } } // else // bSuccess = FALSE ; // if (!bUserAbort) // { // EnableWindow (hwnd, TRUE) ; // DestroyWindow (hDlgPrint) ; // } DeleteDC (hdcPrn) ; return TRUE;//bSuccess && !bUserAbort ;} 问一个单文档视图结构里面的视图大小固定,框架滚动条的问题 如何寻找自己想要的API函数? 请问大家一个图片处理问题,就是把很多照片连在一起,各个照片间可以指定特效过渡,最后生成一个avi或者mpg文件 关于注册表操作问题 ***想做一个类似于VC界面的编译器,请高手指点*** 问一个数据处理的问题 如何判断一个光驱是物理光驱还是虚拟光驱? CSocket connect()失败! VB如何向VC编写的DLL传递参数? 关干数据库的问题。 小心地问一个directshow的问题 求助:开发COM中,如何同一库中的一个COM中创建另一个COM对象?
{
CDC printDC;
CPrintDialog printDlg(FALSE);
DOCINFO docInfo; if(printDlg.DoModal()==IDOK)
{
HDC hDC;
hDC=printDlg.GetPrinterDC();
if(!printDC.Attach(hDC))
return;
}
else
{
printDC.Detach();
return;
}
memset(&docInfo,0,sizeof(docInfo));
docInfo.cbSize=sizeof(DOCINFO);
docInfo.lpszDocName="Print Window";
if(printDC.StartDoc(&docInfo)<=0)
{
printDC.Detach();
return;
}
printDC.StartPage();
//这里进行你的打印工作 printDC.EndPage();
printDC.EndDoc(); printDC.Detach();}
上面的代码取得一个打印机的DC:-)
HDC GetPrinterDC (void)
{
DWORD dwNeeded, dwReturned ;
HDC hdc ;
PRINTER_INFO_4 * pinfo4 ;
PRINTER_INFO_5 * pinfo5 ; if (GetVersion () & 0x80000000) // Windows 98
{
EnumPrinters (PRINTER_ENUM_DEFAULT, NULL, 5, NULL,
0, &dwNeeded, &dwReturned) ; pinfo5 = malloc (dwNeeded) ; EnumPrinters (PRINTER_ENUM_DEFAULT, NULL, 5, (PBYTE) pinfo5,
dwNeeded, &dwNeeded, &dwReturned) ; hdc = CreateDC (NULL, pinfo5->pPrinterName, NULL, NULL) ; free (pinfo5) ;
}
else // Windows NT
{
EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, NULL,
0, &dwNeeded, &dwReturned) ; pinfo4 = malloc (dwNeeded) ; EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, (PBYTE) pinfo4,
dwNeeded, &dwNeeded, &dwReturned) ; hdc = CreateDC (NULL, pinfo4->pPrinterName, NULL, NULL) ; free (pinfo4) ;
}
return hdc ;
}
BOOL PrintMyPage (HWND hwnd)
{
static DOCINFO di = { sizeof (DOCINFO), TEXT ("Print3: Printing") } ;
// BOOL bSuccess = TRUE ;
HDC hdcPrn ;
int xPage, yPage ;
if (NULL == (hdcPrn = GetPrinterDC()))
return FALSE ;
xPage = GetDeviceCaps (hdcPrn, HORZRES) ;
yPage = GetDeviceCaps (hdcPrn, VERTRES) ;
EnableWindow (hwnd, FALSE) ;
// bUserAbort = FALSE ;
// hDlgPrint = CreateDialog (hInst, TEXT ("PrintDlgBox"),
// hwnd, PrintDlgProc) ;
// SetAbortProc (hdcPrn, AbortProc) ;
if (StartDoc (hdcPrn, &di) > 0)
{
if (StartPage (hdcPrn) > 0)
{
PageGDICalls (hdcPrn, xPage, yPage) ;//draw
if (EndPage (hdcPrn) > 0)
EndDoc (hdcPrn) ;
// else
// bSuccess = FALSE ;
}
}
// else
// bSuccess = FALSE ; // if (!bUserAbort)
// {
// EnableWindow (hwnd, TRUE) ;
// DestroyWindow (hDlgPrint) ;
// }
DeleteDC (hdcPrn) ;
return TRUE;//bSuccess && !bUserAbort ;
}