Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Pieshna en 6 Junio 2018, 19:58 pm



Título: Duda acerca de imprimir (en impresora)
Publicado por: Pieshna en 6 Junio 2018, 19:58 pm
Hola buenos dias, tardes o noches...

Queria saber si alguien me puede ayudar con un codigo para imprimir (con impresora) ya que he estado buscando pero no encuentro uno funcional para c++ :-(
Es para un proyecto final de programacion y solo me falta el que se pueda imprimir los datos, ayuda! ;)


Título: Re: Duda acerca de imprimir (en impresora)
Publicado por: SrMcLister en 7 Junio 2018, 14:07 pm
Buenas Pieshna.
¿Podrías dar mas detalles? SO por ejemplo, marca de impresora, puerto al que está conectada...
Un Saludo.


Título: Re: Duda acerca de imprimir (en impresora)
Publicado por: Pieshna en 8 Junio 2018, 01:39 am
Buenas Pieshna.
¿Podrías dar mas detalles? SO por ejemplo, marca de impresora, puerto al que está conectada...
Un Saludo.

Hola, saludos SrMcLister
Estoy usando Windows 10 y estoy programando con dev-c++, la impresora es canon (MP 280), puertos LPT1-3, USB002


Título: Re: Duda acerca de imprimir (en impresora)
Publicado por: ThunderCls en 8 Junio 2018, 20:09 pm
C++ no tiene soporte nativo para la impresion de documentos (al menos que yo sepa), la impresión está condicionada por el sistema operativo que uses, en este caso windows tiene sus drivers y un sistema de spooling, etc. Aqui tienes alguna informacion

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162861(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ff819270(v=vs.85).aspx

De cualquier forma necesitas usar las APIs que te brinda tu sistema operativo y hacer las llamadas desde tu codigo c++. He encontrado este codigo por ahi pero no se si funcionara del todo, igual te podria servir

Código
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <string.h>
  4.  
  5. void printer(char text[])
  6. {
  7.  
  8. // Bring up a dialog to choose the printer
  9. PRINTDLG pd = {0};
  10. pd.lStructSize = sizeof( pd );
  11. pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
  12. pd.nCopies=1;
  13.  
  14. // Show the printer Dialog
  15. PrintDlg( &pd );
  16.  
  17.  
  18. // Zero and then initialize the members of a DOCINFO structure.
  19. DOCINFO di = {0};
  20. di.cbSize = sizeof(DOCINFO);
  21. di.lpszDocName = "Scribble Printout";
  22. di.lpszOutput = (LPTSTR) NULL;
  23. di.lpszDatatype = (LPTSTR) NULL;
  24. di.fwType = 0;
  25.  
  26. // Begin a print job by calling the StartDoc function.
  27. StartDoc(pd.hDC, &di);
  28.  
  29. // Inform the driver that the application is about to begin sending data.
  30. StartPage(pd.hDC);
  31.  
  32. //here we put images\text or other DC things;)
  33.  
  34. //send some text
  35. TextOut(pd.hDC,800,800,text, strlen(text));
  36.  
  37.  
  38. //Lets close  the printer
  39. // Inform the driver that the page is finished.
  40. EndPage(pd.hDC);
  41.  
  42. // Inform the driver that document has ended.
  43. EndDoc(pd.hDC);
  44. }
  45.  
  46. int main ()
  47. {
  48. printer("Hello world");
  49. return 0;
  50. }

Igual puedes buscarte la vida con alguna libreria, estas por ejemplo:

https://www.gtkmm.org/es/index.html
https://developer.gnome.org/gtkmm-tutorial/unstable/sec-printing-example.html.en#sec-printing-example-simple
https://www.codeproject.com/Articles/89/Printing-Class-Library

Saludos y buena suerte


Título: Re: Duda acerca de imprimir (en impresora)
Publicado por: Pieshna en 21 Junio 2018, 06:02 am
C++ no tiene soporte nativo para la impresion de documentos (al menos que yo sepa), la impresión está condicionada por el sistema operativo que uses, en este caso windows tiene sus drivers y un sistema de spooling, etc. Aqui tienes alguna informacion

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162861(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ff819270(v=vs.85).aspx

De cualquier forma necesitas usar las APIs que te brinda tu sistema operativo y hacer las llamadas desde tu codigo c++. He encontrado este codigo por ahi pero no se si funcionara del todo, igual te podria servir

Código
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <string.h>
  4.  
  5. void printer(char text[])
  6. {
  7.  
  8. // Bring up a dialog to choose the printer
  9. PRINTDLG pd = {0};
  10. pd.lStructSize = sizeof( pd );
  11. pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
  12. pd.nCopies=1;
  13.  
  14. // Show the printer Dialog
  15. PrintDlg( &pd );
  16.  
  17.  
  18. // Zero and then initialize the members of a DOCINFO structure.
  19. DOCINFO di = {0};
  20. di.cbSize = sizeof(DOCINFO);
  21. di.lpszDocName = "Scribble Printout";
  22. di.lpszOutput = (LPTSTR) NULL;
  23. di.lpszDatatype = (LPTSTR) NULL;
  24. di.fwType = 0;
  25.  
  26. // Begin a print job by calling the StartDoc function.
  27. StartDoc(pd.hDC, &di);
  28.  
  29. // Inform the driver that the application is about to begin sending data.
  30. StartPage(pd.hDC);
  31.  
  32. //here we put images\text or other DC things;)
  33.  
  34. //send some text
  35. TextOut(pd.hDC,800,800,text, strlen(text));
  36.  
  37.  
  38. //Lets close  the printer
  39. // Inform the driver that the page is finished.
  40. EndPage(pd.hDC);
  41.  
  42. // Inform the driver that document has ended.
  43. EndDoc(pd.hDC);
  44. }
  45.  
  46. int main ()
  47. {
  48. printer("Hello world");
  49. return 0;
  50. }

Igual puedes buscarte la vida con alguna libreria, estas por ejemplo:

https://www.gtkmm.org/es/index.html
https://developer.gnome.org/gtkmm-tutorial/unstable/sec-printing-example.html.en#sec-printing-example-simple
https://www.codeproject.com/Articles/89/Printing-Class-Library

Saludos y buena suerte

Muchas gracias lo probare y gracias también por los links con la información ;) ;-)