locked
How to capture an event our of my application range? RRS feed

  • Question

  • hi,

    I have been working on a WPF application, and i have faced a problem.

    I'd like to capture an event such as MouseWheelEvent that occurred out of the application range.

    I want to attach a MouseWheelEvent to a combobox to change the selected index as a user scrolls his mouse wheel but the pointer is out of the application frame. how can I make it work?

    any help will be appreciated. 

    thank you

    <object cotype="cs" id="SILOBFWOBJECTID" style="width:0px;height:0px;display:block;" type="cosymantecnisbfw"></object>
    Sunday, March 1, 2015 5:07 AM

Answers

  • There are several controls make up a combobox.

    The one which will respond to mousewheel is the dropdown thing with the list in there.

    If you don't have that list visible and are instead just looking at a value then I think this is going to be a problem.

    If the list is dropped down then you can handle previewmousewheel.

    I threw a quick test together.

    <Window 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
     x:Class="wpf3.MainWindow"
    Window.PreviewMouseWheel="Window_MouseWheel"
        Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
    
        </Window.Resources>
    
        <Grid>
            <ComboBox>
                <ComboBoxItem>a</ComboBoxItem>
                <ComboBoxItem>b</ComboBoxItem>
                <ComboBoxItem>c</ComboBoxItem>
                <ComboBoxItem>d</ComboBoxItem>
                <ComboBoxItem>e</ComboBoxItem>
                <ComboBoxItem>f</ComboBoxItem>
            </ComboBox>
        </Grid>
    
    </Window>

    The event handler is empty, I just put a break point on the {.

    If the list a-f is visible then the handler fires whether the mouse is over the window or not.

    If the list is not visible then the handler doesn't fire.

    .

    This behaviour you're trying to do seems a bit odd to me though.  When my mouse is over a browser and I scroll, I want that browser to scroll whether I have some other app open or not.


    Hope that helps.
    Recent Technet articles: Property List Editing;   Dynamic XAML

    Monday, March 2, 2015 9:05 AM

All replies

  • you can capture mouse events only when your application has current focus. in other words, you can subscribe to mouse events on your parent form & when the mouse enters your form, raised events will be captured.

    on the same line, if you need your combobox to respond to mouse events, simply subscribe to the same & it will work but only when its currently on focus i.e. you should already have selected the combobox with your mouse / keyboard.

    hope this helps!

    Sunday, March 1, 2015 5:25 AM
  • thanks for the reply.

    when my combobox is on focus and the mouse is over my application form, it catches the mousewheelwvent and responds.

    but what i want to do is i want the combobox to respond a mouse event when the focus is on the combobox but the user mouse is not over the application form. 

    Is it possible?

    <object cotype="cs" id="SILOBFWOBJECTID" style="width:0px;height:0px;display:block;" type="cosymantecnisbfw"></object>
    Sunday, March 1, 2015 7:07 AM
  • Sunday, March 1, 2015 10:27 AM
  • thanks for the reply.

    I could get and set mouse position using your codes, but it was possible to do so only if the mouse is over the application window. ( <= I think I am having a problem describing the situation with proper words right here)

    let me descrive the condition in which I want to capture mouse events.

     I have a Combobox on my MainWindow. I could get a MouseWheelEvent only if the mouse is over the appication which means the mouse pointer is on the MainWindow. I subscribed a MouseWheelEvent to the MainWindow so that while the mouse is on the window, the items in the combobox will be changed as user scrolls his/her mouse wheel.

    However, once the mouse leaves the MainWindow, I cannot capture any events, even though I made sure the combobox was still focused.

    I have attempted to make it work by capturing Window Messages, but again, once the mouse leaves the MainWindow, noting I could capture.

    What I am looking for is the way to capture the events while the combobox is focused but the mouse has left the window.


    thanks for helping me out.
    Monday, March 2, 2015 6:34 AM
  • There are several controls make up a combobox.

    The one which will respond to mousewheel is the dropdown thing with the list in there.

    If you don't have that list visible and are instead just looking at a value then I think this is going to be a problem.

    If the list is dropped down then you can handle previewmousewheel.

    I threw a quick test together.

    <Window 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
     x:Class="wpf3.MainWindow"
    Window.PreviewMouseWheel="Window_MouseWheel"
        Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
    
        </Window.Resources>
    
        <Grid>
            <ComboBox>
                <ComboBoxItem>a</ComboBoxItem>
                <ComboBoxItem>b</ComboBoxItem>
                <ComboBoxItem>c</ComboBoxItem>
                <ComboBoxItem>d</ComboBoxItem>
                <ComboBoxItem>e</ComboBoxItem>
                <ComboBoxItem>f</ComboBoxItem>
            </ComboBox>
        </Grid>
    
    </Window>

    The event handler is empty, I just put a break point on the {.

    If the list a-f is visible then the handler fires whether the mouse is over the window or not.

    If the list is not visible then the handler doesn't fire.

    .

    This behaviour you're trying to do seems a bit odd to me though.  When my mouse is over a browser and I scroll, I want that browser to scroll whether I have some other app open or not.


    Hope that helps.
    Recent Technet articles: Property List Editing;   Dynamic XAML

    Monday, March 2, 2015 9:05 AM
  • hi, Andy

    thanks for the reply and helping me out here.

    I felt trying to do this was odd to me as well. But waht my supervisor said was that it was actually possible  with Window Forms, and he difinetely wants my aplication acts the same as the Window Forms but in WPF style. While I have focus on the combobox, no matter where the mouse is, he wants the combobox to catch the MouseWheelEvent till another app or controls in my application gets a focus.

    when I tried your source, it worked very well. However, it didnt work when I put it in my source. I am trying to figure out what the diffrences between your sample source and my source are.

    anyways, I really appreciate your help. If you come up with some ideas to solve this, please let me know.

    thank you.


    Monday, March 2, 2015 11:21 AM