Toast window now supports only text too

This commit is contained in:
Jaex 2014-01-09 03:16:48 +02:00
parent 989ceec4f5
commit a251bad6d8
12 changed files with 79 additions and 43 deletions

View file

@ -91,9 +91,9 @@ private void InitializeComponent()
this.pSettings.Controls.Add(this.lblBlackWhiteValue);
this.pSettings.Controls.Add(this.tbBlackWhite);
this.pSettings.Controls.Add(this.rbBlackWhite);
this.pSettings.Location = new System.Drawing.Point(24, 24);
this.pSettings.Location = new System.Drawing.Point(100, 100);
this.pSettings.Name = "pSettings";
this.pSettings.Size = new System.Drawing.Size(320, 408);
this.pSettings.Size = new System.Drawing.Size(320, 396);
this.pSettings.TabIndex = 0;
//
// lblTip
@ -366,7 +366,7 @@ private void InitializeComponent()
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Gray;
this.ClientSize = new System.Drawing.Size(600, 500);
this.ClientSize = new System.Drawing.Size(700, 700);
this.Controls.Add(this.pSettings);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.DoubleBuffered = true;

View file

@ -41,7 +41,7 @@ public MonitorTestForm()
Size = screenBounds.Size;
rbBlackWhite.Checked = true;
tbBlackWhite.Value = 100;
tbBlackWhite.Value = 128;
tbRed.Value = 255;
cbGradient.Items.AddRange(Enum.GetNames(typeof(LinearGradientMode)));
cbGradient.SelectedIndex = 1;

View file

@ -819,5 +819,13 @@ public static Size MeasureText(string text, Font font)
return g.MeasureString(text, font).ToSize();
}
}
public static Size MeasureText(string text, Font font, int width)
{
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
return g.MeasureString(text, font, width).ToSize();
}
}
}
}

View file

