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

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

  1  /// <summary>
  2     /// 帮助信息类
  3     /// </summary>
  4     public class HelpInfo
  5     {
  6         #region Properties
  7 
  8         /// <summary>
  9         /// 矩形框
 10         /// </summary>
 11         public Rectangle HelpRectangle { get; set; }
 12         /// <summary>
 13         /// 矩形框画笔
 14         /// </summary>
 15         public Pen HelpRectanglePen { get; set; }
 16         /// <summary>
 17         /// 描述信息
 18         /// </summary>
 19         public List<TextStyle> HelpExplainStyle { get; set; }
 20         /// <summary>
 21         /// 帮助图片
 22         /// </summary>
 23         public List<ImageStyle> HelpImage { get; set; }
 24         /// <summary>
 25         /// 帮助指导线
 26         /// </summary>
 27         public List<HelpFinger> HelpFinger { get; set; }
 28         #endregion
 29 
 30         #region ctor
 31 
 32         public HelpInfo()
 33         {
 34 
 35         }
 36         #endregion
 37     }
 38 
 39     /// <summary>
 40     /// 画指导线类
 41     /// </summary>
 42     public class HelpFinger
 43     {
 44         /// <summary>
 45         /// 开始位置
 46         /// </summary>
 47         public Point Start { get; private set; }
 48         /// <summary>
 49         /// 结束位置
 50         /// </summary>
 51         public Point End { get; private set; }
 52         /// <summary>
 53         /// 线条宽度
 54         /// </summary>
 55         public float Width { get; private set; }
 56         /// <summary>
 57         /// 线的颜色
 58         /// </summary>
 59         public Color PenColor { get; private set; }
 60         /// <summary>
 61         /// 画带箭头的线
 62         /// </summary>
 63         /// <param name="start">开始位置</param>
 64         /// <param name="end">结束位置</param>
 65         /// <param name="width">线的宽度</param> 
 66         public HelpFinger(Point start, Point end, float width = 1f)
 67         {
 68             this.Start = start;
 69             this.End = end;
 70             this.Width = width;
 71             this.PenColor = Color.YellowGreen;
 72         }
 73         /// <summary>
 74         /// 画带箭头的线
 75         /// </summary>
 76         /// <param name="start">开始位置</param>
 77         /// <param name="end">结束位置</param>
 78         /// <param name="color">线的颜色</param>
 79         /// <param name="width">线的宽度</param> 
 80         public HelpFinger(Point start, Point end, Color color, float width = 1f)
 81         {
 82             this.Start = start;
 83             this.End = end; 
 84             this.PenColor = color;
 85             this.Width = width;
 86         } 
 87     }
 88 
 89     /// <summary>
 90     /// 帮助图片样式设置
 91     /// </summary>
 92     public class ImageStyle
 93     {
 94         /// <summary>
 95         /// 图片位置
 96         /// </summary>
 97         public Point ImagePosition { get; set; }
 98         /// <summary>
 99         /// 要显示的图片
100         /// </summary>
101         public Image Image { get; set; }
102         /// <summary>
103         /// 帮助图片
104         /// </summary>
105         /// <param name="image">要显示的图片</param>
106         /// <param name="point">图片位置</param>
107         public ImageStyle(Image image=null,Point point=new Point() )
108         {
109             this.Image = image;
110             this.ImagePosition = point;
111         } 
112     }
113 
114     /// <summary>
115     /// 描述信息文本类
116     /// </summary>
117     public  class TextStyle
118     {
119         #region Properties
120 
121         /// <summary>
122         /// 文本位置
123         /// </summary>
124         public Point TextExplainStartPosition { get; set; }
125         /// <summary>
126         ///  文本格式
127         /// </summary>
128         public Font TextFont { get; set; }
129         /// <summary>
130         /// 文本的颜色和纹理
131         /// </summary>
132         public SolidBrush TextSolidBrush { get; set; }
133         /// <summary>
134         /// 描述文本
135         /// </summary>
136         public string TextExplain { get; set; }
137         /// <summary>
138         /// 指定应用于所绘制文本的格式化属性(如行距和对齐方式)
139         /// </summary>
140         public StringFormat TextExplainFormat { get; set; } 
141         #endregion
142 
143         #region ctor
144 
145         public TextStyle()
146         {
147 
148         }
149         /// <summary>
150         /// 描述信息
151         /// </summary>
152         /// <param name="textExplain">描述文本</param>
153         /// <param name="textStartPosition">文本位置</param>
154         /// <param name="textFont">文本格式,包括字体、字号和字形属性</param>
155         /// <param name="textSolidBrush">定义单色画笔</param>
156         /// <param name="textExplainFormat">封装文本布局信息</param>
157         public TextStyle(string textExplain, Point textStartPosition, Font textFont = null, SolidBrush textSolidBrush = null, StringFormat textExplainFormat=null)
158         { 
159                 this.TextExplain = textExplain;
160                 this.TextExplainStartPosition = textStartPosition;
161                 this.TextFont = textFont;
162                 this.TextSolidBrush = textSolidBrush;
163                 this.TextExplainFormat = textExplainFormat; 
164         }
165         #endregion
166     }
View Code


