Results 1 to 4 of 4

Thread: screen width

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2008
    Posts
    198

    screen width

    hi all
    i have 2 monitors pluged. i want my application to run on moniter 2.
    how can i do this or how can i know if 2 monitros plugd or 1 moniter

    it will be more bitter to know if i have (1 or 2) becuase if i have only 1 moniter i want the application to run on it.
    IT CTO & System Administrator.

  2. #2
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: screen width

    This is quite complex, but i'm not sure that there is any simple way. It is better to place all these things into a Module, you can call anytime.

    Quote Originally Posted by Edwin Vermeer
    Retrieving information about your dual monitor setup can be done with the code below.


    Code:
    Option Explicit
    
    Public Declare Function LoadLibraryEx Lib "kernel32.dll" Alias "LoadLibraryExA" (ByVal lpFileName As String, ByVal hFile As Long, ByVal dwFlags As Long) As Long
    Public Declare Function GetProcAddress Lib "kernel32.dll" (ByVal hModule As Long, ByVal lpProcName As String) As Long
    Public Declare Function GetModuleHandle Lib "kernel32.dll" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
    Public Declare Function FreeLibrary Lib "kernel32.dll" (ByVal hLibModule As Long) As Boolean
    Public Declare Function EnumDisplayMonitors Lib "user32.dll" (ByVal hdc As Long, ByRef lprcClip As Any, ByVal lpfnEnum As Long, ByVal dwData As Long) As Boolean
    Public Declare Function GetMonitorInfo Lib "user32.dll" Alias "GetMonitorInfoA" (ByVal hMonitor As Long, ByRef lpmi As MONITORINFOEX) As Boolean
    
    Public Const CCHDEVICENAME = 32
    Public Const MONITORINFOF_PRIMARY = &H1
    
    Public Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    
    Public Type MONITORINFOEX
        cbSize As Long
        rcMonitor As RECT
        rcWork As RECT
        dwFlags As Long
        szDevice As String * CCHDEVICENAME
    End Type
    
    Dim MonitorId() As String
    
    Public Sub Test()
    ' At the bottom I have pasted the results on my system.
    Dim i As Integer
        Debug.Print "Number of monitors in this system : " & GetMonitorId
        Debug.Print
        For i = 1 To UBound(MonitorId)
            PrintMonitorInfo (MonitorId(i))
        Next i
    End Sub
    
    Public Function GetMonitorId()
        ReDim MonitorId(0)
        ' Of course dual screen systems are not available on all Win versions.
        If FunctionExist("user32.dll", "EnumDisplayMonitors") = True Then
            If EnumDisplayMonitors(&H0, ByVal &H0, AddressOf MonitorEnumProc, &H0) = False Then
                Failed "EnumDisplayMonitors"
            End If
        End If
        GetMonitorId = UBound(MonitorId)
    End Function
    
    
    Private Sub PrintMonitorInfo(ForMonitorID As String)
    Dim MONITORINFOEX As MONITORINFOEX
        MONITORINFOEX.cbSize = Len(MONITORINFOEX)
        If GetMonitorInfo(CLng(ForMonitorID), MONITORINFOEX) = False Then Failed "GetMonitorInfo"
        With MONITORINFOEX
            Debug.Print "Monitor info for device number : " & ForMonitorID
            Debug.Print "---------------------------------------------------"
            Debug.Print "Device Name : " & .szDevice
            If .dwFlags And MONITORINFOF_PRIMARY Then Debug.Print "Primary Display = True" Else Debug.Print "Primary Display = False"
            With .rcMonitor
                Debug.Print "Monitor Left : " & .Left
                Debug.Print "Monitor Top : " & .Top
                Debug.Print "Monitor Right : " & .Right
                Debug.Print "Monitor Bottom : " & .Bottom
            End With
            With .rcWork
                Debug.Print "Work area Left : " & .Left
                Debug.Print "Work area Top : " & .Top
                Debug.Print "Work area Right : " & .Right
                Debug.Print "Work area Bottom : " & .Bottom
            End With
        End With
        Debug.Print
        Debug.Print
    End Sub
    
    
    Public Function FunctionExist(ByVal strModule As String, ByVal strFunction As String) As Boolean
    Dim hHandle As Long
        hHandle = GetModuleHandle(strModule)
        If hHandle = &H0 Then
            Failed "GetModuleHandle"
            hHandle = LoadLibraryEx(strModule, &H0, &H0): If hHandle = &H0 Then Failed "LoadLibrary"
            If GetProcAddress(hHandle, strFunction) = &H0 Then
                Failed "GetProcAddress"
            Else
                FunctionExist = True
            End If
            If FreeLibrary(hHandle) = False Then Failed "FreeLibrary"
        Else
            If GetProcAddress(hHandle, strFunction) = &H0 Then
                Failed "GetProcAddress"
            Else
                FunctionExist = True
            End If
        End If
    End Function
    
    
    Public Sub Failed(ByVal strFunction As String)
        If errMsg = True Then
            If Err.LastDllError = 0 Then
                MessageBoxEx &H0, strFunction & Chr$(13) & Chr$(10) & Chr$(13) & Chr$(10) & "Failed", "Error", MB_OK Or MB_ICONWARNING Or MB_SETFOREGROUND, 0
            Else
                Errors Err.LastDllError, strFunction
            End If
        End If
    End Sub
    
    
    Public Function MonitorEnumProc(ByVal hMonitor As Long, ByVal hdcMonitor As Long, ByRef lprcMonitor As RECT, ByVal dwData As Long) As Boolean
    Dim ub As Integer
        ub = 0
        On Error Resume Next
        ub = UBound(MonitorId)
        On Error GoTo 0
        ReDim Preserve MonitorId(ub + 1)
        MonitorId(UBound(MonitorId)) = CStr(hMonitor)
        MonitorEnumProc = 1
    End Function

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: screen width

    thanks for the code jim, i can use that
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: screen width

    I tried to use Windows Scripting Host to provide me information about the monitors, but it seems it doesn't work as expected. For me it simply didn't give information about all the displays that I had active (external "Generic Television" + laptop "Standard Display"), instead I always only got information on the display that was active when I first started ("Generic Television").

    So here is the code for those who want to check it out, it is short'n'sweet:
    Code:
    Option Explicit
    
    ' http://msdn.microsoft.com/en-us/library/aa394122(VS.85).aspx
    Public Function Monitors() As Object
        ' get scripting object and make a call to scripting host to get the monitors
        Set Monitors = GetObject("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_DesktopMonitor")
    End Function
    
    Private Sub Command1_Click()
        Dim objMonitor As Object
        ' this loop makes a single call to Monitors function to receive information about the monitors
        For Each objMonitor In Monitors
            ' display information in a message box
            MsgBox "Resolution: " & objMonitor.ScreenWidth & " x " & objMonitor.ScreenHeight & vbNewLine & objMonitor.Availability, vbInformation, objMonitor.Description
        Next objMonitor
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width