@ -1091,7 +1091,7 @@ public static Image LoadImage(string filePath)
{
try
{
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
if (!string.IsNullOrEmpty(filePath) && Helpers.IsImageFile(filePath) && File.Exists(filePath))
{
return Image.FromStream(new MemoryStream(File.ReadAllBytes(filePath)));
}

View file

@ -1,6 +1,6 @@
namespace ShareX
{
partial class SettingsForm
partial class ApplicationSettingsForm
{
/// <summary>
/// Required designer variable.

View file

@ -35,13 +35,13 @@
namespace ShareX
{
public partial class SettingsForm : Form
public partial class ApplicationSettingsForm : Form
{
private bool loaded;
private const int MaxBufferSizePower = 14;
private ContextMenuStrip cmsSaveImageSubFolderPattern;
public SettingsForm()
public ApplicationSettingsForm()
{
InitializeComponent();
LoadSettings();

View file

@ -707,7 +707,7 @@ private void tsmiAutoCapture_Click(object sender, EventArgs e)
private void tsbApplicationSettings_Click(object sender, EventArgs e)
{
using (SettingsForm settingsForm = new SettingsForm())
using (ApplicationSettingsForm settingsForm = new ApplicationSettingsForm())
{
settingsForm.ShowDialog();
}

View file

@ -50,18 +50,33 @@ public partial class NotificationForm : Form
private bool closingAnimation = true;
private int closingAnimationDuration = 2000;
private int closingAnimationInterval = 50;
private Font textFont;
private int textPadding = 5;
private Size textRenderSize;
public NotificationForm(int duration, Size size, Image img, string url)
public NotificationForm(int duration, Size size, Image img, string text, string url)
{
InitializeComponent();
img = ImageHelpers.ResizeImageLimit(img, size);
img = ImageHelpers.DrawCheckers(img);
ToastImage = img;
if (img != null)
{
img = ImageHelpers.ResizeImageLimit(img, size);
img = ImageHelpers.DrawCheckers(img);
size = new Size(img.Width + 2, img.Height + 2);
ToastImage = img;
}
else if (!string.IsNullOrEmpty(text))
{
textFont = new Font("Arial", 10);
textRenderSize = Helpers.MeasureText(text, textFont, size.Width - textPadding * 2);
size = new Size(textRenderSize.Width + textPadding * 2, textRenderSize.Height + textPadding * 2 + 2);
ToastText = text;
}
ToastURL = url;
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
Size = new Size(img.Width + 2, img.Height + 2);
Size = size;
Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - Width - windowOffset, Screen.PrimaryScreen.WorkingArea.Bottom - Height - windowOffset);
tDuration.Interval = duration;
@ -111,41 +126,46 @@ protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(ToastImage, 1, 1, ToastImage.Width, ToastImage.Height);
Rectangle rect = e.ClipRectangle;
if (!string.IsNullOrEmpty(ToastText))
if (ToastImage != null)
{
Rectangle textRect = new Rectangle(0, 0, e.ClipRectangle.Width, 40);
using (SolidBrush brush = new SolidBrush(Color.FromArgb(150, 255, 255, 255)))
g.DrawImage(ToastImage, 1, 1, ToastImage.Width, ToastImage.Height);
}
else if (!string.IsNullOrEmpty(ToastText))
{
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(80, 80, 80), Color.FromArgb(50, 50, 50), LinearGradientMode.Vertical))
{
g.FillRectangle(brush, textRect);
g.FillRectangle(brush, rect);
}
using (Font font = new Font("Arial", 10))
{
g.DrawString(ToastText, font, Brushes.Black, textRect.RectangleOffset(-3));
}
Rectangle textRect = new Rectangle(textPadding, textPadding, textRenderSize.Width + 2, textRenderSize.Height + 2);
g.DrawString(ToastText, textFont, Brushes.Black, textRect);
g.DrawString(ToastText, textFont, Brushes.White, textRect.LocationOffset(1));
}
g.DrawRectangleProper(Pens.Black, e.ClipRectangle);
g.DrawRectangleProper(Pens.Black, rect);
}
public static void Show(int duration, Size size, string imagePath, string url)
public static void Show(int duration, Size size, string imagePath, string text, string url)
{
if (duration > 0 && !size.IsEmpty && !string.IsNullOrEmpty(imagePath) && Helpers.IsImageFile(imagePath) && File.Exists(imagePath))
if (duration > 0 && !size.IsEmpty)
{
Image img = ImageHelpers.LoadImage(imagePath);
NotificationForm form = new NotificationForm(duration, size, img, url);
NativeMethods.ShowWindow(form.Handle, (int)WindowShowStyle.ShowNoActivate);
NativeMethods.SetWindowPos(form.Handle, (IntPtr)SpecialWindowHandles.HWND_TOPMOST, 0, 0, 0, 0,
SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
if (img != null || !string.IsNullOrEmpty(text))
{
NotificationForm form = new NotificationForm(duration, size, img, text, url);
NativeMethods.ShowWindow(form.Handle, (int)WindowShowStyle.ShowNoActivate);
NativeMethods.SetWindowPos(form.Handle, (IntPtr)SpecialWindowHandles.HWND_TOPMOST, 0, 0, 0, 0,
SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
}
}
}
public static void Show(string imagePath, string url)
public static void Show(string imagePath, string text, string url)
{
Show(4000, new Size(400, 300), imagePath, url);
Show(4000, new Size(400, 300), imagePath, text, url);
}
private void NotificationForm_MouseClick(object sender, MouseEventArgs e)
@ -166,9 +186,6 @@ private void NotificationForm_MouseEnter(object sender, EventArgs e)
tOpacity.Stop();
Opacity = 1;
ToastText = ToastURL;
Refresh();
}
private void NotificationForm_MouseLeave(object sender, EventArgs e)
@ -200,6 +217,11 @@ protected override void Dispose(bool disposing)
ToastImage.Dispose();
}
if (textFont != null)
{
textFont.Dispose();
}
base.Dispose(disposing);
}

View file

@ -134,11 +134,11 @@
<Compile Include="Forms\ScreenRegionForm.Designer.cs">
<DependentUpon>ScreenRegionForm.cs</DependentUpon>
</Compile>
<Compile Include="Forms\SettingsForm.cs">
<Compile Include="Forms\ApplicationSettingsForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\SettingsForm.Designer.cs">
<DependentUpon>SettingsForm.cs</DependentUpon>
<Compile Include="Forms\ApplicationSettingsForm.Designer.cs">
<DependentUpon>ApplicationSettingsForm.cs</DependentUpon>
</Compile>
<Compile Include="Forms\NotificationForm.cs">
<SubType>Form</SubType>
@ -235,8 +235,8 @@
<EmbeddedResource Include="Forms\ScreenRegionForm.resx">
<DependentUpon>ScreenRegionForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\SettingsForm.resx">
<DependentUpon>SettingsForm.cs</DependentUpon>
<EmbeddedResource Include="Forms\ApplicationSettingsForm.resx">
<DependentUpon>ApplicationSettingsForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon>

View file

@ -325,7 +325,7 @@ private static void task_UploadCompleted(UploadTask task)
if (task.Info.TaskSettings.GeneralSettings.ShowToastWindowAfterTask)
{
NotificationForm.Show((int)(task.Info.TaskSettings.AdvancedSettings.ToastWindowDuration * 1000),
task.Info.TaskSettings.AdvancedSettings.ToastWindowSize, info.FilePath, result);
task.Info.TaskSettings.AdvancedSettings.ToastWindowSize, info.FilePath, "ShareX - Task completed\r\n" + balloonTipText, result);
}
}

View file

@ -47,7 +47,13 @@ public string Parse(TaskInfo info, string pattern)
if (info.Result != null)
{
string result = string.IsNullOrEmpty(info.Result.ToString()) ? info.FilePath : info.Result.ToString();
string result = info.Result.ToString();
if (string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(info.FilePath))
{
result = info.FilePath;
}
pattern = pattern.Replace("$result", result ?? "");
pattern = pattern.Replace("$url", info.Result.URL ?? "");
pattern = pattern.Replace("$shorturl", info.Result.ShortenedURL ?? "");