前台
<UserControl x:Class="ComputerDropDragControl.TipWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" >
<!--<Grid.Resources>
<Storyboard x:Name="stortBoardOne" x:Key="storyBoardOne" Completed="stortBoardOne_Completed"></Storyboard>
</Grid.Resources>-->
<Popup x:Name="tip">
<Grid x:Name="toolTip" Width="300" Height="220" Opacity="0.8"
MouseLeave="LayoutRoot_MouseLeave" MouseMove="toolTip_MouseMove">
<Grid.Background>
<ImageBrush ImageSource="Img/tipWindowOne.png" x:Name="imageBrushOne"></ImageBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" x:Name="mainContent"
Foreground="Black" FontSize="14" Height="Auto" HorizontalAlignment="Left" Margin="10,10,10,0"
VerticalAlignment="Top" TextWrapping="Wrap" Width="200"/>
</Grid>
</Popup>
</Grid>
</UserControl>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Media.Imaging;
using System.Windows.Browser;
namespace ComputerDropDragControl
{
/// <summary>
/// 提示框
/// 创建人:吴兆娟
/// 创建时间:2011-11-4
/// </summary>
public partial class TipWindow : UserControl
{
#region <<页面加载>>
/// <summary>
/// 页面加载
/// </summary>
public TipWindow()
{
InitializeComponent();
//定时器
//this.stortBoardOne.Duration = new TimeSpan(0, 0, 0, 2, 0);
mainContent.Foreground = new SolidColorBrush(Colors.Black);
mainContent.Text = "这种ToolTip是是是到nsk电脑上开始看神道贺就" + DateTime.Now.Millisecond;
}
#endregion
#region <<控件事件>>
/ <summary>
/ “定时器完成时”发生的事件
/ </summary>
/ <param name="sender"></param>
/ <param name="e"></param>
//private void stortBoardOne_Completed(object sender, EventArgs e)
//{
//}
#endregion
#region <<辅助方法>>
public void BindControl(UIElement uc, NodeFrameView entity)
{
AimUI = uc;
uc.MouseLeave += new MouseEventHandler(ti_MouseLeave);
//uc.MouseEnter += new MouseEventHandler(uc_MouseEnter);
uc.MouseMove += new MouseEventHandler(ti_MouseMove);
}
public void CancelBindControl(UIElement uc)
{
this.tip.IsOpen = false;
uc.MouseLeave -= new MouseEventHandler(ti_MouseLeave);
uc.MouseMove -= new MouseEventHandler(ti_MouseMove);
}
/// <summary>
/// 鼠标离开自定义控件时,隐藏提示框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ti_MouseLeave(object sender, MouseEventArgs e)
{
this.tip.IsOpen = false;
Thread.Sleep(10);
}
/// <summary>
/// 鼠标在自定义控件上移动式时,提示框也跟随移动
/// </summary>
///<param name="sender"></param>
/// <param name="e"></param>
private void ti_MouseMove(object sender, MouseEventArgs e)
{
this.tip.IsOpen = true;
double width = Application.Current.Host.Content.ActualWidth;
double height = Application.Current.Host.Content.ActualHeight;
if (!Application.Current.IsRunningOutOfBrowser)//指示是否从浏览器外状态启动
{
//获取浏览器分辨率信息
ScriptObject screen = (ScriptObject)HtmlPage.Window.GetProperty("screen");
width = (double)screen.GetProperty("width");
height = (double)screen.GetProperty("height");
}
double mouseX = e.GetPosition(null).X;
double mouseY = e.GetPosition(null).Y;
double screenRight = width - mouseX - toolTip.Width - 20;
double screenTop = mouseY - toolTip.Height;
if (screenTop < 0 && screenRight > 0)
{
this.imageBrushOne.ImageSource = new BitmapImage(new Uri("Img/tipWindowTwo.png", UriKind.Relative));
this.tip.HorizontalOffset = e.GetPosition(null).X - 20;
this.tip.VerticalOffset = e.GetPosition(null).Y;
mainContent.Margin = new Thickness(10, 50, 10, 0);
}
else if (screenRight < 0 && screenTop > 0)
{
this.imageBrushOne.ImageSource = new BitmapImage(new Uri("Img/tipWindowFour.png", UriKind.Relative));
this.tip.HorizontalOffset = e.GetPosition(null).X - 275;
this.tip.VerticalOffset = e.GetPosition(null).Y - 220;
mainContent.Margin = new Thickness(10, 10, 10, 0);
}
else if (screenRight < 0 && screenTop < 0)
{
this.imageBrushOne.ImageSource = new BitmapImage(new Uri("Img/tipWindowThree.png", UriKind.Relative));
this.tip.HorizontalOffset = e.GetPosition(null).X - 275;
this.tip.VerticalOffset = e.GetPosition(null).Y;
mainContent.Margin = new Thickness(10, 50, 10, 0);
}
else
{
this.imageBrushOne.ImageSource = new BitmapImage(new Uri("Img/tipWindowOne.png", UriKind.Relative));
this.tip.HorizontalOffset = e.GetPosition(null).X - 200;
this.tip.VerticalOffset = e.GetPosition(null).Y - 220;
mainContent.Margin = new Thickness(10, 10, 10, 0);
}
}
#endregion
//private void tip_MouseMove(object sender, MouseEventArgs e)
//{
// this.tip.IsOpen = true;
//}
#region <<尝试>>
private UIElement AimUI;
private void LayoutRoot_MouseEnter(object sender, MouseEventArgs e)
{
//this.tip.IsOpen = true;
//MessageBox.Show("hello");
//this.tip.IsOpen = true;
//ComputerUnit cu = (ComputerUnit)AimUI;
//cu.MouseLeave -= new MouseEventHandler(ti_MouseLeave);
//cu.MouseMove -= new MouseEventHandler(ti_MouseMove);
}
private void LayoutRoot_MouseLeave(object sender, MouseEventArgs e)
{
this.tip.IsOpen = false;
}
void uc_MouseEnter(object sender, MouseEventArgs e)
{
ComputerUnit a = (ComputerUnit)sender;
this.tip.IsOpen = true;
this.tip.HorizontalOffset = e.GetPosition(null).X - 200;
this.tip.VerticalOffset = e.GetPosition(null).Y - 220;
}
private void toolTip_MouseMove(object sender, MouseEventArgs e)
{
this.tip.IsOpen = true;
}
#endregion
}
}