Jump to content
Sign in to follow this  

Moving window with WinMove

Recommended Posts

Hello! Recently, I've been working on a small thing. I met a problem, and dunno what's wrong. I want to make my window "Stats" move, as the original window moves. Here is my code and what happens to the "Stats" window after dragging the bigger window on it.

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>
#Include <Misc.au3>

Opt("GUIOnEventMode", 1)
Opt("MouseCoordMode", 2) ;MouseGetPos will work for window, not the whole screen

Global $GuiSizeX = 1050, $GuiSizeY = 300, $coordX = 0, $coordY = 0
$hGui = GUICreate("GDIPlus Example", $GuiSizeX, $GuiSizeY, $coordX, $coordY)
$stats = GUICreate("Stats", 200, $GuiSizeY, $coordX + $GuiSizeX, $coordY)
GUISetState(@SW_SHOW, $hGui)
GUISetState(@SW_SHOW, $stats)


; Create Double Buffer
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)
;End Double Buffer

;Graphics here
$hPen = _GDIPlus_PenCreate(0xFFFF0000, 2)
;End of graphics
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $hGui)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $stats)


$windowpos = WinGetPos($hGui)
_MouseTrap($windowpos[0], $windowpos[1], $windowpos[0] + $windowpos[2], $windowpos[1] + $windowpos[3])

While Sleep(100)

Func draw1()
     _GDIPlus_GraphicsClear($hGraphic, 0xFFE8FFE8)
        $pos = MouseGetPos()
    _GDIPlus_GraphicsDrawEllipse ($hGraphic, 100, 70, 50, 50, $hPen) ;head
    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 120, 125, 200, $hPen) ;body

    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 135, 100, 160, $hPen) ;left arm
    _GDIPlus_GraphicsDrawLine ($hGraphic, 100, 160, 170, $pos[1], $hPen);left forearm

    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 120, 125, 200, $hPen) ;right arm
    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 120, 125, 200, $hPen);right forearm

    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 200, 90, 280, $hPen) ;left leg
    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 200, 160, 280, $hPen) ;right leg
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)

Func WM_ERASEBKGND($hWnd, $msg, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    Return 1
EndFunc  ;==>MY_PAINT

Func _Exit()
    GUIRegisterMsg($WM_ERASEBKGND, "")
Func MouseTrap()
    $windowpos2 = WinGetPos($hGui)

    If $windowpos[0] <> $windowpos2[0] Or $windowpos[1] <> $windowpos2[1] Or $windowpos[2] <> $windowpos2[2] Or $windowpos[3] <> $windowpos2[3] Then
        ;tylko jesli zmienilo sie polozenie
        _MouseTrap($windowpos2[0], $windowpos2[1], $windowpos2[0] + $windowpos2[2], $windowpos2[1] + $windowpos2[3])
        WinMove(WinGetTitle($stats), "", $windowpos2[0] + $windowpos2[2], $windowpos2[1])
EndFunc   ;

Posted Image

When u test the script, try to move the big window and notice what happens. Why?

Edited by zwierzak

Share this post

Link to post
Share on other sites

Hello zwierzak,

After looking at the image you included, all I could think was LAG! Looking at your script I think I found the problem.

While Loops work only if a condition is met.. such as 'While 1'. which is the same as 'While True'. In your script your loop is While Sleep(100) which is not a standard condition for a loop. Don't quote me, but I don't think the Sleep function is going to work the way you intended. That small loop without sleep will cause your cpu to overload, giving lots of lag like seen in your image.

I would also add a HotKeySet() to exit the application or include a condition in your loop to check for an exit call such as GUIGetMsg() for $GUI_Event_Close.

Try changing this:

While Sleep(100)

To This:

While 1
    ;add a condition so you can exit the loop as well


Edit: typos

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post

Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...