Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Interface and Graphics > API to change resolution


Reply
 
Thread Tools Display Modes
  #1  
Old 09-26-2004, 05:27 AM
stickmangumby stickmangumby is offline
Regular
 
Join Date: Mar 2004
Posts: 83
Default API to change resolution

I've had minimal contact with API in VB6, and from what I have discerned API seems quite different in VB.net

I'm looking for some API to change the colour depth of the monitor so that i can use a transparency key to make rounded forms, which doesn't seem to work in 32 bit colour.

ThankS
Reply With Quote
  #2  
Old 10-03-2004, 05:06 AM
stickmangumby stickmangumby is offline
Regular
 
Join Date: Mar 2004
Posts: 83
Default

^^bump^^

ive tried using the VB6 API for changing resolution and colour depth, but when .net converted it it went a lot spastic, and i couldnt sort it out nomatter what i did.

im getting desperate!
Reply With Quote
  #3  
Old 10-03-2004, 06:15 AM
Dennis DVR's Avatar
Dennis DVR Dennis DVR is offline
Back in the Game

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Manila Philippines
Posts: 3,533
Default

here's the test code that I made, the original source code was in C# I just converted it to VB.NET it is very similar to the legacy VB6 code.

Code:
Imports System.Runtime.InteropServices 'add this to your VB code 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 CDS_TEST = &H2 Const CDS_FULLSCREEN = &H4 Const DISP_CHANGE_SUCCESSFUL = 0 Const DISP_CHANGE_RESTART = 1 Const HWND_BROADCAST = &HFFFF& Const WM_DISPLAYCHANGE = &H7E& Const SPI_SETNONCLIENTMETRICS = 42 Private Structure SetResolution <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> _ Public dmDeviceName As String 'You can also use the Dim i guess 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 'equivalent of dmFormName * CCFORMNAME <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 Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As String, ByVal iModeNum As Integer, ByRef lpDevMode As SetResolution) As Integer Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef lpDevMode As SetResolution, ByVal dwFlags As Long) As Integer 'you need a command button Private Sub ChangeRes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeRes.Click Dim scrResolution As SetResolution Dim lResult As Integer Dim iAns As Integer ' ' Retrieve info about the current graphics mode ' on the current display device. ' scrResolution.dmDeviceName = "" scrResolution.dmFormName = "" scrResolution.dmSize = Marshal.SizeOf(scrResolution) lResult = EnumDisplaySettings("", -1, scrResolution) ' ' Set the new resolution. Don't change the color ' depth so a restart is not necessary. ' With scrResolution 'this is not necessary in C# I think .dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT 'Or DM_BITSPERPEL .dmPelsWidth = 1024 'here the resolution .dmPelsHeight = 768 '.dmBitsPerPel = 32 (could be 8, 16, 32 or even 4) End With ' ' Change the display settings to the specified graphics mode. ' lResult = ChangeDisplaySettings(scrResolution, CDS_TEST) If lResult = -1 Then MsgBox("Mode not supported", vbSystemModal, "Error") Else lResult = ChangeDisplaySettings(scrResolution, CDS_UPDATEREGISTRY) Select Case lResult Case DISP_CHANGE_RESTART Msgbox("You need to restart your computer", vbInformation, "Resolution Changed") Case DISP_CHANGE_SUCCESSFUL MsgBox("Screen resolution changed", vbInformation, "Resolution Changed") Case Else MsgBox("Mode not supported", vbSystemModal, "Error") End Select End If End Sub

try to experiment on the source code.
I haven't explain the code much just be careful about the declaration type of the variable.
__________________
Avatar by Lebb

[Posting Guidelines] [Standards & Practices Tutorial] [Participate here effectively]
Our knowledge can only be finite, while our ignorance must necessarily be infinite. Karl Popper

Last edited by duane; 10-03-2004 at 08:44 AM. Reason: removed the end class statement
Reply With Quote
  #4  
Old 11-13-2004, 12:22 PM
alink alink is offline
Newcomer
 
