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
Printable View
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
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.
wowww,Thank you Jm,this works perrrfectttt...very rarely,people might have a secondary monitor or something :) so,this is just Greattt!!
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.
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. :)
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
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.
Hey Phill..
That method which Jm gave gives me the whole screen resolution..it doesnt omit the space of taskbar or anything.It works perfect :)
try this..
VB Code:
MsgBox(Screen.PrimaryScreen.Bounds.Width.ToString & "x" & Screen.PrimaryScreen.Bounds.Height.ToString)
And Thank you Mendhak for that one :) I did do the googling too..didnt try very hard yet since I have a dead slow internet connection which I use through gprs.It works less than 1.5kpbs.Thats the reason Im not able to open too many websites and see quickly and morning,I was just getting ready for college too ;)
but,this one which you have me gives me the error saying "Display Change Failed."
I wonder if you tried it.I am having administrative privilages on this account in windows xp,but,anyway,nevermind...Ill just inform the user to change the resolution himself :(
Thanks a lot Jm and Mendhak :)...
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 ;)
I also just discovered the (read-only) SystemInformation.PrimaryMonitorSize property.
yes Jm,Even I tried it and saw,thats why i was asking about changing the resolution :D
Very cool. :thumb:Quote:
Originally Posted by jmcilhinney
The SystemInformation.MonitorCount can also be used to help destermine if the user has dual (or more) monitors.
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??
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.
Worked great, thanks.