Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to properly change the mouse cursor on control hovering


  • Please log in to reply
1 reply to this topic
TheGood
  • Members
  • 589 posts
  • Last active: Mar 22 2014 03:22 PM
  • Joined: 30 Jul 2007
I've had to figure this out recently, so I made some research on the right way to do it. Do not use the WM_MOUSEMOVE message. Use WM_SETCURSOR instead.
This example shows the mouse changing to two different shapes over two different controls, but it can be easily modified for multiple/one control(s) and/or multiple/one cursor(s).

    OnMessage(0x20, "WM_SETCURSOR")
    Gui, Add, Button, hwndhMyControl1, Mouse over me!
    Gui, Add, Text, hwndhMyControl2, Mouse over me!
    Gui, Show
Return

GuiClose:
ExitApp

WM_SETCURSOR() {
    Global hMyControl1, hMyControl2
    Static hCursor1, hCursor2
    
    ;Check over which control the cursor currently is
    MouseGetPos,,,, hMouseOver, 2
    If (hMouseOver = hMyControl1) {
        
        If Not hCursor1 ;Make sure the cursor is already loaded
            hCursor1 := DllCall("LoadCursor", "uint", 0, "uint", 32645) ;IDC_SIZENS
        
        DllCall("SetCursor", "uint", hCursor1)
        Return True ;Stop further processing
        
    } Else If (hMouseOver = hMyControl2) {
        
        If Not hCursor2 ;Make sure the cursor is already loaded
            hCursor2 := DllCall("LoadCursor", "uint", 0, "uint", 32644) ;IDC_SIZEWE
        
        DllCall("SetCursor", "uint", hCursor2)
        Return True ;Stop further processing
        
    } Else Return ;Pass message to the parent
}

This is the source of the information. Note that with AHK, things are a little different. For some reason, child controls in AHK never receive the WM_SETCURSOR message, as suggested in the article. The message is only reported to the OnMessage function when it escalates all the way to the parent window. This is why we have to use MouseGetPos to get the handle of the control on which we're hovering.

adabo
  • Members
  • 385 posts
  • Last active: Jul 30 2015 07:52 PM
  • Joined: 13 May 2010
I've been looking all over for this. Well now I have an excuse to learn a little bit about dll call :)

Thanks for the research!