Skip to content

Commit

Permalink
Implement ApplicationCommands.Copy in MessageBox
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyAnsel committed Feb 24, 2024
1 parent de728b5 commit 77155db
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
Expand Down Expand Up @@ -48,6 +50,8 @@ static MessageBox()

public MessageBox()
{
CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, new ExecutedRoutedEventHandler(ExecuteCopy)));

SetValue(TemplateSettingsPropertyKey, new MessageBoxTemplateSettings());
var handler = new RoutedEventHandler((sender, e) => ApplyDarkMode());
ThemeManager.AddActualThemeChangedHandler(this, handler);
Expand All @@ -58,6 +62,48 @@ public MessageBox()
ThemeManager.AddActualThemeChangedHandler(this, ThemeManager_AddActualThemeChanged);
}

private void ExecuteCopy(object sender, ExecutedRoutedEventArgs e)
{
StringBuilder sb = new();
sb.Append("---------------------------");
sb.AppendLine();
sb.Append(Caption);
sb.AppendLine();
sb.Append("---------------------------");
sb.AppendLine();
sb.Append(Content);
sb.AppendLine();
sb.Append("---------------------------");
sb.AppendLine();
switch (MessageBoxButtons)
{
case MessageBoxButton.OK:
sb.Append(OKButtonText);
break;
case MessageBoxButton.OKCancel:
sb.Append(OKButtonText + " " + CancelButtonText);
break;
case MessageBoxButton.YesNo:
sb.Append(YesButtonText + " " + NoButtonText);
break;
case MessageBoxButton.YesNoCancel:
sb.Append(YesButtonText + " " + NoButtonText + " " + CancelButtonText);
break;
}
sb.AppendLine();
sb.Append("---------------------------");

try
{
new UIPermission(UIPermissionClipboard.AllClipboard).Demand();
Clipboard.SetText(sb.ToString());
}
catch (SecurityException)
{
throw new SecurityException();
}
}

private void ThemeManager_AddActualThemeChanged(object sender, RoutedEventArgs e)
{
if(WindowHelper.GetSystemBackdropType(this) != BackdropType.None)
Expand Down

0 comments on commit 77155db

Please sign in to comment.