Print operation class for printer under Windows VS2015

Source: Internet
Author: User

#include"stdafx.h"#include"CPrinter.h"BOOL Cprinter::getprinterdevice (LPTSTR pszprintername, Hglobal* Phdevnames, hglobal*Phdevmode) {    //if NULL is passed and then assume we are setting app object ' s//DevMode and DevNames    if(Phdevmode = = NULL | | phdevnames = =NULL)returnFALSE; //Open PrinterHANDLE hprinter; if(OpenPrinter (Pszprintername, &hprinter, NULL) = =FALSE)returnFALSE; //obtain PRINTER_INFO_2 structure and close PRINTERDWORD dwbytesreturned, dwbytesneeded; GetPrinter (Hprinter,2Null0, &dwbytesneeded); Printer_info_2* P2 = (printer_info_2*) GlobalAlloc (gptr, dwbytesneeded); if(GetPrinter (Hprinter,2, (LPBYTE) P2, dwbytesneeded,&dwbytesreturned) = =0) {GlobalFree (P2);        ClosePrinter (hprinter); returnFALSE;    } ClosePrinter (Hprinter); //Allocate a global handle for DEVMODEHglobal Hdevmode = GlobalAlloc (GHND,sizeof(*p2->pdevmode) +P2->pdevmode->Dmdriverextra);    ASSERT (Hdevmode); DEVMODE* pDevMode = (devmode*) GlobalLock (Hdevmode);    ASSERT (pDevMode); //copy DEVMODE data from printer_info_2::p DEVMODEmemcpy (pDevMode, P2->pdevmode,sizeof(*p2->pdevmode) +P2->pdevmode->Dmdriverextra);    GlobalUnlock (Hdevmode); //Compute size of DEVNAMES structure from printer_info_2 ' s dataDWORD Drvnamelen = lstrlen (p2->pdrivername) +1;//driver nameDWORD Ptrnamelen = lstrlen (p2->pprintername) +1;//Printer NameDWORD Pornamelen = lstrlen (p2->pportname) +1;//Port Name//Allocate A global handle big enough to the hold DEVNAMES.Hglobal hDevNames =GlobalAlloc (GHND,sizeof(DEVNAMES) +(Drvnamelen+ Ptrnamelen + pornamelen) *sizeof(TCHAR));    ASSERT (hDevNames); DEVNAMES* Pdevnames = (devnames*) GlobalLock (hdevnames);    ASSERT (Pdevnames); //Copy The DEVNAMES information from Printer_info_2//Tcoffset = TCHAR Offset into structure    intTcoffset =sizeof(DEVNAMES)/sizeof(TCHAR); ASSERT (sizeof(DEVNAMES) = = tcoffset*sizeof(TCHAR)); Pdevnames->wdriveroffset =Tcoffset; memcpy ((LPTSTR) Pdevnames+ Tcoffset, p2->pDriverName, Drvnamelen*sizeof(TCHAR)); Tcoffset+=Drvnamelen; Pdevnames->wdeviceoffset =Tcoffset; memcpy ((LPTSTR) Pdevnames+ Tcoffset, p2->pPrinterName, Ptrnamelen*sizeof(TCHAR)); Tcoffset+=Ptrnamelen; Pdevnames->woutputoffset =Tcoffset; memcpy ((LPTSTR) Pdevnames+ Tcoffset, p2->Pportname, Pornamelen*sizeof(TCHAR)); Pdevnames->wdefault =0;    GlobalUnlock (hDevNames);   GlobalFree (p2); //Free printer_info_2//set the new Hdevmode and hDevNames*phdevmode =Hdevmode; *phdevnames =hDevNames; returnTRUE;}/*First, you print the preparation work, get the action handle to the device*/BOOL cprinter::P rintstr (CString strmessage,bool iscut,intprnnum) {#ifdef DEBUG log. LOGF ("%s","Start printing");#endifTCHAR cutstr[4] = {0x1D,0x53,0x31 }; if(! Getprinterdevice (M_strprintdevice.getbuffer (0), &m_hdevnames, &M_hdevmode)) {AfxMessageBox (_t ("Getprinterdevice called failed!")); return false; }    Else{AfxGetApp ()-Selectprinter (M_hdevmode, m_hdevnames);    } m_strprintdevice.releasebuffer ();    CPrintDialog Printerdlg (FALSE); PrinterDlg.m_pd.hDevMode=M_hdevmode; PrinterDlg.m_pd.hDevNames=M_hdevnames;        CDC DC; if(PRINTERDLG.CREATEPRINTERDC () = =NULL) {AfxMessageBox (_t ("CREATEPRINTERDC Failure")); return false; } DC.    Attach (PrinterDlg.m_pd.hDC);                                                                             DocInfo di; //the following content on the Internet a lot, do not explainDi.cbsize =sizeof(DocInfo); Di.lpszdocname= _t ("Drive-by printing test"); Di.lpszdatatype=NULL; Di.lpszoutput=NULL; Di.fwtype=0; CRect Recprint (0,0, DC. GetDeviceCaps (LOGPIXELSX), DC.    GetDeviceCaps (Logpixelsy)); dc. Dptolp (&recprint); dc. SetWindowOrg (0,0);    CFont NewFont; //VERIFY (Newfont.createpointfont, _t ("Arial"), &DC);VERIFY (Newfont.createpointfont (_ttoi (m_fontsize), M_fontname, &DC)); CFont* Oldfont = DC. SelectObject (&NewFont); dc. SetTextAlign (Ta_top|ta_left);    CString Strprint; intNIndex =0; intx =0; inty = -;    CSize textSize; TextSize= DC. GetTextExtent (_t ("xx"),2); dc. STARTDOCW (&di); dc.    StartPage (); dc.           Setmapmode (Mm_text); //based on the width and height of the current font, followed by this height as the row high             while((NIndex = Strmessage.find (_t ("\ n"))) >-1))//Print the contents of the Idc_edit1 edit box, support line wrapping, a line break equals ' \ r \ n ', so at the beginning strmessage + = _t ("\ r \ n"){strprint=Strmessage.left (NIndex); strmessage= Strmessage.mid (NIndex +2); dc.            TEXTOUTW (x, y, strprint); Y+ = textsize.cy;//Move down one line, line height to font height        }        if(m_cut==l"1") {y+= -; dc.        TEXTOUTW (x, y, cutstr); }        Else{y+= -; dc. TEXTOUTW (x,y,_t ("")); } DC.    SelectObject (Oldfont);    Newfont.deleteobject (); dc.    EndPage (); dc.    EndDoc (); DeleteDC (DC.    Detach ()); return true;}

Print operation class for printer under Windows VS2015

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.