如何打印Windows窗体不显示/显示它 [英] How to print a Windows Form without showing/displaying it

查看:208
本文介绍了如何打印Windows窗体不显示/显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动打印系列Windows的形式。我并不需要向他们展示。代码示例我在互联网上的工作中发现,只有当我显示与表单显示()!我需要的数据初始化表并发送到打印机这里是我使用的代码:

I am trying to print automatically a series of Windows-Forms. I don't need to show them. The code examples I found on the internet work only when I display the Form with show()! I need to initialize the form with data and send it to the printer Here is the code I am using:

public partial class Form2_withoutShow : Form{

     PrintDocument PrintDoc;

     Bitmap memImage;

     public Form2_withoutShow (Data data)
     {

        InitializeComponent();

        /*
           Initialize Data (texboxes, charts ect.) here
       */

        this.PrintDoc = new PrintDocument();
        this.PrintDoc.PrintPage += PrintDoc_PrintPage;
        this.PrintDoc.DefaultPageSettings.Landscape = true;
     }


     public void Print()
     {
        this.PrintDoc.Print();
     }

     void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
     {

        int x = SystemInformation.WorkingArea.X;
        int y = SystemInformation.WorkingArea.Y;
        int width = this.Width;
        int height = this.Height; 

        Rectangle bounds = new Rectangle(x, y, width, height);

        Bitmap img = new Bitmap(width, height); 

        this.DrawToBitmap(img, bounds);
        Point p = new Point(10, 10);

        e.Graphics.DrawImage(img, p);
     }

     private void Form2_withoutShow_Load(object sender, EventArgs e)
     {
         // remove TITLEBar
         this.ControlBox = false;
         this.Text = String.Empty;
     }
}



我称之为打印( )从另一个类的方法在for循环和传递数据通过构造函数初始化。

I call the Print() method from another class in for-loop and pass the Data to be initialized through the constructor.

在MSDN的例如捕捉其中的形式应显示在屏幕上的部分。这并不为我工作。我现在使用的方法只产生空窗的打印,如果我不叫显示()。我如何获取数据到表单中,而无需调用显示()的方法?显示它的时候,也不要工作,因为这也是打印效果接近像mimize窗口:最小化窗口

The MSDN example captures the part on the screen where the forms should be displayed. This doesn't work for me. The approach I am using now yields only the print of the empty Window if I don't call show(). How do I get the data into the form without having to call the show() method? Approaches like mimize the windows when displaying it, also don't work because that is also the print result: a minimized window.

推荐答案

显示窗体之前,窗体和控件不在的 创建 状态。要强制窗体和控件要创建它足以调用内部的 CreateControl(布尔fIgnoreVisible) 窗体的方法:

Before showing the form, the form and its controls are not in Created state. To force the form and its controls to be created it's enough to call internal CreateControl(bool fIgnoreVisible) method of your form:

var f = new Form1();
var createControl = f.GetType().GetMethod("CreateControl",
                BindingFlags.Instance | BindingFlags.NonPublic);
createControl.Invoke(f, new object[] { true });

var bm = new Bitmap(f.Width, f.Height);
f.DrawToBitmap(bm, new Rectangle(0, 0, bm.Width, bm.Height));
bm.Save(@"d:\bm.bmp");



同时删除您的表单中有代码加载事件处理程序,并把它们在窗体的构造函数。

Also remove codes which you have in your form Load event handler, and put them in constructor of your form.

注意

还有其他解决方法的问题:

There are also other workarounds for the problem:


  • 例如,您可以显示表单中的位置在屏幕的边界之外和然后再次将其隐藏。在位置设置为( - 32000,-32000)并设置中StartPosition 手动然后显示隐藏的形式绘制位图前。

  • 或作为另一个例子,可以显示与透明度设置为的形式0 然后显示隐藏绘制位图前的形式。

  • For example you can show the form in a location outside of boundary of the screen and then hide it again. Set the Location to (-32000, -32000) and set StartPosition to be Manual then Show and Hide the form before drawing to bitmap.
  • Or as another example, you can show the form with Opacity set to 0 and then Show and Hide the form before drawing to bitmap.

这篇关于如何打印Windows窗体不显示/显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