WPF 后台代码判断鼠标移动的问题

Veary 2014-10-28 10:34:05
如题,想做一个提示的效果,鼠标移入某Button(Botton1),若是鼠标在Button1中不停的移动则无反应,若是鼠标静止不动3秒,弹出提示,问题来了,前边的这个逻辑怎么写呢?

自己乱写了一些,现在觉得摸不到头脑,对这种逻辑的研究很欠缺

<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;
}

}
}


求指教..
...全文
948 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 10 楼 u011785544 的回复:
[quote=引用 9 楼 duanzi_peng 的回复:] 。。。。。。。。。有时间吧。
<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] 此贴 果断收藏了!!!!
Veary 2014-10-29
  • 打赏
  • 举报
回复
引用 9 楼 duanzi_peng 的回复:
。。。。。。。。。有时间吧。
<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
        
    }
已解决。
Veary 2014-10-28
  • 打赏
  • 举报
回复
有木有人 有木有人 有木有人 人工置顶 人工置顶 人工置顶
  • 打赏
  • 举报
回复
。。。。。。。。。有时间吧。
Veary 2014-10-28
  • 打赏
  • 举报
回复
引用 7 楼 duanzi_peng 的回复:
[quote=引用 6 楼 u011785544 的回复:]
[quote=引用 5 楼 duanzi_peng 的回复:]
[quote=引用 4 楼 u011785544 的回复:]
[quote=引用 3 楼 duanzi_peng 的回复:]
WPF 还么有达到 能判断鼠标是否静止 这种程度。


上边那个也是在网上找的例子 貌似不实用 在一段时间内确定两个坐标是否一致这么做不可以吗?[/quote]
这个可以呀,就是你使用timer。不过你不觉得有漏洞么?
比如,timer开启,每隔3s执行。start的时候 记录下当前鼠标位置,2s的时候移动了一下,3s的时候回到原点,那么在这3s的间隔中,你能说你的鼠标静止了三秒??你通过timer记录的只是个坐标位置,是具体的值,值当然可以相同了。
但是 鼠标的状态 是要从开始到结束都保持不变。[/quote]

这可咋搞 真是的 用别的技术能实现么?比如API啥的?[/quote]
没头绪。[/quote]




按照上边的Timer的例子,现在也是不对,能不能帮我改一下,那个误差可以忽略它
  • 打赏
  • 举报
回复
引用 6 楼 u011785544 的回复:
[quote=引用 5 楼 duanzi_peng 的回复:] [quote=引用 4 楼 u011785544 的回复:] [quote=引用 3 楼 duanzi_peng 的回复:] WPF 还么有达到 能判断鼠标是否静止 这种程度。
上边那个也是在网上找的例子 貌似不实用 在一段时间内确定两个坐标是否一致这么做不可以吗?[/quote] 这个可以呀,就是你使用timer。不过你不觉得有漏洞么? 比如,timer开启,每隔3s执行。start的时候 记录下当前鼠标位置,2s的时候移动了一下,3s的时候回到原点,那么在这3s的间隔中,你能说你的鼠标静止了三秒??你通过timer记录的只是个坐标位置,是具体的值,值当然可以相同了。 但是 鼠标的状态 是要从开始到结束都保持不变。[/quote] 这可咋搞 真是的 用别的技术能实现么?比如API啥的?[/quote] 没头绪。
Veary 2014-10-28
  • 打赏
  • 举报
回复
引用 5 楼 duanzi_peng 的回复:
[quote=引用 4 楼 u011785544 的回复:] [quote=引用 3 楼 duanzi_peng 的回复:] WPF 还么有达到 能判断鼠标是否静止 这种程度。
上边那个也是在网上找的例子 貌似不实用 在一段时间内确定两个坐标是否一致这么做不可以吗?[/quote] 这个可以呀,就是你使用timer。不过你不觉得有漏洞么? 比如,timer开启,每隔3s执行。start的时候 记录下当前鼠标位置,2s的时候移动了一下,3s的时候回到原点,那么在这3s的间隔中,你能说你的鼠标静止了三秒??你通过timer记录的只是个坐标位置,是具体的值,值当然可以相同了。 但是 鼠标的状态 是要从开始到结束都保持不变。[/quote] 这可咋搞 真是的 用别的技术能实现么?比如API啥的?
  • 打赏
  • 举报
回复
引用 4 楼 u011785544 的回复:
[quote=引用 3 楼 duanzi_peng 的回复:] WPF 还么有达到 能判断鼠标是否静止 这种程度。
上边那个也是在网上找的例子 貌似不实用 在一段时间内确定两个坐标是否一致这么做不可以吗?[/quote] 这个可以呀,就是你使用timer。不过你不觉得有漏洞么? 比如,timer开启,每隔3s执行。start的时候 记录下当前鼠标位置,2s的时候移动了一下,3s的时候回到原点,那么在这3s的间隔中,你能说你的鼠标静止了三秒??你通过timer记录的只是个坐标位置,是具体的值,值当然可以相同了。 但是 鼠标的状态 是要从开始到结束都保持不变。
Veary 2014-10-28
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
WPF 还么有达到 能判断鼠标是否静止 这种程度。
上边那个也是在网上找的例子 貌似不实用 在一段时间内确定两个坐标是否一致这么做不可以吗?
  • 打赏
  • 举报
回复
WPF 还么有达到 能判断鼠标是否静止 这种程度。
  • 打赏
  • 举报
回复
没法指教,你怎么能知道 鼠标静止了??

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