0

I'm trying to draw a custom button in compact framework, in the 'OnPaint' function I do something like this:

protected override void OnPaint(PaintEventArgs e)
{
    System.Drawing.Imaging.ImageAttributes a = new    
    System.Drawing.Imaging.ImageAttributes();
    e.Graphics.DrawImage(pictureBox1.Image, new Rectangle(0, 0, Width, Height),  
                         0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, a);

    Brush b = new SolidBrush(Color.Black);
    e.Graphics.DrawString(Text, Font, b, 0, 0, ( 
                     new StringFormat(StringFormatFlags.NoWrap)));
}

With this code the font draws in the top left corner of the control (as expected).

My question is, how can I get it to draw centre on the control?

4

2 回答 2

2

Figured it out!

For those interested,

float fontHeight = e.Graphics.MeasureString("ABC", Font).Height;
e.Graphics.DrawString("ABC", Font, b, new RectangleF(0, Height / 2.0f - fontHeight/2.0f, Width, Height), format);

Did it for me.

于 2012-07-27T15:19:49.703 回答
1

I see you've already got your answer, so I'm not even going to test to see if this works on Mobile devices.

I'm putting it out there because it's a tool I came across once and I don't see many references to it:

/// <summary>
/// [static method] Generates a StringFormat object based on the ContentAlignment object
/// </summary>
/// <param name="ca">ContentAlignment value from a System.Windows.Label object</param>
/// <returns>StringFormat</returns>
private static StringFormat GetStringFormatFromContentAllignment(ContentAlignment ca) {
  StringFormat format = new StringFormat();
  switch (ca) {
    case ContentAlignment.TopCenter:
      format.Alignment = StringAlignment.Near;
      format.LineAlignment = StringAlignment.Center;
      break;
    case ContentAlignment.TopLeft:
      format.Alignment = StringAlignment.Near;
      format.LineAlignment = StringAlignment.Near;
      break;
    case ContentAlignment.TopRight:
      format.Alignment = StringAlignment.Near;
      format.LineAlignment = StringAlignment.Far;
      break;
    case ContentAlignment.MiddleCenter:
      format.Alignment = StringAlignment.Center;
      format.LineAlignment = StringAlignment.Center;
      break;
    case ContentAlignment.MiddleLeft:
      format.Alignment = StringAlignment.Center;
      format.LineAlignment = StringAlignment.Near;
      break;
    case ContentAlignment.MiddleRight:
      format.Alignment = StringAlignment.Center;
      format.LineAlignment = StringAlignment.Far;
      break;
    case ContentAlignment.BottomCenter:
      format.Alignment = StringAlignment.Far;
      format.LineAlignment = StringAlignment.Center;
      break;
    case ContentAlignment.BottomLeft:
      format.Alignment = StringAlignment.Far;
      format.LineAlignment = StringAlignment.Near;
      break;
    case ContentAlignment.BottomRight:
      format.Alignment = StringAlignment.Far;
      format.LineAlignment = StringAlignment.Far;
      break;
  }
  return format;
}
于 2012-07-27T18:14:08.897 回答