c#中如何打印窗体

c#中如何打印窗体,第1张

Bitmap memoryImage = null; //定义一个

public static PrintDocument printDocument = new PrintDocument(); //定义一个Print文档对象

private void CaptureScreen()//截屏

{

Form form = this;

if (form != null)

{

Graphics mygraphics = formCreateGraphics();

memoryImage = new Bitmap(formWidth, formHeight, mygraphics);

Graphics memoryGraphics = GraphicsFromImage(memoryImage);

memoryGraphicsCopyFromScreen(formLocationX, formLocationY, 0, 0, formSize);

}

}

在窗体的load里面写:

printDocumentPrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);

printDocumentPrint(); //打印

加上这个事件处理:

void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)

{

eGraphicsClear(ColorWhite);

eGraphicsDrawImage(memoryImage, 0, 0);

}

代码经过测试了。若还不明白,Hi我或email:lijiuning(a)gmailcom

DataGrideView 有个 DrawToBitmap方法可以把你的控件以及上面的数据一起绘制到一张上;(其实这个方法是继承 Control 类的 所以任何控件都可以打印出来)

然后用 PrintDocument 类 在他的PrintPage()事件中打印出来就行了

PrintDocument pd = new PrintDocument();

pdPrintPage += new PrintPageEventHandler(thispd_PrintPage);

具体打印方法上网搜索一下有很多。

private bool morepages = true; private void printDocument1_PrintPage(object sender, SystemDrawingPrintingPrintPageEventArgs e)//打印文件的事件设置 {

if(morepages)//第一页打印的

{

morepages=false;

eGraphicsDrawImage(print_image, 0, 0); eHasMorePages = true;

}

else//第二页打印的

{

eGraphicsDrawImage(memImage, 0, 0); eHasMorePages = false;

}

窗体对话框组件与微软视窗 *** 作系统中的对话框是一样的 也就是说 PrintDialog 组件是 打印 对话框 OpenFileDialog 组件是 打开文件 对话框 依此类推

与以往的 Microsoft Visual Basic 等 Windows 程序设计语言相似 NET 框架提供了 Windows 用户耳熟能详的对话框 对话框的具体用途(如 Printdialog 可用于文件打印等)通常是多种多样的 故而在 NET 框架提供的基础类中不包含用于文件打印 颜色选择等具体 *** 作的代码 而你却可以根据应用程序的需要灵活地实现它们 因此 在 NET 框架下 你不但可以直接应用标准对话框 而且能根据用户的选择作出不同的响应 本文提供的代码其用途就在于此

注意 关于各种对话框的属性 方法和事件的完整描述 可以在相应类的 Members 页面中找到 比如要查看 OpenFileDialog 组件的某一方法 就可以在文档索引的 OpenFileDialog class all members 栏目中找到相关的主题

1.OpenFileDialog 组件

OpenFileDialog 对话框使得用户能够通过浏览本地或者远程的文件系统以打开所选文件 它将返回文件路径和所选的文件名

OpenFileDialog 组件和SaveFileDialog 组件(下文将会详细描述)包含了用于浏览和选取文件所必需的基本代码 有了它们 你就不必为这些功能编写任何代码 进而能够专心实现打开或者保存文件等具体 *** 作

注意 FileDialog 类的 FilterIndex 属性(由于继承的原因 为 OpenFileDialog 和 SaveFileDialog 类所共有) 使用 one based 索引(译者注 指从 开始编号的索引) 此特性将在下文的代码中用到(并且会在相应位置再次强调) 当应用程序通过类型过滤器打开文件时 或者需要保存为特定格式的文件(比如 保存为纯文本文件而不是二进制文件)时 这一点是非常重要的 人们在使用 FilterIndex 属性时却经常忘了它 因此现在务必要把它记住

下列代码通过 Button 控件的 Click 事件调用 OpenFileDialog 组件 当用户选中某个文件 并且单击 OK 的时候 所选的文件将被打开 在本例中 文件内容将被显示在消息框内 以证实文件流被读入

本例假设存在名为 Button 的 Button 控件和名为 OpenFileDialog 的 OpenFileDialog 控件

Visual Basic NOTE: You must import the following namespace: Imports System IO Without this import statement at the beginning of your code the example will not function Private Sub Button _Click(ByVal sender As System Object _ ByVal e As System EventArgs) Handles Button Click If OpenFileDialog ShowDialog() = DialogResult OK Then Dim sr As New StreamReader(OpenFileDialog FileName) MessageBox Show(sr ReadToEnd) sr Close() End If End Sub

// C# // NOTE: You must import the following namespace: // using System IO; // Without this import statement at the beginning // of your code the example will not function private void button _Click(object sender System EventArgs e) { if(openFileDialog ShowDialog() == DialogResult OK) { StreamReader sr = new StreamReader(openFileDialog FileName); MessageBox Show(sr ReadToEnd()); sr Close(); } }

