Skip to content

Commit

Permalink
Add Auto Height option to resize base on the text fix #25
Browse files Browse the repository at this point in the history
  • Loading branch information
LiorBanai committed Feb 19, 2021
1 parent b2edcfa commit 203c8c1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,4 @@ $RECYCLE.BIN/
nuget.exe
*.nupkg
/.vs
/Visual Studio 2019/Visualizers/attribcache140.bin
29 changes: 22 additions & 7 deletions DemoApp/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion DemoApp/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions NotificationWindow/NotificationWindow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>LiorBanai.NotificationWindow</PackageId>
<Authors>LiorBanai</Authors>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<Company>LiorBanai.NotificationWindow</Company>
<Product>LiorBanai.NotificationWindow</Product>
<Copyright>Lior Banai</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/LiorBanai/Notification-Popup-Window</PackageProjectUrl>
<RepositoryUrl>https://github.com/LiorBanai/Notification-Popup-Window</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>fix:Close button does not decrement the positions #5</PackageReleaseNotes>
<PackageReleaseNotes>Add Auto Height</PackageReleaseNotes>
<GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources>
<Description />
</PropertyGroup>

<ItemGroup>
Expand Down
26 changes: 18 additions & 8 deletions NotificationWindow/PopupNotifier.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Microsoft.Win32;
using NotificationWindow.DataTypes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
Expand All @@ -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
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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; }
Expand All @@ -318,7 +320,7 @@ public int HeaderHeight
/// </summary>
public PopupNotifier()
{
// set default values
// set default values
HeaderColor = SystemColors.ControlDark;
BodyColor = SystemColors.Control;
TitleColor = Color.Gray;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -543,7 +552,8 @@ private void PlaySoundIfNeeded()
default:
throw new ArgumentOutOfRangeException();
}
}}
}
}

private void FrmPopup_Closed(object sender, EventArgs e)
{
Expand Down
1 change: 1 addition & 0 deletions NotificationWindow/PopupNotifierForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}

Expand Down
15 changes: 15 additions & 0 deletions NotificationWindow/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace NotificationWindow
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 203c8c1

Please sign in to comment.