zoukankan      html  css  js  c++  java
  • Button中MouseLeftButtonDown和MouseLeftButtonUp事件无法触发的解决方案

    鼠标事件处理实现简单的拖放功能。

    在实现拖放功能中,分为三个步骤:

    1.按下鼠标,触发 MouseLeftButtonDown 事件,选择要拖动的对象。

    2.移动鼠标,触发 MouseMove 事件,移动选择的对象。

    3.放开鼠标,触发 MouseLeftButtonUp 事件,停止捕捉事件。

    但是在实际运行过程中,只能触发MouseMove事件,不能触发MouseLeftButtonDown和MouseLeftButtonUp事件。经过查阅资料,发现这跟路由顺序有关,控件在捕获了MouseLeftButtonDown或MouseLeftButtonUp事件后,会将该事件的"Handled"设置为true,这个属性是用在路由事件中的,当某个控件得到一个RoutedEvent,就会检测Handled是否为true,为true则忽略该事件。并且,控件本身的Click事件,相当于将MouseLeftButtonDown和MouseLeftButtonUp事件抑制掉了,转换成了Click事件。

    解决方案一(部分解决问题):

    设置Button的ClickMode 属性:

    成员名称

    说明

    Release

    指定当按下并松开鼠标左键且鼠标指针位于控件上方时应引发 Click 事件。如果使用的是键盘,则指定在按下并松开空格键或 Enter 键且控件具有键盘焦点时应引发 Click 事件。

    Press

    指定在按下鼠标按键且鼠标指针位于控件上方时应引发 Click 事件。如果使用的是键盘,则指定在按下空格键或 Enter 且控件具有键盘焦点时应引发 Click 事件。

    Hover

    指定当鼠标指针在控件上移动时应引发 Click 事件。

    但是此方法并不能完全解决问题,能达到触发事件的目的,但是不能实现所需的拖放功能。

    解决方案二(完美解决问题):

    在初始化的函数里利用UIElement的AddHandler方法,显式的增加这个事件。

    方法说明:

    UIElement.AddHandler方法 (RoutedEvent, Delegate, Boolean)

    为指定的路由事件添加路由事件处理程序,并将该处理程序添加到当前元素的处理程序集合中。将 handledEventsToo 指定为 true 时,可为已标记为由其他元素在事件路由过程中处理的路由事件调用所提供的处理程序。

    参数说明:

    routedEvent

    类型:System.Windows..::..RoutedEvent

    要处理的路由事件的标识符。

    handler

    类型:System..::..Delegate

    对处理程序实现的引用。

    handledEventsToo

    类型:System..::.Boolean

    如果为 true,则将按以下方式注册处理程序:即使路由事件在其事件数据中标记为已处理,也会调用该处理程序;如果为 false,则使用默认条件注册处理程序,即当路由事件被标记为已处理时,将不调用处理程序。

    默认值为 false。

    初始化函数里面添加的代码:

    1bt1.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.Button_MouseLeftButtonDown), true);
    2 bt1.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(this.Button_MouseLeftButtonUp), true);
    3

    完整代码:

    前台:

    代码
    <UserControl x:Class="sample.s5"
    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">

    <Canvas>
    <Button Canvas.Left="50" Canvas.Top="50" Width="200" Height="120" FontSize="20" x:Name="bt1"
    MouseLeftButtonDown
    ="Button_MouseLeftButtonDown"
    MouseMove
    ="Button_MouseMove"
    MouseLeftButtonUp
    ="Button_MouseLeftButtonUp"
    Click
    ="bt1_Click" ClickMode="Release">
    <Button.Content>
    <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Image Source="HandPrints.jpg" Width="200" Height="70"></Image>
    <TextBlock Text="拖动我" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>

    </StackPanel>
    </Button.Content>

    </Button>
    <TextBlock Text="测a试?" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" MouseMove="TextBlock_MouseMove" MouseLeftButtonUp="TextBlock_MouseLeftButtonUp" Height="35" Width="85"></TextBlock>
    </Canvas>
    </UserControl>

    后台:

    代码
    1 public partial class s5 : UserControl
    2 {
    3 bool trackMouseMove = false;
    4 Point mousePostion;
    5
    6 public s5()
    7 {
    8 InitializeComponent();
    9 bt1.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.Button_MouseLeftButtonDown), true);
    10 bt1.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(this.Button_MouseLeftButtonUp), true);
    11 }
    12
    13 private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    14 {
    15 FrameworkElement element = (FrameworkElement)sender;
    16
    17 //指定相对于整个Silverlight 坐标括系的对象
    18   mousePostion = e.GetPosition(null);
    19
    20 trackMouseMove = true;
    21 if (element != null)
    22 {
    23 element.CaptureMouse();
    24 element.Cursor = Cursors.Hand;
    25 }
    26 }
    27
    28 private void Button_MouseMove(object sender, MouseEventArgs e)
    29 {
    30 FrameworkElement element = (FrameworkElement)sender;
    31 if (trackMouseMove)
    32 {
    33 double deltaV = e.GetPosition(null).Y - mousePostion.Y;
    34 double deltaH = e.GetPosition(null).X - mousePostion.X;
    35 double newTop = deltaV + (double)element.GetValue(Canvas.TopProperty);
    36 double newLeft = deltaH + (double)element.GetValue(Canvas.LeftProperty);
    37
    38 element.SetValue(Canvas.TopProperty, newTop);
    39 element.SetValue(Canvas.LeftProperty, newLeft);
    40
    41 mousePostion = e.GetPosition(null);
    42 }
    43 }
    44
    45 private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    46 {
    47 FrameworkElement element = (FrameworkElement)sender;
    48 trackMouseMove = false;
    49 element.ReleaseMouseCapture();
    50 mousePostion.X = mousePostion.Y = 0;
    51 element.Cursor = null;
    52 }
    53 }
    54  
  • 相关阅读:
    How to alter department in PMS system
    Can't create new folder in windows7
    calculate fraction by oracle
    Long Wei information technology development Limited by Share Ltd interview summary.
    ORACLE BACKUP AND RECOVERY
    DESCRIBE:When you mouse click right-side is open an application and click left-side is attribution.
    ORACLE_TO_CHAR Function
    电脑BOIS设置
    JSP点击表头排序
    jsp+js实现可排序表格
  • 原文地址:https://www.cnblogs.com/junyuz/p/1921672.html
Copyright © 2011-2022 走看看