首页 > 站长学院 > 网络编程 > 正文

使用wxWidgets实现打印功能wxDC居中显示(CodeBlocks)
2015-06-18 11:17:35  吾爱源码     我要评论()
字号:T|T

 1、从wxPrintout派生一个类Printout,类源码如下:

 

        =====文件名:printout.h=====
#ifndef PRINTOUT_H
#define PRINTOUT_H

#include <wx/wx.h>
#include <wx/print.h>
#include <wx/printdlg.h>
#include "wxPrintMain.h"
class wxPrintFrame;
class Printout : public wxPrintout
{
    public:
//        Printout();
        Printout(wxPrintFrame* frame, const wxString &title = wxT("My printout")): wxPrintout(title) { m_frame=frame; }
//        virtual ~Printout();
        virtual void GetPageInfo(int *minPage, int *maxPage,int *pageFrom, int *pageTo);
        virtual bool HasPage(int pageNum);
        virtual void DrawPageOne(wxDC*dc);
        virtual void DrawPageTwo(wxDC *dc);
        virtual bool OnBeginDocument(int startPage, int endPage);
        bool OnPrintPage(int page);
    protected:
    private:
        wxPrintFrame *m_frame;
};

#endif // PRINTOUT_H
 
 
        =====文件名:printout.cpp=====
#include "printout.h"

//Printout::Printout()
//{
//    //ctor
//}

//Printout::~Printout()
//{
//    //dtor
//}

bool Printout::OnBeginDocument(int startPage, int endPage)
{
    if (!wxPrintout::OnBeginDocument(startPage, endPage))
        return false;

    return true;
}

void Printout::GetPageInfo(int *minPage, int *maxPage,int *pageFrom, int *pageTo)
{
    *minPage = 1;
    *maxPage = 2;
    *pageFrom = 1;
    *pageTo = 2;
}

bool Printout::HasPage(int pageNum)
{
    return (pageNum >= 1 && pageNum <= 2);
}

void Printout::DrawPageOne(wxDC*dc)
{
    // 下面的代码可以这样写只是因为我们知道图片的大小是200 x200
    // 如果我们不知道的话,需要先计算图片的大小
    float maxX = 200;
    float maxY = 200;
    // 让我们先设置至少个设备单位的边框50
    float marginX = 50;
    float marginY = 50;
    // 将边框的大小增加到图片的周围
    maxX += (2*marginX );
    maxY += (2*marginY );
    // 获取象素单位的当前设备上下文的大小
    int w, h;
    dc->GetSize (&w, &h);
    //计算一个合适的缩放值
    float scaleX =( float )(w/ maxX );
    float scaleY =( float )(h/ maxY );
    // 选择或者方向上较小的那个XY
    float actualScale = wxMin ( scaleX , scaleY );
    // 计算图片在设备上的合适位置以便居中
    float posX = ( float )(( w*(200*actualScale ))/2.0);
    float posY = ( float )(( h*(200*actualScale ))/2.0);
    // 设置设备平移和缩放
    dc->SetUserScale ( actualScale , actualScale );
    dc->SetDeviceOrigin ( ( long )posX , ( long ) posY );
    // ok现在开始画画
    dc->SetBackground(*wxWHITE_BRUSH );
    dc->Clear();
//    dc->SetFont ( wxGetApp ().m_testFont );
    dc->SetBackgroundMode ( wxTRANSPARENT );
    dc->SetBrush (*wxCYAN_BRUSH );
    dc->SetPen (*wxRED_PEN );
    dc->DrawRectangle (0, 30, 200 , 100);
    dc->DrawText ( wxT (" Rectangle 200 by 100 "), 40, 40);
    dc->SetPen ( wxPen (*wxBLACK ,0, wxDOT_DASH ) );
    dc->DrawEllipse (50 , 140 , 100 , 50);
    dc->SetPen (*wxRED_PEN );
    dc->DrawText ( wxT (" Test message : this is in 10 point text "),10, 180);
}

