I'm writing my own library of controls. I'm working on the check box control and one of the final things that I need help on is drawing the check box in relation to the text. I have a ContentAlignment property that can change where the text appears in the control, but I'm not sure how to adjust the check box in relation to that because I don't know how to get the location of the text in the control.
protected override void OnPaint(PaintEventArgs e)
{
Graphics canvas = e.Graphics;
canvas.SmoothingMode = SmoothingMode.HighQuality;
int x = 0;
int y = (int)(canvas.MeasureString(this.Text, this.Font).Height - 12) / 2;
switch (this.Style)
{
case E.CheckBoxStyle.Check:
canvas.DrawRectangle(new Pen(this.BorderColor), new Rectangle(x, y, 12, 12));
if (this.Checked)
canvas.DrawString("√", new Font("serif", 7f, FontStyle.Bold), Brushes.Black, 1f, 3f);
break;
case E.CheckBoxStyle.Round:
canvas.DrawEllipse(new Pen(this.BorderColor), new Rectangle(x, y, 12, 12));
if (this.Checked)
{
Rectangle region = new Rectangle(x + 1, y + 1, 10, 10);
canvas.FillEllipse(Functions.CreateGradient(region, this.FillColor, 90), region);
}
break;
case E.CheckBoxStyle.Square:
canvas.DrawRectangle(new Pen(this.BorderColor), new Rectangle(x, y, 12, 12));
if (this.Checked)
{
Rectangle region = new Rectangle(x + 1, y + 1, 10, 10);
canvas.FillRectangle(Functions.CreateGradient(region, this.FillColor, 90), region);
}
break;
}
if (!this.AutomaticSize)
{
StringFormat format = new StringFormat();
switch (this.Alignment)
{
case ContentAlignment.TopRight:
format.Alignment = StringAlignment.Far;
format.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.TopLeft:
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.TopCenter:
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.MiddleRight:
format.Alignment = StringAlignment.Far;
format.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.MiddleLeft:
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.MiddleCenter:
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.BottomRight:
format.Alignment = StringAlignment.Far;
format.LineAlignment = StringAlignment.Far;
break;
case ContentAlignment.BottomLeft:
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Far;
break;
case ContentAlignment.BottomCenter:
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Far;
break;
}
canvas.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), ClientRectangle, format);
}
else
{
canvas.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), 14f, 0f);
Size newSize = Size.Round(canvas.MeasureString(this.Text, this.Font));
this.Size = new Size(newSize.Width + 14, newSize.Height);
}
}
One solution is to take away the property completely, I suppose. But that's not ideal. Another, I thought make a rectangle that contains the check box as well as the text and to move that rectangle accordingly but I don't know how to implement that. Thanks for any help! Here's a picture that might help show the problem:
Instead of any of the excessive code used for the switch statement you can use this extension I wrote:
public static Point AlignDrawingPoint(this Graphics g, Size objectSize, Rectangle clientRectangle, ContentAlignment alignment)
{
int margin = 4;
Point center = new Point((clientRectangle.Width >> 1) - (objectSize.Width >> 1), (clientRectangle.Height >> 1) - (objectSize.Height >> 1));
int rightMargin = clientRectangle.Width - (objectSize.Width + margin);
int bottomMargin = clientRectangle.Height - (objectSize.Height + margin);
Point p = Point.Empty;
switch (alignment)
{
case ContentAlignment.TopLeft:
p = new Point(margin, margin);
break;
case ContentAlignment.TopCenter:
p = new Point(center.X, margin);
break;
case ContentAlignment.TopRight:
p = new Point(rightMargin, margin);
break;
case ContentAlignment.MiddleLeft:
p = new Point(margin, center.Y);
break;
case ContentAlignment.MiddleCenter:
p = new Point(center.X, center.Y);
break;
case ContentAlignment.MiddleRight:
p = new Point(rightMargin, center.Y);
break;
case ContentAlignment.BottomLeft:
p = new Point(margin, bottomMargin);
break;
case ContentAlignment.BottomCenter:
p = new Point(center.X, bottomMargin);
break;
case ContentAlignment.BottomRight:
p = new Point(rightMargin, bottomMargin);
break;
}
p.Offset(clientRectangle.X, clientRectangle.Y);
return p;
}
You can use this extension not just for drawing strings, you can use this extension when needing to align anything you need to draw within a rectangle.
But in your case you would do something like this:
g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), AlignDrawingPoint(g.MeasureString(this.Text, this.Font).ToSize(), this.ClientRectangle, this.TextAlign));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.