|
-
Aug 31st, 2005, 10:37 PM
#1
Thread Starter
Fanatic Member
Detecting screen resolution
Hello...
Can someone tell me how to detect the screen resolution from vb.net?? 
Im also looking for it...found nothing so far,so if someone knows,please help me..
Thanks
Godwin
Help someone else with what someone helped you! 
-
Aug 31st, 2005, 10:51 PM
#2
Re: Detecting screen resolution
Screen.PrimaryScreen.Bounds is a Rectangle that accounts for the full resolution. Screen.PrimaryScreen.WorkingArea is a Rectangle that excludes the Taskbar and any other unavailable area. Obviously the code would need to be adjusted for any monitor other than the primary.
-
Aug 31st, 2005, 11:02 PM
#3
Thread Starter
Fanatic Member
Re: Detecting screen resolution
wowww,Thank you Jm,this works perrrfectttt...very rarely,people might have a secondary monitor or something so,this is just Greattt!!
Godwin
Help someone else with what someone helped you! 
-
Aug 31st, 2005, 11:06 PM
#4
Thread Starter
Fanatic Member
Re: Detecting screen resolution
is there anyway to adjust the screen resolution too? like..my program needs a minimum resolution of 1024x768 and if thats not available,it would be great if I could make my program adjust it too.maybe it needs an API to do that.
Godwin
Help someone else with what someone helped you! 
-
Aug 31st, 2005, 11:10 PM
#5
Re: Detecting screen resolution
I believe that was asked and maybe answered just a day or two ago, but I can't remember where. Try a forum search and limit it to the last couple of days. With reagrds to the multiple monitor thing, from personal experience I can highly recommend it if you can get access to a second screen.
-
Aug 31st, 2005, 11:40 PM
#6
Re: Detecting screen resolution
something to note about that method is, it does not include the space taken up by the taskbar, or any other bar like the old MS Office and works stuff.. so most times it will not be a case of 800x600 if you are planning on measuring that way, that's why it's called workingarea.. i think this is an upside rather than a downside, as you can bet that if you were to set your form size to the workingarea it will fit in where it will not interupt anything else used by windows
-
Aug 31st, 2005, 11:45 PM
#7
Re: Detecting screen resolution
You can use the ChangeDisplaySettings API to do this.
VB Code:
Const ENUM_CURRENT_SETTINGS As Integer = -1
Const CDS_UPDATEREGISTRY As Integer = &H1
Const CDS_TEST As Long = &H2
Const CCDEVICENAME As Integer = 32
Const CCFORMNAME As Integer = 32
Const DISP_CHANGE_SUCCESSFUL As Integer = 0
Const DISP_CHANGE_RESTART As Integer = 1
Const DISP_CHANGE_FAILED As Integer = -1
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Integer
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef DEVMODE As DEVMODE, ByVal flags As Long) As Integer
<StructLayout(LayoutKind.Sequential)> Public Structure DEVMODE
<MarshalAsAttribute(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
<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> Public dmFormName As String
Public dmUnusedPadding As Short
Public dmBitsPerPel As Short
Public dmPelsWidth As Integer
Public dmPelsHeight As Integer
Public dmDisplayFlags As Integer
Public dmDisplayFrequency As Integer
End Structure
Public Sub changeRes(ByVal theWidth As Integer, ByVal theHeight As Integer)
Dim DevM As DEVMODE
DevM.dmDeviceName = New [String](New Char(32) {})
DevM.dmFormName = New [String](New Char(32) {})
DevM.dmSize = CShort(Marshal.SizeOf(GetType(DEVMODE)))
If 0 <> EnumDisplaySettings(Nothing, ENUM_CURRENT_SETTINGS, DevM) Then
Dim lResult As Integer
DevM.dmPelsWidth = theWidth
DevM.dmPelsHeight = theHeight
DevM.dmPelsWidth = 1280
DevM.dmPelsHeight = 1024
lResult = ChangeDisplaySettings(DevM, CDS_TEST)
If lResult = DISP_CHANGE_FAILED Then
MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Change Failed")
Else
lResult = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
Select Case lResult
Case DISP_CHANGE_RESTART
MsgBox("You must restart your computer to apply these changes.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Has Changed")
Case DISP_CHANGE_SUCCESSFUL
MsgBox("Display Change Successful.", MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "Screen Resolution Successful")
Case Else
MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Change Failed")
End Select
End If
End If
End Sub
You can also look at ChangeDisplaySettingsEx but neither of these links have examples, you'll have to google as I did for code. Note that the person running the program would need permissions to do this, then only would the code work.
-
Sep 1st, 2005, 01:04 AM
#8
Thread Starter
Fanatic Member
Re: Detecting screen resolution
Godwin
Help someone else with what someone helped you! 
-
Sep 1st, 2005, 01:35 AM
#9
Re: Detecting screen resolution
hehe, actually, i didn't read Jm's post properly, he mentions primaryscreen as the full resolution and workingarea as the excluded taskbar one
sorry guys
-
Sep 1st, 2005, 07:09 PM
#10
Re: Detecting screen resolution
I also just discovered the (read-only) SystemInformation.PrimaryMonitorSize property.
-
Sep 1st, 2005, 09:39 PM
#11
Thread Starter
Fanatic Member
Re: Detecting screen resolution
yes Jm,Even I tried it and saw,thats why i was asking about changing the resolution
Godwin
Help someone else with what someone helped you! 
-
Nov 4th, 2005, 11:54 AM
#12
Frenzied Member
Re: Detecting screen resolution
 Originally Posted by jmcilhinney
I also just discovered the (read-only) SystemInformation.PrimaryMonitorSize property.
Very cool.
The SystemInformation.MonitorCount can also be used to help destermine if the user has dual (or more) monitors.
~Peter

-
Jul 3rd, 2006, 09:05 PM
#13
Member
Re: Detecting screen resolution
Hmmm... When i use this resolutino changing code i get an error.
A call to PInvoke function 'RPG!RPG.ScreenRes::ChangeDisplaySettings' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
This is on the line
lResult = ChangeDisplaySettings(DevM, CDS_TEST)
Any help??
-
Jul 3rd, 2006, 09:11 PM
#14
Re: Detecting screen resolution
The usual culprit when errors like that arise is API declarations converted from VB6 without changing Long to Integer. I notice that the 'flags' argument is declared as type Long. I'm guessing that that is an oversight and it should be Integer. Try changing that declaration and see if it fixes the issue.
-
Jul 3rd, 2006, 09:34 PM
#15
Member
Re: Detecting screen resolution
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|