Hello,

I have a window's handle; with that, I want to restore the window = bring it to front, on top of any other windows I have, and set focus on it.

The only way I can do that (using shortcuts, atm) is by minimizing the currently active window and then restoring the window I want to restore.

It may be a problem with how I listen for hotkeys.


Effects with just using:

SetForegroundWindow(hWnd.ToInt32());
            ShowWindowAsync(hWnd, SW_RESTORE);
            string activeWindowTitle = GetWindowText(hWnd);

IF there's no active window (I'm viewing desktop), it works just fine. Else, my window is restored, but focus stays on the old one.


Ideally, I should be able to activate a window without minimizing the currently active one. :(

private void restore(IntPtr hWnd)
        {
            //minimize current window
            IntPtr ptrCurrentActiveWindow = GetForegroundWindow();
            ShowWindowAsync(ptrCurrentActiveWindow, SW_MINIMIZE);
            
            //restore desired window
            SetForegroundWindow(hWnd.ToInt32());
            ShowWindowAsync(hWnd, SW_RESTORE);
            string activeWindowTitle = GetWindowText(hWnd);

        }

        bool control = false; //CTRL key pressed?
        IntPtr ptrActiveWindow;

      void gkh_KeyUp(object sender, KeyEventArgs e)
        {
            
            //release a key
            
            if (e.KeyCode.ToString().Equals("F9") && control) //CTRL+F9
            {
                //notDone = true;
                //mode = 0;
                //GetDesktopWindowsCaptions();
                restore(ptrActiveWindow);
                e.Handled = true;
                return;
            }


            else if (e.KeyCode.ToString().Equals("LControlKey"))
            { //ctrl released, disable ctrl effect
                control = false;
                e.Handled = true;
                return;
            }

            e.Handled = true ;

        }

You can't put a window in the forground that isn't visible. Reverse your code and show the window first, then set it to forground.

Thank you for the suggestion, Momerath, but it doesn't work.

Currently, I have a hook for listening to global key events. If a shortcut is pressed (Ctrl+F9), a window should become visible.

I have the same code on a Button event and that works perfectly. Somehow, with hotkeys, the currently active window won't lose focus. The desired window starts blinking (:D), but it just won't come in the foreground.

SetForgroundWindow doesn't work the way you think, it used to in older versions of windows, but in xp service pack 3 and newer it just flashes the window on the taskbar unless the parent application of the window already has focus.

This was added to keep windows from poping up and taking focus while a user was typing or playing a game.

So what you have come up with, setting the windows state, is the correct way to do it, you can't just call setforgroundwindow, it won't work, because it was redesigned not to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.