Click here to Skip to main content
12,077,332 members (43,801 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: printing
Is there any easy way to print text file to a default printer connected.
I am using c++(MFC).
Thanks
Posted 11-Aug-12 3:06am
Edited 11-Aug-12 3:07am
v2
Comments
wedagedara 11-Aug-12 8:21am
   
bump
Volynsky Alex 11-Aug-12 8:26am
   
Some example with non-default printer in MFC (but may help),
look here:
http://support.microsoft.com/kb/166129
wedagedara 11-Aug-12 9:03am
   
This will help me. Thanks
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Please look following example:
HDC CMyClass::GetPrinterDC(string devName, HDC* hDC, HANDLE* hPrinter, int NumCopies)
{
    *hDC = 0;
    *hPrinter = 0;
    BOOL bVal = OpenPrinter(const_cast<char*>(devName.c_str()),hPrinter,NULL);
    if(bVal)
    {
        long nSize = DocumentProperties(NULL,*hPrinter,const_cast<char*>(devName.c_str()),NULL,NULL,NULL);
 
        BYTE *dv = new BYTE[nSize];
        memset(dv,0,nSize);
        DEVMODE* devmode = reinterpret_cast<DEVMODE*>(dv);
        // get current printer settings
        if(DocumentProperties(NULL,*hPrinter,const_cast<char*>(devName.c_str()),
                         devmode, NULL,DM_OUT_BUFFER) == IDOK)
        {
            devmode->dmCopies = NumCopies;
            devmode->dmOrientation = DMORIENT_LANDSCAPE;
            devmode->dmFields |= (DM_COPIES|DM_ORIENTATION);
            if(DocumentProperties(NULL,*hPrinter,const_cast<char*>(devName.c_str()),
                         devmode,devmode,DM_OUT_BUFFER | DM_IN_BUFFER) == IDOK)
            {
                *hDC = CreateDC(NULL,const_cast<char*>(devName.c_str()),NULL,devmode);
            }
 
        }
        delete[] dv;
    }
    return *hDC;
}
 
void CMyClass::PrintReport()
{
    char buf[255];
    DWORD dwSize = sizeof(buf);
    // get the default printer
    Registry reg("Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows",HKEY_CURRENT_USER);
    reg.GetString("Device", buf, &dwSize);
    reg.Close();
    if(buf[0] == 0) return; // printer not found
    char *ptr = strchr(buf,',');
    if(ptr)
        *ptr = 0;
    HANDLE hPrinter = 0;
    HDC hDC = 0;
    GetPrinterDC(buf, &hDC,&hPrinter,1);
    if(hDC == 0)
        return;
 
    CDC dc;
    dc.Attach(hDC);     // Get and attach a printer DC
    dc.m_bPrinting = TRUE;
  //
  // remainder of function deleted
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
wedagedara 11-Aug-12 9:02am
   
Thanks, but my language is C++
snehasish nandy 11-Aug-12 9:05am
   
atleast u can get some isea out of it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160212.1 | Last Updated 11 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100