stormbreaker Posted December 7, 2012 Posted December 7, 2012 Hello everyone, I have been working on a script, which makes a circle in the window (a ball perhaps), which allows me to change its position using keyboard. Here's it: expandcollapse popup#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <GDIPlus.au3> #include <Misc.au3> #include <WinAPI.au3> Global $g = 10, $Label2, $CONTROLPOS[2] $CONTROLPOS[0] = 20 $CONTROLPOS[1] = 20 Global $USER32DLL = DllOpen('user32.dll') $DEFAULTVELOCITY = 5 _GDIPlus_StartUp() $Form1 = GUICreate("My Problem", 934, 627, 332, 130) GUISetBkColor(0xffffff) $Label2 = GUICtrlCreateLabel("", 20, 20, 33, 33) $hGraphicLabel = _GDIPlus_GraphicsCreateFromHWND(GUICtrlGetHandle($Label2)) _GDIPlus_GraphicsDrawEllipse($hGraphicLabel, 0,0, 30, 30) _GDIPlus_GraphicsSetSmoothingMode($hGraphicLabel, 2) $hBMPBuff = _GDIPlus_BitmapCreateFromGraphics(33, 33, $hGraphicLabel) $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff) _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2) _GDIPlus_GraphicsDrawEllipse($hGraphic, 0,0, 30, 30) GUISetState(@SW_SHOW) GUIRegisterMsg($WM_PAINT, '_ReDraw') While 1 _Projectile() $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_GraphicsDispose($hGraphicLabel) _WinAPI_DeleteObject($hBMPBuff) _GDIPlus_ShutDown() DllClose($USER32DLL) Exit EndSwitch WEnd Func _Projectile() If _IsPressed('27', $USER32DLL) and $CONTROLPOS[0] < 934-33 then GUICtrlSetPos($Label2, $CONTROLPOS[0] + $DEFAULTVELOCITY) $CONTROLPOS[0] = $CONTROLPOS[0] + $DEFAULTVELOCITY _GDIPlus_GraphicsDrawImage($hGraphicLabel, $hBMPBuff, 0, 0) EndIf If _IsPressed('25', $USER32DLL) and $CONTROLPOS[0] >$DEFAULTVELOCITY then GUICtrlSetPos($Label2, $CONTROLPOS[0] - $DEFAULTVELOCITY) $CONTROLPOS[0] = $CONTROLPOS[0] - $DEFAULTVELOCITY _GDIPlus_GraphicsDrawImage($hGraphicLabel, $hBMPBuff, 0, 0) EndIf If _IsPressed('26', $USER32DLL) and $CONTROLPOS[1] >$DEFAULTVELOCITY then GUICtrlSetPos($Label2, $CONTROLPOS[0], $CONTROLPOS[1] - $DEFAULTVELOCITY) $CONTROLPOS[1] = $CONTROLPOS[1] - $DEFAULTVELOCITY _GDIPlus_GraphicsDrawImage($hGraphicLabel, $hBMPBuff, 0, 0) EndIf If _IsPressed('28', $USER32DLL) and $CONTROLPOS[1] < 627-26 then GUICtrlSetPos($Label2, $CONTROLPOS[0], $CONTROLPOS[1] + $DEFAULTVELOCITY) $CONTROLPOS[1] = $CONTROLPOS[1] + $DEFAULTVELOCITY _GDIPlus_GraphicsDrawImage($hGraphicLabel, $hBMPBuff, 0, 0) EndIf EndFunc Func _ReDraw() _GDIPlus_GraphicsDrawImage($hGraphicLabel, $hBMPBuff, 0, 0) EndFunc Now, as soon as you press an arrow key, the ball moves fine as expected, but if the mouse is moved, the ball moves super-duper fast (something, which is not acceptable in my case) >_< . Please help me resolve this issue. Thank you for your time... With regards, MKISH ---------------------------------------- :bye: Hey there, was I helpful? ---------------------------------------- My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1
Moderators Melba23 Posted December 7, 2012 Moderators Posted December 7, 2012 MKISH,Moving the mouse is sending a message which is picked up by GUIGetMsg - as nothing else much is going on this effectively cuts short the 12-15ms delay that is automatically built into that function and so the whole loop speeds up. So I would do something like this to force a standard delay:While 1 ; Get a timestamp $iBegin = TimerInit() _Projectile() $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_GraphicsDispose($hGraphicLabel) _WinAPI_DeleteObject($hBMPBuff) _GDIPlus_Shutdown() DllClose($USER32DLL) Exit EndSwitch ; Now wait until at least 20 ms have passed - you can increase the time as required, but this is probably the minimum you can use While TimerDiff($iBegin) < 20 Sleep(10) ; This is the minimum Sleep WEnd WEndNow you will always wait 20ms regardless of how many messages are waiting in the queue. stormbreaker 1  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Â
stormbreaker Posted December 8, 2012 Author Posted December 8, 2012 Thanks for the reply Melba23, your solution works like charm. Just another question - if I were to use the OnEvent mode, then will this problem occur or not? ---------------------------------------- :bye: Hey there, was I helpful? ---------------------------------------- My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1
Moderators Melba23 Posted December 8, 2012 Moderators Posted December 8, 2012 MKISH,What do you think? And why not try it and find out? As it is GUIGetMsg that is shortcutting the loop, not using it would prevent that possibility. Keep the _Projectile function in the loop with a suitable Sleep statement so that it runs smoothly - your OnEvent functions will only fire on thos events you specify, so moving the mouse should not affect the loop timing.M23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Â
stormbreaker Posted December 8, 2012 Author Posted December 8, 2012 Ah, was really dumb of me... You rock Melba23, actually wasn't on my pc before. OnEvent mode seems to be a better choice for writing my program. ---------------------------------------- :bye: Hey there, was I helpful? ---------------------------------------- My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now