Click here to Skip to main content
Rate this: bad
good
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 2:06am
Edited 11-Aug-12 2:07am
v2
Comments
wedagedara at 11-Aug-12 8:21am
   
bump
Volynsky Alex at 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 at 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 at 11-Aug-12 9:02am
   
Thanks, but my language is C++
snehasish nandy at 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
0 OriginalGriff 205
1 ProgramFOX 120
2 Richard MacCutchan 100
3 ChauhanAjay 100
4 kbrandwijk 90
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 11 Aug 2012
Copyright © CodeProject, 1999-2014
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