?? frmprint.cs
字號:
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace GoldPrinter
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class frmPrint : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuFile;
private System.Windows.Forms.MenuItem menuFilePageSetup;
private System.Windows.Forms.MenuItem menuFilePrintPreview;
private System.Windows.Forms.MenuItem menuFilePrint;
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmPrint()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuFile = new System.Windows.Forms.MenuItem();
this.menuFilePageSetup = new System.Windows.Forms.MenuItem();
this.menuFilePrintPreview = new System.Windows.Forms.MenuItem();
this.menuFilePrint = new System.Windows.Forms.MenuItem();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFile});
//
// menuFile
//
this.menuFile.Index = 0;
this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFilePageSetup,
this.menuFilePrintPreview,
this.menuFilePrint});
this.menuFile.Text = "&File";
//
// menuFilePageSetup
//
this.menuFilePageSetup.Index = 0;
this.menuFilePageSetup.Text = "Page Set&up";
this.menuFilePageSetup.Click += new System.EventHandler(this.menuFilePageSetup_Click);
//
// menuFilePrintPreview
//
this.menuFilePrintPreview.Index = 1;
this.menuFilePrintPreview.Text = "Print Pre&view";
this.menuFilePrintPreview.Click += new System.EventHandler(this.menuFilePrintPreview_Click);
//
// menuFilePrint
//
this.menuFilePrint.Index = 2;
this.menuFilePrint.Text = "&Print";
this.menuFilePrint.Click += new System.EventHandler(this.menuFilePrint_Click);
//
// dataGrid1
//
this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(2, 14);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(518, 357);
this.dataGrid1.TabIndex = 0;
//
// frmPrint
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.Window;
this.ClientSize = new System.Drawing.Size(520, 374);
this.Controls.Add(this.dataGrid1);
this.Menu = this.mainMenu1;
this.Name = "frmPrint";
this.Text = "SimplePrintingExample";
this.Load += new System.EventHandler(this.frmPrint_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmPrint());
}
private void frmPrint_Load(object sender, System.EventArgs e)
{
this.dataGrid1.DataSource = this.GetDataSource();
}
private void menuFilePageSetup_Click(object sender, System.EventArgs e)
{
}
private void menuFilePrint_Click(object sender, System.EventArgs e)
{
this.Print(true);
}
private void menuFilePrintPreview_Click(object sender, System.EventArgs e)
{
this.Print(false);
}
private void Print(bool print)
{
MisPrinter webmis = new MisPrinter(); //打印組件
//webmis.BackColor = Color.FloralWhite; //背景色
//webmis.IsPrinterMargins = true; //試試看,去掉注釋后就明白了
//webmis.RowsPerPage = 20; //可以指定每頁打印的行數(shù),默認自適應
//webmis.GridBorder = GoldPrinter.GridBorderFlag.SingleBold;
Title title = new Title();
title.Text = "WebMIS.Net快速開發(fā)工具\nWWW.AlinkSoft.COM";
webmis.Title = title; //等效于webmis.Title = "WebMIS.Net快速開發(fā)工具\nWWW.AlinkSoft.COM";
webmis.Caption = " ——之打印源碼,歡迎下載"; //但對象方式(見webmis.Title)更具特性,因為還可以定義字體等
//可以是以'|'分隔的字符串或一維數(shù)組或具有更多特性的Top/Bottom對象
webmis.Top = "作者:周方勇|2004-08-16|版本:V1.0"; //等價于 webmis.Top = new string[]{"作者:周方勇","2004-08-16","版本:V1.0"};
//webmis.Top = new Top(); 此種表示見webmis.Bottom
Header header = new Header(3,4); //行列數(shù)基本不受限制,但超過一頁失去意義,因為以Body為主,以其它為輔
header.IsDrawAllPage = true; //可以指定每頁是否重復打印
header.SetText(0,0,"Header00"); //實際上可以設置header.DataSource
header.SetText(0,1,"Header01"); //DataSource可以是字符串、一維數(shù)組、二維數(shù)組、DataTable、WinDataGrid、WebDataGrid、
header.SetText(0,2,"Header02"); //MSHFlexGrid、HtmlTable等等二維形式
header.SetText(0,3,"Header03"); //
header.SetText(1,0,"Header10"); //同仁們還可以根據(jù)實際應用對GridBase的DataSource進行擴展
header.SetText(1,1,"Header11");
header.SetText(1,2,"Header12");
header.SetText(1,3,"Header13");
header.SetText(2,0,"Header20");
header.SetText(2,1,"Header21");
header.SetText(2,2,"Header22");
header.SetText(2,3,"Header23");
webmis.Header = header;
MultiHeader multiHeader = new MultiHeader(2,6);
multiHeader.SetMergeTextOnColSel(0,0,1," 項目\nNo. .");
multiHeader.IsDrawDiagonalLine = true;
multiHeader.DiagonalLineRows = 2; //試試其它的數(shù)如3 又如1.5F
multiHeader.SetMergeTextOnColSel(1,0,1,"產(chǎn)品\n名稱"); //等價于 //multiHeader.SetText(1,1,"產(chǎn)品\n名稱");
//multiHeader.SetText(0,1,"產(chǎn)品\n名稱");
multiHeader.SetText(0,2,"規(guī)格");
//multiHeader.SetText(0,3,"訂量信息");
multiHeader.SetMergeTextOnRowSel(0,3,5,"訂量信息"); //等價于 //multiHeader.SetText(0,4,"訂量信息");
//multiHeader.SetText(0,5,"訂量信息");
multiHeader.SetText(1,2,"規(guī)格");
multiHeader.SetText(1,3,"單價");
multiHeader.SetText(1,4,"數(shù)量");
multiHeader.SetText(1,5,"金額");
webmis.MultiHeader = multiHeader;
Body body = new Body();
body.ColsAlignString = "LLCRRR"; //由左中右第一個字母組成,對應于每列的對對齊方式
//只要是二維數(shù)組,都可以,因此DataGrid、MSHFlexGrid、DBGrid、DataTable、HtmlTable等等都OK
//這里用DataGrid作一個例子。不過,你自己可以寫一個函數(shù),把沒有支持的二維控件轉(zhuǎn)換成數(shù)組就OK了
//body.DataSource = this.GetDataSource();
//body.DataSource = (DataTable)(this.dataGrid1.DataSource); //或者
//body.DataSource = this.dataGrid1.DataSource; //或者
body.DataSource = this.dataGrid1; //或者
int mcols = 6;
#region 這是數(shù)組的例子,把body.DataSource = (DataTable)(this.dataGrid1.DataSource);注釋掉,去掉下面的注釋看看
/*
int mrows = 50;//111;
string[,] arrstrGrid = new string[mrows,mcols];
for(int i=0 ;i< mrows ; i++)
{
for(int j=0 ;j< mcols-1 ; j++)
{
arrstrGrid[i,j] = (i + 1).ToString() + "行" + (j + 1).ToString() + "列";
}
arrstrGrid[i,0] = (i + 1).ToString();
arrstrGrid[i,mcols-1] = (i + 1).ToString();
}
body.DataSource = arrstrGrid; //body.GridText = arrstrGrid;
*/
#endregion
webmis.Body = body;
webmis.IsSubTotalPerPage = true; //這是打印本頁小計
webmis.SubTotalCol = (mcols-2).ToString() + ";" + (mcols-1).ToString(); //用分號分隔的要求小計的列
Footer footer = new Footer(2,5);
footer.SetText(0,0,"Footer00");
footer.SetText(0,1,"Footer01");
footer.SetText(0,2,"Footer02");
footer.SetText(0,3,"Footer03");
footer.SetText(0,4,"Footer04");
footer.SetText(1,0,"Footer10");
footer.SetText(1,1,"Footer11");
footer.SetText(1,2,"Footer12");
footer.SetText(1,3,"Footer13");
footer.SetText(1,4,"Footer14");
footer.IsDrawAllPage = true;
webmis.Footer = footer;
GoldPrinter.Bottom bottom = new Bottom(); //Top/Bottom的另一種方式
bottom.Text = "打印程序開源版||打印日期:" + System.DateTime.Now.ToLongDateString();
webmis.Bottom = bottom;
//將注釋去掉試試看
Sewing sewing = new Sewing(20,SewingDirectionFlag.Left);
webmis.Sewing = sewing;
webmis.IsSewingLine = true; //打印裝訂線
webmis.IsSubTotalPerPage = true; //每頁加小計
if (print)
{
webmis.Print();
}
else
{
webmis.Preview();
}
webmis.Dispose();
webmis = null;
}
private DataTable GetDataSource()
{
DataTable dt=new DataTable();
int rows = 100;
int cols = 6;
//增加行
for (int intRowIndex=0;intRowIndex<rows;intRowIndex++)
{
dt.Rows.Add(dt.NewRow());
}
//增加新列
for (int intColIndex=0;intColIndex<cols;intColIndex++)
{
//dt.Columns.Add(intColIndex.ToString());
dt.Columns.Add();
//填寫默認值為空串(.NET TextColumn默認為"空")
dt.Columns[intColIndex].DefaultValue="";
}
int i,j;
for(i=0 ;i< rows ; i++)
{
for(j=0 ;j< cols-3 ; j++)
{
dt.Rows[i][j] = (i + 1).ToString() + "行" + (j + 1).ToString() + "列";
dt.Rows[i][cols-3] = (j + 1).ToString() + "." + (i + 1).ToString();
dt.Rows[i][cols-2] = (i + 1).ToString();
}
dt.Rows[i][cols-1] = (double.Parse(dt.Rows[i][cols-2].ToString()) * double.Parse(dt.Rows[i][cols-3].ToString())).ToString();
}
return dt;
}
}//End Class
}//End Namespace
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -