Jump to content

Minimize / Application Switching Problem


 Share

Recommended Posts

I have here a serious problem lol.

Here you have a window without header. To minimize the window I have created a button that will do the job. Easy. Click on it, it will be minimized, click on the taskbar to restore it and the input still has the focus. Easy.

Now comes the real problem: Press the Alt+Tab combination to switch the application, you will notice that this is not possible. How to solve this problem?

Enjoy trying to solve this ;-)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("{ESC}", "_Quit")

Global $h_Win, $h_Btn, $ai_Msg

$h_Win = GUICreate("Test", -1, -1, -1, -1, $WS_POPUP)
$h_Btn = GUICtrlCreateButton("Minimize", 10, 10)
$h_Input = GUICtrlCreateInput("", 10, 120, 100)
GUISetState()
GUICtrlSetState($h_Input, $GUI_FOCUS)

While 1
    If WinGetState($h_Win) = 7 Then WinActivate($h_Win); re-activate/focus after restoring window from minimized state
    $ai_Msg = GUIGetMsg(1)
    Select
        Case $ai_Msg[0] = $GUI_EVENT_CLOSE
            ExitLoop
        Case $ai_Msg[0] = $h_Btn
            GUISetState(@SW_MINIMIZE, $h_Win)
    ;EndCase
    EndSelect
WEnd
    
    
Func _Quit()
    Exit
EndFunc
Link to comment
Share on other sites

Is this line necessary? Without it your script runs fine. With it, your script does an annoying job of hogging all focus no matter where you click.

If WinGetState($h_Win) = 7 Then WinActivate($h_Win); re-activate/focus after restoring window from minimized state
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Yes, it is necessary. In my application, when coming back from the minimized state, the focus is lost (I need to click somewhere in the application to activate it). In my simplified example it seems not to be a problem indeed... but my application is a child window...

Link to comment
Share on other sites

This do what you want?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("{ESC}", "_Quit")

Global $h_Win, $h_Btn, $ai_Msg

$h_Win = GUICreate("Test", -1, -1, -1, -1, $WS_POPUP)
$h_Btn = GUICtrlCreateButton("Minimize", 10, 10)
$h_Input = GUICtrlCreateInput("", 10, 120, 100)
GUISetState()
GUICtrlSetState($h_Input, $GUI_FOCUS)

While 1
;~   If WinGetState($h_Win) = 7 Then WinActivate($h_Win); re-activate/focus after restoring window from minimized state
    $ai_Msg = GUIGetMsg(1)
    Select
        Case $ai_Msg[0] = $GUI_EVENT_CLOSE
            ExitLoop
        Case $ai_Msg[0] = $GUI_EVENT_RESTORE
            WinActivate($h_Win)
        Case $ai_Msg[0] = $h_Btn
            GUISetState(@SW_MINIMIZE, $h_Win)
   ;EndCase
    EndSelect
WEnd
    
    
Func _Quit()
    Exit
EndFunc
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Case $ai_Msg[0] = $GUI_EVENT_RESTORE

WinActivate($h_Win)

Fantastic Ichigo !!! Yes, it now works like a charm !!!

I was not aware that $GUI_EVENT_RESTORE existed, that's why I was puzzled. When thinking about the problem, I knew that a command that would check "OnRestore" would be the solution, but could not find it in the Help file.

You have helped me immensely !!! Thank you !!!

Edited by charvi
Link to comment
Share on other sites

I would like to come back on this point to tell you something that might be of some interest.

Ichigo told me a way to reactivate the window when the user restores an application from the minimized state with the use of $GUI_EVENT_RESTORE, and that works perfectly.

But you do not switch applications only by minimizing the other applications, you can also use ALT+Tab. And this is more difficult to reactivate the AutoIt application when switching back. At this point, the solution from Ichigo does not work in some circumstances. Let's me explain.

I have a main interface created with $h_WinMain=GUICreate(). In this main interface I have a menu bar, and all the selections open a CHILD window, attached to the main interface with $h_Win=GUICreate(,,,,,,,$_WinMain). And when you are IN the child window, you switch to another Windows application with ALT+Tab. It's when you come back to the AutoIt child application that a problem arises: the focus is not on the child window. Where the hell is the focus then? With Ichigo's solution, you would need to click on the child application to manually activate it.

The focus was in fact on the parent window, thus on the $h_WinMain instead of $h_Win.

So, the solution is 1) to make the $h_WinMain variable Global to be recognized in the child windows and 2) add in the While 1 .... WEnd loop a line like:

If WinGetHandle("", "") = $h_WinMain And WinGetState("", "") = 15 Then WinActivate($h_Win)

This way it works very well in addition to Ichigo's solution. Now I am not sure if this is normal or a bug. It is logical that the focus comes back to the handle where he left.

Cheers, Charvi.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...