6 Replies - 279 Views - Last Post: 10 November 2019 - 09:11 AM Rate Topic: -----

#1 Somalia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-January 18

Switch focus back to last active window when cursor is leaving Form?

Posted 09 November 2019 - 05:47 PM

Is this possible somehow?
1)run my application (app is always in front of everything)
2)open a new folder window which is now active
3)moving the mouse cursor over my application window would grey out the folder border
The above is the current behaviour.
Is there a way now to automatically set the last folder window active again, when mouse cursor is not over application/form window anymore?
Is This A Good Question/Topic? 0
  • +

Replies To: Switch focus back to last active window when cursor is leaving Form?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,590
  • Joined: 12-June 08

Re: Switch focus back to last active window when cursor is leaving Form?

Posted 09 November 2019 - 06:23 PM

That's a lot of windows low level hooking to do.

I am having a hard time understanding what's the use case?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Switch focus back to last active window when cursor is leaving Form?

Posted 09 November 2019 - 06:27 PM

Two approaches come to mind:
  • Use a timer to periodically check mouse position
  • Set a global Windows mouse hook

In both cases, you'll need to do some hit testing to figure out if the mouse is currently over your form.

It also may be worth looking through the code of those fun little programs popular during the Win3.x days which had the eyes that follow where the mouse currently is on the screen. I'd like to say that those just checked the current mouse position on each paint message, and the eyes were always invalidated right at the end of each paint message handler, but I truly don't recall how they worked.
Was This Post Helpful? 0
  • +
  • -

#4 Somalia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-January 18

Re: Switch focus back to last active window when cursor is leaving Form?

Posted 10 November 2019 - 07:24 AM

It should work with any window(including open windows explorer folder), but should only affect the window that greyed out, before the mouse cursor went over my application.
Here is a code example that would bring all windows from processlist to front(this can't bring up explorer windows too).
Instead of working with processes, how could I set a condition for last active window instead?
Another important thing is that the window I try to activate must have the same dimensions than before.
In the example below, the dimensions are forced to a specific type of dimensions with ShowWindow(hWnd, 1) , but not to current window dimensions.
        [DllImport("user32.dll")]
        internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        private void Form1_MouseLeave(object sender, EventArgs e)
        {
            IntPtr hWnd; //(IntPtr)
            Process[] processlist = Process.GetProcesses();

            foreach (Process process in processlist)
            {
                if (!String.IsNullOrEmpty(process.MainWindowTitle))
                {
                    hWnd = process.MainWindowHandle;
                    ShowWindow(hWnd, 1);
                    SetForegroundWindow(hWnd);
                }
            }
        }

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Switch focus back to last active window when cursor is leaving Form?

Posted 10 November 2019 - 08:07 AM

Wait. In you orginal post, you said that you bring up a new folder window. (Step 2). So that window as well as your window from Step 1 all belong to you and so you already know that it was you window from Step 1 that was previously active.

So now, even if you bring up a new Explorer window after your application window, then it is still your application window which was active.

What am I missing?

Also by "greyed out", can you be more precise by what you mean? I have been assuming that it is simply the border color that Windows changes to when it is not an active window anymore. Are you talking about "greying out" like when you try to take a screenshot?

As for your code above, why are you even calling ShowWindow()?
Was This Post Helpful? 0
  • +
  • -

#6 Somalia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-January 18

Re: Switch focus back to last active window when cursor is leaving Form?

Posted 10 November 2019 - 08:45 AM

Currently when mouse cursor is over my app window, another window that is active will grey out and keyboard input is no longer available for this window. So far so good.
What I want now is that I can automatically get the keyboard input back for this window when mouse cursor is leaving the app window just like in the image below.
This should work with any window that lost keyboard input, before mouse went over my app window(regardless if this is window of .exe process or explorer folder window).

Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Switch focus back to last active window when cursor is leaving Form?

Posted 10 November 2019 - 09:11 AM

I hope that you realize that what you are asking for is completely against the Windows UI guidelines.

I hate it when people break the Windows UI guidelines. This makes the Windows UI experience inconsistent and people complain that Windows sucks because there is no unified feel to it and the applications that run on it. It's the same people that complain that using various Android phones is so inconsistent. Other than the Apple mystique, the consistency of the UI of the Apple products is what makes them so attractive.

Anyway, when you receive the WM_ACTIVATE message that gets sent to your window when it gets activate contains the window handle of the other window that got deactivated. Unfortunately, the WinForms Form.Activated event doesn't give you access to this information. You'll need to override the windows procedure of your form to intercept the information.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1