Announcement

Collapse
No announcement yet.

Two monitors

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    What event is triggered when a monitor is deconnected?

    Comment


    • #22
      Not sure if there is a better way but

      case %WM_DISPLAYCHANGE

      detects when I connect/disconnect my laptop to my TV (via miracast), and when I change from duplicate to extended

      https://docs.microsoft.com/en-us/win...-displaychange

      Comment


      • #23
        Here some sample code. using EnumDisplayMonitors when %WM_DISPLAYCHANGE is triggered.

        Added: More Monitor info, and direction from primary

        '
        Code:
        [COLOR=#008080]' Using EnumDisplayMonitors
        ' Adapted from Pierre Bellisle's code.
        ' https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/763084-how-to-detect-screen-resolution?p=763089#post763089[/COLOR]
        
        [COLOR=#0000FF]#COMPILE EXE[/COLOR]
        [COLOR=#0000FF]#DIM ALL[/COLOR]
        [COLOR=#0000FF]#INCLUDE[/COLOR] [COLOR=#800040]"Win32Api.inc"[/COLOR]
        
        [COLOR=#8000FF]%MONITORINFOF_PRIMARY[/COLOR] = [COLOR=#FF0000]&H00000001???[/COLOR]
        [COLOR=#008080]'_____________________________________________________________________________[/COLOR]
        
        [COLOR=#0000FF]FUNCTION[/COLOR] MonitorEnum([COLOR=#0000FF]BYVAL[/COLOR] hMonitor [COLOR=#0000FF]AS DWORD[/COLOR], [COLOR=#0000FF]BYVAL[/COLOR] hdcMonitor [COLOR=#0000FF]AS DWORD[/COLOR], _
        [COLOR=#0000FF]BYREF[/COLOR] rectMonitor [COLOR=#0000FF]AS[/COLOR] [COLOR=#009CFF][B]RECT[/B][/COLOR], [COLOR=#0000FF]BYVAL[/COLOR] ppMonitorInfo [COLOR=#0000FF]AS DWORD POINTER[/COLOR]) [COLOR=#0000FF]AS LONG[/COLOR]
        [COLOR=#0000FF]STATIC[/COLOR] pMonitorInfo [COLOR=#0000FF]AS[/COLOR] [COLOR=#009CFF][B]MONITORINFO[/B][/COLOR] [COLOR=#0000FF]POINTER[/COLOR]
        [COLOR=#0000FF]STATIC[/COLOR] sMonitorInfo [COLOR=#0000FF]AS STRING[/COLOR]
        [COLOR=#0000FF]STATIC[/COLOR] MonitorIndex [COLOR=#0000FF]AS LONG[/COLOR]
        
         pMonitorInfo = @ppMonitorInfo
        [COLOR=#0000FF]IF[/COLOR] pMonitorInfo = [COLOR=#FF0000]0[/COLOR] [COLOR=#0000FF]THEN[/COLOR] [COLOR=#008080]'init[/COLOR]
           sMonitorInfo = [COLOR=#800040]""[/COLOR]
           MonitorIndex = [COLOR=#FF0000]0[/COLOR]
        [COLOR=#0000FF]END IF[/COLOR]
         sMonitorInfo   = sMonitorInfo & [COLOR=#0000FF]NUL$[/COLOR]([COLOR=#0000FF]SIZEOF[/COLOR]([COLOR=#009CFF][B]MONITORINFO[/B][/COLOR])) [COLOR=#008080]'Build MONITORINFO array[/COLOR]
         pMonitorInfo   = [COLOR=#0000FF]STRPTR[/COLOR](sMonitorInfo)
         @ppMonitorInfo = pMonitorInfo [COLOR=#008080]'For the function caller[/COLOR]
         @pMonitorInfo[MonitorIndex].cbSize = [COLOR=#0000FF]SIZEOF[/COLOR]([COLOR=#009CFF][B]MONITORINFO[/B][/COLOR])
        [COLOR=#009CFF][B]GetMonitorInfo[/B][/COLOR](hMonitor, @pMonitorInfo[MonitorIndex])
        [COLOR=#0000FF]INCR[/COLOR] MonitorIndex
         @pMonitorInfo[[COLOR=#FF0000]0[/COLOR]].cbSize = MonitorIndex [COLOR=#008080]'DWORD = Recycle .cbSize to Monitor count[/COLOR]
        [COLOR=#0000FF]FUNCTION[/COLOR] = [COLOR=#8000FF]%TRUE[/COLOR] [COLOR=#008080]'Continue enumeration[/COLOR]
        [COLOR=#0000FF]END FUNCTION[/COLOR]
        [COLOR=#008080]'_____________________________________________________________________________[/COLOR]
        
        [COLOR=#0000FF]CALLBACK FUNCTION[/COLOR] mycallback () [COLOR=#0000FF]AS LONG[/COLOR]
        
        [COLOR=#0000FF]SELECT CASE CB.MSG[/COLOR]
        [COLOR=#0000FF]CASE[/COLOR] [COLOR=#8000FF]%WM_NOTIFY[/COLOR]
        [COLOR=#0000FF]CASE[/COLOR] [COLOR=#8000FF]%WM_INITDIALOG[/COLOR]
        [COLOR=#0000FF]CASE[/COLOR] [COLOR=#8000FF]%WM_SIZE[/COLOR]
        [COLOR=#0000FF]CASE[/COLOR] [COLOR=#8000FF]%WM_DESTROY[/COLOR]
        [COLOR=#0000FF]CASE[/COLOR] [COLOR=#8000FF]%WM_DISPLAYCHANGE[/COLOR]
        [COLOR=#0000FF]LOCAL[/COLOR] pMonitorInfo [COLOR=#0000FF]AS[/COLOR] [COLOR=#009CFF][B]MONITORINFO[/B][/COLOR] [COLOR=#0000FF]POINTER[/COLOR]
        [COLOR=#0000FF]LOCAL[/COLOR] sLog         [COLOR=#0000FF]AS WSTRING[/COLOR]
        [COLOR=#0000FF]LOCAL[/COLOR] index        [COLOR=#0000FF]AS LONG[/COLOR]
        [COLOR=#0000FF]LOCAL[/COLOR] MonitorCount [COLOR=#0000FF]AS LONG[/COLOR]
        
                  pMonitorInfo = [COLOR=#FF0000]0[/COLOR] [COLOR=#008080]'Reset static variables in MonitorEnum function[/COLOR]
        [COLOR=#009CFF][B]EnumDisplayMonitors[/B][/COLOR]([COLOR=#0000FF]BYVAL[/COLOR] [COLOR=#8000FF]%NULL[/COLOR], [COLOR=#0000FF]BYVAL[/COLOR] [COLOR=#8000FF]%NULL[/COLOR], [COLOR=#0000FF]BYVAL CODEPTR[/COLOR](MonitorEnum), [COLOR=#0000FF]BYVAL VARPTR[/COLOR](pMonitorInfo))
        [COLOR=#0000FF]IF[/COLOR] pMonitorInfo [COLOR=#0000FF]THEN[/COLOR]
                    MonitorCount = @pMonitorInfo[[COLOR=#FF0000]0[/COLOR]].cbSize [COLOR=#008080]'cbSize of first item is used to hold monitor count[/COLOR]
                    sLog = [COLOR=#800040]"Monitor count"[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](MonitorCount) & [COLOR=#0000FF]$CRLF[/COLOR] [COLOR=#008080]'cbSize of first item is used to hold monitor count[/COLOR]
        [COLOR=#0000FF]FOR[/COLOR] index = [COLOR=#FF0000]0[/COLOR] [COLOR=#0000FF]TO[/COLOR] MonitorCount - [COLOR=#FF0000]1[/COLOR]
        [COLOR=#008080]'msgbox format$(SGN(@pMonitorInfo[index].rcMonitor.nLeft)+2)[/COLOR]
                      sLog &= [COLOR=#800040]"---------------------"[/COLOR] & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#800040]"Monitor"[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](index + [COLOR=#FF0000]1[/COLOR]) & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#0000FF]IIF$[/COLOR](@pMonitorInfo[index].dwFlags = [COLOR=#8000FF]%MONITORINFOF_PRIMARY[/COLOR], [COLOR=#800040]""[/COLOR], [COLOR=#800040]"Direction from Primary: "[/COLOR] )& _
        [COLOR=#0000FF]CHOOSE$[/COLOR]([COLOR=#0000FF]SGN[/COLOR](@pMonitorInfo[index].rcMonitor.nLeft)+[COLOR=#FF0000]2[/COLOR],[COLOR=#0000FF]CHR$$[/COLOR]([COLOR=#FF0000]&H2190[/COLOR]),[COLOR=#800040]""[/COLOR],[COLOR=#0000FF]CHR$$[/COLOR]([COLOR=#FF0000]&H2192[/COLOR]))  & _
        [COLOR=#0000FF]CHOOSE$[/COLOR]([COLOR=#0000FF]SGN[/COLOR](@pMonitorInfo[index].rcMonitor.nTop)+[COLOR=#FF0000]2[/COLOR],[COLOR=#0000FF]CHR$$[/COLOR]([COLOR=#FF0000]&H2191[/COLOR]),[COLOR=#800040]""[/COLOR],[COLOR=#0000FF]CHR$$[/COLOR]([COLOR=#FF0000]&H2193[/COLOR])) & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#800040]"Resolution("[/COLOR]& [COLOR=#0000FF]STR$[/COLOR](@pMonitorInfo[index].rcMonitor.nRight - @pMonitorInfo[index].rcMonitor.nLeft) & _
        [COLOR=#800040]","[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](@pMonitorInfo[index].rcMonitor.nBottom - @pMonitorInfo[index].rcMonitor.nTop) &[COLOR=#800040]")"[/COLOR]  & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#800040]"Resolution X L"[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](@pMonitorInfo[index].rcMonitor.nLeft)  & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#800040]"Resolution X R"[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](@pMonitorInfo[index].rcMonitor.nRight)  & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#800040]"Resolution Y T"[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](@pMonitorInfo[index].rcMonitor.nTop) & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#800040]"Resolution Y B"[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](@pMonitorInfo[index].rcMonitor.nBottom) & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#800040]"WorkArea   X L"[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](@pMonitorInfo[index].rcWork.nLeft)     & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#800040]"WorkArea   X R"[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](@pMonitorInfo[index].rcWork.nRight)     & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#800040]"WorkArea   Y T"[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](@pMonitorInfo[index].rcWork.nTop)    & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#800040]"WorkArea   Y B"[/COLOR] & [COLOR=#0000FF]STR$[/COLOR](@pMonitorInfo[index].rcWork.nBottom)    & [COLOR=#0000FF]$CRLF[/COLOR] & _
        [COLOR=#0000FF]IIF$[/COLOR](@pMonitorInfo[index].dwFlags = [COLOR=#8000FF]%MONITORINFOF_PRIMARY[/COLOR], [COLOR=#800040]"Primary monitor"[/COLOR], [COLOR=#800040]"Secondary monitor"[/COLOR]) & [COLOR=#0000FF]$CRLF[/COLOR]
        
        [COLOR=#0000FF]NEXT[/COLOR]
                    sLog &= [COLOR=#800040]"---------------------"[/COLOR] & [COLOR=#0000FF]$CRLF[/COLOR]
        [COLOR=#0000FF]ELSE[/COLOR]
                    sLog = [COLOR=#800040]"No monitor system"[/COLOR]
        [COLOR=#0000FF]END IF[/COLOR]
        [COLOR=#0000FF]CONTROL SET TEXT CB.HNDL[/COLOR], [COLOR=#FF0000]1001[/COLOR], sLog
        [COLOR=#0000FF]END SELECT[/COLOR]
        [COLOR=#0000FF]END FUNCTION[/COLOR]
        
        [COLOR=#0000FF]FUNCTION PBMAIN[/COLOR]()
        [COLOR=#0000FF]LOCAL[/COLOR] hDlg, Fhdl  [COLOR=#0000FF]AS LONG[/COLOR]
        
        [COLOR=#0000FF]FONT NEW[/COLOR] [COLOR=#800040]"Arial"[/COLOR], [COLOR=#FF0000]10[/COLOR],[COLOR=#FF0000]1[/COLOR] [COLOR=#0000FF]TO[/COLOR] Fhdl
        [COLOR=#0000FF]DIALOG NEW[/COLOR]  [COLOR=#FF0000]0[/COLOR], [COLOR=#800040]"Monitor Change detect and enum"[/COLOR], [COLOR=#FF0000]20[/COLOR], [COLOR=#FF0000]20[/COLOR], [COLOR=#FF0000]800[/COLOR], [COLOR=#FF0000]500[/COLOR],[COLOR=#8000FF]%WS_SYSMENU[/COLOR], [COLOR=#0000FF]TO[/COLOR] hDlg
        [COLOR=#0000FF]CONTROL ADD LABEL[/COLOR], hDlg, [COLOR=#FF0000]1001[/COLOR], [COLOR=#800040]""[/COLOR], [COLOR=#FF0000]10[/COLOR], [COLOR=#FF0000]10[/COLOR], [COLOR=#FF0000]790[/COLOR], [COLOR=#FF0000]490[/COLOR]
        [COLOR=#0000FF]CONTROL SET FONT[/COLOR] hDlg, [COLOR=#FF0000]1001[/COLOR], Fhdl
        [COLOR=#0000FF]DIALOG POST[/COLOR] hDlg, [COLOR=#8000FF]%WM_DISPLAYCHANGE[/COLOR], [COLOR=#FF0000]0[/COLOR], [COLOR=#FF0000]0[/COLOR]              [COLOR=#008080]'enum monitors at dialog start[/COLOR]
        
        [COLOR=#0000FF]DIALOG SHOW MODAL[/COLOR] hDlg , [COLOR=#0000FF]CALL[/COLOR] mycallback
        
        [COLOR=#0000FF]END FUNCTION[/COLOR]
        [COLOR=#008080]'_____________________________________________________________________________
        '[/COLOR]
        
        
        '
        Last edited by Rod Macia; 19 Jul 2020, 02:07 AM. Reason: Added more Monitor info and direction from primary

        Comment


        • #24
          formating...


          Code:
          ' Using EnumDisplayMonitors
          ' Adapted from Pierre Bellisle's code.
          ' https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/763084-how-to-detect-screen-resolution?p=763089#post763089
          
          
          #COMPILE EXE
          #DIM ALL
          #INCLUDE"Win32Api.inc"
          
          %MONITORINFOF_PRIMARY = &H00000001???
          
          
          FUNCTION MonitorEnum(BYVAL hMonitor AS DWORD, BYVAL hdcMonitor AS DWORD, BYREF rectMonitor AS RECT, BYVAL ppMonitorInfo AS DWORD POINTER) AS LONG
          STATIC pMonitorInfo AS MONITORINFO POINTER
          STATIC sMonitorInfo AS STRING
          STATIC MonitorIndex AS LONG
          
              pMonitorInfo = @ppMonitorInfo
              IF pMonitorInfo = 0 THEN
                  sMonitorInfo = ""
                  MonitorIndex = 0
              END IF
              sMonitorInfo   = sMonitorInfo & NUL$(SIZEOF(MONITORINFO)) 'Build MONITORINFO array
              pMonitorInfo   = STRPTR(sMonitorInfo)
              @ppMonitorInfo = pMonitorInfo 'For the function caller
              @pMonitorInfo[MonitorIndex].cbSize = SIZEOF(MONITORINFO)
              GetMonitorInfo(hMonitor, @pMonitorInfo[MonitorIndex])
              INCR MonitorIndex
              @pMonitorInfo[0].cbSize = MonitorIndex    'DWORD = Recycle .cbSize to Monitor count
          
              FUNCTION = %TRUE                          'Continue enumeration
          END FUNCTION
          
          
          CALLBACK FUNCTION mycallback() AS LONG
          LOCAL pMonitorInfo AS MONITORINFO POINTER
          LOCAL sLog         AS WSTRING
          LOCAL index        AS LONG
          LOCAL MonitorCount AS LONG
          
              SELECT CASE CB.MSG
              CASE %WM_DISPLAYCHANGE
          
                  pMonitorInfo = 0
                  EnumDisplayMonitors BYVAL %NULL, BYVAL %NULL, BYVAL CODEPTR(MonitorEnum), BYVAL VARPTR(pMonitorInfo)
                  IF pMonitorInfo THEN
                      MonitorCount = @pMonitorInfo[0].cbSize   'cbSize of first item is used to hold monitor count
                      sLog = "Monitor count" & STR$(MonitorCount) & $CRLF   'cbSize of first item is used to hold monitor count
                      FOR index = 0 TO MonitorCount - 1
                          'msgbox format$(SGN(@pMonitorInfo[index].rcMonitor.nLeft)+2)
                          sLog &= "---------------------" & $CRLF & _
                              "Monitor" + STR$(index + 1) & $CRLF & _
                              IIF$(@pMonitorInfo[index].dwFlags = %MONITORINFOF_PRIMARY, "", "Direction from Primary: ") & _
                              CHOOSE$(SGN(@pMonitorInfo[index].rcMonitor.nLeft)+2,CHR$$(&H2190),"",CHR$$(&H2192)) & _
                              CHOOSE$(SGN(@pMonitorInfo[index].rcMonitor.nTop)+2,CHR$$(&H2191),"",CHR$$(&H2193)) & $CRLF & _
                              "Resolution(" & STR$(@pMonitorInfo[index].rcMonitor.nRight - @pMonitorInfo[index].rcMonitor.nLeft) & _
                              "," & STR$(@pMonitorInfo[index].rcMonitor.nBottom - @pMonitorInfo[index].rcMonitor.nTop) & ")" & $CRLF & _
                              "Resolution X L" & STR$(@pMonitorInfo[index].rcMonitor.nLeft) & $CRLF & _
                              "Resolution X R" & STR$(@pMonitorInfo[index].rcMonitor.nRight) & $CRLF & _
                              "Resolution Y T" & STR$(@pMonitorInfo[index].rcMonitor.nTop) & $CRLF & _
                              "Resolution Y B" & STR$(@pMonitorInfo[index].rcMonitor.nBottom) & $CRLF & _
                              "WorkArea   X L" & STR$(@pMonitorInfo[index].rcWork.nLeft) & $CRLF & _
                              "WorkArea   X R" & STR$(@pMonitorInfo[index].rcWork.nRight) & $CRLF & _
                              "WorkArea   Y T" & STR$(@pMonitorInfo[index].rcWork.nTop)  & $CRLF & _
                              "WorkArea   Y B" & STR$(@pMonitorInfo[index].rcWork.nBottom) & $CRLF & _
                              IIF$(@pMonitorInfo[index].dwFlags = %MONITORINFOF_PRIMARY, "Primary monitor", "Secondary monitor") & $CRLF
          
                      NEXT
                      sLog &= "---------------------" & $CRLF
                  ELSE
                      sLog = "No monitor system"
                  END IF
                  CONTROL SET TEXT CB.HNDL, 1001, sLog
              END SELECT
          END FUNCTION
          
          
          FUNCTION PBMAIN()
          LOCAL hDlg, Fhdl AS LONG
          
              FONT NEW "Arial", 10 ,1 TO Fhdl
              DIALOG NEW 0, "Monitor Change detect and enum", 20, 20, 800, 500, %WS_SYSMENU, TO hDlg
              CONTROL ADD LABEL, hDlg, 1001, "", 10, 10, 790, 490
              CONTROL SET FONT hDlg, 1001, Fhdl
          
              DIALOG POST hDlg, %WM_DISPLAYCHANGE, 0, 0   'enum monitors at dialog start
          
              DIALOG SHOW MODAL hDlg, CALL mycallback
          
          END FUNCTION

          Comment


          • #25
            My dialog is displayed on a monitor. Wonderful.
            If the monitor is switched off (detect by %WM_DISPLAYCHANGE),
            the dialog should be moved to the primary monitor.

            Does somebody has any idea?

            Comment

            Working...
            X