热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

请教高手wxWidgets打印表格问题

打印一个表格的时候不知道该如何描述dc,代码如下:test_sheet.h文件:#ifndef_test_sheet_h#define_test_sheet_h#inc
打印一个表格的时候不知道该如何描述dc,代码如下: 
test_sheet.h文件: 
#ifndef _test_sheet_h 
#define _test_sheet_h 

#include "wx_pch.h" 
#include  

class wxSheet; 

class test_sheet : public wxDialog 

public: 
wxSheet *grid; 
wxTextCtrl *logWin; 
wxLogTextCtrl *logger; 

public: 
test_sheet(wxWindow * parent, 
wxWindowID id , 
const wxString& title, 
const wxPoint & pos , 
const wxSize & size , 
long styl, 
const wxChar * name ); 

virtual ~test_sheet(); 

wxLog *m_logOld; 

bool m_addToSel; 

wxSheet *getSheet(){return grid;}; 
DECLARE_EVENT_TABLE() 
}; 
#endif 
#ifndef WX_PCH_H_INCLUDED 
#define WX_PCH_H_INCLUDED 

#if ( defined(USE_PCH) && !defined(WX_PRECOMP) ) 
#define WX_PRECOMP 
#endif // USE_PCH 

// basic wxWidgets headers 
#include  
// for use xrc files 
#include  

#ifdef __BORLANDC__ 
#pragma hdrstop 
#endif 

#ifndef WX_PRECOMP 
#include  
#endif 

#ifdef USE_PCH 
// put here all your rarely-changing header files 

#endif // USE_PCH 

#endif // WX_PCH_H_INCLUDED 
test_sheet.cpp如下: 
#include "test_sheet.h" 
#include "wx/colordlg.h" 
#include "wx/fontdlg.h" 
#include "wx/artprov.h" 
#include "wx/sheet/sheetspt.h" 

BEGIN_EVENT_TABLE(test_sheet, wxDialog) 

END_EVENT_TABLE() 

test_sheet::test_sheet( 
wxWindow *parent , 
wxWindowID id , 
const wxString& title, 
const wxPoint & pos , 
const wxSize & size , 
long styl, 
const wxChar * name ) 
:wxDialog( 
parent, 
wxID_ANY, 
wxString(_T("sheet dialog")), 
wxDefaultPosition, 
wxDefaultSize//wxSize(800,600) 




//m_addToSel = false; 

//wxSplitterWindow *splitter = new wxSplitterWindow(this, -1); 
//splitter->SetMinimumPaneSize(0); 
//splitter->SetSashGravity(0.9); 

//// to use the splitter simply create one, then your sheet as a child 
//// and initialize the splitter with the sheet. 
//wxSheetSplitter* sheetSplitter = new wxSheetSplitter(splitter, -1, 
// wxPoint(0, 0), wxDefaultSize); 

grid = new wxSheet( this, 
-1, 
wxPoint( 0, 0 ), 
wxDefaultSize ); 

grid->CreateGrid( 0, 0 ); 
grid->AppendRows(9); 
grid->AppendCols(4); 
grid->SetRowHeight( 0, 30); 
grid->SetCellSpan( wxSheetCoords(0, 0), wxSheetCoords(1,4 )); 
grid->SetCellValue(wxSheetCoords(0,0),_("钢带编号 宽度 条数 规 格")); 
grid->SetRowHeight( 1, 48.75); 
grid->SetRowHeight( 2, 48.75); 
grid->SetRowHeight( 3, 48.75); 
grid->SetRowHeight( 4, 48.75); 
grid->SetRowHeight( 5, 48.75); 
grid->SetRowHeight( 6, 48.75); 
grid->SetRowHeight( 7, 48.75); 
grid->SetRowHeight( 8, 48.75); 
grid->SetColWidth( 0, 78 ); 
grid->SetColWidth( 1, 58 ); 
grid->SetColWidth( 2, 24 ); 
grid->SetColWidth( 3, 80 ); 
grid->SetCellSpan( wxSheetCoords(1, 0), wxSheetCoords(8, 1)); 
grid->SetCellValue(wxSheetCoords(1,0),_(" 钢带编号\n 890000\n 带 宽\n 0.62*514\n 边 丝\n 12.00\n 带重\n 0\n 总分数\n 4")); 
grid->SetCellValue(wxSheetCoords(1,1),_("158.2")); 
grid->SetCellValue(wxSheetCoords(1,2),_("2")); 
grid->SetCellValue(wxSheetCoords(1,3),_("51×0。62")); 
grid->SetCellValue(wxSheetCoords(2,1),_("67.7")); 
grid->SetCellValue(wxSheetCoords(3,1),_("117.9")); 
grid->SetCellValue(wxSheetCoords(2,2),_("1")); 
grid->SetCellValue(wxSheetCoords(2,3),_("22×0.62")); 
grid->SetCellValue(wxSheetCoords(3,2),_("1")); 
grid->SetCellValue(wxSheetCoords(3,3),_("38×0.62")); 



test_sheet::~test_sheet() 

//delete wxLog::SetActiveTarget(m_logOld); 


4 个解决方案

#1


printing.h如下: 



#ifndef _printing_h 
#define _printing_h 

#pragma warning(disable: 4267) 
#include "test_sheet.h" 
class MyApp: public wxApp 

public: 
MyApp(){}; 
bool OnInit(); 
int OnExit(); 

wxFont m_testFont; 
}; 

