8,757
社区成员
发帖
与我相关
我的任务
分享<Window x:Class="MouseEnter_Tag_.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="MainBorder_yaowo_RD" TargetType="Border">
<Setter Property="Border.CornerRadius" Value="5"></Setter>
<Setter Property="Border.Background" Value="#0e0e0e"></Setter>
<Setter Property="Border.VerticalAlignment" Value="Top"></Setter>
<Setter Property="Border.HorizontalAlignment" Value="Left"></Setter>
<Setter Property="Grid.ColumnSpan" Value="6"></Setter>
<Setter Property="Grid.Row" Value="1"></Setter>
</Style>
</Window.Resources>
<Grid Name="MainGrid_Window" Background="DarkGreen">
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="8*" />
</Grid.RowDefinitions>
<Button Background="YellowGreen" Width="80" Height="80" HorizontalAlignment="Left" Name="MainButton" MouseEnter="MainButton_MouseEnter_1" Margin="20,0,0,0" Grid.Row="1">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
<Label VerticalAlignment="Top" Height="50" VerticalContentAlignment="Center" Name="Label1" Grid.Row="0" Background="Aqua"></Label>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace MouseEnter_Tag_
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
///
public partial class MainWindow : Window
{
int x, y;
DispatcherTimer timer = new DispatcherTimer();
DispatcherTimer timer1 = new DispatcherTimer();
DateTime timeNow;
Boolean bon = false;
List<Border> borderList = null;
public MainWindow()
{
InitializeComponent();
Label1.Content = DateTime.Now.ToString();
DispatcherTimer timer3 = new DispatcherTimer();
timer3.Interval = TimeSpan.FromSeconds(1);
timer3.Tick += timer3_Tick;
timer3.Start();
}
private void MainButton_MouseEnter_1(object sender, MouseEventArgs e)
{
x = (int)(getPoint().X);
y = (int)(getPoint().Y);
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(3);
timer.Start();
}
void timer3_Tick(object sender, EventArgs e)
{
Label1.Content = DateTime.Now.ToString();
}
private Point getPoint()
{
return Mouse.GetPosition(this);
}
void timer_Tick(object sender, EventArgs e)
{
int _x = (int)(getPoint().X);
int _y = (int)(getPoint().Y);
if ((x == _x) && (y == _y) && bon == false)
{
timeNow = DateTime.Now;
bon = true;
timer.Stop();
}
else if (x != _x || y != _y)
{
x = _x;
y = _y;
timeNow = DateTime.Now;
bon = false;
}
TimeSpan ts = DateTime.Now.Subtract(timeNow);
if (ts.Seconds >= 3 && bon == true)
{
MessageBox.Show("鼠标静止了" + ts.Seconds + "秒");
//borderList = GetChildObjects<Border>(MainGrid_Window);
//if (borderList == null)
//{
// Border border = new Border();
// TextBlock textblock = new TextBlock();
// textblock.Text = "提示";
// textblock.FontFamily = new System.Windows.Media.FontFamily("微软雅黑");
// textblock.FontSize = 12.45;
// textblock.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#8F8F8F"));
// textblock.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
// textblock.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
// textblock.Margin = new Thickness(7, 2, 7, 2);
// border.Child = textblock;
// border.Name = "MainBorder_yaowo";
// border.Margin = new Thickness(mouseposition.X, mouseposition.Y + 19, 0, 0);
// border.Style = (Style)Application.Current.Resources["MainBorder_yaowo_RD"];
// this.MainGrid_Window.Children.Add(border);
//}
}
}
public static List<T> GetChildObjects<T>(DependencyObject obj) where T : FrameworkElement
{
DependencyObject child = null;
List<T> childList = new List<T>();
for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T)
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects<T>(child));
}
return childList;
}
}
}
<Window x:Class="MouseEnterMakeTag.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" >
<Grid MouseEnter="Grid_MouseEnter">
<Button Content="按钮1" Height="61" HorizontalAlignment="Left" Margin="12,12,0,0" Name="btnOK" VerticalAlignment="Top" Width="61" MouseMove="btnOK_MouseMove" MouseLeave="btnOK_MouseLeave" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="371,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Button Content="按钮2" Height="61" HorizontalAlignment="Left" Margin="12,79,0,0" Name="btnCancel" VerticalAlignment="Top" Width="61" MouseMove="button1_MouseMove" MouseLeave="button1_MouseLeave" />
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
DispatcherTimer dtimer = new DispatcherTimer();
Point p;
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
Point p = Mouse.GetPosition(e.Source as IInputElement);
textBox1.Text = p.X.ToString() + ", " + p.Y.ToString();
}
#region 鼠标悬浮在按钮上1s
private void startTimer(object sender, MouseEventArgs e)
{
if (dtimer.IsEnabled == true)
{
dtimer.Stop();
}
dtimer = new DispatcherTimer();
dtimer.Interval = TimeSpan.FromMilliseconds(1000);
dtimer.Tick += show;
dtimer.Start();
dtimer.Tag = sender;
p = e.GetPosition(e.Source as IInputElement);
}
private void stopTimer()
{
if (dtimer.IsEnabled == true)
{
dtimer.Stop();
}
}
private void show(object o, EventArgs e)
{
DispatcherTimer objTimer = (DispatcherTimer)o;
Button objButton = (Button)objTimer.Tag;
MessageBox.Show(p.X.ToString() + "," + p.Y.ToString() + "\n" + objButton.Content.ToString());
dtimer.Stop();
}
#endregion
#region 各个按钮的悬浮事件、鼠标离开事件
private void btnOK_MouseMove(object sender, MouseEventArgs e)
{
startTimer(sender, e);
}
private void btnOK_MouseLeave(object sender, MouseEventArgs e)
{
stopTimer();
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
startTimer(sender, e);
}
private void button1_MouseLeave(object sender, MouseEventArgs e)
{
stopTimer();
}
#endregion
}
已解决。[/quote]
此贴 果断收藏了!!!!<Window x:Class="MouseEnterMakeTag.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" >
<Grid MouseEnter="Grid_MouseEnter">
<Button Content="按钮1" Height="61" HorizontalAlignment="Left" Margin="12,12,0,0" Name="btnOK" VerticalAlignment="Top" Width="61" MouseMove="btnOK_MouseMove" MouseLeave="btnOK_MouseLeave" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="371,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Button Content="按钮2" Height="61" HorizontalAlignment="Left" Margin="12,79,0,0" Name="btnCancel" VerticalAlignment="Top" Width="61" MouseMove="button1_MouseMove" MouseLeave="button1_MouseLeave" />
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
DispatcherTimer dtimer = new DispatcherTimer();
Point p;
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
Point p = Mouse.GetPosition(e.Source as IInputElement);
textBox1.Text = p.X.ToString() + ", " + p.Y.ToString();
}
#region 鼠标悬浮在按钮上1s
private void startTimer(object sender, MouseEventArgs e)
{
if (dtimer.IsEnabled == true)
{
dtimer.Stop();
}
dtimer = new DispatcherTimer();
dtimer.Interval = TimeSpan.FromMilliseconds(1000);
dtimer.Tick += show;
dtimer.Start();
dtimer.Tag = sender;
p = e.GetPosition(e.Source as IInputElement);
}
private void stopTimer()
{
if (dtimer.IsEnabled == true)
{
dtimer.Stop();
}
}
private void show(object o, EventArgs e)
{
DispatcherTimer objTimer = (DispatcherTimer)o;
Button objButton = (Button)objTimer.Tag;
MessageBox.Show(p.X.ToString() + "," + p.Y.ToString() + "\n" + objButton.Content.ToString());
dtimer.Stop();
}
#endregion
#region 各个按钮的悬浮事件、鼠标离开事件
private void btnOK_MouseMove(object sender, MouseEventArgs e)
{
startTimer(sender, e);
}
private void btnOK_MouseLeave(object sender, MouseEventArgs e)
{
stopTimer();
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
startTimer(sender, e);
}
private void button1_MouseLeave(object sender, MouseEventArgs e)
{
stopTimer();
}
#endregion
}
已解决。
有木有人 有木有人 有木有人 人工置顶 人工置顶 人工置顶