Graphics.DrawString 方法

MSDN上的解释:

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

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

MSDN上的实例:

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);

}

应用的实例:

          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);

 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: graphics.drawstring是一个在图形界面中绘制字符串的方法。它可以在指定的位置绘制指定的字符串,并可以设置字体、颜色等属性。在编写图形界面程序时,使用graphics.drawstring可以方便地在界面上显示文本信息。 ### 回答2: graphics.drawstring是Java中用于绘制文本的方法之一。它可以在图形对象上绘制指定的字符串。 首先,需要创建一个Graphics对象,可以通过调用Component类中的getGraphics()方法获得。然后,使用该对象的drawString()方法来绘制文本。drawString()方法有多个重载形式,可以根据需要选择适合的方法drawString()方法的参数包括字符串内容、起始位置的x坐标和y坐标。通过调整这两个坐标的值,可以控制文本的绘制位置。另外,可以使用setFont()方法设置文本的字体、setSize()方法设置文本的大小、setColor()方法设置文本的颜色等,以实现更多的自定义效果。 绘制文本时需要特别注意的是,要确保所绘制的字符串在指定位置不会超出图形对象的范围,否则可能会出现截断或者超出显示范围的问题。可以通过使用字符串的长度来计算文本所需的绘制空间,从而优化绘制效果。 此外,drawString()方法还可以用于在图形上绘制多行文本。可以使用换行符("\n")将多行文本分隔,然后使用多次调用drawString()方法分别绘制每一行,从而实现多行文本的绘制效果。 总而言之,graphics.drawString()是Java中用于绘制文本的方法,可以通过调整参数来控制文本的位置、字体、大小和颜色等,实现多种自定义效果的文本绘制功能。 ### 回答3: graphics.drawstring是Java图形库中的一个方法,用于在图形上绘制字符串。 它的基本语法如下: graphics.drawString(String str, int x, int y) 其中,str是要绘制的字符串,x和y是字符串的起始坐标。起始坐标指的是字符串的左下角位置。 该方法可以用于在图形界面中添加文字标注、标题、注释等。可以通过设置不同的字体、字号和颜色来实现不同的效果。 例如,我们可以使用如下代码在图形上绘制一个简单的字符串: ```java import javax.swing.*; import java.awt.*; public class DrawStringExample extends JFrame { public DrawStringExample() { setSize(400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); } public void paint(Graphics g) { super.paint(g); g.setFont(new Font("Arial", Font.BOLD, 20)); g.drawString("Hello, World!", 100, 100); } public static void main(String[] args) { DrawStringExample example = new DrawStringExample(); example.setVisible(true); } } ``` 在上述例子中,我们继承了JFrame类,并重写了其paint方法,在paint方法中调用了drawString方法来绘制字符串。使用setFont方法设置了字体、字号和样式,使用drawString方法绘制了字符串"Hello, World!",起始坐标为(100, 100)。 总结起来,graphics.drawstring是Java图形库中的一个方法,用于在图形上绘制字符串。通过设置不同的字体、字号和颜色,我们可以创建不同样式的文字标注、标题等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值