Which API or command line to turn on or off a second screen?

Windows specific forum
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Which API or command line to turn on or off a second screen?

Post by Marc56us »

Hi all,

Is there a way to turn ON or OFF a second monitor ?

I have two screens, often I don't use the second one and I would like to put in my little PureBasic Deskboard application, a button to turn it on or off.

Under Windows 10 (and maybe other versions), it is possible to turn it on and off with the keyboard shortcut WIN + P which opens a pane on the right to choose an option (first only, dupplicate, extended, second only).

I searched the net but couldn't find any API or command line that could do it.
Any ideas ?

PS. One funny thing, my second screen is an old 17" that I didn't use because of a lack of horizontal space on my desk.
Looking at the options of Windows 10, I saw that it could rotate the image of any monitor in the desired direction, without the latter having a specific hardware option.
Now I have the help of PB clearly visible at all times on my second monitor in the vertical.
I simply unscrewed the original foot and made supports with my 3D printer

:wink:
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Which API or command line to turn on or off a second scr

Post by Dude »

Marc56us wrote:Under Windows 10 (and maybe other versions), it is possible to turn it on and off with the keyboard shortcut WIN + P which opens a pane on the right to choose an option (first only, dupplicate, extended, second only).

I searched the net but couldn't find any API or command line that could do it.
Any ideas ?
Maybe "DisplaySwitch.exe" with RunProgram():

https://winaero.com/blog/switch-between ... windows-8/

:?: I haven't got two monitors to test it.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Which API or command line to turn on or off a second scr

Post by Marc56us »

Thank you Dude, it works. :P

DisplaySwitch.exe
/internal
/clone
/extend
/external
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4661
Joined: Sun Apr 12, 2009 6:27 am

Re: Which API or command line to turn on or off a second scr

Post by RASHAD »

Hi Marc56us
No external application

1- Switch to 2nd. monitor using adapted version of Danilo
2- Switch the monitor off
3- Switch back to 1st. monitor

Code: Select all

;moves the current foreground window to the next monitor 
;Danilo
;

#MONITOR_DEFAULTTONEAREST = 2

Structure _MonitorInfo
    hMonitor.i
    rect.RECT
EndStructure

Global NewList Monitors._MonitorInfo()

Procedure MonitorEnumProc(hMonitor,hdcMonitor,*lprcMonitor.RECT,dwData)
    AddElement( Monitors() )
    Monitors()\hMonitor = hMonitor
    Monitors()\rect\left    = *lprcMonitor\left
    Monitors()\rect\top     = *lprcMonitor\top
    Monitors()\rect\right   = *lprcMonitor\right
    Monitors()\rect\bottom  = *lprcMonitor\bottom
    ProcedureReturn #True
EndProcedure

Procedure GetMonitors()
    ClearList( Monitors() )
    EnumDisplayMonitors_(0,0,@MonitorEnumProc(),0)
EndProcedure

Procedure MoveWindowToNextMonitor(hWndForeground=0)
    If hWndForeground=0
        hWndForeground = GetForegroundWindow_()
    EndIf
    If hWndForeground And ListSize( Monitors() ) > 1 And (hWndForeground <> GetDesktopWindow_())
        monitor = MonitorFromWindow_(hWndForeground,#MONITOR_DEFAULTTONEAREST)

        GetWindowRect_(hWndForeground,@r.RECT)
        r\right  - r\left
        r\bottom - r\top
        leftoffset = 0

        ResetList( Monitors() )
        While NextElement( Monitors() )
            If Monitors()\hMonitor = monitor
                leftoffset = r\left - Monitors()\rect\left
                If Not NextElement( Monitors() )
                    FirstElement( Monitors() )
                EndIf
                leftoffset = Monitors()\rect\left + leftoffset
                MoveWindow_(hWndForeground,leftoffset,r\top,r\right,r\bottom,#True)
                Break
            EndIf
        Wend
    Else
      MessageRequester("Info","Only one Monitor available",#PB_MessageRequester_Ok |#MB_ICONINFORMATION)
      End
    EndIf
EndProcedure

Procedure EnumWindowsProc(hwnd,lParam)
    If IsWindowVisible_(hWnd)
    title$ = Space(1024)
    GetWindowText_(hwnd,@title$,1024)
    If Trim(title$)
        MenuItem(hWnd, Trim(title$))
    EndIf
    EndIf
    ProcedureReturn #True
EndProcedure

Procedure AddWindows()
    EnumWindows_(@EnumWindowsProc(),0)
EndProcedure

GetMonitors()

OpenWindow(0,0,0,0,0,"",#PB_Window_Invisible)

RegisterHotKey_(WindowID(0),1,#MOD_CONTROL,#VK_F8)

icon = CreateImage(#PB_Any, 16,16)
If StartDrawing( ImageOutput( icon ) )
    For i = 0 To 7
        Box(i,i,15-i*2,15-i*2,RGB(255-i*30,255-i*30,255-i*30))
    Next i
    StopDrawing()
EndIf
;icon = LoadImage(#PB_Any, IconName$)

AddSysTrayIcon(1, WindowID(0), ImageID(icon))

Repeat
    Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
            End
        Case #PB_Event_SysTray
            popup = CreatePopupMenu(#PB_Any)
                MenuItem(2, "ReEnumerate Monitors")
                MenuBar()
                OpenSubMenu("Move Window")
                    AddWindows()
                CloseSubMenu()
                MenuBar()
                MenuItem(1, "Exit")
            DisplayPopupMenu(popup,WindowID(0))
            FreeMenu(popup)
        Case #PB_Event_Menu
          Select EventMenu()
            Case 1 : End
            Case 2 : GetMonitors()
            Default: MoveWindowToNextMonitor( EventMenu() )
          EndSelect
        Case #WM_HOTKEY
            If EventwParam() = 1
                MoveWindowToNextMonitor()
            EndIf
    EndSelect
ForEver

Code: Select all

;Off
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
Delay(500)
;On
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, -1)
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Which API or command line to turn on or off a second scr

Post by Dude »

RASHAD wrote:No external application
DisplaySwitch.exe is part of Windows (not external), but your standalone code is always better, Rashad. :)
Post Reply