What's special about mouse capture and middle mouse button in WPF?
        Posted  
        
            by Scott Bilas
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Scott Bilas
        
        
        
        Published on 2010-06-14T22:02:22Z
        Indexed on 
            2010/06/14
            22:02 UTC
        
        
        Read the original article
        Hit count: 270
        
wpf
When I call CaptureMouse() in response to a MouseDown from the middle mouse button, it will capture and then release the mouse.
Huh?
I've tried using Preview events, setting Handled=true, doesn't make a difference. Am I not understanding mouse capture in WPF?
Here's some minimal sample code that reproduces the problem.
// TestListBox.cs
using System.Diagnostics;
using System.Windows.Controls;
namespace Local
{
    public class TestListBox : ListBox
    {
        public TestListBox()
        {
            MouseDown += (_, e) =>
            {
                Debug.WriteLine("+MouseDown");
                Debug.WriteLine(" Capture: " + CaptureMouse());
                Debug.WriteLine("-MouseDown");
            };
            MouseUp += (_, e) =>
            {
                Debug.WriteLine("+MouseUp");
                ReleaseMouseCapture();
                Debug.WriteLine("-MouseUp");
            };
            GotMouseCapture += (_, e) => Debug.WriteLine("GotMouseCapture");
            LostMouseCapture += (_, e) => Debug.WriteLine("LostMouseCapture");
        }
    }
}
Generating a default WPF app that has this for its main window will use the test class:
<Window x:Class="Local.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Local"
        Title="MainWindow" Height="350" Width="525">
    <local:TestListBox>
        <ListBoxItem>1</ListBoxItem>
        <ListBoxItem>2</ListBoxItem>
        <ListBoxItem>3</ListBoxItem>
        <ListBoxItem>4</ListBoxItem>
    </local:TestListBox>
</Window>
Upon clicking the middle button down, I get this output:
+MouseDown
GotMouseCapture
LostMouseCapture
 Capture: True
-MouseDown
So I'm calling CaptureMouse, which in turn grabs and then releases capture, yet returns true that capture was successfully acquired.
What's going on here? Is it possible that this is something with my Logitech mouse driver doing something goofy, trying to initiate 'ultrascroll' or something?
© Stack Overflow or respective owner