Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

IceAngel

Mudar resolução do monitor durante execução

Recommended Posts

Olá. Preciso mudar a resolução do monitor do usuário durante a execução do meu programa. Gostaria de saber se existe uma classe no .NET Framework (de preferência) ou função na API do Windows para isto. Obrigado.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Guarde esse código, esse foi dificil de achar, achei partes na web e fucei, deu certo aqui. Vou postar como ficou o código VB.

Imports System.Runtime.InteropServices

Public Class Form2

	'API declaration set or get display adpter information
	Private Declare Function EnumDisplayDevices Lib "user32" Alias "EnumDisplayDevicesA" (ByVal Unused As Integer, ByVal iDevNum As Short, ByRef lpDisplayDevice As DISPLAY_DEVICE, ByVal dwFlags As Integer) As Integer
	Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As String, ByVal iModeNum As Integer, ByRef lpDevMode As SetResolution) As Integer
	Private Declare Function ChangeDisplaySettingsEx Lib "user32" Alias "ChangeDisplaySettingsExA" (ByVal lpszDeviceName As String, ByRef lpDevMode As SetResolution, ByVal hWnd As Integer, ByVal dwFlags As Integer, ByVal lParam As Integer) As Integer

	Const CCDEVICENAME As Short = 32
	Const CCFORMNAME As Short = 32
	Const DM_BITSPERPEL = &H40000
	Const DM_PELSWIDTH = &H80000
	Const DM_PELSHEIGHT = &H100000
	Const CDS_UPDATEREGISTRY = &H1

	Const DISPLAY_DEVICE_ATTACHED_TO_DESKTOP = &H1 'Device that is part of desktop
	Const DISPLAY_PRIMARY_DEVICE = &H4 'Primary device

	'Holds the information of display adpter
	Public Structure DISPLAY_DEVICE
		Public cb As Integer
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> _
		Public DeviceName As String
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
		Public DeviceString As String
		Public StateFlags As Short
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
		Public DeviceID As String
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
		Public DeviceKey As String
	End Structure

	'Holds the setting of display adapter
	Public Structure SetResolution
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> _
		Public dmDeviceName As String
		Public dmSpecVersion As Short
		Public dmDriverVersion As Short
		Public dmSize As Short
		Public dmDriverExtra As Short
		Public dmFields As Integer
		Public dmOrientation As Short
		Public dmPaperSize As Short
		Public dmPaperLength As Short
		Public dmPaperWidth As Short
		Public dmScale As Short
		Public dmCopies As Short
		Public dmDefaultSource As Short
		Public dmPrintQuality As Short
		Public dmColor As Short
		Public dmDuplex As Short
		Public dmYResolution As Short
		Public dmTTOption As Short
		Public dmCollate As Short

		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> _
		Public dmFormName As String
		Public dmLogPixels As Short
		Public dmBitsPerPel As Short
		Public dmPelsWidth As Integer
		Public dmPelsHeight As Integer
		Public dmDisplayFlags As Integer
		Public dmDisplayFrequency As Integer
		Public dmICMMethod As Integer
		Public dmICMIntent As Integer
		Public dmMediaType As Integer
		Public dmDitherType As Integer
		Public dmReserved1 As Integer
		Public dmReserved2 As Integer
		Public dmPanningWidth As Integer
		Public dmPanningHeight As Integer
	End Structure

	Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	End Sub

	Private Sub Mudar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mudar.Click
		Dim scrResolution As SetResolution
		Dim DisplayDevice As DISPLAY_DEVICE
		Dim DD() As DISPLAY_DEVICE 'Holds the information of display devices
		Dim RS() As SetResolution ''Holds the settings of display devices
		Dim lResult As Integer = 1
		Dim nMonitor As Short
		Dim nSetting As Short
		'
		' Retrieve info about the current graphics mode
		' on the current display device.
		'
		'cmbResolution.Items.Clear()
		'Get all available desktop monitor
		Do While lResult <> 0
			DisplayDevice.cb = Marshal.SizeOf(DisplayDevice)
			scrResolution.dmSize = Marshal.SizeOf(scrResolution)
			lResult = EnumDisplayDevices(0, nMonitor, DisplayDevice, 0)
			If DisplayDevice.StateFlags = DISPLAY_DEVICE_ATTACHED_TO_DESKTOP Or _
			DisplayDevice.StateFlags = DISPLAY_PRIMARY_DEVICE Or _
			DisplayDevice.StateFlags = (DISPLAY_DEVICE_ATTACHED_TO_DESKTOP Or DISPLAY_PRIMARY_DEVICE) Then 'is it part of the desktop device or is it primary adapter or both
				ReDim Preserve DD(nMonitor)
				DD(nMonitor) = DisplayDevice
			End If
			nMonitor += 1
		Loop

		'set the resolution to all desktop adapter
		For nMonitor = 0 To UBound(DD)
			ReDim Preserve RS(nMonitor)
			RS(nMonitor).dmSize = Marshal.SizeOf(RS(nMonitor))
			With RS(nMonitor)
				.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT 'Or DM_BITSPERPEL
				'Here's the resolution
				.dmPelsWidth = 800
				.dmPelsHeight = 600
			End With
			'change the resolution of Display Adpter
			lResult = ChangeDisplaySettingsEx(DD(nMonitor).DeviceName, RS(nMonitor), 0, CDS_UPDATEREGISTRY, 0)
			If lResult = -1 Then
				MsgBox("Mode not supported", vbSystemModal, "Error")
			End If
		Next
	End Sub
End Class

Abraços...

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.