[Feature] Smooth round corners (#260), version 1.1.2.1

This commit is contained in:
Markus Hofknecht 2021-12-05 17:04:48 +01:00
parent 2c04a00307
commit 8233667fd5
7 changed files with 314 additions and 7 deletions

View file

@ -10,7 +10,7 @@
<Identity
Name="49543SystemTrayMenu.SystemTrayMenu"
Publisher="CN=5884501C-92ED-45DE-9508-9D987C314243"
Version="1.1.1.0" />
Version="1.1.2.0" />
<Properties>
<DisplayName>SystemTrayMenu</DisplayName>

View file

@ -39,5 +39,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.2.0")]
[assembly: AssemblyFileVersion("1.1.2.0")]
[assembly: AssemblyVersion("1.1.2.1")]
[assembly: AssemblyFileVersion("1.1.2.1")]

View file

@ -18,6 +18,8 @@
components.Dispose();
}
drawTimer.Tick += DrawForm;
drawTimer.Dispose();
timerUpdateIcons.Stop();
timerUpdateIcons.Dispose();
fading.Dispose();
@ -229,7 +231,7 @@
this.tableLayoutPanelMenu.Controls.Add(this.tableLayoutPanelTitle, 0, 0);
this.tableLayoutPanelMenu.Controls.Add(this.tableLayoutPanelSearch, 0, 2);
this.tableLayoutPanelMenu.Controls.Add(this.tableLayoutPanelDgvAndScrollbar, 0, 1);
this.tableLayoutPanelMenu.Location = new System.Drawing.Point(1, 1);
this.tableLayoutPanelMenu.Location = new System.Drawing.Point(4, 4);
this.tableLayoutPanelMenu.Margin = new System.Windows.Forms.Padding(0);
this.tableLayoutPanelMenu.Name = "tableLayoutPanelMenu";
this.tableLayoutPanelMenu.RowCount = 3;
@ -351,8 +353,7 @@
this.Controls.Add(this.tableLayoutPanelMenu);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Menu";
this.Opacity = 0.01D;
this.Padding = new System.Windows.Forms.Padding(1);
this.Padding = new System.Windows.Forms.Padding(4);
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "SystemTrayMenu";

View file

