1. Determining if a window is visible
- Posted by marky1124 Jul 23, 2009
- 907 views
Hi,
I'd like to know whether a window is positioned such that it is within the current visible windows desktop. This applies to a dual screen scenario. Sometimes a laptop user will move desk, previously they had their external monitor to the left of their laptop and used a dual screen setup like that, now they've moved desk and their external monitor is to the right of their laptop. My application remembers where a window was placed last time and then positions it in the same place next time it is opened. In this scenario the user can't get to the window that has been positioned to the left of their main screen. I'd like to be able to make the code realise that the saved location is outside of the current visible desktop. I've tried using isVisible(myWin) but that's related to the isVisible(myWin, w32True) setting rather than the position on the virtual desktop.
Any anyone help please?
Cheers, Mark
Here's a few snippets to illustrate the code I'm using
E.g. on closing window, it's position is stored in a config file
myWindowRect = getWindowRect(myWin) writeINI(ConfigFile, "Location", "myWinRect", myWindowRect)
Another day on opening that window it is positioned in the previously recorded location
myWindowRect = readINI(ConfigFile, "Location", "myWinRect", "None") if not equal("None", lTmp) then setWindowRect(myWin, myWindowRect) end if
2. Re: Determining if a window is visible
- Posted by mattlewis (admin) Jul 23, 2009
- 895 views
I'd like to know whether a window is positioned such that it is within the current visible windows desktop.
Does getWindowRect( Screen ) work? If you got that rect, you could compare it to your window.
Matt
3. Re: Determining if a window is visible
- Posted by marky1124 Jul 23, 2009
- 900 views
Does getWindowRect( Screen ) work? If you got that rect, you could compare it to your window.
Matt
I don't think so. My main window is called Window1. If I try and use getWindowRect(Screen) I get
Error code 472 getRect:GetWindowRect failed. Win32Lib v0.70.4 17-Jun-2008
Is Screen a predefined variable?
Here's some code I've been using to play with this problem:
-- code generated by Win32Lib IDE v1.0.4 Build July-06-2008 constant TheProgramType="exw" include Win32Lib.ew without warning -------------------------------------------------------------------------------- -- Window Window1 constant Window1 = createEx( Window, "Window1", 0, Default, Default, 400, 300, 0, 0 ) constant PushButton8 = createEx( PushButton, "Open child window", Window1, 40, 44, 100, 28, 0, 0 ) constant PushButton3 = createEx( PushButton, "Move child off top", Window1, 40, 92, 96, 28, 0, 0 ) constant wLabel2 = createEx( LText, "LText10", Window1, 36, 184, 312, 20, 0, 0 ) constant PushButton9 = createEx( PushButton, "Close child window", Window1, 240, 44, 108, 28, 0, 0 ) constant PushButton5 = createEx( PushButton, "Make Visible", Window1, 144, 92, 88, 28, 0, 0 ) constant PushButton6 = createEx( PushButton, "Move child off bottom", Window1, 240, 92, 108, 28, 0, 0 ) constant PushButton4 = createEx( PushButton, "Close", Window1, 280, 220, 88, 28, 0, 0 ) constant wLabel = createEx( LText, "", Window1, 40, 152, 308, 20, 0, 0 ) --------------------------------------------------------- -------------------------------------------------------------------------------- -- Window childWin constant childWin = createEx( Window, "Window 2", 0, Default, Default, 400, 300, 0, 0 ) --------------------------------------------------------- -------------------------------------------------------------------------------- procedure checkifVisible(sequence pButton) sequence wSize if isVisible(childWin) then setText(wLabel, pButton & ": child is visible") else setText(wLabel, pButton & ": child is NOT visible") end if wSize = getWindowRect(Window1) -- wSize = getWindowRect(Screen) setText(wLabel2, "Left=" & sprint(wSize[1]) & ", Top=" & sprint(wSize[2]) & ", Right=" & sprint(wSize[3]) & ", Bottom=" & sprint(wSize[4])) end procedure -------------------------------------------------------------------------------- procedure Window1_onOpen (integer self, integer event, sequence params)--params is () checkifVisible(getText(self)) end procedure setHandler( Window1, w32HOpen, routine_id("Window1_onOpen")) -------------------------------------------------------------------------------- procedure PushButton8_onClick (integer self, integer event, sequence params)--params is () openWindow(childWin, Normal) checkifVisible(getText(self)) end procedure setHandler( PushButton8, w32HClick, routine_id("PushButton8_onClick")) -------------------------------------------------------------------------------- procedure PushButton9_onClick (integer self, integer event, sequence params)--params is () closeWindow(childWin) checkifVisible(getText(self)) end procedure setHandler( PushButton9, w32HClick, routine_id("PushButton9_onClick")) -------------------------------------------------------------------------------- procedure PushButton3_onClick (integer self, integer event, sequence params)--params is () sequence size size = getCtlSize(childWin) setRect(childWin, 500, -1200, size[1], size[2], w32True) checkifVisible(getText(self)) end procedure setHandler( PushButton3, w32HClick, routine_id("PushButton3_onClick")) -------------------------------------------------------------------------------- procedure PushButton5_onClick (integer self, integer event, sequence params)--params is () sequence size size = getCtlSize(childWin) setRect(childWin, 500, 20, size[1], size[2], w32True) checkifVisible(getText(self)) end procedure setHandler( PushButton5, w32HClick, routine_id("PushButton5_onClick")) -------------------------------------------------------------------------------- procedure PushButton6_onClick (integer self, integer event, sequence params)--params is () sequence size size = getCtlSize(childWin) setRect(childWin, 500, 1200, size[1], size[2], w32True) checkifVisible(getText(self)) end procedure setHandler( PushButton6, w32HClick, routine_id("PushButton6_onClick")) -------------------------------------------------------------------------------- procedure PushButton4_onClick (integer self, integer event, sequence params)--params is () abort(0) end procedure setHandler( PushButton4, w32HClick, routine_id("PushButton4_onClick")) --------------------------------------------------------- WinMain( Window1,Normal )
4. Re: Determining if a window is visible
- Posted by ghaberek (admin) Jul 23, 2009
- 970 views
There is a function called MonitorFromWindow which will provide the monitor on which a window is located. It can be optioned to return NULL if the window is not on a monitor.
-- api function global constant xMonitorFromWindow = registerw32Function( user32, "MonitorFromWindow", {C_LONG,C_INT}, C_LONG ) -- flag values global constant MONITOR_DEFAULTTONULL = 0, MONITOR_DEFAULTTOPRIMARY = 1, MONITOR_DEFAULTTONEAREST = 2 -- how to use monitor = w32Func( xMonitorFromWindow, {getHandle(MyWindow), MONITOR_DEFAULTTONULL} ) if monitor = NULL then -- do stuff here end if
You'll find other fun multi-monitor functions in the Multiple Display Monitors Reference on MSDN.
-Greg
5. Re: Determining if a window is visible
- Posted by marky1124 Jul 24, 2009
- 946 views
That's working a treat. Thank you very much Greg.