请问各位,在使用DrawString在picturebox上画画时,如何让在靠右边缘上的整行文字进行右对齐?
希望能得到函数,谢谢~~~

解决方案 »

  1.   

    Graphics.DrawString Method (String, Font, Brush, Single, Single, StringFormat)
    http://msdn.microsoft.com/en-us/library/aa327576(v=VS.71).aspx
      

  2.   

    StringFormat MembersAlignment    Gets or sets text alignment information.Far          Specifies that text is aligned far from the origin position of the layout rectangle. In a left-to-right layout, the far position is right. In a right-to-left layout, the far position is left.
      

  3.   

    //用MeasureString计算字符串宽度后,再用容器宽度减掉字符串宽度private void button1_Click(object sender, EventArgs e)
            {
                Graphics g = pictureBox1.CreateGraphics();            string str = "这段文字右对齐";            Font font=this.Font;           SizeF size = g.MeasureString(str, font);           g.DrawString(str, font, new SolidBrush(Color.Black), pictureBox1.Width - size.Width, 10);            g.Dispose();
            }
      

  4.   

    如果是多行文字,而且要靠右对齐,那么最好是能够计算出字符串在界面上所占的空间位置(这里是指字符串宽度,以像素为单位)。获取字符串的界面宽度,可以用 Graphics 对象的 MeasureString() 方法,这样,自己可以字符串宽度和 PictureBox 控件的宽度来决定你要画出的每一个字符串的起始位置了。
      

  5.   

    private void button5_Click(object sender, EventArgs e)
    {
        using (Graphics g = this.CreateGraphics())
        {
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Far;
            g.DrawString("窗体[右对齐]显示文本", this.Font, Brushes.Black, ClientRectangle, format);
            format.Alignment = StringAlignment.Near;
            g.DrawString("窗体[左对齐]显示文本", this.Font, Brushes.Black, ClientRectangle, format);
            format.Alignment = StringAlignment.Center;
            g.DrawString("窗体[中置]对齐显示文本", this.Font, Brushes.Black, ClientRectangle, format);
        }
    }
      

  6.   

    private void button5_Click(object sender, EventArgs e)
    {
        using (Graphics g = this.CreateGraphics())
        {
            StringFormat format = new StringFormat();
            format.LineAlignment = StringAlignment.Center;//垂直方向水平中置
            format.LineAlignment = StringAlignment.Near;//顶部对齐
            format.LineAlignment = StringAlignment.Far;//靠近底部
            format.Alignment = StringAlignment.Far;
            g.DrawString("窗体[右对齐]显示文本", this.Font, Brushes.Black, ClientRectangle, format);
            format.Alignment = StringAlignment.Near;
            g.DrawString("窗体[左对齐]显示文本", this.Font, Brushes.Black, ClientRectangle, format);
            format.Alignment = StringAlignment.Center;
            g.DrawString("窗体[中置]对齐显示文本", this.Font, Brushes.Black, ClientRectangle, format);
        }
    }
      

  7.   

    虽然已经结贴。但还是给你不:点到为止。写完整。日后有人搜索到这个帖子,也好能看到正统的GDI+操作方法。
      

  8.   

    哈哈,十分感谢啊!
    本人但凡一看到一个解决问题的方法就速度结贴了,呵呵,sorry~~~
      

  9.   

    一定要用button控件吗?我想打开窗口的时候就能看到文字已经加载在picturebox上了,而不是要通过单击button来实现文字的加载?好心人教教我