namespace prt4
{
    public partial class Form1 : Form
    {
        public int prpage=2;
        private int p = 0;
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            print();
        }               private void button2_Click(object sender, EventArgs e)
        {
            print2();
        }
        private void print()
        {
            PrintDocument prndoc = new PrintDocument();         
            PrintPreviewDialog preddlg = new PrintPreviewDialog();           
            preddlg.Document = prndoc;
            prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);          
            preddlg.ShowDialog();
        }
        private void print2()
        {
            PrintDocument prndoc = new PrintDocument();
            PrintDialog prndlg = new PrintDialog();        
            prndlg.Document = prndoc;         
            prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);           
            prndlg.ShowDialog();
        }        private void OnPrintPage(object sender, PrintPageEventArgs ppea)
        {
            Graphics grfx = ppea.Graphics;            
            ppea.HasMorePages = false;            
            while (prpage >= 1)
            {
                painttable(grfx, p, ppea);
                p++;
                if (prpage == 1)
                {
                    ppea.HasMorePages = false;
                    prpage--;
                    return;
                }
                else if (prpage > 1)
                {
                    ppea.HasMorePages = true;
                    prpage--;
                    return;
                }
            }
        }
        private void painttable(Graphics grfx, int k, PrintPageEventArgs ppea)
        {
            Pen pen = new Pen(Color.Black, 1);
            Pen pen2 = new Pen(Color.Black, 2);
            Rectangle rect = new Rectangle(mtm(15), mtm(40), mtm(181), mtm(228));
            grfx.DrawRectangle(pen2, rect); 
            Font font2 = new Font("宋体", 14);
            Brush brush = Brushes.Black;
            PointF point2 = new PointF();
            fillcaption(grfx, k, ppea);
            point2.X = mtm(17);
            point2.Y = mtm(42);
            string str = k.ToString();
            grfx.DrawStr1ng(str, font2, brush, point2);
        }
        private void fillcaption(Graphics grfx, int a, PrintPageEventArgs ppea)
        {
            Font font = new Font("楷体_GB2312", 22);           
            Pen pen = new Pen(Color.Black, 2);           
            Brush brush = Brushes.Black;
            string str = "学生花名册";
            SizeF sizef = grfx.MeasureString(str, font);
            grfx.DrawString(str, font, brush, (grfx.VisibleCl1pBounds.Width - sizef.Width) / 2, mtm(12));
        }
        private int mtm(double mm)
        {
            double foot = 304.8;    //1 foot equal 304.8 mm
            double inch = foot / 12f;
            double atomUnit = inch / 100;
            return Convert.ToInt32(mm / atomUnit);
        }
 为什么上面和程序什么也打印不出来,
预览时,有两页,单击预览对话框左上角的打印,只打印一张空白纸出来,明明预览看到有内容啊,这是什么么?
帮帮我修改一下,可以多给分哈

解决方案 »

  1.   

    public void Print()
            {
                this.printdoc = new System.Drawing.Printing.PrintDocument();
                this.printdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printdoc_PrintPage);
                this.printdoc.Print();
            }
    private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
      printFont = new Font("Trebuchet MS", 7);
                    string date =datetime.now.ToString("yyMM");
                    DrawString(e, date, GetPoint(0.55m + (0.8m - frm.GetFontWidth(printFont, date)) / 2 - decX, d - decY));}
     private void DrawString(System.Drawing.Printing.PrintPageEventArgs e, string s, Point point)
            {
                e.Graphics.DrawString(s, printFont, brush, GetRect(point));
            }这个是没有打印预览窗体的打印,你试试
      

  2.   


    namespace Steel
    {
        public partial class Form1 : Form
        {
            public int prpage = 2;
            private int p = 0;         public Form1()
            {
                InitializeComponent();
            }        private void button1_Click( object sender, EventArgs e )
            {
                print(); 
            }        private void button2_Click( object sender, EventArgs e )
            {
                print2(); 
            }        private void print()
            {
                PrintDocument prndoc = new PrintDocument();
                PrintPreviewDialog preddlg = new PrintPreviewDialog();
                preddlg.Document = prndoc;
                prndoc.PrintPage += new PrintPageEventHandler( OnPrintPage );            //每次打印完成后要重置这两个变量,以备下次打印时使用
                //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                prndoc.EndPrint += new PrintEventHandler( prndoc_EndPrint );            preddlg.ShowDialog();        }        void prndoc_EndPrint( object sender, PrintEventArgs e )
            {
                //每次打印完成后要重置这两个变量,以备下次打印时使用
                //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                prpage = 2;
                p = 0;
            }        private void print2()
            {
                PrintDocument prndoc = new PrintDocument();
                PrintDialog prndlg = new PrintDialog();
                prndlg.Document = prndoc;
                prndoc.PrintPage += new PrintPageEventHandler( OnPrintPage );            //每次打印完成后要重置这两个变量,以备下次打印时使用
                //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                prndoc.EndPrint += new PrintEventHandler( prndoc_EndPrint );
                
                prndlg.ShowDialog();
                prndoc.Print();
            }        private void OnPrintPage( object sender, PrintPageEventArgs ppea )
            {
                Graphics grfx = ppea.Graphics;
                ppea.HasMorePages = false;
                while ( prpage >= 1 )
                {
                    painttable( grfx, p, ppea );
                    p++;
                    if ( prpage == 1 )
                    {
                        ppea.HasMorePages = false;
                        prpage--;
                        return;
                    }
                    else if ( prpage > 1 )
                    {
                        ppea.HasMorePages = true;
                        prpage--;
                        return;
                    }
                }
            }
            private void painttable( Graphics grfx, int k, PrintPageEventArgs ppea )
            {
                Pen pen = new Pen( Color.Black, 1 );
                Pen pen2 = new Pen( Color.Black, 2 );
                Rectangle rect = new Rectangle( mtm( 15 ), mtm( 40 ), mtm( 181 ), mtm( 228 ) );
                grfx.DrawRectangle( pen2, rect );
                Font font2 = new Font( "宋体", 14 );
                Brush brush = Brushes.Black;
                PointF point2 = new PointF();
                fillcaption( grfx, k, ppea );
                point2.X = mtm( 17 );
                point2.Y = mtm( 42 );
                string str = k.ToString();
                grfx.DrawString( str, font2, brush, point2 );
            }
            private void fillcaption( Graphics grfx, int a, PrintPageEventArgs ppea )
            {
                Font font = new Font( "楷体_GB2312", 22 );
                Pen pen = new Pen( Color.Black, 2 );
                Brush brush = Brushes.Black;
                string str = "学生花名册";
                SizeF sizef = grfx.MeasureString( str, font );
                grfx.DrawString( str, font, brush, ( grfx.VisibleClipBounds.Width - sizef.Width ) / 2, mtm( 12 ) );
                
            }
            private int mtm( double mm )
            {
                double foot = 304.8;    //1 foot equal 304.8 mm
                double inch = foot / 12f;
                double atomUnit = inch / 100;
                return Convert.ToInt32( mm / atomUnit );
            } 
        }
    }楼主这样写法有点不对劲
    建议将 PrintDocument、PrintDialog等打印对象的初始化放到构造函数中去。