Results 1 to 15 of 15

Thread: Detecting screen resolution

  1. #1

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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!

  4. #4

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    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

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Detecting screen resolution

    You can use the ChangeDisplaySettings API to do this.

    VB Code:
    1. Const ENUM_CURRENT_SETTINGS As Integer = -1
    2.     Const CDS_UPDATEREGISTRY As Integer = &H1
    3.     Const CDS_TEST As Long = &H2
    4.  
    5.     Const CCDEVICENAME As Integer = 32
    6.     Const CCFORMNAME As Integer = 32
    7.  
    8.     Const DISP_CHANGE_SUCCESSFUL As Integer = 0
    9.     Const DISP_CHANGE_RESTART As Integer = 1
    10.     Const DISP_CHANGE_FAILED As Integer = -1
    11.  
    12.     Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Integer
    13.     Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef DEVMODE As DEVMODE, ByVal flags As Long) As Integer
    14.  
    15.     <StructLayout(LayoutKind.Sequential)> Public Structure DEVMODE
    16.         <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> Public dmDeviceName As String
    17.         Public dmSpecVersion As Short
    18.         Public dmDriverVersion As Short
    19.         Public dmSize As Short
    20.         Public dmDriverExtra As Short
    21.         Public dmFields As Integer
    22.         Public dmOrientation As Short
    23.         Public dmPaperSize As Short
    24.         Public dmPaperLength As Short
    25.         Public dmPaperWidth As Short
    26.         Public dmScale As Short
    27.         Public dmCopies As Short
    28.         Public dmDefaultSource As Short
    29.         Public dmPrintQuality As Short
    30.         Public dmColor As Short
    31.         Public dmDuplex As Short
    32.         Public dmYResolution As Short
    33.         Public dmTTOption As Short
    34.         Public dmCollate As Short
    35.         <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> Public dmFormName As String
    36.         Public dmUnusedPadding As Short
    37.         Public dmBitsPerPel As Short
    38.         Public dmPelsWidth As Integer
    39.         Public dmPelsHeight As Integer
    40.         Public dmDisplayFlags As Integer
    41.         Public dmDisplayFrequency As Integer
    42.     End Structure
    43.  
    44.     Public Sub changeRes(ByVal theWidth As Integer, ByVal theHeight As Integer)
    45.  
    46.         Dim DevM As DEVMODE
    47.  
    48.         DevM.dmDeviceName = New [String](New Char(32) {})
    49.         DevM.dmFormName = New [String](New Char(32) {})
    50.         DevM.dmSize = CShort(Marshal.SizeOf(GetType(DEVMODE)))
    51.  
    52.  
    53.         If 0 <> EnumDisplaySettings(Nothing, ENUM_CURRENT_SETTINGS, DevM) Then
    54.             Dim lResult As Integer
    55.  
    56.             DevM.dmPelsWidth = theWidth
    57.             DevM.dmPelsHeight = theHeight
    58.             DevM.dmPelsWidth = 1280
    59.             DevM.dmPelsHeight = 1024
    60.  
    61.             lResult = ChangeDisplaySettings(DevM, CDS_TEST)
    62.  
    63.             If lResult = DISP_CHANGE_FAILED Then
    64.                 MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Change Failed")
    65.             Else
    66.  
    67.                 lResult = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
    68.  
    69.                 Select Case lResult
    70.                     Case DISP_CHANGE_RESTART
    71.                         MsgBox("You must restart your computer to apply these changes.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Has Changed")
    72.                     Case DISP_CHANGE_SUCCESSFUL
    73.                         MsgBox("Display Change Successful.", MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "Screen Resolution Successful")
    74.                     Case Else
    75.                         MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Change Failed")
    76.                 End Select
    77.             End If
    78.  
    79.  
    80.         End If
    81.     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.

  8. #8

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: Detecting screen resolution

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

    Help someone else with what someone helped you!

  9. #9
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Detecting screen resolution

    I also just discovered the (read-only) SystemInformation.PrimaryMonitorSize property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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!

  12. #12
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Re: Detecting screen resolution

    Quote 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


  13. #13
    Member
    Join Date
    May 2006
    Posts
    47

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

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    Member
    Join Date
    May 2006
    Posts
    47

    Re: Detecting screen resolution

    Worked great, thanks.

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