Question : FInd Cursor position

Hi

I have a wpf form in which I have a button. When I leave the mouse cursor from the button I have an event. But on that event I want to check if the mouse cursor is in the range of form then do something else do something else.

Kindly advice

Regards
Karan Gupta

Answer : FInd Cursor position

check the code snippet.
i created a single button on my form (see screenshot)
 
case A - cursor is outside the form.
case B - cursor is on the form -> form color changed to dark red.
case C - cursor is on the button -> form color changed back to initial color (white) and button forground color changed to dark red.
once cursor is "leaving" button -> button forground color is back to its initial color (black).

the code snippet can help you understand how to use mouse event handlers on each control to achieve the behavior you wish to apply.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
public partial class Window1 : Window
    {
        Brush _orgBtnForegroundBrush;
        Brush _orgMainWinBackgroundBrush;
        public Window1()
        {
            InitializeComponent();
            _orgBtnForegroundBrush = button1.Foreground;
            _orgMainWinBackgroundBrush = mainWin.Background;            
        }

        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mainWin.Background != _orgMainWinBackgroundBrush)
            {
                mainWin.Background = _orgMainWinBackgroundBrush;
            }

            if (button1.Foreground != Brushes.DarkRed)
            {
                button1.Foreground = Brushes.DarkRed;
            }
        }

        private void button1_MouseLeave(object sender, MouseEventArgs e)
        {
            button1.Foreground = _orgBtnForegroundBrush;
            mainWin.Background = Brushes.DarkRed;
        }

        private void mainWin_MouseLeave(object sender, MouseEventArgs e)
        {
            mainWin.Background = _orgMainWinBackgroundBrush;
        }

        private void button1_MouseEnter(object sender, MouseEventArgs e)
        {
            mainWin.Background = _orgMainWinBackgroundBrush;
        }

        private void mainWin_MouseEnter(object sender, MouseEventArgs e)
        {
            mainWin.Background = Brushes.DarkRed;
        }
    }
 
screenshot
screenshot
 
Random Solutions  
 
programming4us programming4us