Sign in to follow this  
Followers 0
Kiai

Best way to present a GUI next to another window

7 posts in this topic

I have created an application that waits for a certain window to be opened, and when it is, presents a small GUI in the title bar (height of the title bar, contains 4 buttons). I have also set it to move if the user changes the window position. The app loops, sleeps for 1 sec, deletes the GUI if the target window is no longer active. It all works, but when I check this out with Microsoft process explorer, it seems that this code runs the CPU a bit more than I expected -- nothing dramatic but it made an app that was using only 0.7% hit 2.5% at times. Any suggestions on making this more efficient? Thanks.

$size = WinGetPos($title)

If WinActive($title) And @error<>1 Then; if the size check fails because the window has been closed, @error is set to 1

$Open_chart_window = GUICreate("GUI_Open_chart",260, 16, $size[2]+$size[0]-340, $size[1]+4,$WS_POPUP + $WS_BORDER, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)

......

While WinActive($title) or WinActive("GUI_Open_chart") ;Keeps the GUI active as long as the main window is open

$size_new=WinGetPos($title)

If @error<>1 Then

If $size_new[0]<>$size[0] Or $size_new[1]<>$size[1] Or $size_new[2]<>$size[2] Or $size_new[3]<>$size[3] Then

WinMove("GUI_Open_chart","",$size_new[2]+$size_new[0]-340, $size_new[1]+4)

WinActivate($title)

$size=WinGetPos($title)

EndIf

EndIf

Sleep(1000)

WEnd

GUIDelete("GUI_Open_chart") ;close the GUI when the main window is no longer active

Share this post


Link to post
Share on other sites



Are you saying this script caused a different process to use more cpu than normal, or this script uses more cpu than you thought it would?

Share this post


Link to post
Share on other sites

Are you saying this script caused a different process to use more cpu than normal, or this script uses more cpu than you thought it would?

That wasn't very clear was it. This loop seems to take twice or more cpu% than a very similar loop in the same app that doesn't have the getwinpos statement. My question boils down to, is there a better way to essentially attach a gui to another window and have the gui travel with that window if moved, max'd or min'd?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

That wasn't very clear was it. This loop seems to take twice or more cpu% than a very similar loop in the same app that doesn't have the getwinpos statement. My question boils down to, is there a better way to essentially attach a gui to another window and have the gui travel with that window if moved, max'd or min'd?

Not sure, but maybe CPU can be decreased if you add sleep after each CPU intensive command, in this case getwinpos.

I'm assuming if you add sleep in last line after many commands, the CPU would need to process many commands before going to sleep.. so CPU is noticed higher.

But, if you add smaller sleep after each command, it could make the CPU to go unnoticed.

I'm not sure, and I don't know if it can help to decrease CPU or not. But, would like to know.

Also, in your script, I think "If @error<>1 Then" is not necessary as the chance of getting error on wingetpos while the window is active is quite rare. I read If is quite high on CPU so reducing it would be better.

One more thing, alternatively, if you are using it on AutoIt GUIs, you could register message, so you could intercept window move. Using: GUIRegisterMsg($WM_WINDOWPOSCHANGED, "WM_WINDOWPOSCHANGED")

Edited by MDCT

Share this post


Link to post
Share on other sites

I have created an application that waits for a certain window to be opened, and when it is, presents a small GUI in the title bar (height of the title bar, contains 4 buttons). I have also set it to move if the user changes the window position. The app loops, sleeps for 1 sec, deletes the GUI if the target window is no longer active. It all works, but when I check this out with Microsoft process explorer, it seems that this code runs the CPU a bit more than I expected -- nothing dramatic but it made an app that was using only 0.7% hit 2.5% at times. Any suggestions on making this more efficient? Thanks.

$size = WinGetPos($title)

If WinActive($title) And @error<>1 Then; if the size check fails because the window has been closed, @error is set to 1

$Open_chart_window = GUICreate("GUI_Open_chart",260, 16, $size[2]+$size[0]-340, $size[1]+4,$WS_POPUP + $WS_BORDER, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)

......

While WinActive($title) or WinActive("GUI_Open_chart") ;Keeps the GUI active as long as the main window is open

$size_new=WinGetPos($title)

If @error<>1 Then

If $size_new[0]<>$size[0] Or $size_new[1]<>$size[1] Or $size_new[2]<>$size[2] Or $size_new[3]<>$size[3] Then

WinMove("GUI_Open_chart","",$size_new[2]+$size_new[0]-340, $size_new[1]+4)

WinActivate($title)

$size=WinGetPos($title)

EndIf

EndIf

Sleep(1000)

WEnd

GUIDelete("GUI_Open_chart") ;close the GUI when the main window is no longer active

Not directly related to your question but you might have problems using WinGetPos when the application is minimized. If so using _WINAPI_GETWINDOWRECT might help which, as far as I have tried it, returns the correct coordinates for minimzed and normal windows.

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

using WinGetPos when the application is minimized.

Can a window be active and minimized at the same time? I'm not using explorer shell, so the I'm not sure.

In that case my suggestion was wrong, the @error check is quite necessary.

Share this post


Link to post
Share on other sites

Can a window be active and minimized at the same time?

No. But I thought that the OP might want to keep track of wether the app was minimized or not and then place some indicator near it or above it instead of closing his gui each time. Possibly an incorrect guess though.


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

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  
Followers 0