Skip to content

Commit

Permalink
Merge branch 'release-1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Otiel committed Jun 14, 2019
2 parents c3c4eb9 + 6595304 commit 98aafea
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 1.2.0

# New features

* Default button texts (Ok, Cancel, Yes, No) are now localized by looking for the localized strings in user32.dll.
* Hitting Ctrl-C while focus is on the message box will save its content to the clipboard.

# 1.1.1

## Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WpfMessageBox
<h1 align="center"><img alt="Icon" src="docs/images/Icon.32x32.png"> WpfMessageBox</h1>

[![Nuget](https://img.shields.io/nuget/v/wpfmessagebox.svg?logo=nuget)](https://www.nuget.org/packages/WpfMessageBox) [![build:master status](https://img.shields.io/appveyor/ci/Otiel/wpfmessagebox/master.svg?label=build:master&logo=appveyor)](https://ci.appveyor.com/project/Otiel/wpfmessagebox/branch/master) [![build:develop status](https://img.shields.io/appveyor/ci/Otiel/wpfmessagebox/develop.svg?label=build:develop&logo=appveyor)](https://ci.appveyor.com/project/Otiel/wpfmessagebox/branch/develop)

Expand Down
Binary file added docs/images/Icon.16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/Icon.32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/WpfMessageBox/Core/WindowMain.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
MinHeight="150"
MaxWidth="1000"
x:ClassModifier="internal"
KeyDown="Window_KeyDown"
ResizeMode="NoResize"
ShowInTaskbar="False"
SizeToContent="WidthAndHeight"
Expand Down
18 changes: 14 additions & 4 deletions src/WpfMessageBox/Core/WindowMain.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Input;

namespace WpfMessageBoxLibrary {

Expand Down Expand Up @@ -84,10 +85,10 @@ public WindowMain(string message, MessageBoxButton button, MessageBoxImage image
DisplayImage(image);

// Set default values
ButtonCancelText = "_Cancel";
ButtonNoText = "_No";
ButtonOkText = "_OK";
ButtonYesText = "_Yes";
ButtonCancelText = LocalizationHelper.GetCancelButtonText();
ButtonNoText = LocalizationHelper.GetNoButtonText();
ButtonOkText = LocalizationHelper.GetOkButtonText();
ButtonYesText = LocalizationHelper.GetYesButtonText();
CheckBoxText = "";
Header = "";
IsCheckBoxChecked = false;
Expand Down Expand Up @@ -201,5 +202,14 @@ private void SetDefaultAndCancelButtons(MessageBoxButton button) {
throw new NotImplementedException();
}
}

private void Window_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control) {
string content = String.IsNullOrWhiteSpace(Header) ? "" : Header + Environment.NewLine + Environment.NewLine;
content += Message;

Clipboard.SetText(content);
}
}
}
}
94 changes: 94 additions & 0 deletions src/WpfMessageBox/Helpers/LocalizationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace WpfMessageBoxLibrary {

internal static class LocalizationHelper {

private class UserStringNotFoundException: Exception {
}

private const string _defaultCancelButtonText = "_Cancel";
private const string _defaultNoButtonText = "_No";
private const string _defaultOkButtonText = "_OK";
private const string _defaultYesButtonText = "Yes";

/// <summary>
/// Returns the localized string of the native MessageBox "Cancel" button from user32.dll.
/// </summary>
public static string GetCancelButtonText() {
try {
return GetUserString(801);
} catch (UserStringNotFoundException) {
return _defaultCancelButtonText;
}
}

/// <summary>
/// Returns the localized string of the native MessageBox "No" button from user32.dll.
/// </summary>
public static string GetNoButtonText() {
try {
return GetUserString(806);
} catch (UserStringNotFoundException) {
return _defaultNoButtonText;
}
}

/// <summary>
/// Returns the localized string of the native MessageBox "OK" button from user32.dll.
/// </summary>
public static string GetOkButtonText() {
try {
return GetUserString(800);
} catch (UserStringNotFoundException) {
return _defaultOkButtonText;
}
}

/// <summary>
/// Returns the localized string of the native MessageBox "Yes" button from user32.dll.
/// </summary>
public static string GetYesButtonText() {
try {
return GetUserString(805);
} catch (UserStringNotFoundException) {
return _defaultYesButtonText;
}
}

/// <summary>
/// Returns a user String from user32.dll. See the list of ID: http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.kernel/2010-02/msg00129.html
/// </summary>
/// <param name="stringId">The id of the String.</param>
private static string GetUserString(uint stringId) {
IntPtr libraryHandle = GetModuleHandle("user32.dll");
if (libraryHandle == IntPtr.Zero) {
throw new UserStringNotFoundException();
}

var sb = new StringBuilder(1024);
int size = LoadString(libraryHandle, stringId, sb, 1024);

if (size > 0) {
// Replace Win32 mnemonic with WPF mnemonic
sb.Replace('&', '_');

return sb.ToString();
} else {
throw new UserStringNotFoundException();
}
}

#region Dll Imports

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetModuleHandle(string lpModuleName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax);

#endregion
}
}
4 changes: 2 additions & 2 deletions src/WpfMessageBox/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 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.1.0")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyVersion("1.2.0")]
[assembly: AssemblyFileVersion("1.2.0")]
1 change: 1 addition & 0 deletions src/WpfMessageBox/WpfMessageBox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Compile Include="Core\WpfMessageBox.cs" />
<Compile Include="Core\WpfMessageBoxProperties.cs" />
<Compile Include="Helpers\Extensions.cs" />
<Compile Include="Helpers\LocalizationHelper.cs" />
<Compile Include="Helpers\WindowHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Core\WindowMain.xaml.cs">
Expand Down

0 comments on commit 98aafea

Please sign in to comment.