Results 1 to 5 of 5

Thread: Resolution Code

  1. #1

    Thread Starter
    Addicted Member Peter1's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Resolution Code

    Hiya,

    I found a C# module to change the screen resolution. However, i havnt a donkeys with C#. Don't suppose anyone can convert it into VB.NET for me?

    Many Thanks

    Pete
    Attached Files Attached Files

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    VB Code:
    1. ' Original C# Version Copyright 2002 Omniscium, all rights reserved.
    2. ' Converted to VB.NET by Lunatic3
    3. ' Please send your comments :)
    4. Imports System
    5. Imports System.Windows.Forms
    6. Imports System.Drawing
    7. Imports System.Runtime.InteropServices
    8. #Region "DEVMODE_STRUCT"
    9. <StructLayout(LayoutKind.Sequential)> _
    10. Public Structure DEVMODE
    11.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
    12.     Public dmDeviceName As String
    13.     Public dmSpecVersion As Short
    14.     Public dmDriverVersion As Short
    15.     Public dmSize As Short
    16.     Public dmDriverExtra As Short
    17.     Public dmFields As Integer
    18.  
    19.     Public dmOrientation As Short
    20.     Public dmPaperSize As Short
    21.     Public dmPaperLength As Short
    22.     Public dmPaperWidth As Short
    23.  
    24.     Public dmScale As Short
    25.     Public dmCopies As Short
    26.     Public dmDefaultSource As Short
    27.     Public dmPrintQuality As Short
    28.     Public dmColor As Short
    29.     Public dmDuplex As Short
    30.     Public dmYResolution As Short
    31.     Public dmTTOption As Short
    32.     Public dmCollate As Short
    33.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
    34.     Public dmFormName As String ',
    35.     Public dmLogPixels As Short
    36.     Public dmBitsPerPel As Short
    37.     Public dmPelsWidth As Integer
    38.     Public dmPelsHeight As Integer
    39.  
    40.     Public dmDisplayFlags As Integer
    41.     Public dmDisplayFrequency As Integer
    42.  
    43.     Public dmICMMethod As Integer
    44.     Public dmICMIntent As Integer
    45.     Public dmMediaType As Integer
    46.     Public dmDitherType As Integer
    47.     Public dmReserved1 As Integer
    48.     Public dmReserved2 As Integer
    49.  
    50.     Public dmPanningWidth As Integer
    51.     Public dmPanningHeight As Integer
    52. End Structure
    53.  
    54. #End Region
    55.  
    56. #Region "PINVOKEDEF"
    57. Class User32
    58.  
    59.     Public Declare Function EnumDisplaySettings Lib "user32.dll" Alias "EnumDisplaySettingsA" (ByVal deviceName As String, ByVal modeNum As Integer, ByRef devMode As DEVMODE) As Integer
    60.     Public Declare Function ChangeDisplaySettings Lib "user32.dll" Alias "ChangeDisplaySettingsA" (ByRef devMode As DEVMODE, ByVal flags As Integer) As Integer
    61.     Public Const ENUM_CURRENT_SETTINGS As Integer = -1
    62.     Public Const CDS_UPDATEREGISTRY As Integer = &H1
    63.     Public Const CDS_TEST As Integer = &H2
    64.     Public Const DISP_CHANGE_SUCCESSFUL As Integer = 0
    65.     Public Const DISP_CHANGE_RESTART As Integer = 1
    66.     Public Const DISP_CHANGE_FAILED As Integer = -1
    67. End Class
    68. #End Region
    69.  
    70.  
    71.  
    72. Namespace Resolution
    73.     Module Module1
    74.  
    75.        
    76.         Class CResolution
    77.             Shared Sub Main(ByVal args() As String)
    78.                 Dim screen As Screen = screen.PrimaryScreen
    79.                 Console.WriteLine("Screen Width = " & screen.Bounds.Width)
    80.                 Console.WriteLine("Screen Height = " & screen.Bounds.Height)
    81.                 If args.Length <> 2 Then
    82.                     Console.WriteLine("Please pass in the width followed by the height of the resolution you want.")
    83.                     Return
    84.                 End If
    85.                 Dim iWidth As Integer = Int32.Parse(args(0))
    86.                 Dim iHeight As Integer = Int32.Parse(args(1))
    87.                 Dim dm As DEVMODE = New DEVMODE()
    88.                 dm.dmDeviceName = New [String](New Char(32) {})
    89.                 dm.dmFormName = New [String](New Char(32) {})
    90.                 dm.dmSize = CType(Marshal.SizeOf(dm), Short)
    91.                 If 0 <> User32.EnumDisplaySettings(Nothing, User32.ENUM_CURRENT_SETTINGS, dm) Then
    92.                     dm.dmPelsWidth = iWidth
    93.                     dm.dmPelsHeight = iHeight
    94.                     Dim iRet As Integer = User32.ChangeDisplaySettings(dm, User32.CDS_TEST)
    95.                     If iRet = User32.DISP_CHANGE_FAILED Then
    96.                         Console.WriteLine("Change failed")
    97.                     Else
    98.                         iRet = User32.ChangeDisplaySettings(dm, User32.CDS_UPDATEREGISTRY)
    99.                         Select Case iRet
    100.                             Case User32.DISP_CHANGE_SUCCESSFUL
    101.                                 Console.WriteLine("Changed the resolution.")
    102.                             Case User32.DISP_CHANGE_RESTART
    103.                                 Console.WriteLine("You will need to reboot for the change to happen.")
    104.                             Case Else
    105.                                 Console.WriteLine("Failed to change the resolution.")
    106.                         End Select
    107.                     End If
    108.                 End If
    109.             End Sub
    110.         End Class
    111.     End Module
    112. End Namespace
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Addicted Member Peter1's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Thankyou very much, been trying to do this for ages.

    Pete

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    This raises an interesting question....

    Do you think MS will ever make a class to do this internal to the framework? OR are we stuck using the above unmanaged Win32API?

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    It seems to me that almost all of the truly platform-dependent stuff is intentionally left out of the Framework, although hopefully MS will keep going with stuff like Windows Management Interface. I cannot believe things like formatted printing were left out of the Framework "by accident". Here's a funny MS quote:

    ms-help://MS.VSCC/MS.MSDNVS/vbcn7/html/vaconCallingWindowsAPIs.htm
    "Windows API calls were an important part of Visual Basic programming in the past, but are seldom necessary with Visual Basic .NET. Whenever possible, you should use managed code from the .NET Framework to perform tasks instead of Windows API calls."

    ... unless you want to write basic utility software...

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