本文主要是介绍鼠标在一段时间内静止贴出提示标签的效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
后台代码:<span style="font-family: Arial, Helvetica, sans-serif;">public partial class MainWindow : Window</span>
{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 鼠标悬浮在按钮上1sprivate 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}
<span style="font-family: Arial, Helvetica, sans-serif;">前台代码:</span><span style="font-family: Arial, Helvetica, sans-serif;"><pre name="code" class="html"><Window x:Class="MouseEnterMakeTag.MainWindow"</span>
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>
这篇关于鼠标在一段时间内静止贴出提示标签的效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!