Visual Basic > Forms
Form Screen Size in Pixels
Form Screen Size in Pixels ' This is code that I used in VBA, but it should ' just as easily be used in VB. You will need to ' modify the resize code of the form to resize the ' controls to match. ' ' Use the GetDeviceCaps API call to ' determine the size of the screen in pixels ' and the conversion factor of Pixels per ' inch. Find the total width & height of the ' screen in Points by multiplying the number ' of Points per Inch (1/72) to the total ' number of inches (Pixels/Pixels per Inch). ' 'Sample: ' (Code is inserted on a module), and Userform1 ' is assumed to exist) Option Explicit Private Const LOGPIXELSX = 88 ' Logical pixels/inch in X Private Const LOGPIXELSY = 90 ' Logical pixels/inch in Y Private Const HORZRES = 8 ' Horizontal width in pixels Private Const VERTRES = 10 ' Vertical width in pixels Private Const TWIPSPERINCH = 1440 Private Declare Function GetDeviceCaps Lib "gdi32" ( _ ByVal hDc As Long, _ ByVal nIndex As Long _ ) As Long Private Declare Function ReleaseDC Lib "user32" ( _ ByVal hwnd As Long, _ ByVal hDc As Long _ ) As Long Private Declare Function GetDC Lib "user32" ( _ ByVal hwnd As Long _ ) As Long Public Sub usbGetFormSize( _ ByRef x As Variant, _ ByRef y As Variant _ ) Dim hDc As Long, lngRetVal As Long Dim varScreenX As Variant, varScreenY As Variant Dim varPixToInchX As Variant, varPixToInchY As Variant hDc = GetDC(0) 'Get the Screen size in pixels for X & Y varScreenX = GetDeviceCaps(hDc, HORZRES) varScreenY = GetDeviceCaps(hDc, VERTRES) 'Get the conversion of pixels/inch for X & Y varPixToInchX = GetDeviceCaps(hDc, LOGPIXELSX) varPixToInchY = GetDeviceCaps(hDc, LOGPIXELSY) lngRetVal = ReleaseDC(0, hDc) 'Convert to Points. x = (varScreenX / varPixToInchX) * 72 y = (varScreenY / varPixToInchY) * 72 End Sub ' ' This is the code for actually displaying ' the form. ' Public Sub usbShowForm() Dim x As Variant, y As Variant usbGetFormSize x, y Load UserForm1 With UserForm1 .Top = 0 .Left = 0 .Width = x .Height = y End With UserForm1.Show End Sub
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows