JD2022-TU1/main/tools/framework/JD.MessageService/InformationBox.cs

265 lines
7.8 KiB
C#

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace JD.MessageService
{
/// <summary>
/// Information Box.
/// </summary>
[ToolboxItem("Information Box")]
public class InformationBox : Control
{
#region Constants
private const int SpacingBetweenIconAndText = 8;
#endregion
#region Variables
private SystemIconType m_iconType = SystemIconType.Information;
private int m_newLineSpacing = 1;
private string m_text2 = "";
#endregion
#region Constructor
/// <summary>
/// Constructor.
/// </summary>
public InformationBox()
{
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
}
#endregion
#region Properties
/// <summary>
/// Overriden.
/// </summary>
protected override Size DefaultSize
{
get
{
return new Size(300, 80);
}
}
/// <summary>
/// Icon.
/// </summary>
[Description("Indicates the type of icon to show next to the information box."), Category("Appearance"), DefaultValue(typeof(SystemIconType), "Information")]
public SystemIconType Icon
{
get
{
return m_iconType;
}
set
{
m_iconType = value;
Invalidate();
}
}
/// <summary>
/// A second paragraph of text to display.
/// </summary>
[DefaultValue(""), Localizable(true), Description("A second paragraph of text to display."), Category("Appearance")]
public string Text2
{
get
{
return m_text2;
}
set
{
m_text2 = value;
Invalidate();
}
}
private Icon WorkingIcon
{
get
{
switch (Icon)
{
case SystemIconType.Application:
return SystemIcons.Application;
case SystemIconType.Error:
return SystemIcons.Error;
case SystemIconType.Question:
return SystemIcons.Question;
case SystemIconType.Warning:
return SystemIcons.Warning;
}
return SystemIcons.Information;
}
}
/// <summary>
/// Uses the minimum height to fit the text and the icon and the padding.
/// </summary>
public int MinimumHeight
{
get { return WorkingIcon.Height + Padding.Top + Padding.Bottom;}
}
/// <summary>
/// Uses the minimum height to fit the text and the icon and the padding.
/// </summary>
public int MinimumWidth
{
get { return Padding.Left + WorkingIcon.Width + SpacingBetweenIconAndText + Padding.Right; }
}
/// <summary>
/// Maximum width to fit the text and the icon and the padding in the principal screen.
/// </summary>
public int MaximunWidth
{
get
{
Rectangle screen = Screen.PrimaryScreen.WorkingArea;
return screen.Width - MinimumWidth - Location.X * 2;
}
}
/// <summary>
/// Maximum Height to fit the text in the principal screen.
/// </summary>
public int MaximunHeight
{
get
{
Rectangle screen = Screen.PrimaryScreen.WorkingArea;
return screen.Height - Location.Y;
}
}
#endregion
#region Methods
/// <summary>
/// Overriden.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Icon icon = WorkingIcon;
if (RightToLeft == RightToLeft.Yes)
{
e.Graphics.DrawIcon(icon, ClientRectangle.Right - 0x20 + Padding.Right, ClientRectangle.Top + Padding.Top);
}
else
{
e.Graphics.DrawIcon(icon, ClientRectangle.Left + Padding.Left, ClientRectangle.Top + Padding.Top);
}
Rectangle layoutRectangle = base.ClientRectangle;
// enable padding functionality
layoutRectangle.Y += Padding.Top;
layoutRectangle.Height -= Padding.Top + Padding.Bottom;
if (RightToLeft != RightToLeft.Yes)
{
layoutRectangle.X += MinimumWidth - Padding.Right;
}
layoutRectangle.Width -= MinimumWidth;
// Always draw the text2 at the bottom of the client area
// Measure the text2 height
SizeF sizeText2F = e.Graphics.MeasureString(GetText2(), Font, MaximunWidth);
// Adjust both layout rectangle
Rectangle layoutRectangleText2 = layoutRectangle;
layoutRectangleText2.Y = layoutRectangle.Y + layoutRectangle.Height - (int)System.Math.Ceiling(sizeText2F.Height);
layoutRectangleText2.Height = (int)System.Math.Ceiling(sizeText2F.Height);
layoutRectangle.Height -= (int)System.Math.Floor(sizeText2F.Height);
using (StringFormat format = new StringFormat(StringFormat.GenericDefault))
{
if (RightToLeft == RightToLeft.Yes)
{
format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
e.Graphics.DrawString(Text, Font, SystemBrushes.ControlText, layoutRectangle, format);
e.Graphics.DrawString(GetText2(), Font, SystemBrushes.ControlText, layoutRectangleText2, format);
}
}
private string GetText2()
{
if (!string.IsNullOrEmpty(Text2))
{
string lineSpacing = null;
for (int i = 0; i < m_newLineSpacing; i++)
{
lineSpacing += Environment.NewLine;
}
return lineSpacing + Text2;
}
return "";
}
/// <summary>
/// Overriden.
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
Invalidate();
}
/// <summary>
/// Uses the information color as the back color.
/// </summary>
public void UseInformationColor()
{
BackColor = SystemColors.Info;
}
/// <summary>
/// Return the minimum size to fit the text and text2
/// </summary>
public Size GetMiniumSize(Graphics g)
{
SizeF sizeTextF = g.MeasureString(Text, Font, MaximunWidth);
SizeF sizeText2F = g.MeasureString(GetText2(), Font, MaximunWidth);
int width = (int) System.Math.Ceiling(sizeTextF.Width > sizeText2F.Width ? sizeTextF.Width : sizeText2F.Width);
Size clientSize = new Size(width, (int)System.Math.Ceiling(sizeTextF.Height) + (int)System.Math.Ceiling(sizeText2F.Height));
clientSize.Height += Padding.Top + Padding.Bottom;
clientSize.Height = clientSize.Height < MinimumHeight ? MinimumHeight : clientSize.Height;
clientSize.Height = clientSize.Height > MaximunHeight ? MaximunHeight : clientSize.Height;
clientSize.Width += MinimumWidth;
return clientSize;
}
#endregion
}
}