Posted By

kyrathaba on 01/24/11


Tagged

dialog about


Versions (?)

"About" dialog


 / Published in: C#
 

  1. namespace Kyrathasoft.Dialogs.About {
  2.  
  3. using System;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8. using System.Windows.Forms;
  9.  
  10. class clsAbout {
  11.  
  12. public clsAbout(int width, int height) {
  13. this.AboutDlgWidth = width;
  14. this.AboutDlgHeight = height;
  15. }
  16.  
  17. public const string MatchEmailPattern =
  18. @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
  19.  
  20. /*
  21.   * SAMPLE USAGE:
  22.   *
  23.   * using Kyrathasoft.Dialogs;
  24.   *
  25.   * private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
  26.   clsAbout abt = new clsAbout(this.Width - 15, this.Height + 150);
  27.   abt.caption = appTitle + subversionInfo;
  28.   abt.AboutBackColor = Color.White;
  29.   abt.AboutTxtBorderStyle = BorderStyle.FixedSingle;
  30.   //we don't need to verify existence of about file; we alreay did it in Form1_Load()
  31.   abt.aboutTxt = File.ReadAllText(aboutPath);
  32.   string picPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\me.jpg";
  33.   if (File.Exists(picPath)) { abt.devPicPath = picPath; }
  34.   abt.aboutLink = "http://kyrathaba.dcmembers.com";
  35.   abt.emailLink = devEmailUsername + "@gmail.com";
  36.   abt.showAbout();
  37.   }
  38.   *
  39.   * The class itself has the following 'using' requirements:
  40.   *
  41.   * using System;
  42.   * using System.Diagnostics;
  43.   * using System.Drawing;
  44.   * using System.IO;
  45.   * using System.Text.RegularExpressions;
  46.   * using System.Windows.Forms;
  47.   *
  48.   */
  49.  
  50. #region clsAbout_privateMembers
  51. private int _aboutDlgWidth;
  52. private int _aboutDlgHeight;
  53. private string _email;
  54. private string _link;
  55. private string _devPicPath;
  56. private string _aboutTxt;
  57. private string _caption;
  58. private bool _bold;
  59. private bool _italics;
  60. private string _fontName;
  61. private int _fontSize;
  62. private Color _frmBackCol;
  63. private Color _txtBackCol;
  64. private BorderStyle _txtBorderStyle;
  65. private Color _picBoxBackCol;
  66. private BorderStyle _picBorderStyle;
  67. #endregion
  68.  
  69. #region clsAbout_Properties
  70.  
  71. public int AboutDlgWidth {
  72. get { return _aboutDlgWidth; }
  73. set {
  74. _aboutDlgWidth = value;
  75. if (_aboutDlgWidth < 150) {
  76. _aboutDlgWidth = 150;
  77. }
  78. }
  79. }
  80.  
  81. public int AboutDlgHeight {
  82. get { return _aboutDlgHeight; }
  83. set {
  84. _aboutDlgHeight = value;
  85. if (_aboutDlgHeight < 100) {
  86. _aboutDlgHeight = 100;
  87. }
  88. }
  89. }
  90.  
  91. public string emailLink {
  92. get { return _email; }
  93. set { _email = value; }
  94. }
  95.  
  96. public string aboutLink {
  97. get { return _link; }
  98. set { _link = value; }
  99. }
  100.  
  101. public string devPicPath {
  102. get { return _devPicPath; }
  103. set { _devPicPath = value; }
  104. }
  105.  
  106. public string aboutTxt {
  107. get { return _aboutTxt; }
  108. set { _aboutTxt = value; }
  109. }
  110.  
  111. public string caption {
  112. get { return _caption; }
  113. set { _caption = value; }
  114. }
  115.  
  116. public bool aboutBold {
  117. get { return _bold; }
  118. set { _bold = value; }
  119. }
  120.  
  121. public bool aboutItalics {
  122. get { return _italics; }
  123. set { _italics = value; }
  124. }
  125.  
  126. public string aboutFontname {
  127. get { return _fontName; }
  128. set { _fontName = value; }
  129. }
  130.  
  131. public int aboutFontsize {
  132. get { return _fontSize; }
  133. set { _fontSize = value; }
  134. }
  135.  
  136. public Color AboutBackColor {
  137. get { return _frmBackCol; }
  138. set { _frmBackCol = value; }
  139. }
  140.  
  141. public Color AboutTxtBackColor {
  142. get { return _txtBackCol; }
  143. set { _txtBackCol = value; }
  144. }
  145.  
  146. public BorderStyle AboutTxtBorderStyle {
  147. get { return _txtBorderStyle; }
  148. set { _txtBorderStyle = value; }
  149. }
  150.  
  151. public Color PicBoxBackColor {
  152. get { return _picBoxBackCol; }
  153. set { _picBoxBackCol = value; }
  154. }
  155.  
  156. public BorderStyle PicBorderStyle {
  157. get { return _picBorderStyle; }
  158. set { _picBorderStyle = value; }
  159. }
  160.  
  161. #endregion
  162.  
  163. public void showAbout() {
  164.  
  165. Form frmAbout = new Form();
  166. aboutFontname = "Verdana"; //default; user can change this
  167. aboutFontsize = 10; //default; user can change this
  168.  
  169. frmAbout.Size = new Size(AboutDlgWidth, AboutDlgHeight);
  170. frmAbout.StartPosition = FormStartPosition.CenterScreen;
  171. if (_frmBackCol == null) {
  172. frmAbout.BackColor = Color.White;
  173. }
  174. else {
  175. frmAbout.BackColor = AboutBackColor;
  176. }
  177. frmAbout.Text = caption;
  178. frmAbout.FormBorderStyle = FormBorderStyle.FixedDialog;
  179. frmAbout.MaximizeBox = false;
  180. frmAbout.MinimizeBox = false;
  181.  
  182. #region picboxCode
  183.  
  184. PictureBox picAbout = new PictureBox();
  185. picAbout.Cursor = Cursors.Hand;
  186. picAbout.SizeMode = PictureBoxSizeMode.AutoSize;
  187. picAbout.BorderStyle = PicBorderStyle;
  188.  
  189. frmAbout.Controls.Add(picAbout);
  190. picAbout.Size = new Size(100, 100);
  191. picAbout.SizeMode = PictureBoxSizeMode.Zoom;
  192.  
  193. if (PicBoxBackColor == null) {
  194. picAbout.BackColor = Color.White;
  195. }
  196. else {
  197. picAbout.BackColor = PicBoxBackColor;
  198. }
  199.  
  200. if (File.Exists(devPicPath)) {
  201. /* We use a FileStream rather than the commonly used Image.FromFile, so that we won't
  202.   * have a file handle to deal with; file handles can screw things up. For instance, let's say
  203.   * we have the following code in our application's main form's close event:
  204.   *
  205.   * Directory.Delete(Application.StartupPath + "\\mySubDir\\", true);
  206.   *
  207.   * Obviously the intent is to delete the "mySubDir" subdirectory, along with any files and/or
  208.   * subdirectories and their files, that it contains. If there's an image file in one of these
  209.   * subdirs with a file handle still associated with it, we'll get an error. Using FileStream
  210.   * solves this potential issue. */
  211.  
  212. FileStream fs = new FileStream(devPicPath, FileMode.Open, FileAccess.Read);
  213. picAbout.Image = Image.FromStream(fs);
  214. fs.Dispose();
  215. }
  216.  
  217. #endregion
  218.  
  219. #region linkLabelCode
  220.  
  221. LinkLabel lnkAbout = new LinkLabel();
  222. lnkAbout.AutoSize = true;
  223. int pX = lnkAbout.Text.Length;
  224. int pLeft = (frmAbout.Width - pX) / 2 - 40;
  225. lnkAbout.Location = new Point(pLeft, frmAbout.Bottom - 60);
  226. frmAbout.Controls.Add(lnkAbout);
  227. if (aboutLink != string.Empty) {
  228. lnkAbout.Text = aboutLink;
  229. }
  230.  
  231. #endregion
  232.  
  233. #region textboxCode
  234.  
  235. TextBox txtAbout = new TextBox();
  236. txtAbout.Multiline = true;
  237. txtAbout.ReadOnly = true;
  238. txtAbout.BackColor = AboutTxtBackColor;
  239. txtAbout.BorderStyle = AboutTxtBorderStyle;
  240. txtAbout.ScrollBars = ScrollBars.Vertical;
  241. frmAbout.Controls.Add(txtAbout);
  242. txtAbout.Location = new Point(frmAbout.Left + 125, frmAbout.Top + 5);
  243.  
  244. if (File.Exists(devPicPath)) {
  245. txtAbout.Size = new Size(frmAbout.Width - 150, frmAbout.Height - 85);
  246. }
  247. else {
  248. picAbout.Hide();
  249.  
  250. txtAbout.Location = new Point(frmAbout.Left + 5, frmAbout.Top + 5);
  251.  
  252. if (lnkAbout.Text.Length > 0) {
  253. txtAbout.Size = new Size(frmAbout.Width - 25, frmAbout.Height - 85);
  254. }
  255. else {
  256. txtAbout.Size = new Size(frmAbout.Width - 25, frmAbout.Height - 35);
  257. }
  258. }
  259.  
  260. setFontOfControl(txtAbout, aboutFontname, aboutFontsize, aboutBold, aboutItalics);
  261.  
  262. if (_txtBackCol == null) {
  263. txtAbout.BackColor = Color.White;
  264. }
  265. else {
  266. txtAbout.BackColor = AboutTxtBackColor;
  267. }
  268.  
  269. if (aboutTxt != string.Empty) {
  270. txtAbout.Text = aboutTxt + string.Empty.PadRight(100);
  271. txtAbout.SelectionStart = txtAbout.Text.Length;
  272. txtAbout.SelectionLength = 0;
  273. }
  274.  
  275. #endregion
  276.  
  277. #region hookingControlsUpToEventHandlers
  278.  
  279. lnkAbout.Click += new EventHandler(lnkAbout_Click);
  280.  
  281. picAbout.Click += new EventHandler(picAbout_Click);
  282.  
  283. frmAbout.Closed += new EventHandler(frmAbout_Closed);
  284.  
  285. #endregion
  286.  
  287. frmAbout.ShowDialog();
  288.  
  289. }
  290.  
  291. #region clsAboutEventHandlerMethods
  292.  
  293. private void lnkAbout_Click(object sender, EventArgs e) {
  294. try {
  295. Process.Start(aboutLink);
  296. }
  297. catch (Exception ex) {
  298. MessageBox.Show(ex.Message, "Error in clsAbout.lnkAbout_Click()");
  299. }
  300. }
  301.  
  302. private void picAbout_Click(object sender, EventArgs e) {
  303.  
  304. try {
  305.  
  306. System.Diagnostics.ProcessStartInfo obSI = new ProcessStartInfo();
  307.  
  308. obSI.RedirectStandardOutput = false;
  309. obSI.UseShellExecute = true;
  310.  
  311. if (isEmail(emailLink)) {
  312. obSI.FileName = "mailto:" + emailLink;
  313. Process.Start(obSI);
  314. }
  315. else {
  316. string s = "Clicking the picture is intended to email the developer. However, the assigned email address, ";
  317. s += "\"" + emailLink + "\", ";
  318. s += "doesn't appear to be valid.";
  319. MessageBox.Show(s, "Invalid Email Address");
  320. }
  321.  
  322. }
  323. catch (Exception mailExcep) {
  324. MessageBox.Show(mailExcep.Message, "Exception in clsAbout.picAbout_Click()");
  325. }
  326.  
  327. }
  328.  
  329. private void frmAbout_Closed(object sender, EventArgs e) {
  330. try {
  331.  
  332. }
  333. catch (Exception ex) {
  334. MessageBox.Show(ex.Message, "Error in class clsAbout() method frmAbout_Closed()");
  335. }
  336. }
  337.  
  338. #endregion
  339.  
  340. private void setFontOfControl(Control c, string fontname, int fontsize, bool bold, bool italics) {
  341. try{
  342. FontStyle fs = new FontStyle();
  343. if (bold) {
  344. if (italics) {
  345. fs = FontStyle.Bold | FontStyle.Italic;
  346. }
  347. else {
  348. fs = FontStyle.Bold;
  349. }
  350. }
  351. else {
  352. if (italics) {
  353. fs = FontStyle.Italic;
  354. }
  355. else {
  356. fs = FontStyle.Regular;
  357. }
  358. }
  359. Font myFnt = new Font(fontname, fontsize, fs);
  360. c.Font = myFnt;
  361. }
  362. catch (Exception ex) {
  363. MessageBox.Show(ex.Message, "Font Error in class clsAbout() method setFontOfControl()");
  364. }
  365. }
  366.  
  367. private bool isEmail(string sEmail) {
  368. if (sEmail != null) {
  369. return Regex.IsMatch(sEmail, MatchEmailPattern);
  370. }
  371. else {
  372. //if sEmail is null, we don't test at all
  373. return false;
  374. }
  375. }
  376.  
  377. //next right-brace ends clsAbout
  378. }
  379. }

Report this snippet  

You need to login to post a comment.