http://chs.gotdotnet.com/quickstart/winforms/doc/WinFormsPrinting.aspx
解决方案 »
- 【C#】串口通信方面。
- 请问用WPF如何在代码中定义grid的行的高的比例?
- 如何判断2个字符串中的仅仅一个字母区别
- 为什么这样实例化通不过
- C# 空间分配 简单的难题!
- 一个奇怪的问题,未在接口\类型中找到方法
- 关于C#汇出TXT,在线等
- Access大数据量操作,如何提高效率
- 怎样获取文件的属性??
- 哪位在c#中用过com的兄台能帮小弟把这几行vb代码转成c#代码?小弟感激不尽!这是那种会者不难的问题。急...............................
- 为什么不能彻底删掉数据库的记录?
- C#和JAVA程序之间传输数据,如何实现对数据的相互的数字签名和验证,证书和密钥数据是JAVA所在平台生成的。
{
g.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
float scale = resX/ScreenResolution;
foreach (Control c in Controls)
{
string strType = c.GetType().ToString().Substring(c.GetType().ToString().LastIndexOf(".") + 1);
switch (strType)
{
case "Button":
Button b = (Button)c;
ControlPaint.DrawButton(g, ((Button)c).Left, ((Button)c).Top, ((Button)c).Width, ((Button)c).Height, ButtonState.Normal);
g.DrawString(b.Text, b.Font, new SolidBrush(b.ForeColor), b.Left + b.Width/2 - g.MeasureString(b.Text, b.Font).Width/2, b.Top + b.Height/2 - g.MeasureString("a", b.Font).Height/2, new StringFormat());
break;
case "TextBox":
TextBox t = (TextBox)c;
ControlPaint.DrawButton(g, t.Left, t.Top, t.Width, t.Height, ButtonState.Pushed );
g.FillRectangle(new SolidBrush(t.BackColor), t.Left+1, t.Top + 1, t.Width+2, t.Height -2);
g.DrawString(t.Text, t.Font, new SolidBrush(t.ForeColor), t.Left + 2, t.Top + t.Height/2 - g.MeasureString("a", t.Font).Height/2, new StringFormat());
break;
case "CheckBox":
CheckBox cb = (CheckBox)c;
if (cb.Checked)
ControlPaint.DrawCheckBox(g, cb.Left, cb.Top, cb.Height/2, cb.Height/2, ButtonState.Checked);
else
ControlPaint.DrawCheckBox(g, cb.Left, cb.Top, cb.Height/2, cb.Height/2, ButtonState.Normal);
g.DrawString(cb.Text, cb.Font, new SolidBrush(cb.ForeColor), cb.Right -cb.Height - g.MeasureString(cb.Text, cb.Font).Width , cb.Top, new StringFormat());
break;
}
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PrinterResolution pr = e.PageSettings.PrinterResolution;
DrawForm(e.Graphics, pr.X, pr.Y);
}
float ScreenResolution = 96.0f;
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
ScreenResolution = e.Graphics.DpiX;
}
添加以上几个事件。
在窗体上添加如下控件:private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Drawing.Printing.PrintDocument printDocument1;
其中,窗口上要添加它的Paint事件也就是上面的Form1_Paint,printDocument1要添加它的PrintPage事件,也就是上面的printDocument1_PrintPage事件
然后的话,用菜单或按钮调用printDocument1.Print();
就可以进行打印了。也可以直接调用printPreviewDialog1.ShowDialog()事件就可以进行打印预览。
:)
如果有不明白的,发消息给我吧。
printDocument1.PrintPage+=new System.Drawing.Printing.PrintPageEventHandler(this.pd_PrintPage);
printDocument1.print;
cocosoft(pengyun)
supicbc(超能原子)
这两位的。