如何使用PrintDlg获取选定的打印机?

问题描述 投票:0回答:1

我正在测试一个使用 Win32 API 打印发票的动态链接库。到目前为止,发票已正确绘制,但

PrintDlg
函数仅返回有关默认打印机的信息。

PrintDlg
函数是否可以返回有关所选打印机的信息?

代码的最小化版本如下;

   //Variable declarations
   PRINTDLGA *tmpprintdlg = 0;
   char *tmpstr = 0;
   char *tmpstr2 = 0;
   char *tmpstr3 = 0;
   DEVNAMES *tmpdevnames = 0;
   DEVMODEA *tmpdevmode = 0;
   HDC tmphdc = 0;
   int ret = 0;
   int ret2 = 0;

   //Show the print dialog.
   ret = PrntShowPrintDialog(GLB_HWndMainDialog, 
                             &tmpprintdlg);
   if((ret != TSUCCESS) && \
      (ret != FALSE)){
    //Get the error string.
    PrntGetErrorString(&tmpstr);
    MessageBox(NULL, 
               tmpstr, 
               "Error", 
               MB_OK);
    return FALSE;
   } //EndIf

   //Check if the user cancelled the print.
   if(ret == FALSE){
 
    //Returns
    return FALSE;

   } //EndIf

   //Get the selected printer.
   tmpdevnames = (DEVNAMES*)GlobalLock(tmpprintdlg->hDevNames);

   //Use the members in the device names pointer
   //to get information about the printer.

   //Set tmpn to zero.
   tmpn = 0;

   //Get the name of the printer driver.
   tmpn = tmpdevnames->wDriverOffset;
   tmpstr = (char*)(((char*)tmpdevnames) + tmpn);
 
   //Get the name of the printer.
   tmpn = tmpdevnames->wDeviceOffset;
   tmpstr2 = (char*)(((char*)tmpdevnames) + tmpn);

   //Get the name of the printer port.
   tmpn = tmpdevnames->wOutputOffset;
   tmpstr3 = (char*)(((char*)tmpdevnames) + tmpn);

   //Get the device mode for the printer.
   if(tmpprintdlg->hDevMode != NULL){
    tmpdevmode = (DEVMODEA*)GlobalLock(tmpprintdlg->hDevMode);
   } //EndIf 

   //Create a device context for the user selected
   //printer.
   tmphdc = CreateDCA(tmpstr, 
                      tmpstr2, 
                      tmpstr3, 
                      tmpdevmode);
   if(tmphdc == NULL){
    //Get the last Win32 error.
    ret2 = GetLastError();
    //Create an error string.
    PrntCreateErrorString(ret2, \
              TCREATEFAILED, \
              "CreateDC", \
              &tmpstr);
    //Show the error string.
    MessageBox(NULL, \
           tmpstr, \
           "Error", \
           MB_OK);
    //Returns
    return FALSE;
   }

   //Unlock the tmpdevnames memory.
   GlobalUnlock(tmpdevnames);

   //Unlock the tmpdevmode memory.
   if(tmpdevmode != NULL){
    GlobalUnlock(tmpdevmode);
   } //EndIf

这是

PrntShowPrintDialog
函数的代码;

TRESULT PrntShowPrintDialog(_IN HWND inobj, \
                            _INOUT PRINTDLGA **outobj){

 //Variable declarations
 PRINTDLGA *tmpprintdlg = 0;
 HWND tmphndwindow = 0;
 HINSTANCE tmphinst = 0;
 int ret = 0;
 int ret2 = 0;

 //Allocate memory to one PRINTDLGA object.
 tmpprintdlg = (PRINTDLGA*)calloc(1024, sizeof(char));
 if(tmpprintdlg == NULL){
  //Create an error string and set the
  //global static variable.
  PrntCreateErrorString(0, 
                        TMEMALLOCFAILED, 
                        "calloc", 
                        NULL);
  //Returns
  return TMEMALLOCFAILED;
 } //EndIf

 //Set some members in the PRINTDLGA object.
 tmpprintdlg->lStructSize = sizeof(PRINTDLGA);
 tmpprintdlg->hwndOwner = tmphndwindow;
 tmpprintdlg->hDevMode = NULL;
 tmpprintdlg->hDevNames = NULL;
 tmpprintdlg->hDC = NULL;
 tmpprintdlg->Flags = PD_ALLPAGES | \
              PD_ENABLEPRINTHOOK | \
              PD_ENABLESETUPHOOK | \
              PD_NONETWORKBUTTON | \
              PD_NOSELECTION | \
              PD_COLLATE | \
              PD_RETURNDC;
 tmpprintdlg->nMinPage = 0;
 tmpprintdlg->nMaxPage = 200;
 tmpprintdlg->nCopies = 1;
 tmpprintdlg->hInstance = tmphinst;
 tmpprintdlg->lCustData = NULL;
 tmpprintdlg->lpfnPrintHook = PrntDlgPrintProc;
 tmpprintdlg->lpfnSetupHook = PrntDlgSetupProc;
 tmpprintdlg->lpPrintTemplateName = NULL;
 tmpprintdlg->lpSetupTemplateName = NULL;
 tmpprintdlg->hPrintTemplate = NULL;
 tmpprintdlg->hSetupTemplate = NULL;

 //Show the print dialog box. This
 //is a modal dialog.
 ret = PrintDlg(tmpprintdlg);

 //Returns
 *outobj = tmpprintdlg;
 if(ret == 0){
  return FALSE;
 }else{
  return TSUCCESS;
 } //EndIf

} //End PrntShowPrintDialog

钩子过程的两个回调函数是虚拟函数,仅返回

TSUCCESS

c winapi printing
1个回答
0
投票

当您致电

PrintDlg
时,您已经申请了 DC。

使用它。请参阅

PD_RETURNDC
hDC
中的成员
PRINTDLGA

的文档
© www.soinside.com 2019 - 2024. All rights reserved.