using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;
namespace Foo.GUILib
{
static public class WPFHelper
{
///
/// Useful to create a simple wrapping tooltip for a wpf control
///
///
/// Set to 0 to leave default, else set to 0> to define max height
/// Set to 0 to leave default, else set to 0> to define max width
/// a ToolTip Object to assign to a control
public static ToolTip CreateNewStringToolTip(string strToolTipContent, int height, int width)
{
// Create the TextBlock
TextBlock tx = new TextBlock();
if(height > 0)
tx.Height = height;
if(width > 0)
tx.Width = width;
tx.TextWrapping = TextWrapping.Wrap;
tx.Text = strToolTipContent;
// Create the ToolTip with the TextBlock inside
ToolTip tt = new ToolTip();
tt.Content = strToolTipContent;
return tt;
}
///
/// Useful if you want to know if the Mouse cursor is over the specified Wpf Window
/// (Uses windowsforms to do the calculations)
///
/// true if the mouse cursor is over the specified form window, false otherwise
public static bool IsMouseCursorOverWPFormWindow(Window wpfWindow)
{
if (wpfWindow != null)
{
// Get the Point location of the mouse cursor
System.Drawing.Point point = System.Windows.Forms.Cursor.Position;
// Get the rect for the Navigation Form
Point wpfFormLeftTopPoint = new Point(wpfWindow.Left, wpfWindow.Top);
Size wpfFormSize = new Size(wpfWindow.Width, wpfWindow.Height);
Rect rect = new Rect(wpfFormLeftTopPoint, wpfFormSize);
// Return if the Mouse is over the Navigation Form
return (rect.Contains((double)point.X, (double)point.Y));
}
return false;
}
///
/// For Dynamically Creating Text ComboBox Items
///
/// Text Content to Display
/// object to tag to the menu item
///
public static ComboBoxItem CreateAComboBoxItem(string strContent, object tag)
{
if (!String.IsNullOrEmpty(strContent) && (tag != null))
{
ComboBoxItem item = new ComboBoxItem();
item.Content = strContent;
item.Tag = tag;
return item;
}
return null;
}
///
/// For dynamically creating Image sources, this function helps you to create an image out of
/// a Resource Uri. You can then assign this image to a 'Source' property on an image wpf object
///
/// a valid relative Resource Uri String to an image
/// the output image's desired height
/// the output image's desired width
///
public static Image CreateWPFImageFromRelativeResourceUri(string strRelativeResourceUri, int ImageHeight, int ImageWidth)
{
if (!String.IsNullOrEmpty(strRelativeResourceUri))
{
Image myImage = new Image();
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(strRelativeResourceUri, UriKind.Relative);
image.EndInit();
myImage.Stretch = Stretch.Fill;
myImage.Height = ImageHeight;
myImage.Width = ImageWidth;
myImage.Source = image;
return myImage;
}
return null;
}
}
}