打开文件还可以使用 OpenFileDialog 组件的 OpenFile 方法 它将返回文件的每一个字节 在下面的例子中 一个 OpenFileDialog 组件将被实例化 它使用了 cursor 过滤器 以限定用户只能选取光标文件(扩展名为 cur) 一旦某个 cur 文件被选中 窗体的光标就被设成该文件描绘的光标形状

本例假设存在名为 Button 的 Button 控件

Visual Basic Private Sub Button _Click(ByVal sender As System Object _ ByVal e As System EventArgs) Handles Button Click Display an OpenFileDialog so the user can select a Cursor Dim openFileDialog As New OpenFileDialog() openFileDialog Filter = Cursor Files| cur openFileDialog Title = Select a Cursor File

Show the Dialog If the user clicked OK in the dialog and a CUR file was selected open it If openFileDialog ShowDialog() = DialogResult OK Then If openFileDialog FileName <> Then Assign the cursor in the Stream to the Form s Cursor property Me Cursor = New Cursor(openFileDialog OpenFile()) End If End If End Sub

// C# private void button _Click(object sender System EventArgs e) { // Display an OpenFileDialog so the user can select a Cursor OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog Filter = Cursor Files| cur ; openFileDialog Title = Select a Cursor File ;

// Show the Dialog // If the user clicked OK in the dialog and // a CUR file was selected open it if (openFileDialog ShowDialog() == DialogResult OK) { if(openFileDialog FileName != ) { // Assign the cursor in the Stream to the Form s Cursor property this Cursor = new Cursor(openFileDialog OpenFile()); } } }

关于读取文件流的进一步信息 请参阅FileStream BeginRead 方法

2.SaveFileDialog 组件

本对话框允许用户浏览文件系统并且选取将被写入的文件 当然 你还必须为文件写入编写具体代码

下列代码通过 Button 控件的 Click 事件调用 SaveFileDialog 组件 当用户选中某个文件 并且单击 OK 的时候 RichTextBox 控件里的内容将被保存到所选的文件中

本例假设存在名为 Button 的 Button 控件 名为 RichTextBox 的 RichTextBox 控件和名为 OpenFileDialog 的 SaveFileDialog 控件

Visual Basic NOTE: You must import the following namespace: Imports System IO Without this import statement at the beginning of your code the code example will not function Private Sub Button _Click(ByVal sender As System Object _ ByVal e As System EventArgs) Handles Button Click If SaveFileDialog ShowDialog() = DialogResult OK Then RichTextBox SaveFile(SaveFileDialog FileName _ RichTextBoxStreamType PlainText) End If End Sub

// C# // NOTE: You must import the following namespace: // using System IO; // Without this import statement at the beginning // of your code the code example will not function private void button _Click(object sender System EventArgs e) { if((saveFileDialog ShowDialog() == DialogResult OK) { richTextBox SaveFile(saveFileDialog FileName RichTextBoxStreamType PlainText); } }

保存文件还可以用 SaveFileDialog 组件的 OpenFile 方法 它将提供一个用于写入的 Stream 对象

在下面的例子中 有一个包含的 Button 控件 当你单击这个按钮的时候 一个 SaveFileDialog 组件将被打开 它将使用 gif jpeg 和 bmp 类型的文件过滤器 一旦用户通过 Save File 对话框内选中此类文件 按钮上的将被存入其中

本例假设存在名为 Button 的 Button 控件 并且它的 Image 属性被设为某个扩展名为 gif jpeg 或者 bmp 的文件

Visual Basic NOTE: You must import the following namespaces: Imports System IO Imports System Drawing Imaging Without these import statements at the beginning of your code the code example will not function Private Sub Button _Click(ByVal sender As System Object _ ByVal e As System EventArgs) Handles Button Click Display an SaveFileDialog so the user can save the Image assigned to Button Dim saveFileDialog As New SaveFileDialog() saveFileDialog Filter = JPeg Image| jpg|Bitmap Image| bmp|Gif Image| gif saveFileDialog Title = Save an Image File saveFileDialog ShowDialog()

If the file name is not an empty string open it for saving If saveFileDialog FileName <> Then Save the Image via a FileStream created by the OpenFile method Dim fs As FileStream = CType(saveFileDialog OpenFile() FileStream) Save the Image in the appropriate ImageFormat based upon the file type selected in the dialog box NOTE that the FilterIndex property is one based Select Case saveFileDialog FilterIndex Case Me button Image Save(fs ImageFormat Jpeg)

Case Me button Image Save(fs ImageFormat Bmp)

Case Me button Image Save(fs ImageFormat Gif) End Select

fs Close() End If End Sub

