Page 1 of 2

Open Window on Second Desktop

Posted: Mon Jun 15, 2015 6:26 pm
by Chuf
Hello

I'm looking for a way to open a window on a second monitor, I have found a solution even in the English forum but only for OSX have modifies the code and post the times here, perhaps someone knows how to do that under Windows or even better, as cross-platform solution

many Greetings

Code: Select all


mainWindow = OpenWindow(0,50,50,300,300,"Main Window",#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget|#PB_Window_SizeGadget|#PB_Window_SystemMenu)
ButtonGadget(1,10,10,250,50,"2nd Window ON/OFF")
ButtonGadget(2,10,60,250,50,"Send Message to 2nd Window")    
 If mainWindow   
    Repeat
      Event = WaitWindowEvent()
     
     Select Event
     
       Case #PB_Event_Gadget
         
         Select EventGadget()
          Case 1 
            If externalWindow = 1
              externalWindow = 0
              CloseWindow(AppWindow)
            Else
              ScreenArray = CocoaMessage(0, 0, "NSScreen screens")
              If CocoaMessage(0, ScreenArray, "count") < 2
                Debug "No second screen"
              Else
                externalWindow = 1
                CocoaMessage(@Frame.NSRect, CocoaMessage(0, ScreenArray, "objectAtIndex:", 1), "visibleFrame")
                AppWindow = OpenWindow(#PB_Any, 0, 0, 0, 0, "Second screen window", #PB_Window_BorderLess )
                TextGadget(21, 10, 10, 250, 50, "2nd Windows")              
                CocoaMessage(0, WindowID(AppWindow), "setFrame:@", @Frame, "display:", #YES)    
              EndIf  
            EndIf
          Case 2
              If externalWindow = 1
                SetGadgetText(21, "##### OK ####") ; sen a message to 2nd Monitor  
              EndIf
       EndSelect
       
     EndSelect
    Until Event = #PB_Event_CloseWindow
 EndIf

Re: Open Window on Second Desktop

Posted: Mon Jun 15, 2015 6:29 pm
by Julian
PureBasic - Desktop

Overview

The desktop library allows access to information about the user's desktop environment, such as screen width, height, depth, mouse position etc.

Desktop Manual

Desktop Example

Re: Open Window on Second Desktop

Posted: Mon Jun 15, 2015 6:37 pm
by Chuf
i found not a way to send or open a window direct on the 2nd Desktop

Re: Open Window on Second Desktop

Posted: Mon Jun 15, 2015 6:58 pm
by kenmo
Quick example:

Code: Select all

mainWindow = OpenWindow(0,50,50,300,300,"Main Window",#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget|#PB_Window_SizeGadget|#PB_Window_SystemMenu)
ButtonGadget(1,10,10,250,50,"2nd Window ON/OFF")
ButtonGadget(2,10,60,250,50,"Send Message to 2nd Window")   
 If mainWindow   
    Repeat
      Event = WaitWindowEvent()
     
     Select Event
     
       Case #PB_Event_Gadget
         
         Select EventGadget()
          Case 1
            If externalWindow = 1
              externalWindow = 0
              CloseWindow(AppWindow)
            Else
              If ExamineDesktops() < 2
                Debug "No second screen"
              Else
                externalWindow = 1
                AppWindow = OpenWindow(#PB_Any, 0, 0, WindowWidth(0), WindowWidth(0), "Second screen window", #PB_Window_BorderLess )
                TextGadget(21, 10, 10, 250, 50, "2nd Windows")
                
                ; top-left of 2nd desktop
                ;ResizeWindow(AppWindow, DesktopX(1), DesktopY(1), #PB_Ignore, #PB_Ignore)
                
                ; center on 2nd desktop
                ResizeWindow(AppWindow, DesktopX(1) + (DesktopWidth(1) - WindowWidth(AppWindow))/2, DesktopY(1) + (DesktopHeight(1) - WindowHeight(AppWindow))/2, #PB_Ignore, #PB_Ignore)
                
                HideWindow(AppWindow, #False)
              EndIf 
            EndIf
          Case 2
              If externalWindow = 1
                SetGadgetText(21, "##### OK ####") ; sen a message to 2nd Monitor 
              EndIf
       EndSelect
       
     EndSelect
    Until Event = #PB_Event_CloseWindow
 EndIf

Re: Open Window on Second Desktop

Posted: Mon Jun 15, 2015 7:04 pm
by TI-994A
Hi Chuf, and welcome to the PureBasic forum. :D
Chuf wrote:i found not a way to send or open a window direct on the 2nd Desktop
Try this:

Code: Select all

ExamineDesktops()
d2x = DesktopWidth(1)
OpenWindow(0, d2x + 100, 100, 400, 300, "Multiple Desktops", #PB_Window_SystemMenu)
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Hope it helps.

Re: Open Window on Second Desktop

Posted: Mon Jun 15, 2015 7:07 pm
by freak
TI-994A wrote:Try this:

Code: Select all

ExamineDesktops()
d2x = DesktopWidth(1)
OpenWindow(0, d2x + 100, 100, 400, 300, "Multiple Desktops", #PB_Window_SystemMenu)
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Hope it helps.
This assumes that the 2nd Monitor is to the left of the 1st one, which may not always be the case. Better use DesktopX() and DesktopY() to get the right position.

Re: Open Window on Second Desktop

Posted: Mon Jun 15, 2015 7:12 pm
by infratec
Hi,

my untested version:

Code: Select all

EnableExplicit

Structure DesktopStructure
  No.i
  X.i
  Y.i
  Width.i
  Height.i
  Name$
EndStructure



Procedure MoveWindowToDesktop(Window.i, Desktop.i, x.i, y.i, List DesktopList.DesktopStructure(), Flags.i=0)
  
  If IsWindow(Window)
    
    If SelectElement(DesktopList(), Desktop)
      
      If Flags = #PB_Window_ScreenCentered
        x = (DesktopList()\Width - WindowWidth(Window)) / 2
        y = (DesktopList()\Height - WindowHeight(Window)) / 2
      EndIf
      
      ResizeWindow(Window, DesktopList()\X + x, DesktopList()\Y + y, #PB_Ignore, #PB_Ignore)
      
    EndIf
    
  EndIf
  
EndProcedure



Define.i Desktops, i
NewList DesktopList.DesktopStructure()

Desktops = ExamineDesktops() - 1
For i = 0 To Desktops
  AddElement(DesktopList())
  DesktopList()\No = i
  DesktopList()\X = DesktopX(i)
  DesktopList()\Y = DesktopY(i)
  DesktopList()\Width = DesktopWidth(i)
  DesktopList()\Height = DesktopHeight(i)
  DesktopList()\Name$ = DesktopName(i)
  ;Debug DesktopList()\Name$
Next i


OpenWindow(0, 0, 0, 400, 300, "Test", #PB_Window_SystemMenu)

MoveWindowToDesktop(0, 0, 0, 0, DesktopList(), #PB_Window_ScreenCentered)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
At home I have only one monitor :cry:

You not really need the list, you can also get the values of the specified desktop inside the procedure.

Bernd

Re: Open Window on Second Desktop

Posted: Mon Jun 15, 2015 7:30 pm
by TI-994A
freak wrote:Better use DesktopX() and DesktopY() to get the right position.
Quite right; thank you.

This example will open a window on each desktop, identifying them in the title:

Code: Select all

desktops = ExamineDesktops()
For i = 0 To desktops - 1
  OpenWindow(i, DesktopX(i) + 100, DesktopY(i) + 100, 
             400, 300, "Desktop #" + Str(i), #PB_Window_SystemMenu)
Next i
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Good to know which is which. :lol:

Re: Open Window on Second Desktop

Posted: Mon Jun 15, 2015 7:45 pm
by Chuf
many Thanks, the best solution is from kenmo

many many Thanks!!

Re: Open Window on Second Desktop

Posted: Wed Jun 17, 2015 9:35 am
by infratec
Only for completeness:

the version without a list.

Code: Select all

Procedure MoveWindowToDesktop(Window.i, Desktop.i, x.i, y.i, Flags.i=0)
  
  If IsWindow(Window)
  
    If ExamineDesktops() > Desktop
      
      If Flags = #PB_Window_ScreenCentered
        x = (DesktopWidth(Desktop) - WindowWidth(Window)) / 2
        y = (DesktopHeight(Desktop) - WindowHeight(Window)) / 2
      EndIf
      
      ResizeWindow(Window, DesktopX(Desktop) + x, DesktopY(Desktop) + y, #PB_Ignore, #PB_Ignore)
      
    EndIf
    
  EndIf
  
EndProcedure


OpenWindow(0, 0, 0, 400, 300, "Test", #PB_Window_SystemMenu)

;MoveWindowToDesktop(0, 1, 0, 0, #PB_Window_ScreenCentered)
MoveWindowToDesktop(0, 1, 10, 10)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Now tested with 2 monitors :mrgreen:

Bernd

Re: Open Window on Second Desktop

Posted: Thu Jun 18, 2015 9:59 pm
by Chuf
Hi Bernd, have you testet the Programm by OS X or Windows?

Re: Open Window on Second Desktop

Posted: Fri Jun 19, 2015 7:03 am
by infratec
Hi,

tested on windows.
But since it doesn't use any API it should also work on OSX.
I can test it only on a MacBook, but without a second screen :cry:

Bernd

Re: Open Window on Second Desktop

Posted: Fri Jun 19, 2015 3:00 pm
by TI-994A
infratec wrote:...it should also work on OSX.
Hi Bernd. Just tested it with PureBasic v5.31 on OSX Mavericks, and it works perfectly with two screens, with and without centering. :wink:

Re: Open Window on Second Desktop

Posted: Fri Jun 26, 2015 9:40 pm
by Chuf
Hi Bernd, this code run not correct by div. resolution

my second monitor has 1920x1200
my 2nd has 800x600

the 2nd window is to big for the 2nd monitor

Re: Open Window on Second Desktop

Posted: Fri Jun 26, 2015 10:00 pm
by Chuf
sorry my error i´m search for a fullscreen solution for the 2nd monitor your code is OK i´m edit your code for fullscreen

Code: Select all

Procedure MoveWindowToDesktop(Window.i, Desktop.i, x.i, y.i, Flags.i)
  
  If IsWindow(Window)
  
    If ExamineDesktops() > Desktop
;       If Flags = #PB_Window_ScreenCentered
;         x = (DesktopWidth(Desktop) - WindowWidth(Window)) / 2
;         y = (DesktopHeight(Desktop) - WindowHeight(Window)) / 2
;       EndIf
      ;ResizeWindow(Window, DesktopX(Desktop) + x, DesktopY(Desktop) + y, #PB_Ignore, #PB_Ignore)
      ResizeWindow(Window, DesktopX(Desktop), DesktopY(Desktop),DesktopWidth(Desktop),DesktopHeight(Desktop))
      
    EndIf
    
  EndIf
  
EndProcedure