Skip to content

Commit 4358c88

Browse files
committed
boost(Rounded): Add rounded corner to the banner and menus
Fixes #971
1 parent fe26910 commit 4358c88

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace SoundSwitch.UI.Menu.Util;
5+
6+
public static class RoundedCorner
7+
{
8+
private const int OS_WINDOWS_11 = 22000;
9+
10+
// The enum flag for DwmSetWindowAttribute's second parameter, which tells the function what attribute to set.
11+
// Copied from dwmapi.h
12+
private enum DWMWINDOWATTRIBUTE
13+
{
14+
DWMWA_WINDOW_CORNER_PREFERENCE = 33
15+
}
16+
17+
// The DWM_WINDOW_CORNER_PREFERENCE enum for DwmSetWindowAttribute's third parameter, which tells the function
18+
// what value of the enum to set.
19+
// Copied from dwmapi.h
20+
public enum DWM_WINDOW_CORNER_PREFERENCE
21+
{
22+
DWMWCP_DEFAULT = 0,
23+
DWMWCP_DONOTROUND = 1,
24+
DWMWCP_ROUND = 2,
25+
DWMWCP_ROUNDSMALL = 3
26+
}
27+
28+
// Import dwmapi.dll and define DwmSetWindowAttribute in C# corresponding to the native function.
29+
[DllImport("dwmapi.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
30+
private static extern void DwmSetWindowAttribute(IntPtr hwnd,
31+
DWMWINDOWATTRIBUTE attribute,
32+
ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute,
33+
uint cbAttribute);
34+
35+
/// <summary>
36+
/// Round the corner of a menu/form (only for Windows 11)
37+
/// </summary>
38+
/// <param name="handle"></param>
39+
/// <param name="preference"></param>
40+
public static void RoundCorner(IntPtr handle, DWM_WINDOW_CORNER_PREFERENCE preference)
41+
{
42+
if (Environment.OSVersion.Version.Major < 10 || Environment.OSVersion.Version.Build < OS_WINDOWS_11)
43+
{
44+
return;
45+
}
46+
47+
var pref = preference;
48+
DwmSetWindowAttribute(handle, DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE, ref pref, sizeof(uint));
49+
}
50+
51+
52+
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
53+
public static extern IntPtr CreateRoundRectRgn
54+
(
55+
int nLeftRect, // x-coordinate of upper-left corner
56+
int nTopRect, // y-coordinate of upper-left corner
57+
int nRightRect, // x-coordinate of lower-right corner
58+
int nBottomRect, // y-coordinate of lower-right corner
59+
int nWidthEllipse, // width of ellipse
60+
int nHeightEllipse // height of elli
61+
);
62+
}

SoundSwitch/Framework/Banner/BannerForm.cs

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using SoundSwitch.Framework.Audio.Play;
2121
using SoundSwitch.Framework.Threading;
2222
using SoundSwitch.Model;
23+
using SoundSwitch.UI.Menu.Util;
2324
using Timer = System.Windows.Forms.Timer;
2425

2526
namespace SoundSwitch.Framework.Banner
@@ -101,6 +102,8 @@ internal void SetData(BannerData data)
101102
Opacity = .8;
102103
lblTop.Text = data.Title;
103104
lblTitle.Text = data.Text;
105+
Region = Region.FromHrgn(RoundedCorner.CreateRoundRectRgn(0, 0, Width, Height , 20, 20));
106+
104107
_timerHide.Enabled = true;
105108

106109
Show();

SoundSwitch/UI/Component/TrayIcon.cs

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
using SoundSwitch.Model;
3939
using SoundSwitch.Properties;
4040
using SoundSwitch.UI.Forms;
41+
using SoundSwitch.UI.Menu.Util;
4142
using SoundSwitch.Util;
4243
using SoundSwitch.Util.Url;
4344
using TimerForm = System.Windows.Forms.Timer;
@@ -184,6 +185,8 @@ private void PopulateSettingsMenu()
184185
_settingsMenu.Items.Add(TrayIconStrings.about, RessourceHelpSmallBitmap, (sender, e) => new About().Show());
185186
_settingsMenu.Items.Add(new ToolStripSeparator());
186187
_settingsMenu.Items.Add(TrayIconStrings.exit, RessourceExitBitmap, (sender, e) => Application.Exit());
188+
189+
RoundedCorner.RoundCorner(_settingsMenu.Handle, RoundedCorner.DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUNDSMALL);
187190
}
188191

189192
private void OnUpdateClick(object sender, EventArgs eventArgs)
@@ -333,6 +336,8 @@ public void UpdateDeviceSelectionList()
333336
_selectionMenu.Items.Add(new ToolStripSeparator());
334337
_selectionMenu.Items.AddRange(recordingDevices.Select(info => new ToolStripDeviceItem(DeviceClicked, info, info.Equals(defaultRecording))).ToArray());
335338
}
339+
RoundedCorner.RoundCorner(_selectionMenu.Handle, RoundedCorner.DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUNDSMALL);
340+
336341
}
337342

338343
private void DeviceClicked(object sender, EventArgs e)

SoundSwitch/app.manifest

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
33
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
44
<application>
5-
<!-- Windows 10 -->
5+
<!-- Windows 10 and Windows 11 -->
66
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
77
<!-- Windows 8.1 -->
88
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>

0 commit comments

Comments
 (0)