Screen Resolution Changes

G

Guest

Is there any way my application can be notified if the user changes the
screen resolution while running the application (VB.Net 2003). Thanks.
 
K

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
 
H

Herfried K. Wagner [MVP]

Dennis said:
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'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top