Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color picker wpf #37149

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added system menu option on right click on toolbar.
Fixed hide then show removing Mica effect
  • Loading branch information
mantaionut committed Mar 3, 2025
commit d891dfe91c02902fa041c9a55691a98eedad96c3
3 changes: 1 addition & 2 deletions src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
ResizeMode="NoResize"
Topmost="True"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
mc:Ignorable="d">
<e:Interaction.Behaviors>
<behaviors:CloseZoomWindowBehavior />
@@ -24,7 +23,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DockPanel Grid.Row="0" Background="Transparent" MouseLeftButtonDown="TitleBar_MouseDown">
<DockPanel Grid.Row="0" Background="Transparent" MouseLeftButtonDown="TitleBar_MouseDown" MouseRightButtonUp="TitleBar_MouseRightClick">
<Image Source="pack://application:,,,/Assets/ColorPicker/icon.ico"
Width="20" Height="20"
Margin="10,5,5,8"/>
96 changes: 95 additions & 1 deletion src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -3,12 +3,18 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Shell;
using ColorPicker.Helpers;
using ControlzEx.Theming;
using ManagedCommon;
using Microsoft.Diagnostics.Tracing.Parsers.ClrPrivate;
using Microsoft.Win32;
using Windows.Graphics;

namespace ColorPicker
{
@@ -35,7 +41,16 @@

protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
WindowChrome.SetWindowChrome(
this,
new WindowChrome
{
CaptionHeight = 0,
CornerRadius = default,
GlassFrameThickness = new Thickness(-1),
ResizeBorderThickness = ResizeMode == ResizeMode.NoResize ? default : new Thickness(4),
UseAeroCaptionButtons = false,
});
if (OSVersionHelper.IsWindows11())
{
// ResizeMode="NoResize" removes rounded corners. So force them to rounded.
@@ -49,6 +64,30 @@
// On Windows10 ResizeMode="NoResize" removes the border so we add a new one.
MainBorder.BorderThickness = new System.Windows.Thickness(0.5);
}

// Hide then Show with WindowStyle="None" will remove the Mica effect. So manually remove the titlebar.
RemoveWindowTitlebarContents();

base.OnSourceInitialized(e);
}

public void RemoveWindowTitlebarContents()
{
IntPtr handle = new WindowInteropHelper(GetWindow(this)).EnsureHandle();
if (handle == IntPtr.Zero)
{
return;
}

int windowStyleLong = GetWindowLong(handle, GWLSTYLE);

Check warning on line 82 in src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml.cs

GitHub Actions / Check Spelling

`GWLSTYLE` is not a recognized word. (unrecognized-spelling)
windowStyleLong &= ~(int)WindowStyles.WS_SYSMENU;

IntPtr result = SetWindowLong(handle, GWLSTYLE, windowStyleLong);

Check warning on line 85 in src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml.cs

GitHub Actions / Check Spelling

`GWLSTYLE` is not a recognized word. (unrecognized-spelling)
if (result.ToInt64() == 0)
{
int error = Marshal.GetLastWin32Error();
Logger.LogError($"SetWindowLong error {error}");
}
}

private void Close_Click(object sender, RoutedEventArgs e)
@@ -64,6 +103,38 @@
}
}

private void TitleBar_MouseRightClick(object sender, MouseButtonEventArgs e)
{
IntPtr hwnd = new WindowInteropHelper(this).Handle;

// Get the mouse position relative to the screen
Point mousePosition = e.GetPosition(this);

Point screenPoint = PointToScreen(mousePosition);

// Display the system menu at the current mouse position
IntPtr hMenu = GetSystemMenu(hwnd, false);
if (hMenu != IntPtr.Zero)
{
int command = TrackPopupMenu(
hMenu,
TPMLEFTALIGN | TPMRETURNCMD,

Check warning on line 121 in src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml.cs

GitHub Actions / Check Spelling

`TPMLEFTALIGN` is not a recognized word. (unrecognized-spelling)

Check warning on line 121 in src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml.cs

GitHub Actions / Check Spelling

`TPMRETURNCMD` is not a recognized word. (unrecognized-spelling)
(int)screenPoint.X,
(int)screenPoint.Y,
0,
hwnd,
IntPtr.Zero);
if (command > 0)
{
SendMessage(hwnd, WMSYSCOMMAND, new IntPtr(command), IntPtr.Zero);

Check warning on line 129 in src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml.cs

GitHub Actions / Check Spelling

`WMSYSCOMMAND` is not a recognized word. (unrecognized-spelling)
}
}
}

private const int WMSYSCOMMAND = 0x0112;

Check warning on line 134 in src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml.cs

GitHub Actions / Check Spelling

`WMSYSCOMMAND` is not a recognized word. (unrecognized-spelling)
private const int TPMLEFTALIGN = 0x0000;

Check warning on line 135 in src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml.cs

GitHub Actions / Check Spelling

`TPMLEFTALIGN` is not a recognized word. (unrecognized-spelling)
private const int TPMRETURNCMD = 0x0100;

Check warning on line 136 in src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml.cs

GitHub Actions / Check Spelling

`TPMRETURNCMD` is not a recognized word. (unrecognized-spelling)

// The enum flag for DwmSetWindowAttribute's second parameter, which tells the function what attribute to set.
public enum DWMWINDOWATTRIBUTE
{
@@ -85,5 +156,28 @@
DWMWINDOWATTRIBUTE attribute,
ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute,
uint cbAttribute);

[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

[DllImport("user32.dll")]
private static extern int TrackPopupMenu(IntPtr hMenu, int uFlags, int x, int y, int nReserved, IntPtr hWnd, IntPtr prcRect);

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", SetLastError = true)]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

public const int GWLSTYLE = -16;

Check warning on line 175 in src/modules/colorPicker/ColorPickerUI/ColorEditorWindow.xaml.cs

GitHub Actions / Check Spelling

`GWLSTYLE` is not a recognized word. (unrecognized-spelling)

[Flags]
public enum WindowStyles : uint
{
WS_SYSMENU = 0x00080000, // System menu (close/maximize/minimize button area)
}
}
}
Loading
Oops, something went wrong.