using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using System.Windows; using System.Windows.Media.Imaging; using System.Windows.Media; using System.Windows.Interop; namespace Yaulw.WPF { /// /// Useful WPF Snippets /// static public class Snippets { /// /// 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; } /// /// Uses the InteropHelper to get the Handle of a WPF Window /// /// a WPF Window /// an hWnd for the specified WPF Window public static IntPtr GetHandleForWPFWindow(Window window) { WindowInteropHelper InteropHelper = new WindowInteropHelper(window); if (InteropHelper.Handle != IntPtr.Zero) return InteropHelper.Handle; #if NET4 else return InteropHelper.EnsureHandle(); #else else return IntPtr.Zero; #endif } /// /// Uses the InteropHelper to set the Owner Property of a WPF Window /// /// a WPF Window /// Owner hWnd Handle to set for WPF Window public static void SetOwnerForWPFWindow(Window window, IntPtr hOwnerWnd) { WindowInteropHelper InteropHelper = new WindowInteropHelper(window); InteropHelper.Owner = hOwnerWnd; } /// /// Adds the Specified Message Hook the the specified WPF Window. /// Ensures that the WPF Window exists (must be loaded with valid hWnd). /// /// a WPF Window /// a message hook function public static void SetMessageHookForWPFWindow(Window window, HwndSourceHook hook) { WindowInteropHelper interop = new WindowInteropHelper(window); #if NET4 HwndSource source = HwndSource.FromHwnd(interop.EnsureHandle()); #else HwndSource source = HwndSource.FromHwnd(interop.Handle); #endif source.AddHook(hook); } } }