Question

i have a WPF application that i would like to become the current focused window whenever the mouse cursor moves over it. Currently i have a onMouseEnter event that changes the cursor when the mouse moves over it so i know the system is recognizing that, however what i actually want is for the application itself to become focused as if the used had left clicked on it - so i can then perform other ops. At the moment if i move over it the cursor changes but if another app, eg - notepad had focus, focus would return to that after.

Within the onMouseEnter handler i have tried "this.Focus()" and "this.Activate()" but neither of them achive the same result as if i had clicked on the app.

Any ideas?

Was it helpful?

Solution

Edit: Posted answer will not work for WPF. Sorry.

Try looking at this instead for WPF: http://blogs.msdn.com/nickkramer/archive/2006/03/18/554235.aspx

OTHER TIPS

WPF C# Example with a MouseEnter event attached to a Grid control. If another application window has the focus it will remove it and attach the focus to the main window as if the user had left clicked on it.

    private void GrdContent_MouseEnter(object sender, MouseEventArgs e)
    {
        Application.Current.MainWindow.Activate();
    }

In WPF just add this line to the MainWindow constructor to get the expected result.

MouseEnter += (s, e) => Activate();

This original one line answer will only work if you don't interact with any other window. The following will work even in that case but you will need the Nuget package "AutoItX.Dotnet".

private IntPtr myHandle;
public MainWindow() {
  myHandle =  new System.Windows.Interop.WindowInteropHelper(this).Handle;
  MouseEnter += (s, e) => {
    Activate();
    if (AutoIt.AutoItX.WinActive(myHandle) == 0)
      AutoIt.AutoItX.WinActivate(myHandle);
    };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top