2012-05-15 28 views
3

我在这里有一个情况。我需要使用Visual Studio 2010中的C#在Windows窗体中创建员工卡结构。该结构可能包含具有白色背景的标签和图片框。我没有创建它的问题,但我也给这个表单上的一个名为“打印”的按钮,以便用户可以打印该卡。我搜索了它,但没有找到具体的东西。 请帮我一把。在Windows Form C中打印面板#

namespace Employee_Card_Manager 
{ 
public partial class Card : Form 
{ 
    public Card(String a, String b, String c, String d, String e, String f, String g, String h, String i) 
    { 
     InitializeComponent(); 
     this.label2.Text = a; 
     this.label9.Text = b; 
     this.label10.Text = c; 
     this.label11.Text = d; 
     this.label12.Text = e; 
     this.label13.Text = f; 
     this.label14.Text = g; 
     this.label16.Text = h; 
     this.pictureBox1.Image = Image.FromFile(i); 
     Image myimg = Code128Rendering.MakeBarcodeImage(h, 2, true); 
     this.pictureBox2.Image = myimg; 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
      //Print Card Code 
    } 
    } 
} 

该卡模板是如下:

Employee Card Structure

我已经放置在面板控制所有卡的结构和设置的面板背景为白色。你可以填写打印此卡的代码吗? 感谢

+0

看到这个http://msdn.microsoft.com/en-us/library/6he9hz8c.aspx – Rahul

+4

的StackOverflow是不是一个网站,有专业的开发人员为你做免费的工作。这是专业开发人员就真实问题和答案进行协作的地方。在尝试实施具有特定问题的打印功能后,请回来,以了解为什么无法使用它。请显示努力,研究,代码和一个具体问题。 –

回答

11

我已经找到了!

//declare event handler for printing in constructor 
     printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage); 

    //Rest of the code 
    Bitmap MemoryImage; 
    public void GetPrintArea(Panel pnl) 
    { 
     MemoryImage = new Bitmap(pnl.Width, pnl.Height); 
     pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height)); 
    } 
    protected override void OnPaint(PaintEventArgs e) 
    { 
     if (MemoryImage != null) 
     { 
      e.Graphics.DrawImage(MemoryImage, 0, 0); 
      base.OnPaint(e); 
     } 
    } 
    void printdoc1_PrintPage(object sender, PrintPageEventArgs e) 
    { 
     Rectangle pagearea = e.PageBounds; 
     e.Graphics.DrawImage(MemoryImage, (pagearea.Width/2) - (this.panel1.Width/2), this.panel1.Location.Y); 
    } 
    public void Print(Panel pnl) 
    { 
     pannel = pnl; 
     GetPrintArea(pnl); 
     previewdlg.Document = printdoc1; 
     previewdlg.ShowDialog(); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     Print(this.panel1); 
    } 
+2

嗨,奈达可以在打印方法中定义什么是Pannel。谢谢 – XTGX

+0

非常感谢你:) – FreedomDeveloper

+0

为什么第一行用红色colore标出下划线? printdoc1.PrintPage + = new PrintPageEventHandler(printdoc1_PrintPage); –

0

这个MSDN页面不是为你工作? http://msdn.microsoft.com/en-us/library/aa287529(v=vs.71).aspx

编辑:我修改的例子在前面提到的链接,并创建了一个PrintablePanel类,你可以以各种形式重复使用:下面这是可以正常使用的代码

public partial class PrintablePanel : Panel 
{ 
    public PrintablePanel() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 

     base.OnPaint(e); 
    } 

    [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); 


    public Bitmap GetImage() 
    { 
     Bitmap memoryImage = null; 
     Graphics mygraphics = 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); 

     return memoryImage; 
    } 
+0

据我了解这个代码是打印整个表单,在我的情况下,我的意图是打印只有面板... – Azeem

+0

所以,而不是使用这个,你会使用panel1,如果你使用示例代码在链接。 –

+0

@svenv感谢您的努力。您能否告诉我如何使用该类,因为在这里没有名为Print的函数,我想在我的Employee card类中调用... – Azeem