Jump to content

Get monitor where mouse is on screen


draien
 Share

Recommended Posts

Hi

I was searching for a while now and didn't find anything regarding this:

Is there a function that returns the "number" of the monitor where the mouse is currently on screen? The same number that appears when you open windows-resolution and click on "identify" (or something). I want to trigger an event when the number changes. I wrote something that recognizes leaving the main monitor. But I couldn't get any further than that (maybe with _WinAPI_EnumDisplayMonitors/Devices/Settings ?) and I wouldn't catch an event, if someone has 3 monitors (aligned 1--2--3 with 1 being main monitor) and changes from 2 to 3. 

Here is my code:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 279, 388, 192, 124)
$xLabel = GUICtrlCreateLabel("", 16, 48, 36, 17)
$yLabel = GUICtrlCreateLabel("", 16, 72, 36, 17)
$resLabe = GUICtrlCreateLabel(@DesktopWidth & " x " & @DesktopHeight, 16, 112, 136, 17)
$posLabel = GUICtrlCreateLabel("Main", 152, 88, 36, 17)
GUISetState(@SW_SHOW)
Global $iMonitor = 1
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    $xMousePos = MouseGetPos(0)
    $yMousePos = MouseGetPos(1)

    GUICtrlSetData($xLabel,$xMousePos)
    GUICtrlSetData($yLabel,$yMousePos)

    If $xMousePos >= 0 AND $xMousePos < @DesktopWidth AND $yMousePos >= 0 AND $yMousePos < @DesktopHeight Then
        $iMonitor = 1
    Else
        $iMonitor = 2
    EndIf
    Switch $iMonitor
        Case 1
            If GUICtrlRead($posLabel) = "Second" Then
                GUICtrlSetData($posLabel, "Main")
                MsgBox(0,"Monitor switched", "Mouse moved to main monitor")
            EndIf
        Case 2
            If GUICtrlRead($posLabel) = "Main" Then
                GUICtrlSetData($posLabel, "Second")
                MsgBox(0,"Monitor switched", "Mouse moved to second monitor")
            EndIF
    EndSwitch
    Sleep(10)
WEnd

The GUI was just for testing. Maybe you can give me some hints :)?

 

Edited by draien
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

×
×
  • Create New...