Jump to content

Making a skin. How to make: an image on my "button" changes when I moves a mouse on it?


Recommended Posts

How to change an image on my "button" when I move a mouse cursor on it?

Trimmed example is listed below:

Attachments - 2 archived images(30kb).

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $temp[2], $shown =0
AutoItSetOption("MouseCoordMode", 1)
#Region ### START Koda GUI section ### Form=
$img = @DesktopDir
$Form1 = GUICreate("Form1", 302, 158, 353, 305)
GUISetBkColor(0x000000)
$Pic = GUICtrlCreatePic($img & "\1.jpg", 104, 64, 75, 30, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
$Info = GUICtrlCreateInput("", 10, 8, 280, 21)
$contpos = ControlGetPos("Form1", "", "[CLASS:Static; INSTANCE:1]")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


;MouseGetPos() in this example is result of hight(~50%) CPU usage:(  
While 1
    $pos = MouseGetPos()
    If $pos[0] <> $temp[0] Or $pos[1] <> $temp[1] Then; do something when mouse moved
        $winpos = WinGetPos("Form1")
        GUICtrlSetData($Info, "Abs.mouse coords:" & $pos[0] & "x" & $pos[1] & "   |   Button coords:" & $winpos[0] + $contpos[0] & "x" & $winpos[1] + $contpos[1])
        $temp = MouseGetPos() 
        If $pos[0] > $winpos[0] + $contpos[0] And $pos[0] < $winpos[0] + $contpos[0] + $contpos[2] And $pos[1] > $winpos[1] + $contpos[1] And $pos[1] < $winpos[1] +  $contpos[1] + $contpos[3] Then; when you move mouse to 'button', above button:(
            If $shown = 0 Then
                GUICtrlSetImage($Pic, $img & "\1.jpg")
                $shown = 1
            EndIf
        Else
            GUICtrlSetImage($Pic, $img & "\2.jpg")  
            $shown = 0
        EndIf   
    EndIf

As you see, an image changes when mouse moved above ~30px from button, because ControlGetPos returns

the control's position relative to it's client window

ButtonImages.rar

_____________________________________________________________________________

Link to comment
Share on other sites

How to change an image on my "button" when I move a mouse cursor on it?

Look at «GUICtrlSetOnEvent UDF» in my signature.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

And you don't need to check the control position, you have GUIGetCursorInfo() for that:

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

Global $aOld_Mouse_Pos[2], $iPic_Is_Shown = 0
Global $sImage_Path = @ScriptDir

Opt("MouseCoordMode", 1)

$hForm = GUICreate("Form1", 302, 158, 353, 305)
GUISetBkColor(0x000000)

$Pic = GUICtrlCreatePic($sImage_Path & "\1.jpg", 104, 64, 75, 30, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))

$Info = GUICtrlCreateInput("", 10, 8, 280, 21)
$aCtrl_Pos = ControlGetPos($hForm, "", $Pic)

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    
    $aMouse_Pos = MouseGetPos()
    
    If $aMouse_Pos[0] <> $aOld_Mouse_Pos[0] Or $aMouse_Pos[1] <> $aOld_Mouse_Pos[1] Then
        $aOld_Mouse_Pos = MouseGetPos() 
        $aWin_Pos = WinGetPos($hForm)
        
        $aCursorInfo = GUIGetCursorInfo($hForm)
        
        GUICtrlSetData($Info, _
            StringFormat("Abs.mouse coords:%ix%i      |   Button coords:%ix%i", _
                $aMouse_Pos[0], $aMouse_Pos[1], $aWin_Pos[0] + $aCtrl_Pos[0], $aWin_Pos[1] + $aCtrl_Pos[1]))
        
        If $aCursorInfo[4] = $Pic Then
            If $iPic_Is_Shown = 0 Then
                GUICtrlSetImage($Pic, $sImage_Path & "\1.jpg")
                $iPic_Is_Shown = 1
            EndIf
        Else
            GUICtrlSetImage($Pic, $sImage_Path & "\2.jpg")    
            $iPic_Is_Shown = 0
        EndIf    
    EndIf
WEnd

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Using GUICtrlSetOnHover UDF:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUICtrlSetOnHover_UDF.au3>
;

Global $sImage_Path = @ScriptDir

Opt("MouseCoordMode", 1)

$hForm = GUICreate("Form1", 302, 158, 353, 305)
GUISetBkColor(0x000000)

$Pic = GUICtrlCreatePic($sImage_Path & "\2.jpg", 104, 64, 75, 30, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))
_GUICtrl_SetOnHover(-1, "_HoverPic_Proc", "_HoverPic_Proc")

$Info_Input = GUICtrlCreateInput("", 10, 8, 280, 21)

GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_MOUSEMOVE, "WM_MOUSEMOVE")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func WM_MOUSEMOVE($hWnd, $iMsg, $wParam, $lParam)
    Local $aWin_Pos = WinGetPos($hForm)
    Local $aCtrl_Pos = ControlGetPos($hForm, "", $Pic)
    Local $aMouse_Pos = MouseGetPos()
    Local $aCursor_Info = GUIGetCursorInfo($hForm)
    
    GUICtrlSetData($Info_Input, _
        StringFormat("Abs.mouse coords:%ix%i      |   Button coords:%ix%i", _
            $aMouse_Pos[0], $aMouse_Pos[1], $aWin_Pos[0] + $aCtrl_Pos[0], $aWin_Pos[1] + $aCtrl_Pos[1]))
EndFunc

Func _HoverPic_Proc($iCtrlID, $iHoverMode)
    If $iHoverMode = 1 Then
        GUICtrlSetImage($Pic, $sImage_Path & "\1.jpg")
    Else
        GUICtrlSetImage($Pic, $sImage_Path & "\2.jpg") 
    EndIf
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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...