winforms 如何在C#中打印自定义页面?

sgtfey8w  于 2023-06-24  发布在  C#
关注(0)|答案(2)|浏览(152)

我在C# WinForm中添加了一些labelstextboxes,我想用我设置的布局打印该表单。
我试着这样做:

PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.Jumbo_Load);

PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pd;
ppd.ShowDialog();

但这只是打开了一页空白。不是我想要的那个。
如果有人能在这方面帮助我,我将不胜感激。
我还创建了SAP Crystal Report。如何使用查询过滤某些记录?因为那样的话,我就可以打印那份报告了。

2hh7jdfx

2hh7jdfx1#

我不确定你是否有更多的代码,但我有一个类似的情况,看起来是这样的:

PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.Jumbo_Load);

PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pd;
DialogResult result = ppd.ShowDialog();

if (result == DialogResult.OK) {
    pd.Print();
}
zqry0prt

zqry0prt2#

来自MSDN

[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 Bitmap memoryImage;
private void CaptureScreen()
{
   Graphics mygraphics = this.CreateGraphics();
   Size s = this.Size;
   memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
   Graphics memoryGraphics = Graphics.FromImage(memoryImage);
   IntPtr dc1 = mygraphics.GetHdc();
   IntPtr dc2 = memoryGraphics.GetHdc();
   BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
   mygraphics.ReleaseHdc(dc1);
   memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
   CaptureScreen();
   printDocument1.Print();
}

相关问题