?? misprinter.cs
字號:
PrinterPageSetting printerPageSetting;
printerPageSetting = new PrinterPageSetting(mPrintDocument);
printerPageSetting.PrintPage += new PrintPageDelegate(this.PrintPageEventHandler);
printerPageSetting.ShowPrintPreviewDialog();
}
//繪制
private void PrintPageEventHandler(object obj,System.Drawing.Printing.PrintPageEventArgs ev)
{
Graphics g = ev.Graphics ;
this.mGraphics = g;
g.Clear(this.BackColor);
try
{
bool blnMore = this.Draw(g);
if (blnMore)
{
ev.HasMorePages = true;
mCurrentPageIndex++;
}
else
{
ev.HasMorePages = false;
this.mCurrentPageIndex = 1;
}
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
}
/// 對象打印接口
private void OutObject(IDraw outer)
{
// this.mGraphics.ResetTransform();
if (outer != null)
{
outer.Graphics = this.mGraphics;
outer.PrintDocument = this.mPrintDocument;
outer.Sewing = this.Sewing;
outer.RectangleF = new RectangleF(X,Y,Width,outer.Height);
outer.Draw();
this.Y += outer.RectangleF.Height;
}
}
#region 繪制過程
//*****這段代碼確實太長了,應該用重構的手法進行處理,不過,我這里主要是讓大家從頭到尾的順利的看下去
//*****在下一個版本的源代碼中我會整理,大家也可以試一試。
private bool Draw(Graphics g)
{
bool blnHasMorePage = false; //是否還有下一頁標記
if (this._body.Rows < 0)
{
throw new Exception("打印主要網格不能為空,請用Body設置!");
}
Printer printer = new Printer();
printer.Graphics = g;
printer.PrintDocument = this.mPrintDocument;
printer.Sewing = this.Sewing;
//初起打印起點坐標及打印區域的寬
Y = printer.PrinterMargins.Top;
X = printer.PrinterMargins.Left;
Width = printer.PrinterMargins.Width;
#region 畫打印區域及裝訂線
if (IsPrinterMargins)
{
printer.DrawPrinterMargins();
}
if (IsSewingLine && _sewing.Margin > 0)
{
//對象的線長小于0則自動設置
if (this._sewing.LineLen < 0)
{
if (this._sewing.SewingDirection == SewingDirectionFlag.Left)
{
this._sewing.LineLen = printer.PageHeight;
}
else if (this._sewing.SewingDirection == SewingDirectionFlag.Top)
{
this._sewing.LineLen = printer.PageWidth;
}
}
printer.Sewing = this._sewing;
printer.DrawSewing();
}
#endregion
//正標題每頁必重復打印,無需判斷
if (_title != null)
{
OutObject(_title);
}
if (mCurrentPageIndex == 1 || _caption.IsDrawAllPage)
{
if (_caption != null)
{
if (_title != null)
{
_caption.MoveY = this._title.Height + 5;
}
OutObject(_caption);
}
}
if (_title != null || _caption != null)
{
Y += 20; //標題與下面有一定距離
}
if (mCurrentPageIndex == 1 || _top.IsDrawAllPage)
{
OutObject(_top);
}
if (mCurrentPageIndex == 1 || _header.IsDrawAllPage)
{
OutObject(_header);
}
if (_title != null || _caption != null || _top.IsDrawAllPage || _header.IsDrawAllPage)
{
Y += 5; //網格與頁頭距離
}
if (mCurrentPageIndex == 1 || _multiHeader.IsDrawAllPage)
{
OutObject(_multiHeader);
}
#region 主體數據網格
//計算有效高度,便于分頁
float validHeight = printer.PrinterMargins.Height - (Y - printer.PrinterMargins.Top);
if(_footer != null && _footer.IsDrawAllPage)
{
validHeight -= this._footer.Height;
}
if(_bottom != null && _bottom.IsDrawAllPage)
{
validHeight -= this._bottom.Height;
}
if (validHeight < 0)
{
throw new Exception("預留給打印主要網格的空間太小,請適當調整!");
}
//有效高度中當前頁行數
int mRowsInCurPage = 0;
mRowsInCurPage = (int)(validHeight/(float)(this._body.RowHeight));
//如果指定每頁行數,則以其為主
if (this.RowsPerPage > 0 && this.RowsPerPage < mRowsInCurPage)
{
mRowsInCurPage = this.RowsPerPage;
}
if (this.IsSubTotalPerPage)
{
mRowsInCurPage--;
}
//************以Body為主************
string[,] mArrGridText; //保留當前頁文本,用于頁小計
GoldPrinter.Body mbody;
//如果指定每頁行數,則以其為主
if (this.RowsPerPage > 0 && this.RowsPerPage < mRowsInCurPage)
{
mbody = new Body(mRowsInCurPage,this._body.Cols);
}
else
{
//否則自適應
if (mRowsInCurPage > (this._body.Rows - this.mCurrentRowIndex))
{
mRowsInCurPage = this._body.Rows - this.mCurrentRowIndex;
}
mbody = new Body(mRowsInCurPage,this._body.Cols);
}
mbody.ColsAlignString = this._body.ColsAlignString;
//存當前頁的二維文本
mArrGridText = new string[mRowsInCurPage,this._body.Cols];
for(int i = 0 ; i < mRowsInCurPage && mCurrentRowIndex < this._body.Rows ; i++)
{
for(int j = 0 ; j < this._body.Cols ; j++)
{
mArrGridText[i,j] = this._body.GetText(mCurrentRowIndex,j);
}
mCurrentRowIndex++;
}
mbody.GridText = mArrGridText;
OutObject(mbody);
//判斷是否要分頁,只要數據網格行數據大于數據網格行指針,則還有下一頁
if (mCurrentRowIndex < this._body.Rows)
{
blnHasMorePage = true;
}
#region 打印每頁小計,只需要將當前數組用循環累計就OK了,這段程序應專門重構為一個函數,讀者可以自己試一試
if (_isSubTotalPerPage && _subTotalCol != "")
{
try
{
GoldPrinter.MultiHeader mhSubTotal = new MultiHeader(1,this._body.Cols);
mhSubTotal.Graphics = g;
mhSubTotal.PrintDocument = this.mPrintDocument;
mhSubTotal.Sewing = this._sewing;
mhSubTotal.RectangleF = new RectangleF(X,Y,Width,mhSubTotal.Height);
//循環
//....
mhSubTotal.SetText(0,0,"本頁小計");
mhSubTotal.SetText(0,1,"本頁小計");
string[] marrSubTotalCol = this._subTotalCol.Split(';');
Double mdblSubTotal = 0f;
int mintCol = 0;
for(int i = 0 ; i < marrSubTotalCol.Length ; i ++)
{
mintCol = int.Parse(marrSubTotalCol[i].Substring(0,1));
for(int j = 0 ; j < mArrGridText.GetLength(0) ; j++)
{
mdblSubTotal += Double.Parse(mArrGridText[j,mintCol]);
}
mhSubTotal.SetText(0,mintCol,mdblSubTotal.ToString());
}
mhSubTotal.Draw();
Y += mhSubTotal.Height;
}
catch(Exception e)
{}
}
#endregion
#endregion
Y += 5; //網格與頁底距離
//打印頁腳與最底
if (blnHasMorePage == false || _footer.IsDrawAllPage)
{
//這里不再做判斷了,讀者自己對照Body的處理方法去試驗,以加深理解,實際上應是注釋的部分
OutObject(_footer);
/*
if (_footer.IsDrawAllPage)
{
OutObject(_footer);
}
else
{
//與Body同樣的處理
}
*/
}
if (blnHasMorePage == false || _bottom.IsDrawAllPage)
{
if (_bottom.IsDrawAllPage)
{
OutObject(_bottom);
}
else
{
//計算有效高度
validHeight = printer.PrinterMargins.Height - (Y - printer.PrinterMargins.Top);
if (validHeight < _bottom.Height)
{
blnHasMorePage = true;
}
else
{
OutObject(_bottom);
}
}
}
//畫邊框
DrawBorder(g,this._multiHeader,mbody);
mbody = null;
return blnHasMorePage;
}
#endregion
private void DrawBorder(Graphics g,MultiHeader multiHeader,Body body)
{
//網格邊框矩陣
RectangleF mrecGridBorder;
float x,y,width,height;
width = body.RectangleF.Width;
height = body.RectangleF.Height;
if (multiHeader != null)
{
x = multiHeader.RectangleF.X;
y = multiHeader.RectangleF.Y;
height += multiHeader.RectangleF.Height;
}
else
{
x = body.RectangleF.X;
y = body.RectangleF.Y;
}
if (this.IsSubTotalPerPage)
{
GoldPrinter.MultiHeader m = new MultiHeader(1,1);
height += m.RowHeight;
m = null;
}
mrecGridBorder = new RectangleF(x,y,width,height);
Pen pen = new Pen(Color.Black,1);
GoldPrinter.DrawRectangle dr = new DrawRectangle();
dr.Graphics = g;
dr.RectangleF = mrecGridBorder;
dr.Pen = pen;
switch (GridBorder)
{
case GridBorderFlag.Single:
dr.Draw();
break;
case GridBorderFlag.SingleBold:
dr.Pen.Width = 2;
dr.Draw();
if (multiHeader != null)
{
dr.RectangleF = body.RectangleF;
dr.DrawTopLine();
}
break;
case GridBorderFlag.Double:
dr.Draw();
mrecGridBorder = new RectangleF(x-2,y-2,width+4,height+4);
dr.RectangleF = mrecGridBorder;
dr.Draw();
break;
case GridBorderFlag.DoubleBold:
dr.Draw();
mrecGridBorder = new RectangleF(x-2,y-2,width+4,height+4);
dr.RectangleF = mrecGridBorder;
dr.Pen.Width = 2;
dr.Draw();
break;
}
}
#region 將打印的相關信息輸出WriteMetricsToConsole(PrintPageEventArgs ev)
/*
public void WriteMetricsToConsole(PrintPageEventArgs ev)
{
Graphics g = ev.Graphics;
Console.WriteLine ("*****Information about the printer*****");
Console.WriteLine("紙張的大小 ev.PageSettings.PaperSize:" + ev.PageSettings.PaperSize);
Console.WriteLine("打印分辨率 ev.PageSettings.PrinterResolution:" + ev.PageSettings.PrinterResolution);
Console.WriteLine("旋轉的角度 ev.PageSettings.PrinterSettings.LandscapeAngle" + ev.PageSettings.PrinterSettings.LandscapeAngle);
Console.WriteLine("");
Console.WriteLine ("*****Information about the page*****");
Console.WriteLine("頁面的大小 ev.PageSettings.Bounds:" + ev.PageSettings.Bounds);
Console.WriteLine("頁面(同上) ev.PageBounds:" + ev.PageBounds);
Console.WriteLine("頁面的邊距 ev.PageSettings.Margins.:" + ev.PageSettings.Margins);
Console.WriteLine("頁面的邊距 ev.MarginBounds:" + ev.MarginBounds);
Console.WriteLine("水平分辨率 ev.Graphics.DpiX:" + ev.Graphics.DpiX );
Console.WriteLine("垂直分辨率 ev.Graphics.DpiY:" + ev.Graphics.DpiY );
ev.Graphics.SetClip(ev.PageBounds);
Console.WriteLine("ev.Graphics.VisibleClipBounds:" + ev.Graphics.VisibleClipBounds);
SizeF drawingSurfaceSize = new SizeF(
ev.Graphics.VisibleClipBounds.Width * ev.Graphics.DpiX/100,
ev.Graphics.VisibleClipBounds.Height * ev.Graphics.DpiY/100);
Console.WriteLine("drawing Surface Size in Pixels" + drawingSurfaceSize);
}
*/
#endregion
}//End class
}//End Namespace
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -