Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Positioning window
Message
From
11/01/2017 15:58:06
 
 
To
11/01/2017 15:05:02
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01646490
Message ID:
01646539
Views:
55
ok this is the code to position any window given by its caption (title) on the desktop
it position the left,top,width and height of the window
try but change the window caption (i suppose your locale is english)
*--yposition_any_window.prg
*--	startig with Listing child windows for the Windows desktop-Nesw2News
*--adapted to search a specific window done with its title caption .
*--browse the result with informations on hwnd,window dimensions,title window,visibility,class (as option)
*--suppose 1 single instance of window with mycaption title.(it takes the first window)
*--window caption is localized (microsoft windows)...must put the exact localized title to search.change 
*--titles below (for french locale)
*--the setwindoxPos API can position any window given by its handlecan set left,top,width & height

*begin code
_Screen.WindowState=1

Local mycaption
*mycaption="Sans titre - Paint"
*run/n mspaint

*myCaption="Sans titre - Bloc-notes"
*run/n notepad

mycaption="Explorateur de fichiers"  &&warning loclized (here french---in english: files explorer... must be exact title.
Run/N explorer.Exe

Inke(3)  &&wait to make window appear for positionning it

#Define GW_HWNDLAST 1
#Define GW_HWNDNEXT 2
#Define GW_CHILD 5

Do Declare

Create Cursor csResult (HWnd N(12), isvisible N(1),;
	leftpos I, toppos I, rightpos I, botpos I, wincap C(100), classname C(64))
Local hDesktop, hFirstChild, hLastChild, rc, cWinCap, cWinClass,;
	nVisible, nLeft, nTop, nRight, nBottom
hDesktop = GetDesktopWindow()
hFirstChild = GetWindow(hDesktop, GW_CHILD)
hLastChild = GetWindow(hFirstChild, GW_HWNDLAST)
hCurrent = hFirstChild

Do While .T.
	cWinCap = GetWinText(hCurrent)
	cWinClass= GetClsName(hCurrent)
	nVisible = IsWindowVisible(hCurrent)

	rc = Repli(Chr(0),16)
	= GetWindowRect(hCurrent, @rc)
	nLeft = buf2dword(Substr(rc, 1,4))
	nTop = buf2dword(Substr(rc, 5,4))
	nRight = buf2dword(Substr(rc, 9,4))
	nBottom = buf2dword(Substr(rc, 13,4))
	***********test searched window here
	If Allt(Lower(mycaption)) == Allt(Lower(cWinCap))
		Insert Into csResult Values (hCurrent, nVisible,;
			nLeft, nTop, nRight, nBottom, cWinCap, cWinClass)
	Endi

	If hCurrent = hLastChild
		Exit
	Endif
	hCurrent = GetWindow(hCurrent, GW_HWNDNEXT)
Enddo
Locate
*BROWSE NORMAL   NOWAIT    && for isvisible=1
If Reccount()=0
	Messagebox("Window ["+mycaption+" ] not found...cancelling",16+4096,"error",1500)
	Return .F.
Endi
lnHandle=HWnd

If HWnd=0
	Messagebox("fails! no window on desktop....cancelling!",16+4096,"error",1500)
	Return .F.
Endi

*position window here-this is tests for 6 positions
Local xleft,xtop,xwidth,xheight    &&as you want
m.xwidth=500
m.xheight=200
SetWindowPos(m.lnHandle,0, 0,0,m.xwidth+100,m.xheight-50,64)
Inke(1)
SetWindowPos(m.lnHandle,0, 100,0,m.xwidth,m.xheight+100,64)
Inke(1)
SetWindowPos(m.lnHandle,0, 100,300,m.xwidth-50,m.xheight,64)
Inke(1)
SetWindowPos(m.lnHandle,0,400,50,m.xwidth,m.xheight+40,64)
Inke(1)
SetWindowPos(m.lnHandle,0, 50,500,m.xwidth+100,m.xheight+100,64)
Inke(1)
m.xwidth=xwidth+200
m.xheight=m.xheight+200
SetWindowPos(m.lnHandle,0,(Sysmetric(1)-xwidth)/2,(Sysmetric(2)-xheight)/2,m.xwidth,m.xheight,64)  &&centered on screen

Retu


Function GetClsName(hWindow)
	Local nBufsize, cBuffer
	cBuffer = Repli(Chr(0), 250)
	nBufsize = GetClassName(hWindow, @cBuffer, Len(cBuffer))
	Return Substr(cBuffer, 1, nBufsize)


Function GetWinText(hWindow)
	* returns window title bar text -- Win9*/Me/XP/2000
	Local cBuffer, nResult
	cBuffer = Space(250)
	nResult = GetWindowText(hWindow, @cBuffer, Len(cBuffer))
	Return Substr(cBuffer, 1, nResult)
endfunc

Function buf2dword(lcBuffer)
	Return Asc(Substr(lcBuffer, 1,1)) + ;
		BitLShift(Asc(Substr(lcBuffer, 2,1)),  8) +;
		BitLShift(Asc(Substr(lcBuffer, 3,1)), 16) +;
		BitLShift(Asc(Substr(lcBuffer, 4,1)), 24)
endfunc

Procedure Declare
	Declare Integer GetDesktopWindow In user32
	Declare Integer GetWindow In user32 Integer HWnd, Integer wFlag
	Declare Integer GetWindowRect In user32 Integer HWnd, String @lpRect
	Declare Integer IsWindowVisible In user32 Integer HWnd

	Declare Integer GetWindowText In user32;
		INTEGER HWnd, String @lpString, Integer cch

	Declare Integer GetClassName In user32;
		INTEGER HWnd, String lpClassName, Integer nMaxCount

	Declare Integer SetWindowPos In user32;
		INTEGER HWnd, Integer hWndInsertAfter,;
		INTEGER x, Integer Y, Integer cx, Integer cy,;
		INTEGER wFlags
 endproc		
		
*endcode
note : can use findwindow API but i prefer this code .
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform