Page 1 of 1

GetDesktopFromWindow and SetWindowFromDesktopName

Posted: Sun Dec 27, 2009 1:38 am
by DoubleDutch
This post inspird me to do these two handy routines I think others may like:
http://www.purebasic.fr/english/viewtop ... 7&p=295167

Code: Select all

#MONITORINFOF_PRIMARY=1
#MONITOR_DEFAULTTONULL=0
#MONITOR_DEFAULTTOPRIMARY=1
#MONITOR_DEFAULTTONEAREST=2

Structure MONITORINFOEX
	cbSize.l
	rcMonitor.RECT
	rcWork.RECT
	dwFlags.l
	szDevice.s{#CCHDEVICENAME}
EndStructure

Procedure.s GetDesktopFromWindow(window.i)
	Protected result$="",hwnd.i,hmon.i,mi.MONITORINFOEX,n.i,i.i
	If IsWindow(window)
		hwnd=WindowID(window)
		mi\cbSize=SizeOf(mi)
		hmon=MonitorFromWindow_(hwnd,#MONITOR_DEFAULTTONEAREST)
		If hmon
			If GetMonitorInfo_(hmon,mi)
				result$=mi\szDevice
			EndIf
		EndIf
	EndIf
	ProcedureReturn result$ ;If "" there was an error or monitor not found.
EndProcedure

Procedure.i SetWindowFromDesktopName(desktop$,window.i)
	Protected result.i=#False,hwnd.i,hmon.i,mi.MONITORINFOEX,n.i,i.i,pt.POINT
	If IsWindow(window) And desktop$<>""
		hwnd=WindowID(window)
		mi\cbSize=SizeOf(mi)
		n=ExamineDesktops()-1
		For i=0 To n
			If DesktopName(i)=desktop$
				pt\x=DesktopX(i)
				pt\y=DesktopY(i)
				hmon=MonitorFromPoint_(pt\x,pt\y,#MONITOR_DEFAULTTONEAREST)
				If hmon
					If GetMonitorInfo_(hmon,mi)
						MoveWindow_(hwnd,mi\rcWork\left,mi\rcWork\top,mi\rcWork\right-mi\rcWork\left,mi\rcWork\bottom-mi\rcWork\top,1)
						result=#True
						Break
					EndIf
				EndIf
			EndIf
		Next
	EndIf
	ProcedureReturn result ;If #false, there was an error or monitor not found.
EndProcedure

If OpenWindow(0, 100, 200, 195, 260, "PureBasic Window", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget)

	SetWindowFromDesktopName("\\.\DISPLAY2",0)

  Repeat
    Event = WaitWindowEvent()

    If Event = #PB_Event_CloseWindow  ; If the user has pressed on the close button
      Quit = 1
    EndIf

  Until Quit = 1
  
  Debug(GetDesktopFromWindow(0))
  
EndIf

End   ; All the opened windows 
On the SetWindowFromDesktopName routine it also maximises to the selected desktop work area.

It would be handy if something like this was built-in so it could easily be made cross platform...

Re: GetDesktopFromWindow and SetWindowFromDesktopName

Posted: Sun Dec 27, 2009 2:18 am
by ts-soft
:D
thx, very usefull