Jump to content

Run a program in specific windows size


akorx
 Share

Recommended Posts

Hi guys,

I've got a little problem, and perhaps a big if I don't find a solution !

At work we have to change ours screen's settings to use big resolution but one of our application runs very bad if we don't use a resolution of 1024x768. I've tried to find a way on google but I've found nothing. So I've decided to create a little autoit script that will load this program in a windows (that has got the good size).

This my script (it's just a beta that works butnot with all kinds of programs... you will see why at the end) :

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

$prg='c:\Program Files\InfraRecorder\InfraRecorder.exe'

$hGUI = GUICreate("Loader", 1024, 768, 5, 5 ,BitOR($WS_SYSMENU,$WS_CLIPCHILDREN))
GUISetState(@SW_SHOW)

$pid=Run($prg,"",@SW_HIDE)
Sleep(1000)
While ProcessExists($pid) = 0
    Sleep(100)
WEnd
$title=_WinGetByPID($pid,1)
;msgbox(4096,"DEBUG",$title)
;Exit

WinWaitActive($title)

$hPRG= WinGetHandle($title)
_SetParent($hPRG, $hGUI)
WinSetState($hPRG, "", @SW_SHOWMAXIMIZED)

Do
Sleep(10)
Until GuiGetMsg() =-3

Func _SetParent($id_child, $h_parent)
If Not IsHWnd($h_parent) Then $h_parent = HWnd($h_parent)
If Not IsHWnd($id_child) Then $id_child = GUICtrlGetHandle($id_child)
If DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $id_child, "hwnd", $h_parent) <> 0 Then
Return 1
Else
seterror(1)
Return 0
EndIf
EndFunc

Func _WinGetByPID($iPID, $iArray = 1) ; 0 Will Return 1 Base Array & 1 Will Return The First Window.
Local $aError[1] = [0], $aWinList, $sReturn
If IsString($iPID) Then
$iPID = ProcessExists($iPID)
EndIf
$aWinList = WinList()
For $A = 1 To $aWinList[0][0]
If WinGetProcess($aWinList[$A][1]) = $iPID And BitAND(WinGetState($aWinList[$A][1]), 2) Then
If $iArray Then
Return $aWinList[$A][1]
EndIf
$sReturn &= $aWinList[$A][1] & Chr(1)
EndIf
Next
If $sReturn Then
Return StringSplit(StringTrimRight($sReturn, 1), Chr(1))
EndIf
Return SetError(1, 0, $aError)
EndFunc ;==>_WinGetByPID

1- This is a first step and just with infrarecorder.exe and it works... now the next step is to delete this :

Sleep(1000)
While ProcessExists($pid) = 0
    Sleep(100)
WEnd

... because I don't know how many time will take infrarecorder to start completely and perhaps it will never start so I must integrate à timeout.

2- There will be a big bug if the program called (here infrarecorder) opens a second window just after the start (for example if it ask for a password or if it gives tips at startup), because the name of the windows given by $title won't be the good one ! It must the PARENT windows ! perpahs with _WinAPI_GetAncestor() ? How can I do ?

3- The last step will to use $cmdline to give in arguments the size of the window and the name of the program that must be called, like this : loader.exe -w 1024 -h 768 "my program". But there will have a problem with ProcessWait() if there is 2 instances of the program (example we have load infrarecorder twice) how will I do ?

PS :

- computer are one Windows XP SP3 and I've to do this on 300 computers.

- sorry for my bad english, I hope you will understand my query.

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

I would imagine this code would help you along in your solution, But I may be wrong;

$AbsoluteXCoordinate = "0"
$AbsoluteYCoordinate = "0"
$AbsoluteSizeHeight = "1000"
$AbsoluteSizeWidth = "1000"

While 1
   Call("PositionCheck")
   Call("SizeCheck")
   Sleep("60000")
WEnd

Func PositionCheck()
While 1
$CurrentWindowPosition = WinGetPos("Title")
If $CurrentWindowPosition[0] <> $AbsoluteXCoordinate And $CurrentWindowPosition[1] <> $AbsoluteYCoordinate Then
   WinMove("Title", "", $AbsoluteXCoordinate, $AbsoluteYCoordinate, $AbsoluteSizeWidth, $AbsoluteSizeHeight)
   Return
   ExitLoop ;Not sure if this is 100% necessary
ElseIf $CurrentWindowPosition[0] = $AbsoluteXCoordinate And $CurrentWindowPosition[1] = $AbsoluteYCoordinate Then
   ;Window has not moved
   Return
   ExitLoop ;Not sure if this is 100% necessary
WEnd
EndFunc

Func SizeCheck()
While 1
$CurrentWindowSize = WinGetPos("Title")
If $CurrentWindowSize[3] <> $AbsoluteSizeHeight And $CurrentWindowSize[2] <> $AbsoluteSizeWidth Then
   WinMove("Title", "", $AbsoluteXCoordinate, $AbsoluteYCoordinate, $AbsoluteSizeWidth, $AbsoluteSizeHeight)
   Return
   ExitLoop ;Not sure if this is 100% necessary
If $CurrentWindowSize[3] = $AbsoluteSizeHeight And $CurrentWindowSize[2] = $AbsoluteSizeWidth Then
   ;Window has not been resized
   Return
   ExitLoop ;Not sure if this is 100% necessary
WEnd
EndFunc

Ofcourse you'll need to do some modifications to make it 100% fool proof.

You can use WinList to check for duplicate Windows with the same title. Which will also help you close them. Read the help file accordingly. Also, To check for multiple instances of the same window refer to the Title Special Definition link in the WinExists help file. That way you can WinClose the appropriate instance.

Edited by BlackDawn187
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...