protected void OnPaintTitleBar(Graphics g)
{
int y = 0;
// work out where the top of the titleBar actually is
if (this.HeaderHeight > this.TitleBarHeight)
{
y = this.HeaderHeight - this.TitleBarHeight;
}
// draw the titlebar image if we have one
if (this.TitleImage != null)
{
int x = 0;
//int y = 0;
if (this.RightToLeft == RightToLeft.Yes)
{
x = this.Width - this.TitleImage.Width;
}
if (this.Enabled)
{
g.DrawImage(this.TitleImage, x, 0);
}
else
{
ControlPaint.DrawImageDisabled(g, TitleImage, x, 0, this.TitleBackColor);
}
}
// get which collapse/expand arrow we should draw
Image arrowImage = this.ArrowImage;
// get the titlebar's border and padding
Border border = this.TitleBorder;
Padding padding = this.TitlePadding;
// draw the arrow if we have one
if (arrowImage != null)
{
// work out where to position the arrow
int x = this.Width - arrowImage.Width - border.Right - padding.Right;
y += border.Top + padding.Top;
if (this.RightToLeft == RightToLeft.Yes)
{
x = border.Right + padding.Right;
}
// draw it...
if (this.Enabled)
{
g.DrawImage(arrowImage, x, y);
}
else
{
ControlPaint.DrawImageDisabled(g, arrowImage, x, y, this.TitleBackColor);
}
}
// check if we have any text to draw in the titlebar
if (this.Text.Length > 0)
{
// a rectangle that will contain our text
Rectangle rect = new Rectangle();
// work out the x coordinate
if (this.TitleImage == null)
{
rect.X = border.Left + padding.Left;
}
else
{
rect.X = this.TitleImage.Width + border.Left;
}
// work out the y coordinate
ContentAlignment alignment = this.TitleAlignment;
switch (alignment)
{
case ContentAlignment.MiddleLeft:
case ContentAlignment.MiddleCenter:
case ContentAlignment.MiddleRight: rect.Y = ((this.HeaderHeight - this.TitleFont.Height) / 2) + ((this.HeaderHeight - this.TitleBarHeight) / 2) + border.Top + padding.Top;
break;
case ContentAlignment.TopLeft:
case ContentAlignment.TopCenter:
case ContentAlignment.TopRight: rect.Y = (this.HeaderHeight - this.TitleBarHeight) + border.Top + padding.Top;
break;
case ContentAlignment.BottomLeft:
case ContentAlignment.BottomCenter:
case ContentAlignment.BottomRight: rect.Y = this.HeaderHeight - this.TitleFont.Height;
break;
}
// the height of the rectangle
rect.Height = this.TitleFont.Height;
// make sure the text stays inside the header
if (rect.Bottom > this.HeaderHeight)
{
rect.Y -= rect.Bottom - this.HeaderHeight;
}
// work out how wide the rectangle should be
if (arrowImage != null)
{
rect.Width = this.Width - arrowImage.Width - border.Right - padding.Right - rect.X;
}
else
{
rect.Width = this.Width - border.Right - padding.Right - rect.X;
}
// don't wrap the string, and use an ellipsis if
// the string is too big to fit the rectangle
StringFormat sf = new StringFormat();
sf.FormatFlags = StringFormatFlags.NoWrap;
sf.Trimming = StringTrimming.EllipsisCharacter;
// should the string be aligned to the left/center/right
switch (alignment)
{
case ContentAlignment.MiddleLeft:
case ContentAlignment.TopLeft:
case ContentAlignment.BottomLeft: sf.Alignment = StringAlignment.Near;
break;
case ContentAlignment.MiddleCenter:
case ContentAlignment.TopCenter:
case ContentAlignment.BottomCenter: sf.Alignment = StringAlignment.Center;
break;
case ContentAlignment.MiddleRight:
case ContentAlignment.TopRight:
case ContentAlignment.BottomRight: sf.Alignment = StringAlignment.Far;
break;
}
if (this.RightToLeft == RightToLeft.Yes)
{
sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
if (this.TitleImage == null)
{
rect.X = this.Width - rect.Width - border.Left - padding.Left;
}
else
{
rect.X = this.Width - rect.Width - this.TitleImage.Width - border.Left;
}
}
// draw the text
using (SolidBrush brush = new SolidBrush(this.TitleColor))
{
//g.DrawString(this.Text, this.TitleFont, brush, rect, sf);
if (this.Enabled)
{
g.DrawString(this.Text, this.TitleFont, brush, rect, sf);
}
else
{
ControlPaint.DrawStringDisabled(g, this.Text, this.TitleFont, SystemColors.ControlLightLight, rect, sf);
}
}
}
// check if windows will let us show a focus rectangle
// if we have focus
if (this.Focused && base.ShowFocusCues)
{
if (this.ShowFocusCues)
{
// for some reason, if CanCollapse is false the focus rectangle
// will be drawn 2 pixels higher than it should be, so move it down
if (!this.CanCollapse)
{
y += 2;
}
ControlPaint.DrawFocusRectangle(g, new Rectangle(2, y, this.Width - 4, this.TitleBarHeight - 3));
}
}
}