winForm帮助信息

在项目开发中,由于没更新一块内容,帮助文档都得及时更新,否则将导致最新的应用程序与帮助文档不一致。为此,写了一个帮助页面,这样就可以实时看到帮助信息。

首先,新建了一个帮助信息类,代码如下:

  /// <summary>
/// 帮助信息类
/// </summary>
public class HelpInfo
{
#region Properties /// <summary>
/// 矩形框
/// </summary>
public Rectangle HelpRectangle { get; set; }
/// <summary>
/// 矩形框画笔
/// </summary>
public Pen HelpRectanglePen { get; set; }
/// <summary>
/// 描述信息
/// </summary>
public List<TextStyle> HelpExplainStyle { get; set; }
/// <summary>
/// 帮助图片
/// </summary>
public List<ImageStyle> HelpImage { get; set; }
/// <summary>
/// 帮助指导线
/// </summary>
public List<HelpFinger> HelpFinger { get; set; }
#endregion #region ctor public HelpInfo()
{ }
#endregion
} /// <summary>
/// 画指导线类
/// </summary>
public class HelpFinger
{
/// <summary>
/// 开始位置
/// </summary>
public Point Start { get; private set; }
/// <summary>
/// 结束位置
/// </summary>
public Point End { get; private set; }
/// <summary>
/// 线条宽度
/// </summary>
public float Width { get; private set; }
/// <summary>
/// 线的颜色
/// </summary>
public Color PenColor { get; private set; }
/// <summary>
/// 画带箭头的线
/// </summary>
/// <param name="start">开始位置</param>
/// <param name="end">结束位置</param>
/// <param name="width">线的宽度</param>
public HelpFinger(Point start, Point end, float width = 1f)
{
this.Start = start;
this.End = end;
this.Width = width;
this.PenColor = Color.YellowGreen;
}
/// <summary>
/// 画带箭头的线
/// </summary>
/// <param name="start">开始位置</param>
/// <param name="end">结束位置</param>
/// <param name="color">线的颜色</param>
/// <param name="width">线的宽度</param>
public HelpFinger(Point start, Point end, Color color, float width = 1f)
{
this.Start = start;
this.End = end;
this.PenColor = color;
this.Width = width;
}
} /// <summary>
/// 帮助图片样式设置
/// </summary>
public class ImageStyle
{
/// <summary>
/// 图片位置
/// </summary>
public Point ImagePosition { get; set; }
/// <summary>
/// 要显示的图片
/// </summary>
public Image Image { get; set; }
/// <summary>
/// 帮助图片
/// </summary>
/// <param name="image">要显示的图片</param>
/// <param name="point">图片位置</param>
public ImageStyle(Image image=null,Point point=new Point() )
{
this.Image = image;
this.ImagePosition = point;
}
} /// <summary>
/// 描述信息文本类
/// </summary>
public class TextStyle
{
#region Properties /// <summary>
/// 文本位置
/// </summary>
public Point TextExplainStartPosition { get; set; }
/// <summary>
/// 文本格式
/// </summary>
public Font TextFont { get; set; }
/// <summary>
/// 文本的颜色和纹理
/// </summary>
public SolidBrush TextSolidBrush { get; set; }
/// <summary>
/// 描述文本
/// </summary>
public string TextExplain { get; set; }
/// <summary>
/// 指定应用于所绘制文本的格式化属性(如行距和对齐方式)
/// </summary>
public StringFormat TextExplainFormat { get; set; }
#endregion #region ctor public TextStyle()
{ }
/// <summary>
/// 描述信息
/// </summary>
/// <param name="textExplain">描述文本</param>
/// <param name="textStartPosition">文本位置</param>
/// <param name="textFont">文本格式,包括字体、字号和字形属性</param>
/// <param name="textSolidBrush">定义单色画笔</param>
/// <param name="textExplainFormat">封装文本布局信息</param>
public TextStyle(string textExplain, Point textStartPosition, Font textFont = null, SolidBrush textSolidBrush = null, StringFormat textExplainFormat=null)
{
this.TextExplain = textExplain;
this.TextExplainStartPosition = textStartPosition;
this.TextFont = textFont;
this.TextSolidBrush = textSolidBrush;
this.TextExplainFormat = textExplainFormat;
}
#endregion
}

