Miguel Angel Torres Master


Registrado: 23 Abr 2006Mensajes: 282Ubicación: Siegen, Alemania
|
Publicado: Dom 23 Abr, 2006 11:39 pm Asunto: Codigo para Imprimir de un Archivo |
|
|
El siguiente código lo implementé basado en uno encontrado aquí, pero encontré unos breves errores aquel , le hice la modificación respectiva y lo probe, dandome cuenta que funciona correctamente.
El siguiente programa lee un archivo con extensión .txt, lo pone en un memo y cuando doy click en un boton de la forma, imprime el texto.
Recordar añadir un TButton, un TMemo y un TPrintDialog.
//---------------------------------------------------------------------------
//--Mejorado Por Miguel Angel Torres Arredondo
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <Printers.hpp>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
FILE *HiScores;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
HiScores = fopen("Hi.doc", "r"); //Leo el archivo
Memo1->Lines->LoadFromFile("C:\\Hi.txt");//Cargo el archivo en el Memo
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (PrintDialog1->Execute()) {
Printer()->Canvas->Font = Memo1->Font; //Asignamos la fuente de impresion
int lineHeight = abs(MulDiv(Printer()->Canvas->Font->Size,
GetDeviceCaps(Printer()->Handle, LOGPIXELSY), 72)); // determinamos altura de la linea
lineHeight *= 3; //Yo aumente la altura de linea para no amontonar los renglones
//lineHeight *= 1.4;
int linesPerPage = (Printer()->PageHeight/lineHeight) - 2;
int lineCount = 2; // iniciamos la impresion en la linea 2 para dejar margen (en vez de la 0)
Printer()->BeginDoc();
for (int i=0;i< Memo1->Lines->Count;i++) { //Barremos todas las lineas del memo
if (lineCount++ == linesPerPage) { //Si sobre paso el número de líneas por hoja, creo otra hoja
lineCount = 2;
Printer()->NewPage();
}
String s = Memo1->Lines->Strings[i];
Printer()->Canvas->TextOut(0, lineCount * lineHeight, s); //Imprimo cada Linea barrida
}
Printer()->EndDoc();
}
}
//--------------------------------------------------------------------------- |
|