private Size UpdateMessageSizing()
{
// Update size of the message label but with a maximum width
using (Graphics g = CreateGraphics())
{
// Find size of the label when it has a maximum length of 400
_messageText.UpdateFont();
Size messageSize = g.MeasureString(_text, _messageText.Font, 400).ToSize();
// Work out DPI adjustment factor
float factorX = g.DpiX > 96 ? (1.0f * g.DpiX / 96) : 1.0f;
float factorY = g.DpiY > 96 ? (1.0f * g.DpiY / 96) : 1.0f;
messageSize.Width = (int)((float)messageSize.Width * factorX);
messageSize.Height = (int)((float)messageSize.Height * factorY);
// Always add on ad extra 5 pixels as sometimes the measure size does not draw the last
// character it contains, this ensures there is always definitely enough space for it all
messageSize.Width += 5;
_messageText.Size = messageSize;
}
// Resize panel containing the message text
Padding panelMessagePadding = _panelMessageText.Padding;
_panelMessageText.Width = _messageText.Size.Width + panelMessagePadding.Horizontal;
_panelMessageText.Height = _messageText.Size.Height + panelMessagePadding.Vertical;
// Find size of icon area plus the text area added together
Size panelSize = _panelMessageText.Size;
if (_messageIcon.Image != null)
{
panelSize.Width += _panelMessageIcon.Width;
panelSize.Height = Math.Max(panelSize.Height, _panelMessageIcon.Height);
}
// Enforce a minimum size for the message area
panelSize = new Size(Math.Max(_panelMessage.Size.Width, panelSize.Width),
Math.Max(_panelMessage.Size.Height, panelSize.Height));
// Note that the width will be ignored in this update, but that is fine as
// it will be adjusted by the UpdateSizing method that is the caller.
_panelMessage.Size = panelSize;
return panelSize;
}