2012-05-15 29 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