WinForm的2种活动页截屏打印方法
来源:互联网 发布:珍宝岛 知乎 编辑:程序博客网 时间:2024/06/05 17:37
这个程序我做了3天,主要是用来将WINFORM的当前活动页打印出来,2种方法完全不一样,一种是没边框的一种是有边框的。如图
有横向和从向打印,全是用代码写称的(随便带了2种最新的IP获得方法,NET2.0我们都不用原来旧的方法)
源代码如下:
/// <summary>
/// FormPrint
/// 窗体打印
///
/// 修改纪录
///
/// 2007.07.31 版本:1.5 JinGangBo 新增获得本地IP地址与通过域名获得IP地址
/// 2007.07.30 版本:1.4 JinGangBo 新增设置打印按钮功能。
/// 2007.07.27 版本:1.3 JinGangBo 实现打印设置,修改横向打印,新增了焦点和ESC,界面排布修改
/// 2007.07.27 版本:1.2 JinGangBo 实现横向打印和纵向打印,打印预览最大化,及自动调及100%,及直接打印。
/// 2007.07.26 版本:1.1 JinGangBo 代码修改与整理
/// 2007.07.26 版本:1.0 JinGangBo 使用2种方法进行了窗体打印
///
/// 版本:1.3
///
/// <author>
/// <name>JinGangBo</name>
/// <date>2007.07.26</date>
/// </author>
/// </summary>
public partial class FormPrint : Form
{
public FormPrint()
{
InitializeComponent();
}
private Bitmap memoryImageOne;
private Image memoryImageTwo;
//新建打印设置
PrintDocument myPrintDocumentSet = new PrintDocument();
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private void GetPrint_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyValue)
{
case 118:
// 点击了F7按钮
this.PrintPageOne();
break;
case 119:
// 点击了F8按钮
this.PrintPageTwo();
break;
case 67:
this.Close();
break;
case 27:
this.Close();
break;
}
}
#region private void PrintPageOne() 打印预览方法一
/// <summary>
/// 打印预览
/// </summary>
private void PrintPageOne()
{
//设置为忙碌状态
this.Cursor = Cursors.WaitCursor;
//创建当前屏幕的DC对象
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
//创建以当前活动页大小为标准的位图对象
memoryImageOne = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImageOne);
//得到屏幕DC
IntPtr dc1 = mygraphics.GetHdc();
//得到位图的DC
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
//释放DC
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
//新建打印预览窗体
PrintPreviewDialog myPrintPreviewDialogOne = new PrintPreviewDialog();
//新建打印对象
PrintDocument myPrintDocumentOne = new PrintDocument();
//新建打印设置
PageSetupDialog myPageSetupDialogOne = new PageSetupDialog();
//新建打印输出
myPrintDocumentOne.PrintPage += new PrintPageEventHandler(myPrintDocumentOne_PrintPage);
//获取打印预览
myPrintPreviewDialogOne.Document = myPrintDocumentOne;
//将预揽调制100%
myPrintPreviewDialogOne.PrintPreviewControl.Zoom = 1.0;
//将预览窗体最大化
((System.Windows.Forms.Form)myPrintPreviewDialogOne).WindowState = FormWindowState.Maximized;
//打开打印预览窗口
myPrintPreviewDialogOne.ShowDialog();
//设置鼠标默认状态
this.Cursor = Cursors.Default;
}
#endregion
#region private void PrintPageTwo() 打印预览方法二
/// <summary>
/// 打印预览
/// </summary>
private void PrintPageTwo()
{
//设置为忙碌状态
this.Cursor = Cursors.WaitCursor;
//获得ALT+PRINT热键
SendKeys.SendWait("%{PRTSC}");
//新建打印预览窗体
PrintPreviewDialog myPrintPreviewDialogTwo = new PrintPreviewDialog();
//新建打印对象
PrintDocument myPrintDocumentTwo = new PrintDocument();
//新建打印输出
myPrintDocumentTwo.PrintPage += new PrintPageEventHandler(myPrintDocumentTwo_PrintPage);
//新建打印设置
PageSetupDialog myPageSetupDialogTwo = new PageSetupDialog();
//获取打印预览
myPrintPreviewDialogTwo.Document = myPrintDocumentTwo;
//将预揽调制100%
myPrintPreviewDialogTwo.PrintPreviewControl.Zoom = 1.0;
//将预览窗体最大化
((System.Windows.Forms.Form)myPrintPreviewDialogTwo).WindowState = FormWindowState.Maximized;
//打开打印预览窗口
myPrintPreviewDialogTwo.ShowDialog();
//设置鼠标默认状态
this.Cursor = Cursors.Default;
}
#endregion
#region private void PrintPageImmediacy() 直接打印
/// <summary>
/// 直接打印
/// </summary>
private void PrintPageImmediacy()
{
//设置为忙碌状态
this.Cursor = Cursors.WaitCursor;
//获得ALT+PRINT热键
SendKeys.SendWait("%{PRTSC}");
//新建打印预览窗体
PrintPreviewDialog myPrintPreviewDialogTwo = new PrintPreviewDialog();
//新建打印对象
PrintDocument myPrintDocumentTwo = new PrintDocument();
//新建打印输出
myPrintDocumentTwo.PrintPage += new PrintPageEventHandler(myPrintDocumentTwo_PrintPage);
//新建打印设置
PageSetupDialog myPageSetupDialogTwo = new PageSetupDialog();
//获取打印预览
myPrintPreviewDialogTwo.Document = myPrintDocumentTwo;
//将预揽调制100%
myPrintPreviewDialogTwo.PrintPreviewControl.Zoom = 1.0;
//直接打印
myPrintDocumentTwo.Print();
//设置鼠标默认状态
this.Cursor = Cursors.Default;
}
#endregion
#region private void PrintSet() 打印设置
/// <summary>
/// 打印设置
/// </summary>
private void PrintSet()
{
//设置为忙碌状态
this.Cursor = Cursors.WaitCursor;
//获得ALT+PRINT热键
SendKeys.SendWait("%{PRTSC}");
//新建打印输出
myPrintDocumentSet.PrintPage += new PrintPageEventHandler(myPrintDocumentTwo_PrintPage);
//新建打印设置
PageSetupDialog myPageSetupDialog = new PageSetupDialog();
//在打印设置中填充图象
myPageSetupDialog.Document = myPrintDocumentSet;
myPageSetupDialog.PageSettings.Landscape = true;
myPageSetupDialog.ShowDialog(this);
//保存设置
myPrintDocumentSet.DefaultPageSettings = myPageSetupDialog.PageSettings;
myPrintDocumentSet.PrinterSettings = myPageSetupDialog.PrinterSettings;
//设置鼠标默认状态
this.Cursor = Cursors.Default;
}
#endregion
private void myPrintDocumentOne_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (radiobtnvertinal.Checked == true)
{
//绘制打印预览方法一
e.Graphics.DrawImage(memoryImageOne, 0, 0);
}
else
{
//图片旋转90
memoryImageOne.RotateFlip(RotateFlipType.Rotate90FlipXY);
e.Graphics.DrawImage(memoryImageOne, 0, 0);
}
}
private void myPrintDocumentTwo_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//从剪切板中获得图片
memoryImageTwo = Clipboard.GetImage();
if (radiobtnvertinal.Checked == true)
{
//绘制打印预览方法一
e.Graphics.DrawImage(memoryImageTwo, 0, 0);
}
else
{
//图片旋转90
memoryImageTwo.RotateFlip(RotateFlipType.Rotate90FlipXY);
e.Graphics.DrawImage(memoryImageTwo, 0, 0);
}
}
private void btnPirntSet_Click(object sender, EventArgs e)
{
this.PrintSet();
}
private void btnPrint_Click(object sender, EventArgs e)
{
myPrintDocumentSet.Print();
}
private void btnPrintNow_Click(object sender, EventArgs e)
{
this.PrintPageImmediacy();
}
private void btnPrintPageOne_Click_1(object sender, EventArgs e)
{
this.PrintPageOne();
}
private void btnPrintPageTwo_Click_1(object sender, EventArgs e)
{
this.PrintPageTwo();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void FormPrint_Load(object sender, EventArgs e)
{
btnCancel.Focus();
}
private void BtnLocalIP_Click(object sender, EventArgs e)
{
//取得本地的机器名
string strHostName = System.Net.Dns.GetHostName();
//根据字符串型的主机名称,得到IP地址
//IP获得过时写法[否决的]
//System.Net.IPHostEntry myHostinfo = System.Net.Dns.GetHostByName(strHostName);
//IP最新获得方法
System.Net.IPHostEntry myHostinfo = System.Net.Dns.GetHostEntry(strHostName);
System.Net.IPAddress myIpAddress = myHostinfo.AddressList[0];
MessageBox.Show(myIpAddress.ToString());
}
private void btnFieldIP_Click(object sender, EventArgs e)
{
//域名IP获得过时写法[否决的]
//string strIP = System.Net.Dns.GetHostByName("www.sina.com.cn").AddressList[0].ToString();
string strIP = System.Net.Dns.GetHostEntry("www.sina.com.cn").AddressList[0].ToString();
MessageBox.Show(strIP);
}
这里是全部的代码,我想这个不难吧,有什么不懂的可以问我,这个程序我已经放在网上了,如果各位想下的话可以点击这个地址http://download.csdn.net/down/232641/wudiwushen
- WinForm的2种活动页截屏打印方法
- Winform(XtraReport)实现打印方法(转载)
- Winform 打印
- .net winform 打印窗体的方法(摘自VS2005帮助文档);
- Winform 窗体间传值的10种方法
- winform 快捷键的两种实现方法
- WinForm+VS2005下ReportViewer的打印问题
- c# winform页面信息的打印
- Winform rdlc 报表的直接打印
- android销毁活动的方法
- 活动的最佳实践之启动活动的最佳方法
- web 打印,web打印控件的三种实现方法
- web打印,web打印控件的三种实现方法
- vb打印的几种方法003--分散打印
- web打印,web打印控件的三种实现方法
- 将磁盘分区标为活动或取消活动的方法
- 将磁盘分区标为活动或取消活动的方法
- Office2007 嵌入WINFORM的方法
- 想起自己的痴恋
- rs.open参数含义
- JavaScript 是怎样让互联网变慢的
- Hibernate学习(八):检索方式
- HR--配置HR的员工照片存储和上载的步骤
- WinForm的2种活动页截屏打印方法
- 我会尽快回来的
- hibernate可复用组件的hbm文件路径配置
- 今天2007年八月28日写的读取文本文件中的列名和数据进sql server数据库
- SD--Message Control Configure
- J2ME优秀网站
- 2007.08.22双向链表(前插操作,删除操作)
- NUnit2.0详细使用方法
- 关于VBS调用MSWinsock控件(转)