Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

Como imprimir un TreeView?

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Consultas
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: Como imprimir un TreeView? Responder citando

¿Alguien conoce algun metodo para imprimir un TreeView sin utilizar QuickReport (ver https://cppbuilder.es/viewtopic.php?t=4)?
He estado haciendo algunas pruebas utilizando el canvas (basandome en el ejemplo que utiliza QuickReport) con resultados hasta el momento catastroficos.

saludos y gracias por adelantado
diego
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Código:
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TImpresion *Impresion;
//---------------------------------------------------------------------------
__fastcall TImpresion::TImpresion(TComponent* Owner)
    : TForm(Owner)
{

}
//---------------------------------------------------------------------------

void __fastcall TImpresion::Button1Click(TObject *Sender)
{
TRect   MyRect, MyOther;
MyRect = Rect(0,0,TreeView1->Width,TreeView1->Height);
MyOther = Rect(0,0,TreeView1->Width, TreeView1->Height);
QuickReport2->QRImage1->Width = TreeView1->Width;
QuickReport2->QRImage1->Height = TreeView1->Height;
QuickReport2->QRImage1->Picture->Bitmap->Width=TreeView1->Width-4;
QuickReport2->QRImage1->Picture->Bitmap->Height=TreeView1->Height-4;
QuickReport2->QRImage1->Canvas->CopyRect(MyOther, TreeView1->Canvas, MyRect);

QuickReport2->Preview();

}
//---------------------------------------------------------------------------
void __fastcall TImpresion::Button2Click(TObject *Sender)
{
TRect   MyRect, MyOther;
MyRect = Rect(0,0,TreeView1->Width,TreeView1->Height);
MyOther = Rect(0,0,TreeView1->Width, TreeView1->Height);
image->Width = TreeView1->Width;
image->Height = TreeView1->Height;
// con bordes bsSingle
image->Picture->Bitmap->Width=TreeView1->Width-4;
image->Picture->Bitmap->Height=TreeView1->Height-4;
// con bordes bsNone (o sin bordes :)
// Image1->Picture->Bitmap->Width=TreeView1->Width;
// Image1->Picture->Bitmap->Height=TreeView1->Height;
image->Canvas->CopyRect(MyOther, TreeView1->Canvas, MyRect);
}
//---------------------------------------------------------------------------
void __fastcall TImpresion::Button3Click(TObject *Sender)
{
    Button2->Click();

    TPrinter *p = Printer();

    // crear un dc para la imagen
    HDC h_dc = image->Picture->Bitmap->Canvas->Handle;
    int bmp_w = image->Picture->Bitmap->Width,
    bmp_h = image->Picture->Bitmap->Height;
    HDC h_mem_dc = ::CreateCompatibleDC (h_dc);
    HBITMAP h_mem_bmp = ::CreateCompatibleBitmap (h_dc, bmp_w, bmp_h);
    HBITMAP h_old_bmp = ::SelectObject (h_mem_dc, h_mem_bmp);

    bool is_pal_dev = false;
    LOGPALETTE *pal;
    HPALETTE h_pal, h_old_pal;

    if (::GetDeviceCaps (image->Canvas->Handle, RASTERCAPS) & RC_PALETTE)
    {
        pal = static_cast<tagLOGPALETTE*>(malloc (sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256)));
        memset (pal, 0, sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256));
        pal->palVersion = 0x300;
        pal->palNumEntries = ::GetSystemPaletteEntries(image->Canvas->Handle, 0, 256, pal->palPalEntry);
        if (pal->palNumEntries != 0)
        {
            h_pal = ::CreatePalette (pal);
            h_old_pal = ::SelectPalette (h_mem_dc, h_pal, false);
            is_pal_dev = true;
        }
        else
        {
            free (pal);
        }
    }

    // copiar la imagen en el dc
    ::BitBlt (h_mem_dc, 0, 0, bmp_w, bmp_h, h_dc, 0, 0, SRCCOPY);

    if (is_pal_dev)
    {
        ::SelectPalette (h_mem_dc, h_old_pal, false);
        ::DeleteObject (h_pal);
    }

    // borrar el dc
    ::SelectObject (h_mem_dc, h_old_bmp);
    ::DeleteDC (h_mem_dc);

    // tomar memoria para una estructura BITMAPINFO
    HANDLE h_bmp_info = ::GlobalAlloc (GHND, sizeof (BITMAPINFO) + (sizeof (RGBQUAD) * 256));
    BITMAPINFO* bmp_info = static_cast<tagBITMAPINFO*>(::GlobalLock (h_bmp_info));
    // inicializar la estructura
    memset (bmp_info, NULL, sizeof (BITMAPINFO) + (sizeof (RGBQUAD) * 255));
    bmp_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmp_info->bmiHeader.biPlanes = 1;
    bmp_info->bmiHeader.biBitCount = 8;
    bmp_info->bmiHeader.biWidth = bmp_w;
    bmp_info->bmiHeader.biHeight = bmp_h;
    bmp_info->bmiHeader.biCompression = BI_RGB;

    // calculamos memoria necesaria para los bits
    ::GetDIBits (h_dc, h_mem_bmp, 0, bmp_h, NULL, bmp_info, DIB_RGB_COLORS);

    // Reservamos memoria para los bits
    HANDLE h_bits = GlobalAlloc (GHND, bmp_info->bmiHeader.biSizeImage);
    void *bits = ::GlobalLock (h_bits);

    //  obtenemos los bits
    ::GetDIBits (h_dc, h_mem_bmp, 0, bmp_h, bits, bmp_info, DIB_RGB_COLORS);

    // fijamos paleta
    if (is_pal_dev)
    {
        for (int i = 0; i < pal->palNumEntries; i++)
        {
            bmp_info->bmiColors[i].rgbRed = pal->palPalEntry[i].peRed;
            bmp_info->bmiColors[i].rgbGreen = pal->palPalEntry[i].peGreen;
            bmp_info->bmiColors[i].rgbBlue = pal->palPalEntry[i].peBlue;
        }
        free (pal);
    }

    // imprimimos
    p->BeginDoc ();

    // escalar el tamaño
    int scale_x, scale_y;
    int factor_reduccion = 5;
    if (p->PageWidth < p->PageHeight)
    {
        scale_x = p->PageWidth/factor_reduccion;
        scale_y = image->Picture->Height * (p->PageWidth / bmp_w)/factor_reduccion;
    }
    else
    {
        scale_x = image->Picture->Width * (p->PageHeight / bmp_h)/factor_reduccion;
        scale_y = p->PageHeight/factor_reduccion;
    }

    is_pal_dev = false;
    if (::GetDeviceCaps (h_dc, RASTERCAPS) & RC_PALETTE)
    {
        pal = static_cast<tagLOGPALETTE*>(malloc (sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256)));
        memset (pal, 0, sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * 256));
        pal->palVersion = 0x300;
        pal->palNumEntries = 256;
        for (int i = 0; pal->palNumEntries; i++)
        {
            pal->palPalEntry[i].peRed = bmp_info->bmiColors[i].rgbRed;
            pal->palPalEntry[i].peGreen = bmp_info->bmiColors[i].rgbGreen;
            pal->palPalEntry[i].peBlue = bmp_info->bmiColors[i].rgbBlue;
        }
        h_pal = CreatePalette(pal);
        free (pal);
        h_old_pal = SelectPalette(p->Canvas->Handle, h_pal, false);
        is_pal_dev = true;
    }

    // enviamos bits a la impresora
    StretchDIBits(p->Canvas->Handle, 0, 0, scale_x, scale_y,
      0, 0, bmp_w, bmp_h, bits,bmp_info, DIB_RGB_COLORS, SRCCOPY);

    // fin de impresion
    p->EndDoc ();

    // limpiamos
    ::DeleteObject (h_mem_bmp);
    if (is_pal_dev)
    {
        ::SelectObject (p->Canvas->Handle, h_old_pal);
        ::DeleteObject (h_pal);
    }
    ::GlobalUnlock (bits);
    ::GlobalFree (h_bits);
    ::GlobalUnlock (bmp_info);
    ::GlobalFree (h_bmp_info);

}
//---------------------------------------------------------------------------
void __fastcall TImpresion::Button4Click(TObject *Sender)
{
Printer()->BeginDoc();
TreeView1->PaintTo(Printer()->Handle, 10, 10);
Printer()->EndDoc();
}
//---------------------------------------------------------------------------
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

