none
How to prevent getting focus from mouse click? RRS feed

  • Question

  • hi,

    I wrote in Windows froms application for prevent focus from mouse click but i cant do same to wpf because there isnt any form.
    so what i want is i am writing On Screen Keyboard but every mouse click getting focus to wpf application , i dont want focus to any button or to my window title bar click but i want them to do mouse click function properly. ( similar OSK in windows Vista )

    any answer very useful.
    thank you for your interest.
    Tuesday, July 15, 2008 9:27 AM

All replies

  • You should only have to set Focusable to false on the root Page or Window.

    Bigsby, Lisboa, Portugal
    Tuesday, July 15, 2008 10:08 AM
  • All focus set false already but nothing was change. i am pressing google search bar to write then press my On Screen Keyboard's button (any button like "a" , "backspace" etc...) then my application gets focus. All my buttons's , grids's , windows's focus is false but still getting focus.
    Tuesday, July 15, 2008 11:00 AM
  • You could try to preview the mouseclick on a control.

    PreviewLeftMouseButtonDown

    When you set the Handled of the argument to true, it will not evaluate the control, thus will not focus it..

    Greetings,

    Rick
    • Proposed as answer by IsisTheDamned Wednesday, July 16, 2008 12:11 PM
    Tuesday, July 15, 2008 12:36 PM
  • None of that will work. You need to use the same technique from Windows Forms, responding to the WM_MOUSEACTIVATE message and handling the message, returning MA_NOACTIVATE.

            private void Window_Loaded(object sender, RoutedEventArgs e)  
            {  
                HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);  
                source.AddHook(new HwndSourceHook(WndProc));  
            }  
     
            private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)  
            {  
                const int WM_MOUSEACTIVATE = 0x0021;  
                const int MA_NOACTIVATE = 3;  
     
                if (msg == WM_MOUSEACTIVATE)  
                {  
                    handled = true;  
                    return new IntPtr(MA_NOACTIVATE);  
                }  
     
                return IntPtr.Zero;  
            }  
     

    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    • Marked as answer by blockedlord Tuesday, July 15, 2008 2:47 PM
    • Marked as answer by blockedlord Tuesday, July 15, 2008 2:47 PM
    • Marked as answer by blockedlord Tuesday, July 15, 2008 2:47 PM
    • Marked as answer by blockedlord Tuesday, July 15, 2008 2:47 PM
    • Marked as answer by blockedlord Tuesday, July 15, 2008 2:47 PM
    • Unmarked as answer by blockedlord Tuesday, July 15, 2008 2:47 PM
    • Unmarked as answer by blockedlord Tuesday, July 15, 2008 2:47 PM
    • Unmarked as answer by blockedlord Tuesday, July 15, 2008 2:55 PM
    Tuesday, July 15, 2008 12:40 PM
  • None of that will work. You need to use the same technique from Windows Forms, responding to the WM_MOUSEACTIVATE message and handling the message, returning MA_NOACTIVATE.

            private void Window_Loaded(object sender, RoutedEventArgs e)  
            {  
                HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);  
                source.AddHook(new HwndSourceHook(WndProc));  
            }  
     
            private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)  
            {  
                const int WM_MOUSEACTIVATE = 0x0021;  
                const int MA_NOACTIVATE = 3;  
     
                if (msg == WM_MOUSEACTIVATE)  
                {  
                    handled = true;  
                    return new IntPtr(MA_NOACTIVATE);  
                }  
     
                return IntPtr.Zero;  
            }  
     


    This is what i am looking for but still getting focus and i know the solution in Windows forms. i am giving you code that i can prevent mouse click and looking for a wpf code.
    thank you.

    Code :

            private const int WS_EX_NOACTIVATE = 0x08000000;
            private const int WM_MOUSEACTIVATE = 0x0021;
            private const int MA_NOACTIVATE = 0x0003;
            protected override CreateParams CreateParams
            {
                get
                {
                    CreateParams createParams = base.CreateParams;
                    createParams.ExStyle = WS_EX_NOACTIVATE;
                    return createParams;
                }
            }
            protected override void WndProc(ref Message m)
            {
                //If we're being activated because the mouse clicked on us...
                if (m.Msg == WM_MOUSEACTIVATE)
                {
                    //Then refuse to be activated, but allow the click event to pass through (don't use MA_NOACTIVATEEAT)
                    m.Result = (IntPtr)MA_NOACTIVATE;
                }
                else
                    base.WndProc(ref m);
            }

    Tuesday, July 15, 2008 2:57 PM
  • any clue about upper code?
    Wednesday, July 16, 2008 10:58 AM
  • I just gave you the code, for WPF. The above code that I pasted was not for Windows Forms. As I said, you need to use the same technique but apply it slightly differently because it's WPF. Start a new empty WPF project and apply that code, and you will not be able to activate the window with the mouse.
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    Wednesday, July 16, 2008 11:17 AM
  • Try yourself. it is still getting focus.
    try to write 'r' or something to google search bar with your code.

    Wednesday, July 16, 2008 8:25 PM
  • I have tried it. I tried it when I originally wrote the code for you, and I tried it again this morning when you claimed it wasn't WPF code. It works fine.

    I've no idea why you are now talking about Google. We're talking about a WPF Window and a method of stopping the window getting activated when a mouse button is clicked on it.
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    Wednesday, July 16, 2008 9:51 PM
  • I didn't mean it isn't a wpf code. I mean something missing in it becuase I am writing an On Screen Keyboard so i dont want any element get focus also i am using SendKeys to send keyboard keys to any textbox outside of my application but when i tried your code  something getting focus in window.  I cant send any key to a textbox outside of my application .  You can try to send keyboard keys with SendKeys  to watch if is there something getting focus and also i really thankful with your concern.
    A.
    Thursday, July 17, 2008 11:52 AM
  • If you also want buttons etc not to get the focus you'll need to set their Focusable properties to false. I've no idea how SendKeys is relevant to this question.
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    Thursday, July 17, 2008 5:22 PM
  • all item's focuable property is false.
    think that text cursor is at google search bar or any textbox outside of my application when i press my application button text cursor lost from google search bar. this is the problem.
    sorry for taking your time Tim.
    Friday, July 18, 2008 2:49 PM
  • Hi blockedlord,

    Have you success keep NOT losing focus on google search bar when click on button of your application?

    I have the same problem with you.

    MA
    Sunday, November 23, 2008 2:57 AM
  • Good news for you blockedlord:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/df6b6882-7c57-411a-b8e0-7377cfdbaff9

    using System;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Interop;
    using System.Windows.Input;
    using System.Windows.Media;

    protected override void OnActivated(EventArgs e)
            {
                base.OnActivated(e);

     

                //Set the window style to noactivate.
                WindowInteropHelper helper = new WindowInteropHelper(this);
                SetWindowLong(helper.Handle, GWL_EXSTYLE,
                    GetWindowLong(helper.Handle, GWL_EXSTYLE) | WS_EX_NOACTIVATE);

            }

     

            private const int GWL_EXSTYLE = -20;
            private const int WS_EX_NOACTIVATE = 0x08000000;

     

            [DllImport("user32.dll")]
            public static extern IntPtr SetWindowLong(IntPtr hWnd,

                                                      int nIndex,

                                                      int dwNewLong);

            [DllImport("user32.dll")]
            public static extern int GetWindowLong(IntPtr hWnd,

                                                   int nIndex);

    • Proposed as answer by LeeViEW Friday, July 24, 2009 8:23 AM
    Friday, July 24, 2009 8:23 AM
  • I know I'm ten years late to the game on this one, but I had noticed no one had really addressed this for use with vb.net and I got it working, recently, in windows 10.

        Private Const WS_EX_NOACTIVATE As Integer = &H8000000
        Private Const WM_MOUSEACTIVATE As Integer = &H21
        Private Const MA_NOACTIVATE As Integer = &H3
    
        Protected Overrides ReadOnly Property CreateParams As 
        CreateParams
            Get
                Dim NcreateParams As CreateParams = 
                   MyBase.CreateParams
                NcreateParams.ExStyle = WS_EX_NOACTIVATE
                Return NcreateParams
            End Get
        End Property
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_MOUSEACTIVATE Then
                m.Result = CType(MA_NOACTIVATE, IntPtr)
            Else
                MyBase.WndProc(m)
            End If
        End Sub
    I hope this helps someone else who may or may not come along like I did lol
    Tuesday, June 11, 2019 2:59 AM