Visual Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 17974
Number of posts: 55343

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Change windows Screen resolution Posted by rezaab2002 on 26 Sept 2005 at 1:30 AM
How Can i Change Windows Screen Resolution ?
is There Any API Function or Ohter Ways? (Visual Basic Or c#)
Report
Re: Change windows Screen resolution Posted by Scottg1989 on 13 Mar 2006 at 2:13 AM
This message was edited by Scottg1989 at 2006-3-13 2:14:17

: 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.


Report
Re: Change windows Screen resolution Posted by sad.boy on 13 Mar 2006 at 7:26 AM
: This message was edited by Scottg1989 at 2006-3-13 2:14:17

: : 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
Report
Re: Change windows Screen resolution Posted by dokken2 on 13 Mar 2006 at 12:01 PM
: : This message was edited by Scottg1989 at 2006-3-13 2:14:17

: : : 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



Report
Re: Change windows Screen resolution Posted by Scottg1989 on 14 Mar 2006 at 3:51 AM
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}

Report
Re: Change windows Screen resolution Posted by dokken2 on 15 Mar 2006 at 9:18 AM
: 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 -

  ChangeScreenRes 320, 240
  ChangeScreenRes 640, 480
  ChangeScreenRes 800, 600



put in a module-

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




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.