xPageLayoutControl1打印预览代码

short m_CurrentPrintPage = 0;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//this code will be called when the PrintPreviewDialog.Show method is called
//set the PageToPrinterMapping property of the Page. This specifies how the page
//is mapped onto the printer page. By default the page will be tiled
//get the selected mapping option
string sPageToPrinterMapping = (string)this.cboPageToPrinterMapping.SelectedItem;
if (sPageToPrinterMapping == null)
//if no selection has been made the default is tiling
this.axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile;
else if (sPageToPrinterMapping.Equals("2: Tile"))
this.axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile;
else if (sPageToPrinterMapping.Equals("0: Crop"))
this.axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingCrop;
else if (sPageToPrinterMapping.Equals("1: Scale"))
this.axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingScale;
else
this.axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile;

//get the resolution of the graphics device used by the print preview (including the graphics device)
short dpi = (short)e.Graphics.DpiX;
MessageBox.Show(dpi.ToString());
//envelope for the device boundaries
IEnvelope devBounds = new EnvelopeClass();
//get page
IPage page = this.axPageLayoutControl1.Page;

//the number of printer pages the page will be printed on
short printPageCount;
printPageCount = this.axPageLayoutControl1.get_PrinterPageCount(0);
m_CurrentPrintPage++;

//the currently selected printer
IPrinter printer = this.axPageLayoutControl1.Printer;
//get the device bounds of the currently selected printer
page.GetDeviceBounds(printer, m_CurrentPrintPage, 0, dpi, devBounds);

//structure for the device boundaries
tagRECT deviceRect;
//Returns the coordinates of lower, left and upper, right corners
double xmin, ymin, xmax, ymax;
devBounds.QueryCoords(out xmin, out ymin, out xmax, out ymax);
//initialize the structure for the device boundaries
deviceRect.bottom = (int)ymax;
deviceRect.left = (int)xmin;
deviceRect.top = (int)ymin;
deviceRect.right = (int)xmax;

//determine the visible bounds of the currently printed page
IEnvelope visBounds = new EnvelopeClass();
page.GetPageBounds(printer, m_CurrentPrintPage, 0, visBounds);

//get a handle to the graphics device that the print preview will be drawn to
IntPtr hdc = e.Graphics.GetHdc();

//print the page to the graphics device using the specified boundaries
ITrackCancel m_TrackCancel = new CancelTrackerClass();
this.axPageLayoutControl1.ActiveView.Output(hdc.ToInt32(), dpi, ref deviceRect, visBounds, m_TrackCancel);

//release the graphics device handle
e.Graphics.ReleaseHdc(hdc);

//check if further pages have to be printed
if (m_CurrentPrintPage < printPageCount)
e.HasMorePages = true; //document_PrintPage event will be called again
else
e.HasMorePages = false;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值