A Windows Forms RTL MessageBox implementation that wraps native Win32 MessageBox with automatic Persian button text translation and custom font support. Uses Win32 hooks for real-time button translation while maintaining full compatibility with standard MessageBox API.
$ dotnet add package Barnamenevis.Net.RtlMessageBox.WindowsFormsA Windows Forms-compatible RTL MessageBox implementation that wraps the native Win32 MessageBox with automatic Persian button text translation and custom font support.
This library provides a drop-in replacement for the standard Windows Forms MessageBox with full RTL (Right-to-Left) support, automatic Persian button translation, and custom font integration. It uses Win32 hooks to modify the native MessageBox at runtime, ensuring compatibility with all Windows MessageBox features.
using Barnamenevis.Net.RtlMessageBox.WindowsForms;
// Simple message
var result = RtlMessageBox.Show("سلام دنیا!");
// With title
var result = RtlMessageBox.Show("پيغام شما ارسال شد.", "موفقیت");
// Yes/No question
var result = RtlMessageBox.Show(
"آیا مطمئن هستید که میخواهید ادامه دهید؟",
"تأیید عملیات",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
// Configure font settings (typically in Form constructor or Main method)
RtlMessageBox.PreferredFontName = "Vazirmatn FD";
RtlMessageBox.PreferredFontPointSize = 10.0;
RtlMessageBox.ApplyCustomFont = true;
// Now all MessageBoxes will use the custom font
var result = RtlMessageBox.Show("این متن با فونت دلخواه نمایش داده میشود.");
| Property | Type | Default | Description |
|---|---|---|---|
PreferredFontName | string | "Vazirmatn FD" | Font family name for dialog text |
PreferredFontPointSize | double | 10.0 | Font size in points |
ApplyCustomFont | bool | true | Enable/disable custom font application |
All standard WPF MessageBox.Show overloads are supported for compatibility:
MessageBoxResult Show(string messageBoxText)
MessageBoxResult Show(string messageBoxText, string caption)
MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options)
MessageBoxResult Show(Window owner, string messageBoxText)
MessageBoxResult Show(Window owner, string messageBoxText, string caption)
MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button)
MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options)
The library uses a CBT (Computer-Based Training) Hook to intercept MessageBox creation:
// Creates a custom font using Win32 CreateFontIndirect
private static IntPtr CreateDialogFont(string faceName, double pointSize)
{
// Calculate proper font height based on screen DPI
// Create LOGFONT structure with Persian font settings
// Return font handle for use with WM_SETFONT
}
| Button ID | Persian Text | English Equivalent |
|---|---|---|
| IDOK (1) | تایید | OK |
| IDCANCEL (2) | انصراف | Cancel |
| IDYES (6) | بله | Yes |
| IDNO (7) | خیر | No |
| IDRETRY (4) | تلاش مجدد | Retry |
| IDIGNORE (5) | نادیده گرفتن | Ignore |
| IDABORT (3) | قطع | Abort |
| IDTRYAGAIN (10) | تلاش مجدد | Try Again |
| IDCONTINUE (11) | ادامه | Continue |
try
{
// Some operation that might fail
PerformOperation();
}
catch (Exception ex)
{
RtlMessageBox.Show(
$"خطا در انجام عملیات: {ex.Message}",
"خطا",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
private void DeleteButton_Click(object sender, EventArgs e)
{
var result = RtlMessageBox.Show(
"آیا مطمئن هستید که میخواهید این فایل را حذف کنید؟\nاین عملیات قابل بازگشت نیست.",
"تأیید حذف",
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
DeleteFile();
}
}
private void FormClosing_Handler(object sender, FormClosingEventArgs e)
{
if (HasUnsavedChanges)
{
var result = RtlMessageBox.Show(
"تغییرات ذخیره نشدهای وجود دارد. آیا میخواهید آنها را ذخیره کنید؟",
"ذخیره تغییرات",
MessageBoxButton.YesNoCancel,
MessageBoxImage.Question);
switch (result)
{
case MessageBoxResult.Yes:
SaveChanges();
break;
case MessageBoxResult.No:
// Continue closing without saving
break;
case MessageBoxResult.Cancel:
e.Cancel = true; // Cancel the close operation
break;
}
}
}
private void ShowAbout()
{
RtlMessageBox.Show(
"نرمافزار مدیریت فایل\nنسخه 1.0.0\n\nتوسعه یافته توسط تیم برنامهنویس",
"درباره برنامه",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
This library works perfectly with Windows Forms Designer-created applications:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// Configure RTL MessageBox
RtlMessageBox.PreferredFontName = "Vazirmatn FD";
RtlMessageBox.ApplyCustomFont = true;
}
private void saveButton_Click(object sender, EventArgs e)
{
// Use RTL MessageBox instead of standard MessageBox
RtlMessageBox.Show("فایل با موفقیت ذخیره شد.", "موفقیت");
}
}
Set up font configuration once in your application:
// In Program.cs or Main method
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Configure RTL MessageBox globally
RtlMessageBox.PreferredFontName = "Vazirmatn FD";
RtlMessageBox.PreferredFontPointSize = 9.0;
RtlMessageBox.ApplyCustomFont = true;
Application.Run(new MainForm());
}
The library implements smart default button selection:
The library automatically applies RTL options to the native MessageBox:
private static MessageBoxOptions AddRtl(MessageBoxOptions options)
{
return options | MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign;
}
Works seamlessly with the FontInstaller utility:
// In Program.cs
static void Main()
{
// Install fonts at application startup
try
{
var installedCount = FontInstaller.InstallApplicationFonts();
if (installedCount > 0)
{
// Configure RTL MessageBox to use newly installed fonts
RtlMessageBox.PreferredFontName = "Vazirmatn FD";
}
}
catch
{
// Fall back to system fonts if installation fails
}
Application.Run(new MainForm());
}