Jump to content

Focus Follows Mouse


willichan
 Share

Recommended Posts

A client wanted a way to have this setting, but it is not in the normal configuration dialogs.

config.au3

#include-once

Opt("MustDeclareVars", 1)       ;0=no, 1=require pre-declare
Opt("TrayAutoPause",   0)       ;0=no pause, 1=Pause
Opt("TrayMenuMode", 0)      ;0=append, 1=no default menu, 2=no automatic check, 4=menuitemID  not return
Opt("TrayIconHide", 0)      ;0=show, 1=hide tray icon

Global Const $MyName=StringLeft(@ScriptName, StringInStr(@ScriptName,".", 0, -1)-1) ;get just the name portion of the script/exe name
Global Const $MyMutex=$MyName & "-2971006DC1682D92" ;name the mutex for this app
If _MutexExists($MyMutex) Then Exit ;exit if another instance of this app is running

Func _MutexExists($sOccurenceName)
    ;Credit goes to martin for this function
    Local $ERROR_ALREADY_EXISTS = 183, $handle, $lastError
    $sOccurenceName = StringReplace($sOccurenceName, "\", "")
    $handle = DllCall("kernel32.dll", "int", "CreateMutex", "int", 0, "long", 1, "str", $sOccurenceName)
    $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    Return $lastError[0] = $ERROR_ALREADY_EXISTS
EndFunc  ;==>_MutexExists

FocusFollowsMouse.au3

#include "config.au3"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <WindowsConstants.au3>

Global $CurrentFull
Global $Currentffm ;0x01
Global $Currentrof ;0x40
Global $Newffm
Global $Newrof

_ReadCurrent()

Opt("GUIOnEventMode", 1)
#region ### START Koda GUI section ### Form=frm_main.kxf
Global $frm_main = GUICreate("Focus Follows Mouse", 138, 142, 192, 124)
Global $frm_main_loop = True
GUISetOnEvent($GUI_EVENT_CLOSE, "frm_mainClose")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "frm_mainMinimize")
GUISetOnEvent($GUI_EVENT_RESTORE, "frm_mainRestore")
Global $frm_main_ffm = GUICtrlCreateCheckbox("Focus follows mouse", 8, 8, 129, 17)
GUICtrlSetState($frm_main_ffm, $Currentffm)
GUICtrlSetOnEvent($frm_main_ffm, "frm_main_ffmClick")
Global $frm_main_rof = GUICtrlCreateCheckbox("Raise on focus", 8, 32, 129, 17)
GUICtrlSetState($frm_main_rof, $Currentrof)
GUICtrlSetOnEvent($frm_main_rof, "frm_main_rofClick")
Global $frm_main_ok = GUICtrlCreateButton("OK", 8, 56, 123, 25)
GUICtrlSetOnEvent($frm_main_ok, "frm_main_okClick")
Global $frm_main_cancel = GUICtrlCreateButton("Cancel", 8, 88, 123, 25)
GUICtrlSetOnEvent($frm_main_cancel, "frm_main_cancelClick")
Global $frm_main_status = _GUICtrlStatusBar_Create($frm_main)
Global $frm_main_status_PartsWidth[1] = [-1]
_GUICtrlStatusBar_SetParts($frm_main_status, $frm_main_status_PartsWidth)
_GUICtrlStatusBar_SetText($frm_main_status, "", 0)
GUISetState(@SW_SHOW, $frm_main)
#endregion ### END Koda GUI section ###

While $frm_main_loop
    Sleep(100)
WEnd

Exit


Func frm_main_cancelClick()
    $frm_main_loop = False
EndFunc   ;==>frm_main_cancelClick

Func frm_main_ffmClick()
    _UpdateStatus()
EndFunc   ;==>frm_main_ffmClick

Func frm_main_okClick()
    GUISetState(@SW_HIDE, $frm_main)
    $frm_main_loop = False
    If ($Newffm <> $Currentffm) Or ($Newrof <> $Currentrof) Then
        If $Newffm == $GUI_CHECKED Then
            BitOR($CurrentFull, 1)
        Else
            BitAND($CurrentFull, 4294967294)
        EndIf
        If $Newrof == $GUI_CHECKED Then
            BitOR($CurrentFull, 64)
        Else
            BitAND($CurrentFull, 4294967231)
        EndIf
        RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "UserPreferencesMask", "REG_BINARY", Binary("0x" & _reverse(Hex($CurrentFull))))
    EndIf
EndFunc   ;==>frm_main_okClick

Func frm_main_rofClick()
    _UpdateStatus()
EndFunc   ;==>frm_main_rofClick

Func frm_mainClose()
    frm_main_cancelClick()
EndFunc   ;==>frm_mainClose

Func frm_mainMinimize()
EndFunc   ;==>frm_mainMinimize

Func frm_mainRestore()
EndFunc   ;==>frm_mainRestore

Func _ReadCurrent()
    $CurrentFull = Number(RegRead("HKEY_CURRENT_USER\Control Panel\Desktop", "UserPreferencesMask"))
    If BitAND($CurrentFull, 1) > 0 Then
        $Currentffm = $GUI_CHECKED
    Else
        $Currentffm = $GUI_UNCHECKED
    EndIf
    If BitAND($CurrentFull, 64) > 0 Then
        $Currentrof = $GUI_CHECKED
    Else
        $Currentrof = $GUI_UNCHECKED
    EndIf
    $Newffm = $Currentffm
    $Newrof = $Currentrof
EndFunc   ;==>_ReadCurrent

Func _UpdateStatus()
    $Newffm = GUICtrlRead($frm_main_ffm)
    $Newrof = GUICtrlRead($frm_main_rof)
    If ($Newffm <> $Currentffm) Or ($Newrof <> $Currentrof) Then
        _GUICtrlStatusBar_SetText($frm_main_status, "*Restart Required", 0)
    Else
        _GUICtrlStatusBar_SetText($frm_main_status, "", 0)
    EndIf
EndFunc   ;==>_UpdateStatus

Func _reverse($hexnum) ;flip byte order
    Local $le = $hexnum
    Local $be = ""
    If Mod(StringLen($le), 2) > 0 Then $le = "0" & $le
    While $le <> ""
        $be &= StringRight($le, 2)
        $le = StringLeft($le, StringLen($le) - 2)
    WEnd
    Return $be
EndFunc   ;==>_reverse

--- Edit ---

BTW, this only works with Win2K and WinXP.

Edited by willichan
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...