您的位置:首页 > 编程语言 > C#

C#2.0 print winform 打印窗体数据试试

2009-02-12 13:56 381 查看
1

using System;
  2

using System.Collections.Generic;
  3

using System.ComponentModel;
  4

using System.Data;
  5

using System.Drawing;
  6

using System.Drawing.Drawing2D;
  7

using System.Text;
  8

using System.Windows.Forms;
  9

using System.Globalization; 
 10


 11

namespace PrintTest
 12





{
 13



    /**//// <summary>
 14

    /// 打印报告测试 
 15

    /// 2009-02-06 涂聚文 geovindu@163.com
 16

    /// </summary>
 17

    public partial class PintUlrtDocument : Form
 18



    

{
 19



        /**//// <summary>
 20

        /// 打印B超报告测试
 21

        /// </summary>
 22

        public PintUlrtDocument()
 23



        

{
 24

            InitializeComponent();
 25

        }
 26



        /**//// <summary>
 27

        /// 打印
 28

        /// </summary>
 29

        /// <param name="sender"></param>
 30

        /// <param name="e"></param>
 31

        private void btnPrint_Click(object sender, EventArgs e)
 32



        

{
 33


 34

        }
 35



        /**//// <summary>
 36

        /// 打印浏览
 37

        /// </summary>
 38

        /// <param name="sender"></param>
 39

        /// <param name="e"></param>
 40

        private void btnPrintView_Click(object sender, EventArgs e)
 41



        

{
 42

            if (MessageBox.Show("是否打印预览?", "打印预览", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
 43



            

{
 44

                this.printPreviewDialog1.Document = this.printDocument1;
 45

                printPreviewDialog1.ShowDialog();
 46

            }
 47

            else
 48



            

{
 49

                this.printDocument1.Print();//直接打印
 50

            }
 51

        }
 52



        /**//// <summary>
 53

        /// 打印设置
 54

        /// </summary>
 55

        /// <param name="sender"></param>
 56

        /// <param name="e"></param>
 57

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
 58



        

{
 59

            //字体设置
 60

            //this.dateTimePicker1.Format=dateTimePicker1.CustomFormat;
 61

            dateTimePicker1.CustomFormat = "yyyy-MM-dd";
 62

            //System.DateTime currentTime = new System.DateTime(); 
 63


 64

            DateTime date = DateTime.Now;
 65

            String format = "u";
 66

            string year=DateTime.Now.Year.ToString();
 67

            string month=date.ToString(format).Substring(5,2);
 68

            string day = date.ToString(format).Substring(8, 2);
 69


 70

            Font tFont = new Font("宋体", 20, FontStyle.Bold);
 71

            Font mFont = new Font("宋体", 15, FontStyle.Regular);
 72

            Font bFont = new Font("宋体", 10, FontStyle.Regular);
 73

            Font lFont = new Font("宋体", 10, FontStyle.Regular);
 74

            e.Graphics.DrawString("缔友计算机信息技术有限公司", tFont, Brushes.Black, 180, 10);
 75

            e.Graphics.DrawString("电脑超声诊断报告", mFont, Brushes.Black, 250, 40);
 76

            e.Graphics.DrawString("超声号:", bFont, Brushes.Black, 20, 70);
 77

            e.Graphics.DrawString(this.txtUrltID.Text.Trim(), bFont, Brushes.Black, 70, 70);
 78

            e.Graphics.DrawString("检查医生:", bFont, Brushes.Black, 290, 70);
 79

            e.Graphics.DrawString(this.combPhyName.Text.Trim(), bFont, Brushes.Black, 360, 70);
 80

            e.Graphics.DrawString("检查部位:", bFont, Brushes.Black, 20,90);
 81

            e.Graphics.DrawString(this.combCheckBody.Text.Trim(), bFont, Brushes.Black, 80, 90);
 82

            //划直线
 83

            Pen Lapiz = new Pen(Color.Black, 1);
 84

            int PosY = e.MarginBounds.Top;
 85

            int PosX = e.MarginBounds.Height;
 86

            e.Graphics.DrawLine(Lapiz, 20, PosY+10, 810, PosY+10);
 87

            
 88

            e.Graphics.DrawString("图像特征:", bFont, Brushes.Black, 20, 120);
 89

            e.Graphics.DrawString(this.txtPicImpress.Text.Trim(), bFont, Brushes.Black, 40, 140);
 90

            e.Graphics.DrawString("身份证图像", bFont, Brushes.Black, 420, 120);
 91

            e.Graphics.DrawImage(this.pictureBox2.Image, 450, 140, 350, 350);
 92

            //
 93

            e.Graphics.DrawString("超声提示:", bFont, Brushes.Black, 20, 300);
 94

            e.Graphics.DrawString(this.combMessage.Text, bFont, Brushes.Black, 40, 320);
 95

            string emty = "      ";
 96

            e.Graphics.DrawString("医师签字:", bFont, Brushes.Black, 600, 500);
 97

            e.Graphics.DrawString("___________________", bFont, Brushes.Black,680, 500);
 98

            e.Graphics.DrawString(this.combPhyName.Text+emty, lFont, Brushes.Black, 680, 500);
 99

            e.Graphics.DrawString("报告日期:", bFont, Brushes.Black, 600, 520);
100

            e.Graphics.DrawString(year, bFont, Brushes.Black, 680, 520);
101

            e.Graphics.DrawString("年", bFont, Brushes.Black, 720, 520);
102

            e.Graphics.DrawString(month, bFont, Brushes.Black, 740, 520);
103

            e.Graphics.DrawString("月", bFont, Brushes.Black, 760, 520);
104

            e.Graphics.DrawString(day, bFont, Brushes.Black, 790, 520);
105

            e.Graphics.DrawString("日", bFont, Brushes.Black, 810, 520);
106

            
107

            
108

            //Graphics G;
109

            //G = this.CreateGraphics();
110

            //G.DrawLine(new Pen(Color.Red, 10), 50, 10, 50, 100);
111


112

            //Graphics grfx = e.Graphics;
113

            //grfx.DrawString(Text, Font, Brushes.Black, 0, 0);
114

            //SizeF sizef = grfx.MeasureString(Text, Font);
115

            //grfx.DrawLine(Pens.Black, sizef.ToPointF(), grfx.VisibleClipBounds.Size.ToPointF());
116

            //grfx.DrawLine(Pens.Red, sizef.ToPointF(),grfx.VisibleClipBounds.Size.ToPointF());
117


118



            画斜线#region 画斜线
119

            //Graphics graphics = e.Graphics;
120

            //Pen pen = new Pen(Color.Red, 5);
121



            /**///// Opaque aqua
122

            //SolidBrush brush = new SolidBrush(Color.FromArgb(255, 180, 255, 255));
123



            /**///// Create a plus-shaped region by forming the union of two rectangles.
124

            //Region region = new Region(new Rectangle(50, 0, 50, 150));
125

            //region.Union(new Rectangle(0, 50, 150, 50));
126

            //graphics.FillRegion(brush, region);
127



            /**///// Set the clipping region.
128

            //graphics.SetClip(region, CombineMode.Replace);
129


130



            /**///// Draw two clipped lines.
131

            //graphics.DrawLine(pen, 0, 30, 150, 160);
132

            //graphics.DrawLine(pen, 40, 20, 190, 150);
133


134

            #endregion
135


136



            画多边形#region 画多边形
137

            //GraphicsPath path = new GraphicsPath();
138

            //path.StartFigure();
139

            //path.AddArc(175, 50, 50, 50, 0, -180);
140

            //path.AddLine(100, 0, 250, 20);
141

            //path.StartFigure();
142

            //path.AddLine(50, 20, 5, 90);
143



            /**/////path.AddCurve(points, 3);
144

            //path.AddLine(50, 150, 150, 180);
145

            //path.CloseFigure();
146

            //e.Graphics.DrawPath(new Pen(Color.Black), path);
147


148

            // e.Graphics.DrawString("Report Title", new Font("Arial", 16), Brushes.Black, 95, 70);
149

            #endregion 
150



            页底部#region 页底部
151

            // Draw footer
152

    //e.Graphics.DrawImage(pictureBox2.Image,  95, e.PageBounds.Height - 87);
153

    //Font drawFont =new Font("Arial",8);
154


155

    //e.Graphics.DrawString("Report Title", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 90);
156

    //e.Graphics.DrawString("Printed", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 76);
157

    //e.Graphics.DrawString("Printed By", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 62);
158


159



    /**///// Draw some grid lines to add structure to the footer information
160

    //e.Graphics.DrawLine(Pens.Gray, 246, e.PageBounds.Height - 90, 246, e.PageBounds.Height - 48);
161

    //e.Graphics.DrawLine(Pens.Gray, 188, e.PageBounds.Height - 75, 550, e.PageBounds.Height - 75);
162

    //e.Graphics.DrawLine(Pens.Gray, 188, e.PageBounds.Height - 61, 550, e.PageBounds.Height - 61);
163


164

    //e.Graphics.DrawString("Report", drawFont, Brushes.Black, 250, e.PageBounds.Height - 90);
165

    //e.Graphics.DrawString(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(), drawFont, Brushes.Black, 250, e.PageBounds.Height - 76);
166

    //e.Graphics.DrawString("Andrew", drawFont, Brushes.Black, 250, e.PageBounds.Height - 62);
167

            #endregion 
168

}
169



        /**//// <summary>
170

        /// 创建图片
171

        /// </summary>
172

        /// <param name="e"></param>
173

        public void DrawImageRectF(PaintEventArgs e)
174



        

{
175


176

            // Create image.
177

            Image newImage = Image.FromFile(Application.StartupPath + "//Customers Pics//CustPic.bmp");
178


179

            // Create rectangle for displaying image.
180

            RectangleF rect = new RectangleF(100.0F, 100.0F, 450.0F, 150.0F);
181


182

            // Draw image to screen.
183

            e.Graphics.DrawImage(newImage, rect);
184

        }
185



        /**//// <summary>
186

        /// 加载窗体
187

        /// </summary>
188

        /// <param name="sender"></param>
189

        /// <param name="e"></param>
190

        private void PintUlrtDocument_Load(object sender, EventArgs e)
191



        

{
192

            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
193

            pictureBox2.Image = Image.FromFile(Application.StartupPath + "//Customers Pics//CustPic.bmp");
194

        }
195



        /**//// <summary>
196

        /// 画直线
197

        /// </summary>
198

        /// <param name="e"></param>
199

        public void DrawLinePoint(PaintEventArgs e)
200



        

{
201

            //   Create   pen.  
202

            Pen blackPen = new Pen(Color.Black, 3);
203

            //   Create   points   that   define   line.  
204

            Point point1 = new Point(100, 100);
205

            Point point2 = new Point(500, 100);
206

            //   Draw   line   to   screen.  
207

            e.Graphics.DrawLine(blackPen, point1, point2);
208

        }   
209



        /**//// <summary>
210

        /// 画直线
211

        /// </summary>
212

        /// <param name="e"></param>
213

        protected override void OnPaint(PaintEventArgs e)
214



        

{
215

            Pen myPen = new Pen(Color.Red); 
216

            Graphics g = e.Graphics;
217

            myPen.Width = 5;
218

            g.DrawLine(myPen, 1, 1, 100, 100);
219


220

        }
221


222



        身份证#region  身份证
223

        //private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
224

        //{
225

        //    OracleLob myLob = null; //定义Oracle的Lob对象  
226

        //    MemoryStream myMs = null; //定义内存流  
227

        //    OracleDataReader myRead = null; //定义数据读取器  
228

        //    myCmd.CommandText = "select   *   from   mytab";
229

        //    myCmd.Connection = myConn;
230

        //    int js = 0; //循环计数器  
231

        //    int bound = 350; //表格边界  
232

        //    int i = 0;
233


234

        //    try
235

        //    {
236

        //        if (myConn.State == ConnectionState.Closed)
237

        //        {
238

        //            myConn.Open();
239

        //        }
240


241

        //        myRead = myCmd.ExecuteReader();
242


243

        //        while (myRead.Read())
244

        //        {
245

        //            if (myRead.GetOracleLob(1) != null)
246

        //            {
247

        //                myLob = myRead.GetOracleLob(1);
248

        //            }
249


250

        //            this.asBarcode1.Typ = BarcodeType.Code39; //设置条型码为code39格式  
251

        //            this.asBarcode1.Text = myRead["ID"].ToString();         //设置输出条型码  
252

        //            myMs = new MemoryStream((byte[])myLob.Value);
253

        //            photo = new Bitmap(myMs);
254


255

        //            Graphics myDc = e.Graphics;
256

        //            myDc.DrawRectangle(new Pen(Brushes.Black), 100, 50 + bound * js, 600, 260);
257

        //            myDc.DrawLine(new Pen(Brushes.Black), new Point(100, 80 + bound * js), new Point(700, 80 + bound * js));
258

        //            myDc.DrawString("姓名", new Font("宋体", 12), Brushes.Black, new Point(110, 57 + bound * js));
259

        //            myDc.DrawLine(new Pen(Brushes.Black), new Point(155, 50 + bound * js), new Point(155, 80 + bound * js));
260

        //            myDc.DrawString(myRead["name"].ToString(), new Font("宋体", 12), Brushes.Black, new Point(165, 57 + bound * js));
261

        //            myDc.DrawLine(new Pen(Brushes.Black), new Point(240, 50 + bound * js), new Point(240, 80 + bound * js));
262

        //            myDc.DrawString("性别", new Font("宋体", 12), Brushes.Black, new Point(250, 57 + bound * js));
263

        //            myDc.DrawLine(new Pen(Brushes.Black), new Point(295, 50 + bound * js), new Point(295, 80 + bound * js));
264

        //            myDc.DrawString(myRead["sex"].ToString(), new Font("宋体", 12), Brushes.Black, new Point(305, 57 + bound * js));
265

        //            myDc.DrawLine(new Pen(Brushes.Black), new Point(335, 50 + bound * js), new Point(335, 80 + bound * js));
266

        //            myDc.DrawString("身份证号码", new Font("宋体", 12), Brushes.Black, new Point(345, 57 + bound * js));
267

        //            myDc.DrawLine(new Pen(Brushes.Black), new Point(445, 50 + bound * js), new Point(445, 80 + bound * js));
268

        //            myDc.DrawString(myRead["id"].ToString(), new Font("宋体", 12), Brushes.Black, new Point(455, 57 + bound * js));
269

        //            this.asBarcode1.Left = 110;
270

        //            this.asBarcode1.Top = 90 + bound * js;
271

        //            this.asBarcode1.DrawBarcode(myDc);
272

        //            myDc.DrawLine(new Pen(Brushes.Black), new Point(100, 130 + bound * js), new Point(700, 130 + bound * js));
273

        //            myDc.DrawLine(new Pen(Brushes.Black), new Point(240, 130 + bound * js), new Point(240, 310 + bound * js));
274

        //            myDc.DrawImage((Image)photo, new Point[]{  
275

        //new   Point(110,140   +   bound   *   js),  
276

        //new   Point(230,140   +   bound   *   js),  
277

        //new   Point(110,280   +   bound   *   js)  
278

        //});
279

        //            myDc.DrawString("一代证像片", new Font("宋体", 12), Brushes.Black, new Point(125, 285 + bound * js));
280

        //            myDc.DrawLine(new Pen(Brushes.Black), new Point(380, 130 + bound * js), new Point(380, 310 + bound * js));
281

        //            // myDc.DrawImage((Image)photo,new   Point[]{  
282

        //            //       new   Point(250,140   +   bound   *   js), //二代证像片  
283

        //            //       new   Point(370,140   +   bound   *   js),  
284

        //            //       new   Point(250,280   +   bound   *   js)  
285

        //            //       });  
286

        //            myDc.DrawString("二代证像片", new Font("宋体", 12), Brushes.Black, new Point(265, 285 + bound * js));
287


288

        //            js++;
289


290

        //            e.HasMorePages = true;  
291

        //        }
292

        //    }
293

        //    catch (Exception err)
294

        //    {
295

        //        MessageBox.Show(err.Message);
296

        //    }
297

        //    finally
298

        //    {
299

        //        myMs.Close();
300

        //        myRead.Close();
301


302

        //        if (myConn.State == ConnectionState.Open)
303

        //        {
304

        //            myConn.Close();
305

        //        }
306

        //    }
307

        //} 
308

        #endregion 
309



        /**//// <summary>
310

        /// 
311

        /// </summary>
312

        /// <param name="args"></param>
313

        //public   static   void   date(string[]   args)    
314

        //{
315

        //        DateTime   dt   =   DateTime.Now;
316

        //        String[]   format   =   {
317

        //                "d ",   "D ",
318

        //                "f ",   "F ",
319

        //                "g ",   "G ",
320

        //                "m ",
321

        //                "r ",
322

        //                "s ",
323

        //                "t ",   "T ",
324

        //                "u ",   "U ",
325

        //                "y ",
326

        //                "dddd,   MMMM   dd   yyyy ",
327

        //                //"ddd,   MMM   d   / " '/ "yy ",
328

        //                "dddd,   MMMM   dd ",
329

        //                "M/yy ",
330

        //                "dd-MM-yy ",
331

        //        };
332

        //        String   date;
333

        //        for   (int   i   =   0;   i   <   format.Length;   i++)   {
334

        //                date   =   dt.ToString(format[i],   DateTimeFormatInfo.InvariantInfo);
335

        //                Console.WriteLine(String.Concat(format[i],   "   : "   ,   date));
336

        //        } 
337

        //}
338


339

    }
340

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息