Bueno, antes que nada mil gracias. Me diste para elejir! Un lujo.
Ahora vienen las inevitables preguntas.
1)
Código:
void __fastcall TImpresion::Button1Click(TObject *Sender)
{
TRect   MyRect, MyOther;
MyRect = Rect(0,0,TreeView1->Width,TreeView1->Height);
MyOther = Rect(0,0,TreeView1->Width, TreeView1->Height);
QuickReport2->QRImage1->Width = TreeView1->Width;
QuickReport2->QRImage1->Height = TreeView1->Height;
QuickReport2->QRImage1->Picture->Bitmap->Width=TreeView1->Width-4;
QuickReport2->QRImage1->Picture->Bitmap->Height=TreeView1->Height-4;
QuickReport2->QRImage1->Canvas->CopyRect(MyOther, TreeView1->Canvas, MyRect);

QuickReport2->Preview();
}
En el ejemplo anterior, con QuickReport, tengo un problema que no pude solucionar aun (lo habia probado antes). Cuando la imagen de la arboresencia supera la pagina, esta queda trunca. Sabés como hacer para que continue el la pagina siguiente?
Bueno en realidad el tema esta en que el canvas toma solo lo que se ve en ese momento en la pantalla. El resto de los items, que solo los veo si me deslizo con el control bar quedan fuera. Sad

2)
Código:
void __fastcall TImpresion::Button2Click(TObject *Sender)
{
TRect   MyRect, MyOther;
MyRect = Rect(0,0,TreeView1->Width,TreeView1->Height);
MyOther = Rect(0,0,TreeView1->Width, TreeView1->Height);
image->Width = TreeView1->Width;
image->Height = TreeView1->Height;
// con bordes bsSingle
image->Picture->Bitmap->Width=TreeView1->Width-4;
image->Picture->Bitmap->Height=TreeView1->Height-4;
// con bordes bsNone (o sin bordes :)
   // Image1->Picture->Bitmap->Width=TreeView1->Width;
   // Image1->Picture->Bitmap->Height=TreeView1->Height;
image->Canvas->CopyRect(MyOther, TreeView1->Canvas, MyRect);
}
Aca el problema es que no se como declaraste "image". TBitmap no es ni a palos.

3)
Código:
void __fastcall TImpresion::Button4Click(TObject *Sender)
{
Printer()->BeginDoc();
TreeView1->PaintTo(Printer()->Handle, 10, 10);
Printer()->EndDoc();
}
Este ejemplo tambien lo habia probado. El problema que tengo es que la imagen es impresa en un tamaño que no supera 1cm x 1cm.

saludos
diego
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mostrar mensajes de anteriores:   
Cambiar a:  
No puedes publicar temas nuevos en este foro
No puedes responder a temas en este foro
No puedes editar tus mensajes en este foro
No puedes borrar tus mensajes en este foro
No puedes votar en las encuestas de este foro


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.