Jump to content
Sign in to follow this  
JBeardNC

Re-center GUI

Recommended Posts

JBeardNC

I have a GUI that I don't want my users to ignore. I've already got it set so that they can't minimize it or close it without choosing an option but I'm afraid that they'll move it entirely off the screen. How can I make it so that if its moved, it automatically re-centers itself?

AdvaTHANKSnce

Share this post


Link to post
Share on other sites
enaiman

Not so difficult - basically you need to get the position of the window periodically and if the position is different move it to the new position.

Proof of concept:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
$width = 330
$height = 260
$Form1 = GUICreate("Form1", 337, 264, @DesktopWidth/2 - $width/2, @DesktopHeight/2 - $height/2)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetState(@SW_SHOW)
$wPos = WinGetPos($Form1)

While 1
    Sleep(250)
    $newPos = WinGetPos($Form1)
    If $newPos[0] <> $wPos[0] Or $newPos[1] <> $wPos[1] Then WinMove($Form1, "", $wPos[0], $wPos[1])
WEnd

Func Form1Close()
    Exit
EndFunc

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
Yoriz

Another way.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Global $iGuiWidth = 500, $iGuiHeight = 500, $iGuiXPos = (@DesktopWidth/2)-$iGuiWidth/2, $iGuiYPos = (@DesktopHeight/2)-$iGuiHeight/2

$hGUI = GUICreate("Cant move me HaHa", $iGuiWidth, $iGuiHeight, $iGuiXPos, $iGuiYPos)
GUISetState()

GUIRegisterMsg($WM_WINDOWPOSCHANGING, "WM_WINDOWPOSCHANGING")

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

Func WM_WINDOWPOSCHANGING($hWnd, $Msg, $wParam, $lParam)
    Local $stWinPos = DllStructCreate("uint;uint;int;int;int;int;uint", $lParam)
    Local $iLeft = DllStructGetData($stWinPos, 3)
    Local $iTop = DllStructGetData($stWinPos, 4)
    Local $iWidth = DllStructGetData($stWinPos, 5)
    Local $iHeight = DllStructGetData($stWinPos, 6)
    If $iLeft <> $iGuiXPos Then DllStructSetData($stWinPos, 3, $iGuiXPos)
    If $iTop <> $iGuiYPos Then DllStructSetData($stWinPos, 4, $iGuiYPos)
    If $iWidth <> $iGuiWidth Then DllStructSetData($stWinPos, 5, $iGuiWidth)
    If $iHeight <> $iGuiHeight Then DllStructSetData($stWinPos, 6, $iGuiHeight)
EndFunc

GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.

Share this post


Link to post
Share on other sites
martin

I have a GUI that I don't want my users to ignore. I've already got it set so that they can't minimize it or close it without choosing an option but I'm afraid that they'll move it entirely off the screen. How can I make it so that if its moved, it automatically re-centers itself?

AdvaTHANKSnce

To stop a window being moved you handle the $SC_MOVE message

#include <GUIConstantsEx.au3>

Const $WM_SYSCOMMAND = 0x0112
Const $SC_MOVE = 0xF010

$gui = GUICreate("Stuck Here")
GUISetState()

GUIRegisterMsg($WM_SYSCOMMAND, "On_WM_SYSCOMMAND")
winsetontop($gui,"",1)

While GUIGetMsg() <> -3
WEnd

Func On_WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0xFFF0) = $SC_MOVE Then Return False;stop moves

    Return $GUI_RUNDEFMSG
EndFunc

;another way is to have no title bar to drag

#include <windowsconstants.au3>

$g2 = GUICreate("",200,200,-1,-1,$WS_POPUP)
GUISetState()
GUICtrlCreatelabel("",0,0,200,2)
GUICtrlSetBkColor(-1,0)
GUICtrlCreatelabel("",0,0,2,200)
GUICtrlSetBkColor(-1,0)
GUICtrlCreatelabel("",198,0,2,200)
GUICtrlSetBkColor(-1,0)
GUICtrlCreatelabel("",0,198,200,2)
GUICtrlSetBkColor(-1,0)
GUICtrlCreateLabel("something to say",20,80)
winsetontop($g2,"",1)
sleep(7000)
Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Joemonkey