// C# // NOTE: You must import the following namespaces: // using System IO; // using System Drawing Imaging; // Without these import statements at the beginning of your code // the code example will not function private void button _Click(object sender System EventArgs e) { // Display an SaveFileDialog so the user can save the Image // assigned to Button SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog Filter = JPeg Image| jpg|Bitmap Image| bmp|Gif Image| gif ; saveFileDialog Title = Save an Image File ; saveFileDialog ShowDialog();

// If the file name is not an empty string open it for saving if(saveFileDialog FileName != ) { // Save the Image via a FileStream created by the OpenFile method FileStream fs = (FileStream)saveFileDialog OpenFile(); // Save the Image in the appropriate ImageFormat based upon the // File type selected in the dialog box // NOTE that the FilterIndex property is one based switch(saveFileDialog FilterIndex) { case : this button Image Save(fs ImageFormat Jpeg); break;

case : this button Image Save(fs ImageFormat Bmp); break;

case : this button Image Save(fs ImageFormat Gif); break; }

fs Close(); } }

关于写入文件流的进一步信息 请参阅 FileStream BeginWrite 方法

3.ColorDialog 组件

此对话框显示颜色列表 并且返回所选的颜色

与前两种对话框不同 ColorDialog 组件很容易实现其主要功能(挑选颜色) 选取的颜色将成为 Color 属性的设定值 因此 使用颜色就和设定属性值一样简单 在下面的例子中 按钮控制的 Click 事件将会开启一个 ColorDialog 组件 一旦用户选中某种颜色 并且单击了 OK 按钮的背景将被设成所选的颜色 本例假设存在名为 Button 的 Button 组件和名为 ColorDialog 的 ColorDialog 组件

Visual Basic Private Sub Button _Click(ByVal sender As System Object _ ByVal e As System EventArgs) Handles Button Click If ColorDialog ShowDialog() = DialogResult OK Then Button BackColor = ColorDialog Color End If End Sub

// C# private void button _Click(object sender System EventArgs e) { if(colorDialog ShowDialog() == DialogResult OK) { button BackColor = colorDialog Color; } }

ColorDialog 组件具有 AllowFullOpen 属性 当其设为 False 的时候 Define Custom Colors 按钮将会失效 此时用户只能使用预定义的调色板 此外 它还有一个 SolidColorOnly 属性 当其设为 true 时 用户将不能使用抖动颜色

4.FontDialog 组件

此对话框允许用户选择字体 以改变其 weight 和 size 等属性

被选中的字体将成为 Font 属性的设定值 因此 使用字体也和设定属性值一样简单 在本例通过 Button 控件的 Click 事件调用 FileDialog 组件 当用户选中一个字体 并且单击 OK 的时候 TextBox 控件的 Font 属性将被设成所选的字体 本例假设存在名为 Button 的 Button 控件 名为 TextBox 的 TextBox 控件和名为 FontDialog 的 FontDialog 组件

Visual Basic Private Sub Button _Click(ByVal sender As System Object _ ByVal e As System EventArgs) Handles Button Click If FontDialog ShowDialog() = DialogResult OK Then TextBox Font = FontDialog Font End If End Sub

// C# private void button _Click(object sender System EventArgs e) { if(fontDialog ShowDialog() == DialogResult OK) { textBox Font = fontDialog Font; } }

FontDialog 元件还包括 MinSize 和 MaxSize 属性 它们决定了允许用户选择的字体的最小和最大点数 还有一个 ShowColor 属性 当其设为 True 时 用户可以从对话框的下拉列表中选取字体的颜色

5.PrintDocument 类

以下三个会话框 PrintDialog 组件 PageSetupDialog 组件和 PrintPreviewDialog 控件 都与 PrintDocument 类有关 PrintDocument 类用于文档打印前的设置 设定其属性 以改变文档外观和打印方式 再将其实例输出到打印机 通常的步骤是 ( ) 生成 PrintDocument 类的一个实例 ( ) 设置 PageSetupDialog 组件的属性 ( ) 使用 PrintPreviewDialog 控件进行预览 ( ) 通过 PrintDialog 组件打印出来

关于 PrintDocument 类的进一步资料 请参阅 PrintDocument Class

6.PrintDialog 元件

此对话框允许用户指定将要打印的文档 除此之外 它还能用于选择打印机 决定打印页 以及设置打印相关属性 通过它可以让用户文档打印更具灵活性 他们既能打印整个文档 又能打印某个片断 还能打印所选区域

使用 PrintDialog 组件时要注意它是如何与 PrinterSettings 类进行交互的 PrinterSettings 类用于设定纸张来源 分辨率和加倍放大等打印机特征属性 每项设置都是 PrinterSettings 类的一个属性 通过 PrintDialog 类可以改变关联到文档的 PrinterSetting 类实例(由PrintDocument PrinterSettings 指定)的特征属性值