Join Date: Nov 2004
Posts: 4
Default Using VB .NET to change the resolution on a secondary monitor.

Thanks for your solution Duane. I was wondering if it's possible to change the resolution on a secondary monitor. When having dual monitors on "dual view" the program only changes the resolution on monitor 1 and not 2.
Reply With Quote
  #5  
Old 11-14-2004, 04:47 AM
Dennis DVR's Avatar
Dennis DVR Dennis DVR is offline
Back in the Game

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Manila Philippines
Posts: 3,533
Default

Hi, alink

I don't use dual monitor so I'm not sure if it will work for you but i'm positive that it can be done using EnumDisplayDevices and ChangeDisplaySetiingsEx API function.

This is what i've come-up.
Code:
Imports System.Runtime.InteropServices 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 CDS_TEST = &H2 'Const CDS_FULLSCREEN = &H4 'Const DISP_CHANGE_SUCCESSFUL = 0 'Const DISP_CHANGE_RESTART = 1 'Const HWND_BROADCAST = &HFFFF& 'Const WM_DISPLAYCHANGE = &H7E& 'Const SPI_SETNONCLIENTMETRICS = 42 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 Private 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 Private 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 '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 'Command Button Click event Private Sub ChangeRes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeRes.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 'This part can be used if you want to retrieve all the available resolution for the desktop adapter (make sure the resolution supported by monitor) ' Do While EnumDisplaySettings(DisplayDevice.DeviceName, nSetting, scrResolution) <> 0 ' cmbResolution.Items.Add(DisplayDevice.DeviceString & " " & scrResolution.dmPelsWidth & " x " & scrResolution.dmPelsHeight & " " & scrResolution.dmBitsPerPel & " Bits") ' nSetting += 1 ' Loop ' cmbResolution.Sorted = True 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 = 1024 .dmPelsHeight = 768 '.dmBitsPerPel = 32 (could be 8, 16, 32 or even 4) 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

You should experiment on the code, as I have said I never tried the code on a dual monitor, but I'm positive that the above API can do the trick.

You can also set different resolution for each display adapter.

Note: I use ChangeDisplaySettingsEx to change the resolution of other display adapter which is part of the desktop. ChangeDisplySetting change the default adpter only.
__________________
Avatar by Lebb

[Posting Guidelines] [Standards & Practices Tutorial] [Participate here effectively]
Our knowledge can only be finite, while our ignorance must necessarily be infinite. Karl Popper

Last edited by duane; 11-14-2004 at 07:01 AM.
Reply With Quote
  #6  
Old 11-17-2004, 06:22 PM
Dennis DVR's Avatar
Dennis DVR Dennis DVR is offline
Back in the Game

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Manila Philippines
Posts: 3,533
Default

PM
Quote:
Originally Posted by alink
Hey Duane, first off thanks again for your solution. It worked great in VB.net. I was trying to port it to vb6 and ran into a few problems. I also posted a reply on the appropriate thread but I dont see it today... Any ideas why it was deleted? I copy and pasted what I had done so far and explained I had trouble interpreting the marshalas command since im unsure of how it works.

I really like how in vb.net I could use a class and then just have all my forms call that, but I couldnt do something similar in vb6, so was trying to do it all in one form, but ran into probs as above.

Regards, and I'll be sure to pass on your helpfulness in the future
Quote:
Originally Posted by alink
Hey Duane,

I originally didn't find my post and was wondering if it was deleted. I checked a few hours later and decided to search using all posts by me and found it. I'll try it out and let you know what happens. Thanks again for being so quick to reply and so helpful.
Hi alink

Your last post isn't VB.NET related so I asked the moderator to move it to its proper thread

Just for the record, here's the link
http://www.xtremevbtalk.com/showthread.php?t=198335
__________________
Avatar by Lebb

[Posting Guidelines] [Standards & Practices Tutorial] [Participate here effectively]
Our knowledge can only be finite, while our ignorance must necessarily be infinite. Karl Popper
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:

Powered by liquidweb