void Printout::DrawPageTwo(wxDC *dc)
{
    int ppiScreenX, ppiScreenY;
    GetPPIScreen(&ppiScreenX, &ppiScreenY);
    int ppiPrinterX, ppiPrinterY;
    GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
    float scale = (float )((float )ppiPrinterX/(float)ppiScreenX);
    int pageWidth, pageHeight;
    int w, h;
    dc->GetSize(&w, &h);
    GetPageSizePixels(&pageWidth, &pageHeight);
    float overallScale = scale*( float )(w/(float)pageWidth);
    dc->SetUserScale(overallScale, overallScale) ;
    float logUnitsFactor = (float )(ppiPrinterX/(scale*25.4));
    float logUnits = (float)(50*logUnitsFactor);
    dc->SetPen(*wxBLACK_PEN);
    dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250);
    dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits));
    dc->SetBackgroundMode(wxTRANSPARENT);
    dc->SetBrush(*wxTRANSPARENT_BRUSH);
//    dc->SetFont(wxGetApp().m_testFont);
    dc->DrawText(wxT("Some test text"), 200, 300 );
}

bool Printout::OnPrintPage(int page)
{
    wxDC *dc = GetDC();
    if (dc)
    {
        if (page == 1)
        {
            DrawPageOne(dc);
        }

        else if (page == 2)
        {
            DrawPageTwo(dc);
        }
        MapScreenSizeToPage();
        dc->DrawText(wxString::Format(wxT("PAGE %d"), page), 0, 0);
        return true;
    }
    else
        return false;
}

 2、定义一个全局变量并初始化,使用后清理内存

wxPrintData *g_printData = NULL;
wxPageSetupDialogData* g_pageSetupData = NULL;


g_printData = new wxPrintData;
g_pageSetupData = new wxPageSetupDialogData;

 

delete g_printData;

delete g_pageSetupData;

 

 

3、实现打印预览

void wxPrintFrame::OnPrintPreviewClicked(wxCommandEvent& event)
{
    wxPrintPreview *preview =new wxPrintPreview(new Printout(this), new Printout(this), g_printData);
    if (!preview->IsOk())
    {
        delete preview;
        wxMessageBox(_T("There was a problem previewing"),_T("打印预览"),wxOK);
        return;
    }
    wxPreviewFrame *frame =new wxPreviewFrame(preview, this, wxT("打印预览实例"), wxPoint(100, 100), wxSize(600, 650));
    frame->Centre(wxBOTH);
    frame->Initialize();
    frame->Show();
}

 

4、实现打印功能

void wxPrintFrame::OnPrintClicked(wxCommandEvent& event)
{
    wxPrintDialogData printDialogData(* g_printData);

    wxPrinter printer(&printDialogData);
    Printout po(this, wxT("My printout"));
    if (!printer.Print(this, &po, true /*prompt*/))
    {
        if (wxPrinter::GetLastError() == wxPRINTER_ERROR)
        {
            wxMessageBox(wxT("There was a problem printing. Perhaps your current printer is not set correctly?"));
        }
        else
        {
            wxMessageBox(wxT("You canceled printing"));
        }
    }
    else
    {
        (*g_printData) = printer.GetPrintDialogData().GetPrintData();
    }
}

 

 

5、实现打印设置

void wxPrintFrame::OnPrintSetupClicked(wxCommandEvent& event)
{
    (*g_pageSetupData) = *g_printData;

    wxPageSetupDialog pageSetupDialog(this, g_pageSetupData);
    pageSetupDialog.ShowModal();

    (*g_printData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData();
    (*g_pageSetupData) = pageSetupDialog.GetPageSetupDialogData();
}

本站地址:www.52codes.net

关键词: wxWidgets wxWidgets打印

责任编辑:吾爱源码

我要评论

已有位网友参与评论

网站地图

软件下载

源码下载 | 吾爱源码 | 论坛

实用工具

联系我们 | 软件提交 | 版权声明 | 意见建议 | 网站地图

本站所有资源均来自互联网,本站只做收集,如本站侵犯到您的版权请发送邮件给我们,我们收到邮件后会在第一时间删除!商务QQ:1674653

Copyright (C) 1997-2012 www.52codes.net 版权所有