DECLARE_APP(MyApp) 

class MyCanvas; 
class MyFrame: public wxFrame 

public: 
MyCanvas *canvas; 
wxBitmap m_bitmap; 
MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size); 

void Draw(wxDC& dc); 
void OnPrint(wxCommandEvent& event); 
void OnPrintPreview(wxCommandEvent& event); 
void OnPageSetup(wxCommandEvent& event); 
void OnPrintout(wxCommandEvent& event); 
protected: 

void Init(); 

DECLARE_EVENT_TABLE() 


public: 
test_sheet *m_Sheet; 
}; 

// Define a new canvas which can receive some events 
class MyCanvas: public wxScrolledWindow 

public: 
MyCanvas(wxFrame *frame, const wxPoint& pos, const wxSize& size, long style = wxRETAINED); 
~MyCanvas(void){}; 

virtual void OnDraw(wxDC& dc); 
void OnEvent(wxMouseEvent& event); 

DECLARE_EVENT_TABLE() 
}; 

class MyPrintout: public wxPrintout 

public: 
MyPrintout(const wxChar *title = _T("My printout")):wxPrintout(title) {} 
bool OnPrintPage(int page); 
bool HasPage(int page); 
bool OnBeginDocument(int startPage, int endPage); 
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo); 
void DrawPageOne(); 
void DrawPageTwo(); 

}; 

#define WXPRINT_QUIT 100 
#define WXPRINT_PRINT 101 
#define WXPRINT_PAGE_SETUP 103 
#define WXPRINT_PREVIEW 104 
#define WXPRINT_OUT 105 


#endif 

#2


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并且将其输出啊,有高手指点下么,谢谢

#3


建议看看wx自己带的打印例子。

#4


另外,关于wxWidgets的问题,还是在forums.wxwidgets.org里问比较好,到英文论坛问:那里回答很活跃,高手特别多!
wxWidgets开发者们经常也在里面回答问题。

推荐阅读
  • 题目描述输入整型数组和排序标识,对其元素按照升序或降序进行排序(一组测试用例可能会有多组数据)本题有多组输入,请使用whil ... [详细]
  • 开发笔记:sql盲注之报错注入(附自动化脚本)
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了sql盲注之报错注入(附自动化脚本)相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 下面是一个用openssl实现获取https网页内容的demo,整个流程比较简单,主要封装的API如下staticinthttps_init(http ... [详细]
  • [USACO 2006 November Gold] 玉米地Corn Fields
    题目描述  FarmerJohn新买了一块长方形的牧场,这块牧场被划分成M行N列(1<M<12;1<N<12),每一格都是一块正方形的土地。FJ打 ... [详细]
  • 在ROS系统中,参数读写一般通过xml或者yaml格式的文件,其中yaml用得比较多。这是一种可读性高,轻量级的标记语言,简单好用。对于yaml文件,ros中用的较早版本的yaml- ... [详细]
  • 如何理解MyBatis动态SQL
    本篇内容主要讲解“如何理解MyBatis动态SQL”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何理解M ... [详细]
  • [二分图]JZOJ 4612 游戏
    DescriptionInputOutputSampleInput44#****#****#*xxx#SampleOutput5DataConstraint分析非常眼熟࿰ ... [详细]
  • 题目链接:杭电多校7-VirtualJudgevjudge上题目显示的有问题,我下面附上官方题目:样例输入:32201 ... [详细]
  • 在这一期的SendMessage函数应用中,我将向大家介绍如何利用消息函数来扩展树型列表(TreeView)控件的功能相信对于树型列表控件大家十分的熟悉, ... [详细]
  • IDEA实用插件Lombok
    LombokLombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法。通常,我们所定义的对象和b ... [详细]
  • 使用RSACryptoServiceProvider进行公钥加密我已经在CodeProject上发表了一篇文章,解释了如何使用RSA提供程序进行加密和解密:RSA私钥加密虽然200 ... [详细]
  • 2022年Python面试题一.Python基础二.企业面试题结束语🥇🥇🥇✅作者简介:大家好我是编程IDὌ ... [详细]
  • 编译原理c语言词法分析器,用C语言实现一个真正的词法分析器
    词法分析,是编译器的第一个模块,也是最简单的模块。最简单,指的是相对于编译器这种大型程序而言,与一般的代码相比还是有点复杂的 ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了opencv类简单分析:Point相关的知识,希望对你有一定的参考价值。定义 ... [详细]
  • 3357: [Usaco2004]等差数列
    3357:[Usaco2004]等差数列TimeLimit:10SecMemoryLimit:128MBSubmit:321Solved:153[Submit][Status][D ... [详细]
author-avatar
多米音乐_34024963
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有