如何在控件外捕获Mouse Down事件? [英] How to capture Mouse Down event outside a control?

查看:94
本文介绍了如何在控件外捕获Mouse Down事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mouse Capture不起作用:

  public   partial   class  MainWindow:Window 
{
public MainWindow( )
{
InitializeComponent();

Button button1 = new Button();
button1.Width = 50 ;
button1.Height = 20 ;

this .Content = button1;

Mouse.Capture(button1);
button1.PreviewMouseDown + = Down;
}

private void Down( object sender,MouseButtonEventArgs e)
{
MessageBox.Show( Hello World!); // 不显示
}
}

解决方案

请看我对这个问题的评论。



你正试图将鼠标捕获到甚至没有渲染的元素。怎么可能有用呢?有时应捕获鼠标,但应该只在相对较短的时间内暂时完成。例如,如果在其 MouseDown 事件中捕获鼠标,则可以实现类似按钮的行为,以便能够处理 MouseUp event(例如),即使鼠标指针离开元素的边界,但是应该立即在第二个事件的句柄中取消捕获鼠标。这只是一个例子。



请参阅:

https://msdn.microsoft.com/en-us/library/system.windows.uielement.capturemouse%28v=vs .110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.input.mouse.capture%28v=vs.110 %29.aspx [ ^ ]。



请仔细阅读备注部分。有问题吗?



-SA


将代码替换为下一代以使其正常工作:

  public   partial   class  MainWindow:Window 
{
public MainWindow()
{
的InitializeComponent();

Button button1 = new Button();
button1.Width = 50 ;
button1.Height = 20 ;

this .Content = button1;

this .Loaded + = delegate {Mouse.Capture(button1); };
button1.PreviewMouseDown + = Down;
}

private void Down( object sender,MouseButtonEventArgs e)
{
MessageBox.Show( Hello World!); // 显示
}
}


Mouse Capture doesn't work:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Button button1 = new Button();
        button1.Width = 50;
        button1.Height = 20;

        this.Content = button1;

        Mouse.Capture(button1);
        button1.PreviewMouseDown += Down;
    }

    private void Down(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("Hello World!"); // doesn't show
    }
}

解决方案

Please see my comment to the question.

You are trying to capture the mouse to the element which is not even rendered. How could it possibly work? Mouse should be captured sometimes, but it should be done only temporarily, for relatively short period of time. For example, you can implement button-like behavior, if you capture mouse in its MouseDown event, to be able to handle a MouseUp event (for example), even when the mouse pointer goes out of the element's boundaries, but then the mouse should be un-captured in the handle on this second event immediately. This is just an example.

Please see:
https://msdn.microsoft.com/en-us/library/system.windows.uielement.capturemouse%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.input.mouse.capture%28v=vs.110%29.aspx[^].

Read "Remarks" section thoroughly. Any questions?

—SA


Replace your code by the next to get it working:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Button button1 = new Button();
        button1.Width = 50;
        button1.Height = 20;

        this.Content = button1;

        this.Loaded += delegate { Mouse.Capture(button1); };
        button1.PreviewMouseDown += Down;
    }

    private void Down(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("Hello World!"); // shows
    }
}


这篇关于如何在控件外捕获Mouse Down事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