动态用就是动态创建,这个很简单吧,哪儿要用就在哪儿new要打印的话,需要显示出来,把里面的内容复制,输出到打印机

解决方案 »

  1.   

    你在哪里要打印的时候,初下化一下这个控件,并且设置它的编码格式,然后打印就行了
    建议去下一个Barcode,这个蛮好用的,我一直用这个.
      

  2.   

    给你一个我测试的源码,是BarCode的,你去下一个dll就行了,我下面是打印整个panel,不是只是条码图片,
    某些情况下要条码和文字信息一同打印,所以我全放到一个panel上,然后打印出来的
    代码如下: public partial class BarCodeSetupBox : Form
        {
            public BarCodeSetupBox()
            {
                InitializeComponent();
            }        private void comType_SelectedIndexChanged(object sender, EventArgs e)
            {
                barcodeControl2.BarcodeType = GetBarcodeType(comType.Text);
                Invalidate();
            }        BarcodeType GetBarcodeType(string cType)
            {
                BarcodeType bt = BarcodeType.CODE128A;
                switch (cType.ToUpper())
                {
                    case "C2OF5":
                        bt = BarcodeType.C2OF5;
                        break;
                    case "EAN128A":
                        bt = BarcodeType.EAN128A;
                        break;
                }
                return bt;
            }        private void cheCode39_CheckedChanged(object sender, EventArgs e)
            {
                barcodeControl2.ShowCode39StartStop = cheCode39.Checked;
                Invalidate();
            }        private void txtData_TextChanged(object sender, EventArgs e)
            {
                barcodeControl2.Data = txtData.Text;
                Invalidate();
            }        private void txtSubData_TextChanged(object sender, EventArgs e)
            {
                barcodeControl2.AddOnData = txtSubData.Text;
                Invalidate();
            }        private void comDataShow_SelectedIndexChanged(object sender, EventArgs e)
            {
                barcodeControl2.TextPosition = GetTextPosition(comDataShow.Text);
                Invalidate();
            }        BarcodeTextPosition GetTextPosition(string str)
            {
                switch (str)
                {
                    case "顶部":
                        return BarcodeTextPosition.Above;
                    case "不显示":
                        return BarcodeTextPosition.NotShown;
                    case "底部":
                    default:
                        return BarcodeTextPosition.Below;
                }
            }        private void BarCodeSetupBox_Load(object sender, EventArgs e)
            {
                comType.SelectedIndex = 0;
                comDataShow.SelectedIndex = 0;
                Invalidate();
            }        private void vistaButton1_Click(object sender, EventArgs e)
            {
                CaptureScreen();
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                PrintPreviewDialog cppd = new PrintPreviewDialog();
                cppd.Document = pd;
                cppd.ShowDialog();
            }        void pd_PrintPage(object sender, PrintPageEventArgs e)
            {
                //Graphics g = e.Graphics;
                //barcodeControl2.Draw(g, barcodeControl2.ClientRectangle, GraphicsUnit.Inch, 0.01f, 0, null);
                //g.Dispose();
                e.Graphics.DrawImage(memoryImage, 0, 0);        }
            [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()
            {
                //如果你只打印图片大小的话把this.控件名称.CreateGraphics()就行了,下面一样
                Graphics mygraphics = this.panel1.CreateGraphics();//创建的是整个panel
                Size s = this.panel1.Size;//取panel大小
                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.panel1.ClientRectangle.Width, this.panel1.ClientRectangle.Height, dc1, 0, 0, 13369376);
                mygraphics.ReleaseHdc(dc1);
                memoryGraphics.ReleaseHdc(dc2);
            }        private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                barcodeControl2.StretchText = checkBox1.Checked;
            }        void txtTitle_TextChanged(object sender, System.EventArgs e)
            {
                barcodeControl2.CopyRight = txtTitle.Text;
            }        private void vistaButton3_Click(object sender, EventArgs e)
            {
                System.IO.Stream myStream;
                SaveFileDialog fileDialog = new SaveFileDialog();
                fileDialog.Filter = "GIF files (*.gif)|*.gif|PNG files (*.png)|*.png|JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg|BMP files (*.bmp;*.dib)|*.bmp;*.dib|TIFF files (*.tif)|*.tif";
                fileDialog.FilterIndex = 1;
                fileDialog.FileName = DateTime.Now.ToString("yyyyMMddHHmmss");
                fileDialog.RestoreDirectory = false;
                ImageFormat[] afmt = { ImageFormat.Gif, ImageFormat.Png, ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Tiff };            if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    if ((myStream = fileDialog.OpenFile()) != null)
                    {
                        int barcodeWidth =  1;
                        int barcodeHeight = 50;
                        barcodeControl2.MakeImage(afmt[fileDialog.FilterIndex - 1], barcodeWidth, barcodeHeight, true, false, null, myStream);
                        myStream.Close();
                    }
                }
            }        private void vistaButton2_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
        }