dcsimg
December 3, 2016
Hot Topics:

Screen Resolutions

  • November 19, 2002
  • By Steve Anderson
  • Send Email »
  • More Articles »

For example if you design a program that uses 800 pixels by 600 pixels and the user of the machine running your application has their screen size set to 640 pixels by 480 pixels, 1024 pixels by 768 pixels or higher then your application may not be displayed correctly.

If you determine the screen resolution then you can warn the user that their display settings may effect the functionality of your application. You could, if you the user wanted, use the method later in this article that will let you dynamically change the resolution for the user and then on exit of you application set the resolution back to the previous state.

The API function that returns system metrics, including the screen resolution, is the GetSystemMetrics API function. It contains only one parameter.

nIndex – This parameter specifies the metric or configuration setting that you want to retrieve. Although there are over 150 constants that you can use for this parameter, in this article I will explain how to use just two of them to retrieve the vertical and horizontal reoulution of the screen. (For a full list of constants, check out this tip)

Constant Description
SM_CXSCREEN You may have noticed that the constant with CX in it is for the x axis or horizontal axis if you prefer to call it that. The simply returns the width of the screen, which for most users is 800 pixels by 600 pixels.
SM_CYSCREEN This is simply the opposite of SM_CXSCREEN, and is the height of the screen or the y-axis of the screen in algebraic form.

To use this API function simply call it by:

<Variable> = GetSystemMetrics(<Constant>)

The API function will then return the value depending on the constant you have used.

To return the screen resolution you can construct a simple function from the information above:

Public Function GetScreenResolution() As String
GetScreenResolution = GetSystemMetrics(SM_CXSCREEN) & _
" pixels by " & GetSystemMetrics(SM_CYSCREEN) & " pixels"
End Function

Now you can change this function to what ever you like. One possible change that you may make would to be to check that the user has the 800 pixels by 600 pixels screen resolution set. You could do this by returning true to the function if the user has a 800 pixels by 600 pixels screen resolution and vice versa. Below is an example of this

' Check whether screen resolution is set to 800x600
' Returns True if the resolution is at 800x600 else
' False is returned

Public Function GetScreenResolution() As Boolean
Dim lTemp As String
' Temporary string to hold returned screen
' resolution
lTemp = GetSystemMetrics(SM_CXSCREEN) & _
"x" & GetSystemMetrics(SM_CYSCREEN)
' Call the API function twice to return
' screen size for each axis as format into
' the temporary string
If lTemp = "800x600" Then
  ' Check whether resolution is set to 800x600
  GetScreenResolution = True
  ' True
Else
  GetScreenResolution = False
  ' False
End If
End Function

You should now be able to implement this into your application for your own needs.

The functions here are demonstrated in the GetScreenRes demo project.





Page 2 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.


Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel