How can you access the dimensions of the screen in Visual Basic? I have looked online and it says to use Screen.width and Screen.length, but it doesn't recognize those properties... any tips?
Screen Dimensions in Visual Basic
Asked Answered
In VB you can use Screen.Width
and Screen.Height
. They're not in VBA but you can use an API call instead. Add these declarations:
Public Declare Function GetSystemMetrics Lib "user32.dll" (ByVal index As Long) As Long
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1
Then use like so:
MsgBox GetSystemMetrics(SM_CXSCREEN) & "x" & GetSystemMetrics(SM_CYSCREEN)
I'm sort of new to visual basic... so where should I put this code? –
Oily
@Oily Put the first three lines at the top of the relevant file (outside of functions). Put the actual call(s) to GetSystemMetrics where you need the values. –
Jurisprudence
Is there a way to center something on a form? –
Oily
@Monkeyanator: Just to clarify, center something on a form or a form on the screen? –
Jurisprudence
@Monkeyanator: I didn't think VBA's UserForms could be run-time resizeable could they? But okay, suppose you have a UserForm with a Button1 on it. Center it with this code on the form: Private Sub UserForm_Resize(): On Error Resume Next: Button1.Move (Me.InsideWidth - Button1.Width) / 2, (Me.InsideHeight - Button1.Height) / 2: End Sub: That should be four lines but you can't do separate lines in comments on this site. –
Jurisprudence
let us continue this discussion in chat –
Oily
It's better to create a new question instead of asking a second question in the comments to an answer –
Alwitt
© 2022 - 2024 — McMap. All rights reserved.