Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there any easy way to print text file to a default printer connected.
I am using c++(MFC).
Thanks
Posted
Updated 11-Aug-12 2: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

Please look following example:
C++
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
}
 
Share this answer
 
 
Share this answer
 
Comments
wedagedara 11-Aug-12 9:02am    
Thanks, but my language is C++
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900