代码 如下
private void button4_Click(object sender, System.EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); this.dlg_printpreview.Document = pd; if(dlg_printpreview.ShowDialog() == DialogResult.OK)
pd.Print(); } private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Font st = new Font("Arial",14,FontStyle.Bold);
ev.Graphics.DrawString("hello",st,Brushes.Black,100,100);
}在预览时无法显示pd_PrintPage中的内容,请问如何解决???
private void button4_Click(object sender, System.EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); this.dlg_printpreview.Document = pd; if(dlg_printpreview.ShowDialog() == DialogResult.OK)
pd.Print(); } private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Font st = new Font("Arial",14,FontStyle.Bold);
ev.Graphics.DrawString("hello",st,Brushes.Black,100,100);
}在预览时无法显示pd_PrintPage中的内容,请问如何解决???
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Printing;namespace WindowsApplication3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(136, 104);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Location = new System.Drawing.Point(192, 15);
this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
this.printPreviewDialog1.Visible = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
printDocument1.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); this.printPreviewDialog1.Document = printDocument1; if(this.printPreviewDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Font st = new Font("Arial",14,FontStyle.Bold);
ev.Graphics.DrawString("hello",st,Brushes.Black,100,100);
} }
}
加我msn: [email protected] 获得演示版本打印小组件,与国外的scriptx类似
http://www.fcsoft.com.cn/eprint/index.htm 有在线演示
pd.Print();改成
dlg_printpreview.ShowDialog() ;试试