PrintDialog 组件将包含特征属性设置的 PrintDocument 类的实例提交到打印机 应用 PrintDialog 组件进行文档打印的范例 请参见 Creating Standard Windows Forms Print Jobs

7.PageSetupDialog 组件

PageSetupDialog 组件用于显示打印布局 纸张大小和其它页面选项 如同其他对话框一样 可以通过 ShowDialog 方法调用 PageSetupDialog 组件 此外 必须生成一个 PrintDocument 类的实例 也即被打印的文档 而且必须安装了一台本地或者远程打印机 否则 PageSetupDialog 组件将无法获取打印格式以供用户选择

使用 PageSetupDialog 组件时必须注意它是如何与 PageSettings 类进行交互的 PageSettings 类决定页面如何被打印 比如打印方向 页面大小和边距等 每项设置都是 PageSettings 类的一个属性 PageSetupDialog 类可以改变 PageSettings 类实例(由 PrintDocument DefaultPageSettings 指定)的上述选项

在下列代码中 Button 控件的 Click 事件处理程序开启一个 PageSetupDialog 组件 其 Document 属性被设成某个存在的文档 其 Color 属性被设成 false

本例假设存在名为 Button 的 Button 控件 名为 myDocument 的 PrintDocument 控件和名为 PageSetupDialog 的 PageSetupDialog 组件

Visual Basic Private Sub Button _Click(ByVal sender As System Object _ ByVal e As System EventArgs) Handles Button Click The print document myDocument used below is merely for an example You will have to specify your own print document PageSetupDialog Document = myDocument Set the print document s color setting to false so that the page will not be printed in color PageSetupDialog Document DefaultPageSettings Color = False PageSetupDialog ShowDialog() End Sub

// C# private void button _Click(object sender System EventArgs e) { // The print document myDocument used below // is merely for an example // You will have to specify your own print document pageSetupDialog Document = myDocument; // Set the print document s color setting to false // so that the page will not be printed in color pageSetupDialog Document DefaultPageSettings Color = false; pageSetupDialog ShowDialog(); }

8.PrintPreviewDialog 控件

与其他对话框不同 PrintPreviewDialog 控件对整个应用程序或者其它控件没有影响 因为它仅仅在对话框里显示内容 此对话框用于显示文档 主要是打印之前的预览

调用 PrintPreviewDialog 控件 也是使用 ShowDialog 方法 同时 必须生成 PrintDocument 类的一个实例 也即被打印的文档

注意 当使用 PrintPreviewDialog 控件时 也必须已经安装了一台本地或者远程打印机 否则 PrintPreviewDialog 组件将无法获取被打印文档的外观

PrintPreviewDialog 控件通过 PrinterSettings 类和 PageSettings 类进行设置 分别与 PageDialog 组件和 PageSetupDialog 组件相似 此外 PrintPreviewDialog 控件的 Document 属性所指定的被打印文档 同时作用于 PrinterSettings 类和 PageSettings 类 其内容被显示在预览窗口中

在下列代码中 通过 Button 控件的 Click 事件调用 PrintPreviewDialog 控件 被打印文档在 Document 属性中指定 注意 代码中没有指定被打印文档

本例假设存在名为 Button 的 Button 控件 名为 myDocument 的 PrintDocument 组件和名为 PrintPreviewDialog 的 PrintPreviewDialog 控件

Visual Basic Private Sub Button _Click(ByVal sender As System Object _ ByVal e As System EventArgs) Handles Button Click The print document myDocument used below is merely for an example You will have to specify your own print document PrintPreviewDialog Document = myDocument PrintPreviewDialog ShowDialog() End Sub

// C# private void button _Click(object sender System EventArgs e) { // The print document myDocument used below // is merely for an example // You will have to specify your own print document printPreviewDialog Document = myDocument; printPreviewDialog ShowDialog() }

lishixinzhi/Article/program/net/201311/14815

这是记事本中打印按钮的代码,希望能帮助到你:

private PrintDocument printdocument1 = new PrintDocument();

private void MenuItemPrint_Click(object sender, EventArgs e)

{

printDialog1AllowSomePages = true;//启用页选项

printDialog1ShowHelp = true;//启用帮助按钮

printDialog1PrinterSettings = new PrinterSettings();//获取打印机对话框修改的属性

DialogResult result = printDialog1ShowDialog();

try//捕获打印机的异常

{

if (result == DialogResultOK)

{

printdocument1Print();

}

}

catch (Exception ee)

{

MessageBoxShow(eeMessageToString());

}

}

以上就是关于c#中如何打印窗体全部的内容,包括:c#中如何打印窗体、C#怎样打印出DataGrideView中的数据、C# printdocument 分页打印等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9439746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存