Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Change windows Screen resolution

How Can i Change Windows Screen Resolution ?
is There Any API Function or Ohter Ways? (Visual Basic Or c#)

Comments

  • Scottg1989Scottg1989 Posts: 6Member
    [b][red]This message was edited by Scottg1989 at 2006-3-13 2:14:17[/red][/b][hr]
    : How Can i Change Windows Screen Resolution ?
    : is There Any API Function or Ohter Ways? (Visual Basic Or c#)
    :
    This is the same sort of thing that I am looking to do as well. Any help would be greatly appreiciated! Thanx.


  • sad.boysad.boy Posts: 90Member
    : [b][red]This message was edited by Scottg1989 at 2006-3-13 2:14:17[/red][/b][hr]
    : : How Can i Change Windows Screen Resolution ?
    : : is There Any API Function or Ohter Ways? (Visual Basic Or c#)
    : :
    : This is the same sort of thing that I am looking to do as well. Any help would be greatly appreiciated! Thanx.
    :
    :
    :
    i like to add a question to this ;)
    first please say how to understand what is the current resolution
    and now tell how to change it
  • dokken2dokken2 Posts: 532Member
    : : [b][red]This message was edited by Scottg1989 at 2006-3-13 2:14:17[/red][/b][hr]
    : : : How Can i Change Windows Screen Resolution ?
    : : : is There Any API Function or Ohter Ways? (Visual Basic Or c#)
    : : :
    : : This is the same sort of thing that I am looking to do as well. Any help would be greatly appreiciated! Thanx.
    : :
    : :
    : :
    : i like to add a question to this ;)
    : first please say how to understand what is the current resolution
    : and now tell how to change it
    :


    I found this in my files, should do what you need-

    : Hi all,
    :
    : I have a multi-user pc, and I want to let a program change the resolution of the desktop for each user (for example: my dad wants his desktop to be 1024x768 and I want it to be 1280x1024) So now my question is: can you change the resolution in windows xp with a vb program? (if so, can someone give me the code). If someone knows a program which can do this, it will be fine to me too.
    :
    : Greetings Bart
    :

    I have a module called ChangeScrnRes.bas:

    Option Explicit
    Private Const WM_DISPLAYCHANGE = &H7E
    Private Const HWND_BROADCAST = &HFFFF&
    Private Const EWX_LOGOFF = 0
    Private Const EWX_SHUTDOWN = 1
    Private Const EWX_REBOOT = 2
    Private Const EWX_FORCE = 4
    Private Const CCDEVICENAME = 32
    Private Const CCFORMNAME = 32
    Private Const DM_BITSPERPEL = &H40000
    Private Const DM_PELSWIDTH = &H80000
    Private Const DM_PELSHEIGHT = &H100000
    Private Const CDS_UPDATEREGISTRY = &H1
    Private Const CDS_TEST = &H4
    Private Const DISP_CHANGE_SUCCESSFUL = 0
    Private Const DISP_CHANGE_RESTART = 1
    Private Const BITSPIXEL = 12
    Private Type DEVMODE
    dmDeviceName As String * CCDEVICENAME
    dmSpecVersion As Integer
    dmDriverVersion As Integer
    dmSize As Integer
    dmDriverExtra As Integer
    dmFields As Long
    dmOrientation As Integer
    dmPaperSize As Integer
    dmPaperLength As Integer
    dmPaperWidth As Integer
    dmScale As Integer
    dmCopies As Integer
    dmDefaultSource As Integer
    dmPrintQuality As Integer
    dmColor As Integer
    dmDuplex As Integer
    dmYResolution As Integer
    dmTTOption As Integer
    dmCollate As Integer
    dmFormName As String * CCFORMNAME
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
    End Type
    Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
    Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
    Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Any) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Private nDC As Long
    Private Bits As Long

    Private OldX As Long, OldY As Long

    Public Sub ChangeScreenRes(X As Long, Y As Long)
    Dim DevM As DEVMODE, ScInfo As Long, erg As Long, an As VbMsgBoxResult

    On Error GoTo Abort

    'Get the info into DevM
    erg = EnumDisplaySettings(0&, 0&, DevM)
    'This is what we're going to change
    DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
    DevM.dmPelsWidth = X 'ScreenWidth
    DevM.dmPelsHeight = Y 'ScreenHeight
    DevM.dmBitsPerPel = Bits '(can be 8, 16, 24, 32 or even 4)
    'Now change the display and check if possible
    erg = ChangeDisplaySettings(DevM, CDS_TEST)

    Abort:
    End Sub

    Public Sub ScreenStartUp()
    On Error GoTo Abort
    OldX = Screen.Width / Screen.TwipsPerPixelX
    OldY = Screen.Height / Screen.TwipsPerPixelY

    nDC = CreateDC("DISPLAY", vbNullString, vbNullString, ByVal 0&)
    Bits = GetDeviceCaps(nDC, BITSPIXEL)

    Abort:
    End Sub

    Public Sub ScreenCleanUp()
    On Error GoTo Abort
    'restore the screen resolution
    ChangeScreenRes OldX, OldY
    'delete our device context
    DeleteDC nDC

    Abort:
    End Sub




    So what you do is call ScreenStartUp() once and then call ChangeScreenRes() to change it, then exit the program.

    This code determines the current user's log on name:

    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

    Dim strUserName As String
    dim LogOnName as string

    'find out username
    strUserName = String(100, Chr$(0))
    GetUserName strUserName, 100
    LogOnName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)




    So then all you have to do is

    if logonname = bart then
    changescreenres 1280,1024
    else
    changescreenres 1024,768
    end if



  • Scottg1989Scottg1989 Posts: 6Member
    Thanks for the code but would you be able to lay out more clearly which bits go in the module, and which bits go in the form code.
    {C0d3d .Gh05t}

  • dokken2dokken2 Posts: 532Member
    : Thanks for the code but would you be able to lay out more clearly which bits go in the module, and which bits go in the form code.
    : {C0d3d .Gh05t}
    :
    :
    to change the screen res, call ChangeScreenRes and pass the desired resolution -

    [code]
    ChangeScreenRes 320, 240
    ChangeScreenRes 640, 480
    ChangeScreenRes 800, 600
    [/code]


    put in a module-

    [code]
    Option Explicit

    'SCREEN RESOLUTION - PUT IN MODULE
    Private Const WM_DISPLAYCHANGE = &H7E
    Private Const HWND_BROADCAST = &HFFFF&
    Private Const EWX_LOGOFF = 0
    Private Const EWX_SHUTDOWN = 1
    Private Const EWX_REBOOT = 2
    Private Const EWX_FORCE = 4
    Private Const CCDEVICENAME = 32
    Private Const CCFORMNAME = 32
    Private Const DM_BITSPERPEL = &H40000
    Private Const DM_PELSWIDTH = &H80000
    Private Const DM_PELSHEIGHT = &H100000
    Private Const CDS_UPDATEREGISTRY = &H1
    Private Const CDS_TEST = &H4
    Private Const DISP_CHANGE_SUCCESSFUL = 0
    Private Const DISP_CHANGE_RESTART = 1
    Private Const BITSPIXEL = 12
    Private Type DEVMODE
    dmDeviceName As String * CCDEVICENAME
    dmSpecVersion As Integer
    dmDriverVersion As Integer
    dmSize As Integer
    dmDriverExtra As Integer
    dmFields As Long
    dmOrientation As Integer
    dmPaperSize As Integer
    dmPaperLength As Integer
    dmPaperWidth As Integer
    dmScale As Integer
    dmCopies As Integer
    dmDefaultSource As Integer
    dmPrintQuality As Integer
    dmColor As Integer
    dmDuplex As Integer
    dmYResolution As Integer
    dmTTOption As Integer
    dmCollate As Integer
    dmFormName As String * CCFORMNAME
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
    End Type
    Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
    Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
    Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Any) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Private nDC As Long
    Private Bits As Long

    Private OldX As Long, OldY As Long


    Public Sub ChangeScreenRes(X As Long, Y As Long)
    Dim DevM As DEVMODE, ScInfo As Long, erg As Long, an As VbMsgBoxResult

    On Error GoTo Abort

    'Get the info into DevM
    erg = EnumDisplaySettings(0&, 0&, DevM)
    'This is what we're going to change
    DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
    DevM.dmPelsWidth = X 'ScreenWidth
    DevM.dmPelsHeight = Y 'ScreenHeight
    DevM.dmBitsPerPel = Bits '(can be 8, 16, 24, 32 or even 4)
    'Now change the display and check if possible
    erg = ChangeDisplaySettings(DevM, CDS_TEST)

    Abort:
    End Sub


    Public Sub ScreenStartUp()
    On Error GoTo Abort
    OldX = Screen.Width / Screen.TwipsPerPixelX
    OldY = Screen.Height / Screen.TwipsPerPixelY

    nDC = CreateDC("DISPLAY", vbNullString, vbNullString, ByVal 0&)
    Bits = GetDeviceCaps(nDC, BITSPIXEL)

    Abort:
    End Sub


    Public Sub ScreenCleanUp()
    On Error GoTo Abort
    'restore the screen resolution
    ChangeScreenRes OldX, OldY
    'delete our device context
    DeleteDC nDC

    Abort:
    End Sub
    [/code]
Sign In or Register to comment.