[转] C# 绘制报表,使用Graphics.DrawString 方法

原文 Graphics.DrawString 方法

在指定位置并且用指定的 Brush 和Font 对象绘制指定的文本字符串。

public void DrawString(
	string s,
	Font font,
	Brush brush,
	float x,
	float y
)


MSDN上的实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public  void  DrawStringFloat(PaintEventArgs e)
 
{
 
// Create string to draw
 
. String drawString =  "Sample Text" // Create font and brush.
 
Font drawFont =  new  Font( "Arial" , 16);
 
  SolidBrush drawBrush =  new  SolidBrush(Color.Black); // Create point for upper-left corner of drawing.
 
  float  x = 150.0F;  float  y = 150.0F; // Draw string to screen.
 
e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y);
 
}
 
  

 

应用的实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
          private  void  Myprintpage1(Graphics formGraphics,  int  w,  int  h)
        {
 
            Pen myPen =  new  Pen(Color.FromArgb(255, Color.Black), 1.0F);
            Font MyFont1 =  new  Font( "宋体" , 12, FontStyle.Bold);
            Font MyFont2 =  new  Font( "宋体" , 10, FontStyle.Bold);
 
            formGraphics.TranslateTransform(100.0F, 50.0F);
            //画表格横线
 
 
            //画表格竖线
 
            for  ( int  i = 200; i < 360; i += 50)
            {
                formGraphics.DrawLine(myPen,  new  Point(0, i),  new  Point(600, i));
                formGraphics.DrawLine(myPen,)
            }
 
            for  ( int  i = 0; i < 750; i += 150)
            {
                formGraphics.DrawLine(myPen,  new  Point(i, 200),  new  Point(i, 350));
            }
 
            //画表格斜线
            formGraphics.DrawLine(myPen,  new  Point(0, 200),  new  Point(150, 250));
            //formGraphics.DrawLine(myPen, new Point(150, 125), new Point(300, 125));
            //formGraphics.DrawLine(myPen, new Point(150, 175), new Point(300, 175));
            //写字  
            formGraphics.DrawString( "    ---数据报表---" new  Font( "宋体" , 20, FontStyle.Bold), Brushes.DimGray, 100, -10);
 
            formGraphics.DrawString( "试验日期(Date)      :_______________" , MyFont1, Brushes.DimGray, 0, 50);
            formGraphics.DrawString( "操作人员(Operator):_______________" , MyFont1, Brushes.DimGray, 0, 75);
 
            formGraphics.DrawString( "试件类型(Parts Type):_______________" , MyFont1, Brushes.DimGray, 330, 50);
            formGraphics.DrawString( "试件编号(Parts No):_______________" , MyFont1, Brushes.DimGray, 330, 75);
 
            formGraphics.DrawString( "上号(UP):_______________" , MyFont1, Brushes.DimGray, 0, 100);
            formGraphics.DrawString( "下号(DOWN):_______________" , MyFont1, Brushes.DimGray, 330, 100);
 
          
            formGraphics.DrawString( "电压" , MyFont1, Brushes.DimGray, 190, 220);
 
            //formGraphics.DrawString("  (Forward Speed)", MyFont2, Brushes.DimGray, 300, 110);
            formGraphics.DrawString( "电流" , MyFont1, Brushes.DimGray, 340, 220);
 
            // formGraphics.DrawString("  (Backward Speed)", MyFont2, Brushes.DimGray, 455, 110);
            formGraphics.DrawString( "备用" , MyFont1, Brushes.DimGray, 490, 220);
 
            formGraphics.DrawString( "试验数据(Date)" , MyFont1, Brushes.DimGray, 0, 270);
            formGraphics.DrawString( "数据单位(Unit)" , MyFont1, Brushes.DimGray, 0, 320);
 
            formGraphics.DrawString( "操作人员(Operator):_______________   检验者(Checker):_______________" , MyFont1, Brushes.DimGray, 0, 970);
 
            formGraphics.DrawString(DateTime.Now.ToString( "yyyy/MM/dd" ), MyFont1, Brushes.DimGray, 180, 50);
            formGraphics.DrawString(global.temstr[0], MyFont1, Brushes.DimGray, 180, 75);
            formGraphics.DrawString(global.temstr[2], MyFont1, Brushes.DimGray, 510, 50);
            formGraphics.DrawString(global.temstr[1], MyFont1, Brushes.DimGray, 510, 75);
 
            formGraphics.DrawString(global.temstr[3], MyFont1, Brushes.DimGray, 180, 100);
            formGraphics.DrawString(global.temstr[4], MyFont1, Brushes.DimGray, 500, 100);
 
            formGraphics.DrawString( " " , MyFont1, Brushes.DimGray, 190, 270); //
            formGraphics.DrawString( " " , MyFont1, Brushes.DimGray, 340, 270); //
            formGraphics.DrawString( " " , MyFont1, Brushes.DimGray, 490, 270);
 
 
 
            formGraphics.DrawString( "V" , MyFont1, Brushes.DimGray, 190, 320);
 
            formGraphics.DrawString( "A" , MyFont1, Brushes.DimGray, 340, 320);
 
            formGraphics.DrawString( " " , MyFont1, Brushes.DimGray, 490, 320);
 
}
 
  

 

https://img-blog.csdn.net/20140716170343149?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRmlyZTg3MDkyM2NoZW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast

 

 


没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。
分类:  C#、WebService



    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/5826716.html,如需转载请自行联系原作者



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,绘制文字的方法有多种。下面是一些常用的方法: 1. 使用Graphics对象的DrawString方法绘制文字。这是最常用的方法,可以在任何继承自Control的控件中使用。 ``` using System.Drawing; Graphics g = this.CreateGraphics(); // 获取控件的Graphics对象 g.DrawString("Hello World", new Font("Arial", 12), Brushes.Black, new PointF(10, 10)); // 绘制文字 ``` 2. 使用TextRenderer类绘制文字。这个类提供了一些高级的文字渲染功能。 ``` using System.Windows.Forms; using System.Drawing; TextRenderer.DrawText(this.CreateGraphics(), "Hello World", new Font("Arial", 12), new Point(10, 10), Color.Black); ``` 3. 使用Bitmap对象绘制文字。这个方法可以将文字绘制到一个位图中,然后再将位图显示在控件上。 ``` using System.Drawing; Bitmap bmp = new Bitmap(this.Width, this.Height); Graphics g = Graphics.FromImage(bmp); g.DrawString("Hello World", new Font("Arial", 12), Brushes.Black, new PointF(10, 10)); this.BackgroundImage = bmp; ``` 4. 使用PrintDocument对象绘制文字。这个方法可以将文字绘制到一个打印文档中。 ``` using System.Drawing.Printing; private void btnPrint_Click(object sender, EventArgs e) { PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(PrintPage); pd.Print(); } private void PrintPage(object sender, PrintPageEventArgs e) { Graphics g = e.Graphics; g.DrawString("Hello World", new Font("Arial", 12), Brushes.Black, new PointF(10, 10)); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值