帮助页面代码如下:

  1 /// <summary>
  2         /// 帮助图片
  3         /// </summary>
  4         private Image helpImage = null;
  5         /// <summary>
  6         /// 存储不透明窗体及关闭按钮窗体
  7         /// </summary>
  8         private List<Form> listForm = new List<Form>();
  9 
 10         public HelpForm(Control control, List<HelpInfo> helpInfo)
 11         {
 12             InitializeComponent();
 13             if (control == null)
 14             {
 15                 MessageBox.Show("没有获取到相关窗体信息!");
 16                 return;
 17             }
 18             this.Width = control.Width;
 19             this.Height = control.Height;
 20             this.Location = control.Location;
 21             this.StartPosition = FormStartPosition.Manual;
 22             helpImage = new Bitmap(control.Width, control.Height);  //创建与参数control同等大小的空白图片
 23             this.picHelpImage.BackgroundImage = helpImage;
 24             this.picHelpImage.BackgroundImageLayout = ImageLayout.Center;
 25             //helpImage添加帮助信息 + private void PictureHandle(List<HelpInfo> helpInfo)  
 26             PictureHandle(helpInfo);
 27             this.BackColor = Color.Black;
 28             this.Opacity = 0.7; 
 29         }
 30 
 31         /// <summary>
 32         /// 处理图片 添加帮助信息
 33         /// </summary>
 34         /// <param name="helpInfo">帮助信息</param>
 35         private void PictureHandle(List<HelpInfo> helpInfo)
 36         { 
 37             if (helpInfo != null)
 38             {
 39                 foreach (var currentInfo in helpInfo)
 40                 {
 41                     //helpImage添加说明信息
 42                     helpImage = HelpInfoHandle(currentInfo.HelpRectangle, currentInfo.HelpRectanglePen, currentInfo.HelpExplainStyle, currentInfo.HelpImage, currentInfo.HelpFinger);
 43                 }
 44             }  
 45         } 
 46 
 47         /// <summary>
 48         /// 将帮助信息添加到图片上
 49         /// </summary>
 50         /// <param name="helpRectangle">矩形框</param>
 51         /// <param name="helpRectanglePen">矩形框画笔</param>
 52         /// <param name="helpExplainStyle">帮助信息</param>
 53         /// <param name="imageStyles">帮助图片</param>
 54         /// <param name="fingers">帮助指导线</param>
 55         /// <returns>处理后的图片</returns>
 56         private Image HelpInfoHandle(Rectangle helpRectangle, Pen helpRectanglePen, List<TextStyle> helpExplainStyles, List<ImageStyle> imageStyles, List<HelpFinger> fingers)
 57         {
 58             if (helpImage != null)
 59             {
 60                 Graphics graphice = Graphics.FromImage(helpImage);
 61                 //添加说明性图片
 62                 if (imageStyles != null)
 63                 {
 64                     AddHelpPicture(imageStyles);
 65                 }
 66                 bool noneRectangle = helpRectangle.X.Equals(0) && helpRectangle.X.Equals(helpRectangle.Y) && helpRectangle.Width.Equals(0) && helpRectangle.Width.Equals(helpRectangle.Height);
 67                 if (!noneRectangle)
 68                 {
 69                     helpRectanglePen = helpRectanglePen ?? new Pen(Color.Orange);
 70                     DrawRoundRectangle(graphice, helpRectanglePen, helpRectangle, 6);
 71                     helpRectanglePen.Dispose();
 72                 }
 73                 //画带箭头的线,只对不封闭曲线有用
 74                 if (fingers != null)
 75                 {
 76                     foreach (var finger in fingers)
 77                     {
 78                         if (finger != null)
 79                         {
 80                             Pen pen = new Pen(finger.PenColor);
 81                             pen.Width = finger.Width;
 82                             pen.DashStyle = DashStyle.Solid; //实线
 83                             pen.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头
 84                             graphice.DrawLine(pen, finger.Start, finger.End);
 85                         }
 86                     }
 87                 }
 88                 //将图片添加说明性文字
 89                 if (helpExplainStyles != null)
 90                 {
 91                     foreach (var helpExplainStyle in helpExplainStyles)
 92                     {
 93                         if (helpExplainStyle != null)
 94                         {
 95                             helpExplainStyle.TextSolidBrush = helpExplainStyle.TextSolidBrush ?? new SolidBrush(Color.White);
 96                             helpExplainStyle.TextFont = helpExplainStyle.TextFont ?? new Font("宋体", 15); 
 97                             graphice.DrawString(helpExplainStyle.TextExplain, helpExplainStyle.TextFont, helpExplainStyle.TextSolidBrush,
 98                             helpExplainStyle.TextExplainStartPosition, helpExplainStyle.TextExplainFormat);
 99                         }
100                     }
101                 }
102                 graphice.Dispose();
103             }
104             return helpImage;
105         }
106 
107         /// <summary>
108         /// 添加帮助图片信息
109         /// </summary>
110         /// <param name="imageStyles">帮助图片集合</param>
111         private void AddHelpPicture(List<ImageStyle> imageStyles)
112         {
113             foreach (var imageStyle in imageStyles)
114             {
115                 if (imageStyle != null && imageStyle.Image != null)
116                 {
117                     Form picForm = new Form(); 
118                     picForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
119                     picForm.MinimumSize = new System.Drawing.Size(1,1);
120                     picForm.Width = imageStyle.Image.Width;
121                     picForm.Height = imageStyle.Image.Height; 
122                     picForm.BackgroundImage = imageStyle.Image;
123                     picForm.BackgroundImageLayout = ImageLayout.Center;
124                     picForm.Location = imageStyle.ImagePosition;
125                     picForm.StartPosition = FormStartPosition.Manual;   
126                     picForm.TopMost = true;
127                     picForm.Click += new EventHandler(picHelpImage_Click);  
128                     listForm.Add(picForm);
129                     picForm.Show();
130                 }
131             }
132         } 
133        
134         /// <summary>
135         /// 画圆角矩形
136         /// </summary>
137         /// <param name="graphics">画布</param>
138         /// <param name="pen">画笔</param>
139         /// <param name="rectangle">矩形</param>
140         /// <param name="cornerRadius">角度</param> 
141         private static void DrawRoundRectangle(Graphics graphics, Pen pen, Rectangle rectangle, int cornerRadius)
142         {
143             using (GraphicsPath path = CreateRoundedRectanglePath(rectangle, cornerRadius))
144             {
145                 try
146                 {
147                     graphics.DrawPath(pen, path);
148                 }
149                 catch (ArgumentException argumentException)
150                 {
151                     throw new Exception(argumentException.Message);
152                 }
153                 catch (Exception exception)
154                 {
155                     throw new Exception(exception.Message);
156                 } 
157             }
158         } 
159 
160         /// <summary>
161         /// 创建圆角矩形框
162         /// </summary>
163         /// <param name="rectangle">矩形框</param>
164         /// <param name="cornerRadius">角度</param>
165         /// <returns>圆角矩形框</returns>
166         internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rectangle, int cornerRadius)
167         {
168             GraphicsPath roundedRect = new GraphicsPath();
169             roundedRect.AddArc(rectangle.X, rectangle.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
170             roundedRect.AddLine(rectangle.X + cornerRadius, rectangle.Y, rectangle.Right - cornerRadius * 2, rectangle.Y);
171             roundedRect.AddArc(rectangle.X + rectangle.Width - cornerRadius * 2, rectangle.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
172             roundedRect.AddLine(rectangle.Right, rectangle.Y + cornerRadius * 2, rectangle.Right, rectangle.Y + rectangle.Height - cornerRadius * 2);
173             roundedRect.AddArc(rectangle.X + rectangle.Width - cornerRadius * 2, rectangle.Y + rectangle.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
174             roundedRect.AddLine(rectangle.Right - cornerRadius * 2, rectangle.Bottom, rectangle.X + cornerRadius * 2, rectangle.Bottom);
175             roundedRect.AddArc(rectangle.X, rectangle.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
176             roundedRect.AddLine(rectangle.X, rectangle.Bottom - cornerRadius * 2, rectangle.X, rectangle.Y + cornerRadius * 2);
177             roundedRect.CloseFigure();
178             return roundedRect;
179         } 
180         
181         /// <summary>
182         /// 单击帮助窗体时关闭
183         /// </summary>
184         /// <param name="sender"></param>
185         /// <param name="e"></param>
186         private void picHelpImage_Click(object sender, EventArgs e)
187         {
188             if (listForm != null && listForm.Count > 0)
189             {
190                 foreach (Form form in listForm)
191                 {
192                     form.Close();
193                 }
194                 listForm.Clear();
195             }
196             this.Close();
197         } 
View Code

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

posted on 2014-07-16 17:12  那些年的某一天  阅读(460)  评论(0编辑  收藏  举报