Open Window on Second Desktop

Just starting out? Need help? Post your questions and find answers here.
Chuf
User
User
Posts: 11
Joined: Sun Jun 14, 2015 8:25 pm

Open Window on Second Desktop

Post 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
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: Open Window on Second Desktop

Post 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
Chuf
User
User
Posts: 11
Joined: Sun Jun 14, 2015 8:25 pm

Re: Open Window on Second Desktop

Post by Chuf »

i found not a way to send or open a window direct on the 2nd Desktop
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Open Window on Second Desktop

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Open Window on Second Desktop

Post 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.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Open Window on Second Desktop

Post 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.
quidquid Latine dictum sit altum videtur
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Open Window on Second Desktop

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Open Window on Second Desktop

Post 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:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Chuf
User
User
Posts: 11
Joined: Sun Jun 14, 2015 8:25 pm

Re: Open Window on Second Desktop

Post by Chuf »

many Thanks, the best solution is from kenmo

many many Thanks!!
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Open Window on Second Desktop

Post 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
Chuf
User
User
Posts: 11
Joined: Sun Jun 14, 2015 8:25 pm

Re: Open Window on Second Desktop

Post by Chuf »

Hi Bernd, have you testet the Programm by OS X or Windows?
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Open Window on Second Desktop

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Open Window on Second Desktop

Post 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:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Chuf
User
User
Posts: 11
Joined: Sun Jun 14, 2015 8:25 pm

Re: Open Window on Second Desktop

Post 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
Chuf
User
User
Posts: 11
Joined: Sun Jun 14, 2015 8:25 pm

Re: Open Window on Second Desktop

Post 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
Post Reply