Skip to content

Instantly share code, notes, and snippets.

@kyrathasoft
Created June 16, 2014 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyrathasoft/4e36a7cf8385c1cb4f79 to your computer and use it in GitHub Desktop.
Save kyrathasoft/4e36a7cf8385c1cb4f79 to your computer and use it in GitHub Desktop.
about Dlg
namespace Kyrathasoft.Dialogs.About {
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
class clsAbout {
public clsAbout(int width, int height) {
this.AboutDlgWidth = width;
this.AboutDlgHeight = height;
}
public const string MatchEmailPattern =
@"^[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]$";
/*
* SAMPLE USAGE:
*
* using Kyrathasoft.Dialogs;
*
* private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
clsAbout abt = new clsAbout(this.Width - 15, this.Height + 150);
abt.caption = appTitle + subversionInfo;
abt.AboutBackColor = Color.White;
abt.AboutTxtBorderStyle = BorderStyle.FixedSingle;
//we don't need to verify existence of about file; we alreay did it in Form1_Load()
abt.aboutTxt = File.ReadAllText(aboutPath);
string picPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\me.jpg";
if (File.Exists(picPath)) { abt.devPicPath = picPath; }
abt.aboutLink = "http://kyrathaba.dcmembers.com";
abt.emailLink = devEmailUsername + "@gmail.com";
abt.showAbout();
}
*
* The class itself has the following 'using' requirements:
*
* using System;
* using System.Diagnostics;
* using System.Drawing;
* using System.IO;
* using System.Text.RegularExpressions;
* using System.Windows.Forms;
*
*/
#region clsAbout_privateMembers
private int _aboutDlgWidth;
private int _aboutDlgHeight;
private string _email;
private string _link;
private string _devPicPath;
private string _aboutTxt;
private string _caption;
private bool _bold;
private bool _italics;
private string _fontName;
private int _fontSize;
private Color _frmBackCol;
private Color _txtBackCol;
private BorderStyle _txtBorderStyle;
private Color _picBoxBackCol;
private BorderStyle _picBorderStyle;
#endregion
#region clsAbout_Properties
public int AboutDlgWidth {
get { return _aboutDlgWidth; }
set {
_aboutDlgWidth = value;
if (_aboutDlgWidth < 150) {
_aboutDlgWidth = 150;
}
}
}
public int AboutDlgHeight {
get { return _aboutDlgHeight; }
set {
_aboutDlgHeight = value;
if (_aboutDlgHeight < 100) {
_aboutDlgHeight = 100;
}
}
}
public string emailLink {
get { return _email; }
set { _email = value; }
}
public string aboutLink {
get { return _link; }
set { _link = value; }
}
public string devPicPath {
get { return _devPicPath; }
set { _devPicPath = value; }
}
public string aboutTxt {
get { return _aboutTxt; }
set { _aboutTxt = value; }
}
public string caption {
get { return _caption; }
set { _caption = value; }
}
public bool aboutBold {
get { return _bold; }
set { _bold = value; }
}
public bool aboutItalics {
get { return _italics; }
set { _italics = value; }
}
public string aboutFontname {
get { return _fontName; }
set { _fontName = value; }
}
public int aboutFontsize {
get { return _fontSize; }
set { _fontSize = value; }
}
public Color AboutBackColor {
get { return _frmBackCol; }
set { _frmBackCol = value; }
}
public Color AboutTxtBackColor {
get { return _txtBackCol; }
set { _txtBackCol = value; }
}
public BorderStyle AboutTxtBorderStyle {
get { return _txtBorderStyle; }
set { _txtBorderStyle = value; }
}
public Color PicBoxBackColor {
get { return _picBoxBackCol; }
set { _picBoxBackCol = value; }
}
public BorderStyle PicBorderStyle {
get { return _picBorderStyle; }
set { _picBorderStyle = value; }
}
#endregion
public void showAbout() {
Form frmAbout = new Form();
aboutFontname = "Verdana"; //default; user can change this
aboutFontsize = 10; //default; user can change this
frmAbout.Size = new Size(AboutDlgWidth, AboutDlgHeight);
frmAbout.StartPosition = FormStartPosition.CenterScreen;
if (_frmBackCol == null) {
frmAbout.BackColor = Color.White;
}
else {
frmAbout.BackColor = AboutBackColor;
}
frmAbout.Text = caption;
frmAbout.FormBorderStyle = FormBorderStyle.FixedDialog;
frmAbout.MaximizeBox = false;
frmAbout.MinimizeBox = false;
#region picboxCode
PictureBox picAbout = new PictureBox();
picAbout.Cursor = Cursors.Hand;
picAbout.SizeMode = PictureBoxSizeMode.AutoSize;
picAbout.BorderStyle = PicBorderStyle;
frmAbout.Controls.Add(picAbout);
picAbout.Size = new Size(100, 100);
picAbout.SizeMode = PictureBoxSizeMode.Zoom;
if (PicBoxBackColor == null) {
picAbout.BackColor = Color.White;
}
else {
picAbout.BackColor = PicBoxBackColor;
}
if (File.Exists(devPicPath)) {
/* We use a FileStream rather than the commonly used Image.FromFile, so that we won't
* have a file handle to deal with; file handles can screw things up. For instance, let's say
* we have the following code in our application's main form's close event:
*
* Directory.Delete(Application.StartupPath + "\\mySubDir\\", true);
*
* Obviously the intent is to delete the "mySubDir" subdirectory, along with any files and/or
* subdirectories and their files, that it contains. If there's an image file in one of these
* subdirs with a file handle still associated with it, we'll get an error. Using FileStream
* solves this potential issue. */
FileStream fs = new FileStream(devPicPath, FileMode.Open, FileAccess.Read);
picAbout.Image = Image.FromStream(fs);
fs.Dispose();
}
#endregion
#region linkLabelCode
LinkLabel lnkAbout = new LinkLabel();
lnkAbout.AutoSize = true;
int pX = lnkAbout.Text.Length;
int pLeft = (frmAbout.Width - pX) / 2 - 40;
lnkAbout.Location = new Point(pLeft, frmAbout.Bottom - 60);
frmAbout.Controls.Add(lnkAbout);
if (aboutLink != string.Empty) {
lnkAbout.Text = aboutLink;
}
#endregion
#region textboxCode
TextBox txtAbout = new TextBox();
txtAbout.Multiline = true;
txtAbout.ReadOnly = true;
txtAbout.BackColor = AboutTxtBackColor;
txtAbout.BorderStyle = AboutTxtBorderStyle;
txtAbout.ScrollBars = ScrollBars.Vertical;
frmAbout.Controls.Add(txtAbout);
txtAbout.Location = new Point(frmAbout.Left + 125, frmAbout.Top + 5);
if (File.Exists(devPicPath)) {
txtAbout.Size = new Size(frmAbout.Width - 150, frmAbout.Height - 85);
}
else {
picAbout.Hide();
txtAbout.Location = new Point(frmAbout.Left + 5, frmAbout.Top + 5);
if (lnkAbout.Text.Length > 0) {
txtAbout.Size = new Size(frmAbout.Width - 25, frmAbout.Height - 85);
}
else {
txtAbout.Size = new Size(frmAbout.Width - 25, frmAbout.Height - 35);
}
}
setFontOfControl(txtAbout, aboutFontname, aboutFontsize, aboutBold, aboutItalics);
if (_txtBackCol == null) {
txtAbout.BackColor = Color.White;
}
else {
txtAbout.BackColor = AboutTxtBackColor;
}
if (aboutTxt != string.Empty) {
txtAbout.Text = aboutTxt + string.Empty.PadRight(100);
txtAbout.SelectionStart = txtAbout.Text.Length;
txtAbout.SelectionLength = 0;
}
#endregion
#region hookingControlsUpToEventHandlers
lnkAbout.Click += new EventHandler(lnkAbout_Click);
picAbout.Click += new EventHandler(picAbout_Click);
frmAbout.Closed += new EventHandler(frmAbout_Closed);
#endregion
frmAbout.ShowDialog();
}
#region clsAboutEventHandlerMethods
private void lnkAbout_Click(object sender, EventArgs e) {
try {
Process.Start(aboutLink);
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Error in clsAbout.lnkAbout_Click()");
}
}
private void picAbout_Click(object sender, EventArgs e) {
try {
System.Diagnostics.ProcessStartInfo obSI = new ProcessStartInfo();
obSI.RedirectStandardOutput = false;
obSI.UseShellExecute = true;
if (isEmail(emailLink)) {
obSI.FileName = "mailto:" + emailLink;
Process.Start(obSI);
}
else {
string s = "Clicking the picture is intended to email the developer. However, the assigned email address, ";
s += "\"" + emailLink + "\", ";
s += "doesn't appear to be valid.";
MessageBox.Show(s, "Invalid Email Address");
}
}
catch (Exception mailExcep) {
MessageBox.Show(mailExcep.Message, "Exception in clsAbout.picAbout_Click()");
}
}
private void frmAbout_Closed(object sender, EventArgs e) {
try {
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Error in class clsAbout() method frmAbout_Closed()");
}
}
#endregion
private void setFontOfControl(Control c, string fontname, int fontsize, bool bold, bool italics) {
try{
FontStyle fs = new FontStyle();
if (bold) {
if (italics) {
fs = FontStyle.Bold | FontStyle.Italic;
}
else {
fs = FontStyle.Bold;
}
}
else {
if (italics) {
fs = FontStyle.Italic;
}
else {
fs = FontStyle.Regular;
}
}
Font myFnt = new Font(fontname, fontsize, fs);
c.Font = myFnt;
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Font Error in class clsAbout() method setFontOfControl()");
}
}
private bool isEmail(string sEmail) {
if (sEmail != null) {
return Regex.IsMatch(sEmail, MatchEmailPattern);
}
else {
//if sEmail is null, we don't test at all
return false;
}
}
//next right-brace ends clsAbout
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment