Home All Groups Group Topic Archive Search About

Screen Resolution Changes

Author
7 Mar 2006 1:15 AM
Dennis
Is there any way my application can be notified if the user changes the
screen resolution while running the application (VB.Net 2003). Thanks.
--
Dennis in Houston

Author
7 Mar 2006 11:41 AM
Ken Tucker [MVP]
Dennis,

            Use a wmi __InstanceModificationEvent to be notified of the
screen resolution changes.  This sample uses the wmi
Win32_DisplayConfiguration class to be notified of the change.  Add a
reference to system.management.  You will need a listbox on the form to
display the display adapter name, height, and width.


Imports System.Management

Public Class Form1
    Dim WithEvents w As ManagementEventWatcher
    Dim q As WqlEventQuery
    Delegate Sub LoadList()

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        w.Stop()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Try
            q = New WqlEventQuery
            q.QueryString = "SELECT * FROM" & _
                        " __InstanceModificationEvent WITHIN 1 " & _
                        "WHERE TargetInstance isa
""Win32_DisplayConfiguration"""
            w = New ManagementEventWatcher(q)
            w.Start()
        Catch ex As Exception
            Trace.WriteLine(ex.ToString)
        End Try
        GetDeviceSettings()
    End Sub

    Private Sub GetDeviceSettings()
        ListBox1.Items.Clear()
        Dim moReturn As Management.ManagementObjectCollection
        Dim moSearch As Management.ManagementObjectSearcher
        Dim mo As Management.ManagementObject

        moSearch = New Management.ManagementObjectSearcher("Select * from
Win32_DisplayConfiguration")

        moReturn = moSearch.Get
        For Each mo In moReturn
            ListBox1.Items.Add(mo("Caption").ToString)
            ListBox1.Items.Add(String.Format("Height {0}",
mo("PelsHeight")))
            ListBox1.Items.Add(String.Format("Width {0}", mo("PelsWidth")))
        Next

    End Sub

    Private Sub w_EventArrived(ByVal sender As Object, ByVal e As
System.Management.EventArrivedEventArgs) Handles w.EventArrived
        For Each p As Process In Process.GetProcesses
            Trace.WriteLine(p.MainWindowTitle)
        Next
        ListBox1.Invoke(New LoadList(AddressOf GetDeviceSettings))
    End Sub
End Class


Ken
-----------------
Show quoteHide quote
"Dennis" <Den***@discussions.microsoft.com> wrote in message
news:8604F36D-BA6D-4DBF-90CC-F61A90D63816@microsoft.com...
> Is there any way my application can be notified if the user changes the
> screen resolution while running the application (VB.Net 2003). Thanks.
> --
> Dennis in Houston
Author
7 Mar 2006 2:01 PM
Herfried K. Wagner [MVP]
"Dennis" <Den***@discussions.microsoft.com> schrieb:
> Is there any way my application can be notified if the user changes the
> screen resolution while running the application (VB.Net 2003).

Untested:  'SystemEvents.DisplaySettingsChanged'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 Mar 2006 12:16 AM
Dennis
Thanks Herfried and Ken.  I'll give both approaches a try.
--
Dennis in Houston


Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "Dennis" <Den***@discussions.microsoft.com> schrieb:
> > Is there any way my application can be notified if the user changes the
> > screen resolution while running the application (VB.Net 2003).
>
> Untested:  'SystemEvents.DisplaySettingsChanged'.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>