Not so difficult - basically you need to get the position of the window periodically and if the position is different move it to the new position.

Proof of concept:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
$width = 330
$height = 260
$Form1 = GUICreate("Form1", 337, 264, @DesktopWidth/2 - $width/2, @DesktopHeight/2 - $height/2)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetState(@SW_SHOW)
$wPos = WinGetPos($Form1)

While 1
    Sleep(250)
    $newPos = WinGetPos($Form1)
    If $newPos[0] <> $wPos[0] Or $newPos[1] <> $wPos[1] Then WinMove($Form1, "", $wPos[0], $wPos[1])
WEnd

Func Form1Close()
    Exit
EndFunc

Is there a way to incorporate this into a GUI that also has a button? It seems if I try to use this along with a button the sleep prevents the button from being pushed. What I would like to do is have my GUI pop up, the user can hit Yes at any time (not waiting for a Sleep function) but have the GUI re-pop to center once every 60 minutes. Here is my existing code

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

AutoItSetOption("GUIEventOptions", 1)

$Form1 = GUICreate("title", 447, 205, -1, -1, Default, $WS_EX_TOPMOST)
    GUISetFont(12)
    GUICtrlCreateLabel("blah blah blah click Yes to reboot. ", 10, 10)
    $Yesbutton = GUICtrlCreateButton("Yes", 274, 152, 75, 25)
    GUISetState(@SW_SHOW)
    
    While 1
        $msg = GUIGetMsg()
        Select
        Case $msg = $Yesbutton
            Run("shutdown.exe /r /t 0")
        Exit
        EndSelect
    WEnd

Share this post


Link to post
Share on other sites
Realm

Joemonkey,

First, Welcome to the AutoIt Forums :graduated:

Try this:

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

AutoItSetOption("GUIEventOptions", 1)

$Form1 = GUICreate("title", 447, 205, -1, -1, Default, $WS_EX_TOPMOST)
GUISetFont(12)
GUICtrlCreateLabel("blah blah blah click Yes to reboot. ", 10, 10)
$Yesbutton = GUICtrlCreateButton("Yes", 274, 152, 75, 25)
GUISetState(@SW_SHOW)
$wPos = WinGetPos($Form1)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $Yesbutton
            Run("shutdown.exe /r /t 0")
            Exit
    EndSelect
    $newPos = WinGetPos($Form1)
    If $newPos[0] <> $wPos[0] Or $newPos[1] <> $wPos[1] Then WinMove($Form1, "", $wPos[0], $wPos[1])    
WEnd

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
Joemonkey

Thanks for the fast reply Realm, I had code similar to that working but I only want the window to check every 60 minutes to see if it needs to reposition itself, not instantly. It seems if I put a Sleep command in there it breaks the functionality of my Yes button.

Share this post


Link to post
Share on other sites
Realm

Hello Joemonkey,

All you need to do is put a timer check into it like this:

#include <WindowsConstants.au3>

AutoItSetOption("GUIEventOptions", 1)

$Form1 = GUICreate("title", 447, 205, -1, -1, Default, $WS_EX_TOPMOST)
GUISetFont(12)
GUICtrlCreateLabel("blah blah blah click Yes to reboot. ", 10, 10)
$Yesbutton = GUICtrlCreateButton("Yes", 274, 152, 75, 25)
GUISetState(@SW_SHOW)
$wPos = WinGetPos($Form1)

$timer = TimerInit() ;added initial timestamp
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $Yesbutton
            Run("shutdown.exe /r /t 0")
            Exit
    EndSelect
    If TimerDiff($timer) > 60000 Then ;check if a 60,000 milliseconds/minute has passed
        $newPos = WinGetPos($Form1)
        If $newPos[0] <> $wPos[0] Or $newPos[1] <> $wPos[1] Then WinMove($Form1, "", $wPos[0], $wPos[1]) 
        $timer = TimerInit();resets the timestamp
    EndIf
WEnd

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.