From 203c8c1564a26361fef4c69f97e5ebcc0a31ffd1 Mon Sep 17 00:00:00 2001 From: Lior Banai Date: Fri, 19 Feb 2021 22:07:24 +0200 Subject: [PATCH] Add Auto Height option to resize base on the text fix #25 --- .gitignore | 1 + DemoApp/Form1.Designer.cs | 29 +++++++++++++++----- DemoApp/Form1.cs | 3 +- NotificationWindow/NotificationWindow.csproj | 5 ++-- NotificationWindow/PopupNotifier.cs | 26 ++++++++++++------ NotificationWindow/PopupNotifierForm.cs | 1 + NotificationWindow/Utils.cs | 15 ++++++++++ 7 files changed, 62 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 3f787eb..5faac86 100644 --- a/.gitignore +++ b/.gitignore @@ -159,3 +159,4 @@ $RECYCLE.BIN/ nuget.exe *.nupkg /.vs +/Visual Studio 2019/Visualizers/attribcache140.bin diff --git a/DemoApp/Form1.Designer.cs b/DemoApp/Form1.Designer.cs index 1eb403f..3381037 100644 --- a/DemoApp/Form1.Designer.cs +++ b/DemoApp/Form1.Designer.cs @@ -78,6 +78,7 @@ private void InitializeComponent() this.rbBeep = new System.Windows.Forms.RadioButton(); this.rbAsterisk = new System.Windows.Forms.RadioButton(); this.chkbSound = new System.Windows.Forms.CheckBox(); + this.chkbAutoHeight = new System.Windows.Forms.CheckBox(); this.contextMenuStrip1.SuspendLayout(); this.gbSound.SuspendLayout(); this.SuspendLayout(); @@ -95,7 +96,7 @@ private void InitializeComponent() // // btnShow // - this.btnShow.Location = new System.Drawing.Point(12, 301); + this.btnShow.Location = new System.Drawing.Point(12, 344); this.btnShow.Name = "btnShow"; this.btnShow.Size = new System.Drawing.Size(75, 23); this.btnShow.TabIndex = 1; @@ -340,7 +341,7 @@ private void InitializeComponent() // btnMore // this.btnMore.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btnMore.Location = new System.Drawing.Point(911, 304); + this.btnMore.Location = new System.Drawing.Point(911, 343); this.btnMore.Name = "btnMore"; this.btnMore.Size = new System.Drawing.Size(153, 23); this.btnMore.TabIndex = 24; @@ -398,7 +399,7 @@ private void InitializeComponent() // // txtBorder // - this.txtBorder.Location = new System.Drawing.Point(207, 232); + this.txtBorder.Location = new System.Drawing.Point(207, 275); this.txtBorder.Name = "txtBorder"; this.txtBorder.Size = new System.Drawing.Size(100, 24); this.txtBorder.TabIndex = 29; @@ -407,7 +408,7 @@ private void InitializeComponent() // label9 // this.label9.AutoSize = true; - this.label9.Location = new System.Drawing.Point(13, 236); + this.label9.Location = new System.Drawing.Point(13, 279); this.label9.Name = "label9"; this.label9.Size = new System.Drawing.Size(188, 17); this.label9.TabIndex = 30; @@ -415,7 +416,7 @@ private void InitializeComponent() // // txtHeaderHeight // - this.txtHeaderHeight.Location = new System.Drawing.Point(207, 260); + this.txtHeaderHeight.Location = new System.Drawing.Point(207, 303); this.txtHeaderHeight.Name = "txtHeaderHeight"; this.txtHeaderHeight.Size = new System.Drawing.Size(100, 24); this.txtHeaderHeight.TabIndex = 31; @@ -424,7 +425,7 @@ private void InitializeComponent() // label11 // this.label11.AutoSize = true; - this.label11.Location = new System.Drawing.Point(14, 264); + this.label11.Location = new System.Drawing.Point(14, 307); this.label11.Name = "label11"; this.label11.Size = new System.Drawing.Size(94, 17); this.label11.TabIndex = 32; @@ -548,11 +549,24 @@ private void InitializeComponent() this.chkbSound.Text = "Enable Sounds"; this.chkbSound.UseVisualStyleBackColor = true; // + // chkbAutoHeight + // + this.chkbAutoHeight.AutoSize = true; + this.chkbAutoHeight.Checked = true; + this.chkbAutoHeight.CheckState = System.Windows.Forms.CheckState.Checked; + this.chkbAutoHeight.Location = new System.Drawing.Point(15, 228); + this.chkbAutoHeight.Name = "chkbAutoHeight"; + this.chkbAutoHeight.Size = new System.Drawing.Size(136, 21); + this.chkbAutoHeight.TabIndex = 34; + this.chkbAutoHeight.Text = "Auto size (height)"; + this.chkbAutoHeight.UseVisualStyleBackColor = true; + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 17F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1078, 336); + this.ClientSize = new System.Drawing.Size(1078, 375); + this.Controls.Add(this.chkbAutoHeight); this.Controls.Add(this.gbSound); this.Controls.Add(this.chkbSound); this.Controls.Add(this.txtHeaderHeight); @@ -655,6 +669,7 @@ private void InitializeComponent() private System.Windows.Forms.RadioButton rbCustom; private System.Windows.Forms.TextBox txtSoundPath; private System.Windows.Forms.Button btnOpemSoundFile; + private System.Windows.Forms.CheckBox chkbAutoHeight; } } diff --git a/DemoApp/Form1.cs b/DemoApp/Form1.cs index 5558b85..0e67f52 100644 --- a/DemoApp/Form1.cs +++ b/DemoApp/Form1.cs @@ -39,6 +39,7 @@ private void btnShow_Click(object sender, EventArgs e) popupNotifier1.ContentColor = lblTextColor.BackColor; popupNotifier1.TitleColor = lblColorValue.BackColor; popupNotifier1.PlaySystemSoundOnPopup = chkbSound.Checked; + popupNotifier1.AutoContentHeight = chkbAutoHeight.Checked; if (rbAsterisk.Checked) { popupNotifier1.SystemSoundType = SystemSoundType.Asterisk; @@ -114,7 +115,7 @@ private void btnMore_Click(object sender, EventArgs e) popupNotifier2.ContentColor = lblTextColor.BackColor; popupNotifier2.TitleColor = lblColorValue.BackColor; popupNotifier2.PlaySystemSoundOnPopup = chkbSound.Checked; - + popupNotifier2.AutoContentHeight = chkbAutoHeight.Checked; if (rbExclamation.Checked) { popupNotifier2.SystemSoundType = SystemSoundType.Exclamation; diff --git a/NotificationWindow/NotificationWindow.csproj b/NotificationWindow/NotificationWindow.csproj index 6a0b6f8..b6a2768 100644 --- a/NotificationWindow/NotificationWindow.csproj +++ b/NotificationWindow/NotificationWindow.csproj @@ -12,7 +12,7 @@ true LiorBanai.NotificationWindow LiorBanai - 1.2.0 + 1.3.0 LiorBanai.NotificationWindow LiorBanai.NotificationWindow Lior Banai @@ -20,8 +20,9 @@ https://github.com/LiorBanai/Notification-Popup-Window https://github.com/LiorBanai/Notification-Popup-Window git - fix:Close button does not decrement the positions #5 + Add Auto Height true + diff --git a/NotificationWindow/PopupNotifier.cs b/NotificationWindow/PopupNotifier.cs index 0ed536a..22a75a5 100644 --- a/NotificationWindow/PopupNotifier.cs +++ b/NotificationWindow/PopupNotifier.cs @@ -1,4 +1,6 @@ -using System; +using Microsoft.Win32; +using NotificationWindow.DataTypes; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; @@ -8,8 +10,6 @@ using System.Media; using System.Runtime.InteropServices; using System.Windows.Forms; -using Microsoft.Win32; -using NotificationWindow.DataTypes; using Timer = System.Windows.Forms.Timer; namespace NotificationWindow @@ -101,7 +101,9 @@ static void ShowInactiveTopmost(Form frm) [Category("Title"), DefaultValue(typeof(Color), "Gray")] [Description("Color of the title text.")] public Color TitleColor { get; set; } - + [Category("Content"), DefaultValue(false)] + [Description("Auto size of content.")] + public bool AutoContentHeight { get; set; } [Category("Content"), DefaultValue(typeof(Color), "ControlText")] [Description("Color of the content text.")] public Color ContentColor { get; set; } @@ -307,7 +309,7 @@ public int HeaderHeight [Category("Behavior"), DefaultValue(10)] [Description("Type of system sound to use")] public SystemSoundType SystemSoundType { get; set; } - + [Category("Behavior"), DefaultValue("")] [Description("Custom system sound to use. File ath to sound file")] public string SystemSoundFilePath { get; set; } @@ -318,7 +320,7 @@ public int HeaderHeight /// public PopupNotifier() { - // set default values + // set default values HeaderColor = SystemColors.ControlDark; BodyColor = SystemColors.Control; TitleColor = Color.Gray; @@ -436,6 +438,13 @@ public void Popup() } frmPopup.Size = Size; + if (AutoContentHeight) + { + var size = Utils.MeasureString(ContentText, ContentFont); + var lines = (int)Math.Ceiling(size.Width / frmPopup.Size.Width); + frmPopup.Size = Size = new Size(frmPopup.Size.Width, (int)Math.Ceiling(size.Height * lines) + HeaderHeight + 30); + } + SetNextPosition(); frmPopup.Closed += FrmPopup_Closed; opacityStart = 0; @@ -511,7 +520,7 @@ private void PlaySoundIfNeeded() object o = key?.GetValue(null); // pass null to get (Default) if (o != null) { - using (SoundPlayer theSound = new SoundPlayer((string) o)) + using (SoundPlayer theSound = new SoundPlayer((string)o)) { theSound.Play(); } @@ -543,7 +552,8 @@ private void PlaySoundIfNeeded() default: throw new ArgumentOutOfRangeException(); } - }} + } + } private void FrmPopup_Closed(object sender, EventArgs e) { diff --git a/NotificationWindow/PopupNotifierForm.cs b/NotificationWindow/PopupNotifierForm.cs index 7640d30..f6ef78e 100644 --- a/NotificationWindow/PopupNotifierForm.cs +++ b/NotificationWindow/PopupNotifierForm.cs @@ -367,6 +367,7 @@ private void PopupNotifierForm_Paint(object sender, PaintEventArgs e) Cursor = mouseOnLink ? Cursors.Hand : Cursors.Default; Brush brushText = mouseOnLink ? brushLinkHover : brushContent; e.Graphics.DrawString(Parent.ContentText, Parent.ContentFont, brushText, RectContentText); + } } diff --git a/NotificationWindow/Utils.cs b/NotificationWindow/Utils.cs index 6a08a11..bfb3f5e 100644 --- a/NotificationWindow/Utils.cs +++ b/NotificationWindow/Utils.cs @@ -1,4 +1,5 @@ using System; +using System.Drawing; using System.Runtime.InteropServices; namespace NotificationWindow @@ -82,5 +83,19 @@ static long GetLastInputTime() return ((idleTime > 0) ? (idleTime / 1000) : 0); } public static TimeSpan IdleTime() => TimeSpan.FromSeconds(GetLastInputTime()); + + public static SizeF MeasureString(string s, Font font) + { + SizeF result; + using (var image = new Bitmap(1, 1)) + { + using (var g = Graphics.FromImage(image)) + { + result = g.MeasureString(s, font); + } + } + + return result; + } } }