帮助页面代码如下:

 /// <summary>
/// 帮助图片
/// </summary>
private Image helpImage = null;
/// <summary>
/// 存储不透明窗体及关闭按钮窗体
/// </summary>
private List<Form> listForm = new List<Form>(); public HelpForm(Control control, List<HelpInfo> helpInfo)
{
InitializeComponent();
if (control == null)
{
MessageBox.Show("没有获取到相关窗体信息!");
return;
}
this.Width = control.Width;
this.Height = control.Height;
this.Location = control.Location;
this.StartPosition = FormStartPosition.Manual;
helpImage = new Bitmap(control.Width, control.Height); //创建与参数control同等大小的空白图片
this.picHelpImage.BackgroundImage = helpImage;
this.picHelpImage.BackgroundImageLayout = ImageLayout.Center;
//helpImage添加帮助信息 + private void PictureHandle(List<HelpInfo> helpInfo)
PictureHandle(helpInfo);
this.BackColor = Color.Black;
this.Opacity = 0.7;
} /// <summary>
/// 处理图片 添加帮助信息
/// </summary>
/// <param name="helpInfo">帮助信息</param>
private void PictureHandle(List<HelpInfo> helpInfo)
{
if (helpInfo != null)
{
foreach (var currentInfo in helpInfo)
{
//helpImage添加说明信息
helpImage = HelpInfoHandle(currentInfo.HelpRectangle, currentInfo.HelpRectanglePen, currentInfo.HelpExplainStyle, currentInfo.HelpImage, currentInfo.HelpFinger);
}
}
} /// <summary>
/// 将帮助信息添加到图片上
/// </summary>
/// <param name="helpRectangle">矩形框</param>
/// <param name="helpRectanglePen">矩形框画笔</param>
/// <param name="helpExplainStyle">帮助信息</param>
/// <param name="imageStyles">帮助图片</param>
/// <param name="fingers">帮助指导线</param>
/// <returns>处理后的图片</returns>
private Image HelpInfoHandle(Rectangle helpRectangle, Pen helpRectanglePen, List<TextStyle> helpExplainStyles, List<ImageStyle> imageStyles, List<HelpFinger> fingers)
{
if (helpImage != null)
{
Graphics graphice = Graphics.FromImage(helpImage);
//添加说明性图片
if (imageStyles != null)
{
AddHelpPicture(imageStyles);
}
bool noneRectangle = helpRectangle.X.Equals() && helpRectangle.X.Equals(helpRectangle.Y) && helpRectangle.Width.Equals() && helpRectangle.Width.Equals(helpRectangle.Height);
if (!noneRectangle)
{
helpRectanglePen = helpRectanglePen ?? new Pen(Color.Orange);
DrawRoundRectangle(graphice, helpRectanglePen, helpRectangle, );
helpRectanglePen.Dispose();
}
//画带箭头的线,只对不封闭曲线有用
if (fingers != null)
{
foreach (var finger in fingers)
{
if (finger != null)
{
Pen pen = new Pen(finger.PenColor);
pen.Width = finger.Width;
pen.DashStyle = DashStyle.Solid; //实线
pen.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头
graphice.DrawLine(pen, finger.Start, finger.End);
}
}
}
//将图片添加说明性文字
if (helpExplainStyles != null)
{
foreach (var helpExplainStyle in helpExplainStyles)
{
if (helpExplainStyle != null)
{
helpExplainStyle.TextSolidBrush = helpExplainStyle.TextSolidBrush ?? new SolidBrush(Color.White);
helpExplainStyle.TextFont = helpExplainStyle.TextFont ?? new Font("宋体", );
graphice.DrawString(helpExplainStyle.TextExplain, helpExplainStyle.TextFont, helpExplainStyle.TextSolidBrush,
helpExplainStyle.TextExplainStartPosition, helpExplainStyle.TextExplainFormat);
}
}
}
graphice.Dispose();
}
return helpImage;
} /// <summary>
/// 添加帮助图片信息
/// </summary>
/// <param name="imageStyles">帮助图片集合</param>
private void AddHelpPicture(List<ImageStyle> imageStyles)
{
foreach (var imageStyle in imageStyles)
{
if (imageStyle != null && imageStyle.Image != null)
{
Form picForm = new Form();
picForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
picForm.MinimumSize = new System.Drawing.Size(,);
picForm.Width = imageStyle.Image.Width;
picForm.Height = imageStyle.Image.Height;
picForm.BackgroundImage = imageStyle.Image;
picForm.BackgroundImageLayout = ImageLayout.Center;
picForm.Location = imageStyle.ImagePosition;
picForm.StartPosition = FormStartPosition.Manual;
picForm.TopMost = true;
picForm.Click += new EventHandler(picHelpImage_Click);
listForm.Add(picForm);
picForm.Show();
}
}
} /// <summary>
/// 画圆角矩形
/// </summary>
/// <param name="graphics">画布</param>
/// <param name="pen">画笔</param>
/// <param name="rectangle">矩形</param>
/// <param name="cornerRadius">角度</param>
private static void DrawRoundRectangle(Graphics graphics, Pen pen, Rectangle rectangle, int cornerRadius)
{
using (GraphicsPath path = CreateRoundedRectanglePath(rectangle, cornerRadius))
{
try
{
graphics.DrawPath(pen, path);
}
catch (ArgumentException argumentException)
{
throw new Exception(argumentException.Message);
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
}
} /// <summary>
/// 创建圆角矩形框
/// </summary>
/// <param name="rectangle">矩形框</param>
/// <param name="cornerRadius">角度</param>
/// <returns>圆角矩形框</returns>
internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rectangle, int cornerRadius)
{
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rectangle.X, rectangle.Y, cornerRadius * , cornerRadius * , , );
roundedRect.AddLine(rectangle.X + cornerRadius, rectangle.Y, rectangle.Right - cornerRadius * , rectangle.Y);
roundedRect.AddArc(rectangle.X + rectangle.Width - cornerRadius * , rectangle.Y, cornerRadius * , cornerRadius * , , );
roundedRect.AddLine(rectangle.Right, rectangle.Y + cornerRadius * , rectangle.Right, rectangle.Y + rectangle.Height - cornerRadius * );
roundedRect.AddArc(rectangle.X + rectangle.Width - cornerRadius * , rectangle.Y + rectangle.Height - cornerRadius * , cornerRadius * , cornerRadius * , , );
roundedRect.AddLine(rectangle.Right - cornerRadius * , rectangle.Bottom, rectangle.X + cornerRadius * , rectangle.Bottom);
roundedRect.AddArc(rectangle.X, rectangle.Bottom - cornerRadius * , cornerRadius * , cornerRadius * , , );
roundedRect.AddLine(rectangle.X, rectangle.Bottom - cornerRadius * , rectangle.X, rectangle.Y + cornerRadius * );
roundedRect.CloseFigure();
return roundedRect;
} /// <summary>
/// 单击帮助窗体时关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void picHelpImage_Click(object sender, EventArgs e)
{
if (listForm != null && listForm.Count > )
{
foreach (Form form in listForm)
{
form.Close();
}
listForm.Clear();
}
this.Close();
}

这样,在其他要添加帮助信息的页面中实例化该窗体后就可以看到帮助信息

上一篇:Hibernate- 动态实例查询


下一篇:Python学习(6)循环语句