@ -7,8 +7,11 @@ namespace SystemTrayMenu.UserInterface
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using SystemTrayMenu.DataClasses;
using SystemTrayMenu.DllImports;
@ -17,12 +20,15 @@ namespace SystemTrayMenu.UserInterface
internal partial class Menu : Form
{
private const int CornerRadius = 8;
private readonly Fading fading = new();
private bool isShowing;
private bool directionToRight;
private int rotationAngle;
private bool mouseDown;
private Point lastLocation;
private double opacity;
private Timer drawTimer = new();
internal Menu()
{
@ -31,7 +37,7 @@ namespace SystemTrayMenu.UserInterface
{
if (!IsDisposed && !Disposing)
{
Opacity = newOpacity;
opacity = newOpacity;
}
}
@ -222,10 +228,27 @@ namespace SystemTrayMenu.UserInterface
{
CreateParams createparams = base.CreateParams;
createparams.ExStyle |= 0x80;
if (!DesignMode)
{
createparams.ExStyle |= 0x00080000;
}
return createparams;
}
}
protected override void OnLoad(EventArgs e)
{
if (!DesignMode)
{
drawTimer.Interval = 1000 / 60;
drawTimer.Tick += DrawForm;
drawTimer.Start();
}
base.OnLoad(e);
}
internal void ResetSearchText()
{
textBoxSearch.Text = string.Empty;
@ -569,6 +592,57 @@ namespace SystemTrayMenu.UserInterface
return base.ProcessCmdKey(ref msg, keys);
}
protected override void OnPaint(PaintEventArgs e)
{
if (DesignMode)
{
Graphics graphics = e.Graphics;
Rectangle gradientRectangle = new(0, 0, this.Width - 1, this.Height - 1);
Brush b = new LinearGradientBrush(gradientRectangle, AppColors.DarkModeBackground, AppColors.DarkModeBackground, 0.0f);
graphics.SmoothingMode = SmoothingMode.HighQuality;
RoundedRectangle.FillRoundedRectangle(graphics, b, gradientRectangle, CornerRadius);
}
base.OnPaint(e);
}
private void DrawForm(object pSender, EventArgs pE)
{
using (Bitmap backImage = new(this.Width, this.Height))
{
using (Graphics graphics = Graphics.FromImage(backImage))
{
Rectangle gradientRectangle = new(0, 0, this.Width - 1, this.Height - 1);
using (Brush b = new LinearGradientBrush(gradientRectangle, AppColors.DarkModeBackground, AppColors.DarkModeBackground, 0.0f))
{
graphics.SmoothingMode = SmoothingMode.HighQuality;
RoundedRectangle.FillRoundedRectangle(graphics, b, gradientRectangle, CornerRadius);
foreach (Control ctrl in this.Controls)
{
using (Bitmap bmp = new(ctrl.Width, ctrl.Height))
{
Rectangle rect = new(0, 0, ctrl.Width, ctrl.Height);
ctrl.DrawToBitmap(bmp, rect);
graphics.DrawImage(bmp, ctrl.Location);
}
}
try
{
PerPixelAlphaBlend.SetBitmap(backImage, Left, Top, Handle, opacity);
}
catch { }; //todo disposed exception
}
}
}
}
private static void SetDoubleBuffer(Control ctl, bool doubleBuffered)
{
typeof(Control).InvokeMember(

View file

@ -0,0 +1,60 @@
// <copyright file="PerPixelAlphaBlend.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.UserInterface
{
using System;
using System.Drawing;
using System.Drawing.Imaging;
internal static class PerPixelAlphaBlend
{
public static void SetBitmap(Bitmap bitmap, int left, int top, IntPtr handle, double opacityInPercentage)
{
int opacity = (int)(opacityInPercentage * 255);
SetBitmap(bitmap, (byte)opacity, left, top, handle);
}
public static void SetBitmap(Bitmap bitmap, byte opacity, int left, int top, IntPtr handle)
{
if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
{
throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
}
IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
IntPtr hBitmap = IntPtr.Zero;
IntPtr oldBitmap = IntPtr.Zero;
try
{
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
oldBitmap = Win32.SelectObject(memDc, hBitmap);
Win32.Size size = new(bitmap.Width, bitmap.Height);
Win32.Point pointSource = new(0, 0);
Win32.Point topPos = new(left, top);
Win32.BLENDFUNCTION blend = new();
blend.BlendOp = Win32.AC_SRC_OVER;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = opacity;
blend.AlphaFormat = Win32.AC_SRC_ALPHA;
Win32.UpdateLayeredWindow(handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
}
finally
{
Win32.ReleaseDC(IntPtr.Zero, screenDc);
if (hBitmap != IntPtr.Zero)
{
Win32.SelectObject(memDc, oldBitmap);
Win32.DeleteObject(hBitmap);
}
Win32.DeleteDC(memDc);
}
}
}
}

View file

@ -0,0 +1,73 @@
// <copyright file="RoundedRectangle.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.UserInterface
{
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using SystemTrayMenu.DataClasses;
using SystemTrayMenu.DllImports;
using SystemTrayMenu.Helper;
using SystemTrayMenu.Utilities;
public static class RoundedRectangle
{
public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
{
int diameter = radius * 2;
Size size = new(diameter, diameter);
Rectangle arc = new(bounds.Location, size);
GraphicsPath path = new();
if (radius == 0)
{
path.AddRectangle(bounds);
return path;
}
// top left arc
path.AddArc(arc, 180, 90);
// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);
// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);
// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
return path;
}
public static void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius)
{
if (graphics == null)
{
throw new ArgumentNullException("graphics");
}
if (brush == null)
{
throw new ArgumentNullException("brush");
}
using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
{
graphics.FillPath(brush, path);
}
}
}
}

99
UserInterface/Win32.cs Normal file
View file

@ -0,0 +1,99 @@
// <copyright file="Win32.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.UserInterface
{
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using SystemTrayMenu.DataClasses;
using SystemTrayMenu.DllImports;
using SystemTrayMenu.Helper;
using SystemTrayMenu.Utilities;
internal class Win32
{
public enum Bool
{
False = 0,
True
};
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public Int32 x;
public Int32 y;
public Point(Int32 x, Int32 y) { this.x = x; this.y = y; }
}
[StructLayout(LayoutKind.Sequential)]
public struct Size
{
public Int32 cx;
public Int32 cy;
public Size(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; }
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct ARGB
{
public byte Blue;
public byte Green;
public byte Red;
public byte Alpha;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
public const Int32 ULW_COLORKEY = 0x00000001;
public const Int32 ULW_ALPHA = 0x00000002;
public const Int32 ULW_OPAQUE = 0x00000004;
public const byte AC_SRC_OVER = 0x00;
public const byte AC_SRC_ALPHA = 0x01;
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll", ExactSpelling = true)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll", ExactSpelling = true)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool DeleteObject(IntPtr hObject);
}
}