我正在尝试自动打印一系列Windows窗体。我不需要给他们看。我在Internet上找到的代码示例仅在显示带有show()
!的表单时有效。我需要使用数据初始化表单并将其发送到打印机,这是我正在使用的代码:
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;
}
}
我Print()
在for循环中从另一个类调用该方法,并通过构造函数传递要初始化的数据。
MSDN示例捕获了屏幕上应显示表单的部分。这对我不起作用。如果我不打电话,我现在使用的方法只产生空窗口的打印show()
。如何在不调用show()
方法的情况下将数据放入表单?像在显示时最小化窗口之类的方法也不起作用,因为这也是打印结果:最小化的窗口。
在显示表单之前,表单及其控件未处于Created
状态。要强制创建表单及其控件,只需调用CreateControl(bool fIgnoreVisible)
表单的内部方法即可:
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");
还要删除表单Load
事件处理程序中具有的代码,并将其放入表单的构造函数中。
注意
对于该问题,还有其他解决方法:
Location
于(-32000, -32000)
和一套StartPosition
可Manual
再Show
和Hide
绘制位图前的形式。Opacity
设置为0
,然后Show
和Hide
绘制位图前的形式。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句