Page 1 of 1

App runs on #Desktop?

Posted: Mon Apr 12, 2010 7:24 pm
by Frarth
Is there a way to find out the specific desktop an application is running on if a user has multiple desktops? This seems useful if one is to examine the minimum allowed resolution for one's app...

Any insight greatly appreciated

Frank

Re: App runs on #Desktop?

Posted: Mon Apr 12, 2010 7:29 pm
by ts-soft
Use the Desktop lib in PB. If you subtract the y pos from desktopweigth without negative you are on second desktop, and so on :wink:

Re: App runs on #Desktop?

Posted: Mon Apr 12, 2010 7:43 pm
by Frarth
You mean desktopheight?

Re: App runs on #Desktop?

Posted: Mon Apr 12, 2010 7:56 pm
by ts-soft
desktop width!

Code: Select all

Res. 16:10
1680 * 1050

Desktop Primary 1        Secundary 2       ... 3

-----------------------------------------------------
                  |                   |
 0 - 1679          |       1680 - 3359 | 3360 -
                  |                   |
------------------------------------------------------

Re: App runs on #Desktop?

Posted: Mon Apr 12, 2010 8:04 pm
by Frarth
Ah, so if the first desktop has width 1440, where X = 0, then the second desktop's X will be 1440, right?

Edit: right, you just added the answer to my question... Thankx!

Re: App runs on #Desktop?

Posted: Mon Apr 12, 2010 8:58 pm
by freak
You are assuming that the monitors are all next to each other (with the primary one on the left), but that does not have to be the case.

You should use DesktopX() and DesktopY() so it will work with all possible configurations.

Re: App runs on #Desktop?

Posted: Mon Apr 12, 2010 9:11 pm
by ts-soft
but how can i test which desktop, if the user moves the window to secondary?
I can only use WindowX(0), and this give me a value like my description above.

The primary is not always on the left, but the values of WindowX(0) works in this way!

Re: App runs on #Desktop?

Posted: Mon Apr 12, 2010 9:27 pm
by freak
All coordinates in PB are "virtual screen coordinates". This means they are relative to the top/left corner of the primary monitor, even if you are on another monitor. DesktopX/DesktopY tells you the location of the monitors in these coordinates as well. (so its the location of the top/left corner of the specified monitor in relation to the primary one.

See here about the virtual screen: http://msdn.microsoft.com/en-us/library/dd145136.aspx

Example:

Code: Select all

Procedure DesktopFromPoint(x, y)
  Count = ExamineDesktops()
  For i = 0 To Count-1
    dx = DesktopX(i)
    dy = DesktopY(i)
    dw = DesktopWidth(i)
    dh = DesktopHeight(i)
    
    If x >= dx And x <= dx+dw And y >= dy And y <= dy+dh
      ProcedureReturn i
    EndIf  
  Next i  
  
  ProcedureReturn -1 ; point is outside of all monitors
EndProcedure

OpenWindow(0, 100, 100, 200, 50, "Monitor check", #PB_Window_SystemMenu)
TextGadget(0, 10, 10, 180, 25, "Window is on Desktop: " + Str(DesktopFromPoint(WindowX(0), WindowY(0))))

Repeat
  Select WaitWindowEvent()
  
    Case #PB_Event_CloseWindow
      End
      
    Case #PB_Event_MoveWindow
      SetGadgetText(0, "Window is on Desktop: " + Str(DesktopFromPoint(WindowX(0), WindowY(0))))
    
  EndSelect
ForEver
This will work with any monitor configuration.

Re: App runs on #Desktop?

Posted: Mon Apr 12, 2010 9:40 pm
by ts-soft
Nice code, thanks

Re: App runs on #Desktop?

Posted: Tue Apr 13, 2010 5:01 am
by Frarth
freak wrote:

Code: Select all

    If x >= dx And x <= dx+dw And y >= dy And y <= dy+dh
      ProcedureReturn i
    EndIf
On the sideline, would it not be more optimal to code:

Code: Select all

If x >= dx 
  If x <= dx+dw
    If y >= dy
      If y <= dy+dh
        ProcedureReturn i
      EndIf
    EndIf
  EndIf
EndIf
This would be the case if the compiler were to examine all conditions, even if the first fails.

Re: App runs on #Desktop?

Posted: Tue Apr 13, 2010 8:21 am
by Demivec
Frarth wrote:
freak wrote:

Code: Select all

    If x >= dx And x <= dx+dw And y >= dy And y <= dy+dh
      ProcedureReturn i
    EndIf
On the sideline, would it not be more optimal to code:

Code: Select all

If x >= dx 
  If x <= dx+dw
    If y >= dy
      If y <= dy+dh
        ProcedureReturn i
      EndIf
    EndIf
  EndIf
EndIf
This would be the case if the compiler were to examine all conditions, even if the first fails.
You wouldn't have to do that because the complier uses shortcut evaluation. It will only evaluate all the conditions if each one is successively and sequentially true.

Re: App runs on #Desktop?

Posted: Tue Apr 13, 2010 10:00 am
by Fred
Yes, both are equivalent, so the shorted the better ;)

Re: App runs on #Desktop?

Posted: Tue Apr 13, 2010 4:52 pm
by Frarth
Thanks for elaborating on this!

Frank