using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;
using System.Threading;
using System.Windows.Threading;
using System.Globalization;
namespace GummingCommon
{
///
/// This class is common message box dialog
///
public partial class MessageBoxServiceView : BaseDialog
{
///
/// Initialize for this Class
///
public MessageBoxServiceView()
{
InitializeComponent();
}
private void MessageBoxServiceView_Loaded(object sender, RoutedEventArgs e)
{
try
{
double heightOffset = this.ActualHeight - TextBoxBorder.ActualHeight;
double widthOffset = this.ActualWidth - TextBoxBorder.ActualWidth;
//Find the ideal width (using the longest word)
string longestString = txtMessage.Text.Split(' ', '\n').OrderByDescending(s => s.Length).FirstOrDefault();
double idealWidth;
if (!string.IsNullOrWhiteSpace(longestString))
{
idealWidth = MeasureString(txtMessage, longestString).Width + widthOffset + txtMessage.Padding.Left + txtMessage.Padding.Right + txtMessage.Margin.Left + txtMessage.Margin.Right + 15; //Add a little just in case
}
else
idealWidth = this.MinWidth;
idealWidth = ClipLength(idealWidth, this.MinWidth, this.MaxWidth);
//Measure the desire height using the ideal width
txtMessage.Measure(new Size(idealWidth - widthOffset, double.MaxValue));
double idealHeight = txtMessage.DesiredSize.Height + heightOffset + 15 + txtMessage.Padding.Top + txtMessage.Padding.Bottom + txtMessage.Margin.Top + txtMessage.Margin.Bottom; //Add a little just in case;
//If vertical scrollbar is going to be shown, then remeasure with scrollbar width
if (idealHeight > this.MaxHeight)
{
idealWidth = ClipLength(idealWidth + SystemParameters.VerticalScrollBarWidth + 2, this.MinWidth, this.MaxWidth);
txtMessage.Measure(new Size(idealWidth - widthOffset, double.MaxValue));
idealHeight = txtMessage.DesiredSize.Height + heightOffset + 5;
}
double width = ClipLength(idealWidth, this.MinWidth, this.MaxWidth); ;
double height = ClipLength(idealHeight, this.MinHeight, this.MaxHeight);
//recalculate the window's top/left
this.Left -= (width - this.ActualWidth) / 2;
this.Top -= (height - this.ActualHeight) / 2;
this.Width = width;
this.Height = height;
}
catch
{
//Ignore exception since it is just purely cosmetic
}
}
private static double ClipLength(double value, double min, double max)
{
value = Math.Max(value, min);
value = Math.Min(value, max);
return value;
}
private static Size MeasureString(TextBox tb, string s)
{
FormattedText formattedText = new FormattedText(
s,
//new string('O', s.Length),
CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface(tb.FontFamily, tb.FontStyle, tb.FontWeight, tb.FontStretch),
tb.FontSize,
Brushes.Black) { MaxLineCount = 1, Trimming = TextTrimming.None } ;
return new Size(formattedText.Width, formattedText.Height);
}
///
/// for to resolve the problem : How to open same window after it has closed?
///
///
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
//if (CanMuchOpened)
{
typeof(Window).GetField("_isClosing", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this, false);
e.Cancel = true;
Hide();
}
}
}
}