printing.cpp如下:
#include "wx/wxprec.h"
#include "wx/wx.h"
#include
#include "wx/metafile.h"
#include "wx/print.h"
#include "wx/printdlg.h"
#include "wx/image.h"
#include "wx/accel.h"
#include
#include
#include "printing.h"
MyFrame *frame = (MyFrame *) NULL;
wxPrintData *g_printData = (wxPrintData*) NULL ;
MyCanvas *canvas;
wxBufferedPaintDC* dcs;
test_sheet* sheet;
wxPageSetupDialogData* g_pageSetupData = (wxPageSetupDialogData*) NULL;
//bool WritePageHeader(wxPrintout *printout, wxDC *dc, const wxChar *text, float mmToLogical);
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit(void)
{
wxInitAllImageHandlers();
m_testFont.Create(10, wxSWISS, wxNORMAL, wxNORMAL);
g_printData = new wxPrintData;
g_pageSetupData = new wxPageSetupDialogData;
// copy over initial paper size from print record
(*g_pageSetupData) = *g_printData;
// Set some initial page margins in mm.
g_pageSetupData->SetMarginTopLeft(wxPoint(15, 15));
g_pageSetupData->SetMarginBottomRight(wxPoint(15, 15));
// Create the main frame window
frame = new MyFrame((wxFrame *) NULL, _T("testview"),
wxPoint(0, 0), wxSize(800, 600));
frame->CreateStatusBar(1);
wxMenu *file_menu = new wxMenu;
file_menu->Append(WXPRINT_PRINT, _T("&Print..."), _T("Print"));
file_menu->Append(WXPRINT_PAGE_SETUP, _T("Page Set&up..."), _T("Page setup"));
file_menu->Append(WXPRINT_PREVIEW, _T("Print Pre&view"), _T("Preview"));
file_menu->Append(WXPRINT_OUT, _T("&out..."), _T("out"));
wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, _T("&File"));
frame->SetMenuBar(menu_bar);
MyCanvas *canvas = new MyCanvas(frame, wxPoint(0, 0), wxSize(100, 100), wxRETAINED|wxHSCROLL|wxVSCROLL);
canvas->SetScrollbars(10, 10,50, 50);
frame->canvas = canvas;
// dcs = new wxWindowDC(sheet);
frame->Centre(wxBOTH);
frame->Show();
SetTopWindow(frame);
return true;
}
int MyApp::OnExit()
{
delete g_printData;
delete g_pageSetupData;
return 1;
}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(WXPRINT_PRINT, MyFrame::OnPrint)
EVT_MENU(WXPRINT_PREVIEW, MyFrame::OnPrintPreview)
EVT_MENU(WXPRINT_PAGE_SETUP, MyFrame::OnPageSetup)
EVT_MENU(WXPRINT_OUT, MyFrame::OnPrintout)
END_EVENT_TABLE()
MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
wxFrame(frame, wxID_ANY, title, pos, size),
m_Sheet(NULL),
canvas(NULL)
{
canvas = NULL;
}
void MyFrame::Init()
{
wxFrame::Init();
}
void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
{
wxPrintDialogData printDialogData(* g_printData);
wxPrinter printer(& printDialogData);
MyPrintout printout(_T("My printout"));
if (!printer.Print(this, &printout, true /*prompt*/))
{
if (wxPrinter::GetLastError() == wxPRINTER_ERROR)
wxMessageBox(_T("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _T("Printing"), wxOK);
else
wxMessageBox(_T("You canceled printing"), _T("Printing"), wxOK);
}
else
{
(*g_printData) = printer.GetPrintDialogData().GetPrintData();
}
}
void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
{
wxPrintDialogData printDialogData(* g_printData);
wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printDialogData);
if (!preview->Ok())
{
delete preview;
wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK);
return;
}
wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(0, 0), wxSize(800, 600));
frame->Centre(wxBOTH);
frame->Initialize();
frame->Show();
}
void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
{
(*g_pageSetupData) = *g_printData;
wxPageSetupDialog pageSetupDialog(this, g_pageSetupData);
pageSetupDialog.ShowModal();
(*g_printData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData();
(*g_pageSetupData) = pageSetupDialog.GetPageSetupDialogData();
}
void MyFrame::OnPrintout(wxCommandEvent& WXUNUSED(event))
{
static int it = 0;
if (it == 0)
{
if (m_Sheet == NULL)
m_Sheet = new test_sheet(
frame,
wxID_ANY,
wxString(_T("sheet dialog")),
wxDefaultPosition,
wxSize(600,400),
0 ,
_("nihao")
);
if (m_Sheet != NULL)
{
m_Sheet->SetSize(800,600);
m_Sheet->Show();
}
it = 1;
return;
}
it = 0;
wxPrintDialogData printDialogData(* g_printData);
wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printDialogData);
if (!preview->Ok())
{
delete preview;
wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK);
return;
}
wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(0, 0), wxSize(800, 600));
frame->Centre(wxBOTH);
frame->Initialize();
frame->Show();
}
void MyFrame::Draw(wxDC& dc)
{
if (m_Sheet != NULL)
{
wxPaintDC dcs(this->m_Sheet);
dc.Blit(0,0,800,600,&dcs,0,0,wxCOPY,false,-1,-1);
}
//dc.Blit(0,0,800,600,dcs,0,0,wxCOPY,false,-1,-1);
}
BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
END_EVENT_TABLE()
MyCanvas::MyCanvas(wxFrame *frame, const wxPoint& pos, const wxSize& size, long style):
wxScrolledWindow(frame, wxID_ANY, pos, size, style)
{
SetBackgroundColour(* wxWHITE);
}
void MyCanvas::OnDraw(wxDC& dc)
{
//dc.Blit(100,100,400,420,dcs,100,100,wxCOPY,false,-1,-1);
//dc.DrawText(_("包头市华盛不锈钢型材有限公司"),91,21);
frame->Draw(dc);
}
void MyCanvas::OnEvent(wxMouseEvent& WXUNUSED(event))
{
}
bool MyPrintout::OnPrintPage(int page)
{
wxDC *dc = GetDC();
if (dc)
{
if (page == 1)
DrawPageOne();
else if (page == 2)
DrawPageTwo();
MapScreenSizeToPage();
wxChar buf[200];
wxSprintf(buf, wxT("PAGE %d"), page);
//dc->DrawText(buf, 0, 0);
return true;
}
else
return false;
}
bool MyPrintout::OnBeginDocument(int startPage, int endPage)
{
if (!wxPrintout::OnBeginDocument(startPage, endPage))
return false;
return true;
}
void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
{
*minPage = 1;
*maxPage = 2;
*selPageFrom = 1;
*selPageTo = 2;
}
bool MyPrintout::HasPage(int pageNum)
{
return (pageNum == 1 || pageNum == 2);
}
void MyPrintout::DrawPageOne()
{
wxCoord maxX = 800;
wxCoord maxY = 600;
FitThisSizeToPageMargins(wxSize(maxX, maxY), *g_pageSetupData);
wxRect fitRect = GetLogicalPageMarginsRect(*g_pageSetupData);
wxCoord xoff = (fitRect.width - maxX) / 2;
wxCoord yoff = (fitRect.height - maxY) / 2;
OffsetLogicalOrigin(xoff, yoff);
/*wxPrinterDC * dct = new wxPrinterDC(frame->m_Sheet);
this->SetDC(dct);*/
frame->Draw(*GetDC());
}
void MyPrintout::DrawPageTwo()
{
}
这样打印的结果是黑屏,
到底如何才能捕捉到m_Sheet并且将其输出啊,有高手指点下么,谢谢