Discussion:
Determining screen size
(too old to reply)
Peter
2008-07-23 16:01:01 UTC
Permalink
I've got some macros for positioning and sizing the word window but currently
am using hard coded numbers for width, height, left and top parameters. These
numbers were obtained empirically for my desktop. When I take documents to my
laptop with different screen dimensions, these macros do not produce the
intended effect.

I am simply asking how I can determine the screen dimensions and how they
relate to the word window. It appears I may have to take title bars and word
task bar into account. I was wondering if anyone has either figured this all
out or better yet has code that does this.
Jonathan West
2008-07-23 16:47:39 UTC
Permalink
Post by Peter
I've got some macros for positioning and sizing the word window but currently
am using hard coded numbers for width, height, left and top parameters. These
numbers were obtained empirically for my desktop. When I take documents to my
laptop with different screen dimensions, these macros do not produce the
intended effect.
I am simply asking how I can determine the screen dimensions and how they
relate to the word window. It appears I may have to take title bars and word
task bar into account. I was wondering if anyone has either figured this all
out or better yet has code that does this.
System.HorizontalResolution and System.VerticalResolution will give you the
number of pixels on the screen in each direction. Unfortunately, this is not
quite enough, and you also need to know the points-per-pixel ratio in each
direction, as UserForm sizes and positions are determined in points, not
pixels. This ratio varies according to the screen resolution and the system
font size settings in Windows.

Place the following code in a separate module, and you will have available
functions PixelsPerInchX and PixelsPerInchY, which will allow you to make
conversions as needed. You may need to adjust the code to remove line
breaks.


' Device capabilities
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal
nIndex As Long) As Long
Private Const LOGPIXELSY = 90 ' Logical pixels/inch in Y
Private Const LOGPIXELSX = 88 ' Logical pixels/inch in X

' Used to obtain screen device context
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal
hDC As Long) As Long

Public Function PixelsPerInchX() As Long
Dim hWnd As Long
Dim hDC As Long
' Retrieves the number of pixels per logical
' inch in the X-direction on screen.
hWnd = GetDesktopWindow()
hDC = GetDC(hWnd)
PixelsPerInchX = GetDeviceCaps(hDC, LOGPIXELSX)
Call ReleaseDC(hWnd, hDC)
End Function

Public Function PixelsPerInchY() As Long
Dim hWnd As Long
Dim hDC As Long
' Retrieves the number of pixels per logical
' inch in the Y-direction on screen.
hWnd = GetDesktopWindow()
hDC = GetDC(hWnd)
PixelsPerInchY = GetDeviceCaps(hDC, LOGPIXELSY)
Call ReleaseDC(hWnd, hDC)
End Function

The code above is adapted from Karl Peterson's NCMetrics class module,
available on his site http://vb.mvps.org. That site is full of useful little
routines for this sort of thing. Originally written in and for VB6, large
parts of his code can be dropped into VBA projects unmodified, and the site
indicates which of his code samples have been checked for VBA compatibility.
A very useful resource, I use stuff from his site all the time.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Peter
2008-07-23 18:54:07 UTC
Permalink
Works great thank you! I have one more problem: The system.verticalResolution
function gives the entire screen's vertical dimensions. I now have to
subtract the space taken up by the Windows taskbar. This needs to be obtained
from the system as well (not hard-coded) because I use two rows for my
Windows Taskbar on my desktop but only one on my laptop. Do you have code to
determine the location and size of the Windows Taskbar?

Thanks,
Peter
Post by Jonathan West
Post by Peter
I've got some macros for positioning and sizing the word window but currently
am using hard coded numbers for width, height, left and top parameters. These
numbers were obtained empirically for my desktop. When I take documents to my
laptop with different screen dimensions, these macros do not produce the
intended effect.
I am simply asking how I can determine the screen dimensions and how they
relate to the word window. It appears I may have to take title bars and word
task bar into account. I was wondering if anyone has either figured this all
out or better yet has code that does this.
System.HorizontalResolution and System.VerticalResolution will give you the
number of pixels on the screen in each direction. Unfortunately, this is not
quite enough, and you also need to know the points-per-pixel ratio in each
direction, as UserForm sizes and positions are determined in points, not
pixels. This ratio varies according to the screen resolution and the system
font size settings in Windows.
Place the following code in a separate module, and you will have available
functions PixelsPerInchX and PixelsPerInchY, which will allow you to make
conversions as needed. You may need to adjust the code to remove line
breaks.
' Device capabilities
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal
nIndex As Long) As Long
Private Const LOGPIXELSY = 90 ' Logical pixels/inch in Y
Private Const LOGPIXELSX = 88 ' Logical pixels/inch in X
' Used to obtain screen device context
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal
hDC As Long) As Long
Public Function PixelsPerInchX() As Long
Dim hWnd As Long
Dim hDC As Long
' Retrieves the number of pixels per logical
' inch in the X-direction on screen.
hWnd = GetDesktopWindow()
hDC = GetDC(hWnd)
PixelsPerInchX = GetDeviceCaps(hDC, LOGPIXELSX)
Call ReleaseDC(hWnd, hDC)
End Function
Public Function PixelsPerInchY() As Long
Dim hWnd As Long
Dim hDC As Long
' Retrieves the number of pixels per logical
' inch in the Y-direction on screen.
hWnd = GetDesktopWindow()
hDC = GetDC(hWnd)
PixelsPerInchY = GetDeviceCaps(hDC, LOGPIXELSY)
Call ReleaseDC(hWnd, hDC)
End Function
The code above is adapted from Karl Peterson's NCMetrics class module,
available on his site http://vb.mvps.org. That site is full of useful little
routines for this sort of thing. Originally written in and for VB6, large
parts of his code can be dropped into VBA projects unmodified, and the site
indicates which of his code samples have been checked for VBA compatibility.
A very useful resource, I use stuff from his site all the time.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Jonathan West
2008-07-23 19:19:37 UTC
Permalink
Post by Peter
Works great thank you! I have one more problem: The
system.verticalResolution
function gives the entire screen's vertical dimensions. I now have to
subtract the space taken up by the Windows taskbar. This needs to be obtained
from the system as well (not hard-coded) because I use two rows for my
Windows Taskbar on my desktop but only one on my laptop. Do you have code to
determine the location and size of the Windows Taskbar?
What I can give you is the coordinates (in pixels) of the available screen
real-estate excluding the taskbar. (The following code is adapted from a
sample on Randy Birch's website http://vbnet.mvps.org - another very useful
resource for oddments of this kind). Put the following into a separate
module.

Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Const SPI_GETWORKAREA& = 48

Private Declare Function SystemParametersInfo Lib "user32" _
Alias "SystemParametersInfoA" _
(ByVal uAction As Long, _
ByVal uParam As Long, _
lpvParam As Any, _
ByVal fuWinIni As Long) As Long

Public Function ScreenArea() As RECT
Dim rc As RECT
Call SystemParametersInfo(SPI_GETWORKAREA, 0&, rc, 0&)
ScreenArea = rc
End Function

You can't assume the taskbar is at the bottom, it could be at the top or the
side. Hence, this function gives you a user-defined type with 4 parameters,
Bottom, Top, Left & Right, giving the position in pixels of the desktop
area.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Peter
2008-07-23 19:37:02 UTC
Permalink
Thanks again for the excellent and speedy responses!
In my code I use:
Dim l As Long, t As Long, b As Long, r As Long, ScrDim As RECT

Set ScrDim = ScreenArea
l = ScrDim.Left
t = ScrDim.Top
r = ScrDim.Right
b = ScrDim.Bottom

I get an error "Object Required" at the Set call.

What am I doing wrong?

Thanks,
Peter
Post by Jonathan West
Post by Peter
Works great thank you! I have one more problem: The
system.verticalResolution
function gives the entire screen's vertical dimensions. I now have to
subtract the space taken up by the Windows taskbar. This needs to be obtained
from the system as well (not hard-coded) because I use two rows for my
Windows Taskbar on my desktop but only one on my laptop. Do you have code to
determine the location and size of the Windows Taskbar?
What I can give you is the coordinates (in pixels) of the available screen
real-estate excluding the taskbar. (The following code is adapted from a
sample on Randy Birch's website http://vbnet.mvps.org - another very useful
resource for oddments of this kind). Put the following into a separate
module.
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Const SPI_GETWORKAREA& = 48
Private Declare Function SystemParametersInfo Lib "user32" _
Alias "SystemParametersInfoA" _
(ByVal uAction As Long, _
ByVal uParam As Long, _
lpvParam As Any, _
ByVal fuWinIni As Long) As Long
Public Function ScreenArea() As RECT
Dim rc As RECT
Call SystemParametersInfo(SPI_GETWORKAREA, 0&, rc, 0&)
ScreenArea = rc
End Function
You can't assume the taskbar is at the bottom, it could be at the top or the
side. Hence, this function gives you a user-defined type with 4 parameters,
Bottom, Top, Left & Right, giving the position in pixels of the desktop
area.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Tony Jollans
2008-07-23 21:00:57 UTC
Permalink
ScrDim is not an object, so you don't want to *set* it. Just use

ScrDim = ScreenArea
--
Enjoy,
Tony
Post by Peter
Thanks again for the excellent and speedy responses!
Dim l As Long, t As Long, b As Long, r As Long, ScrDim As RECT
Set ScrDim = ScreenArea
l = ScrDim.Left
t = ScrDim.Top
r = ScrDim.Right
b = ScrDim.Bottom
I get an error "Object Required" at the Set call.
What am I doing wrong?
Thanks,
Peter
Post by Jonathan West
Post by Peter
Works great thank you! I have one more problem: The
system.verticalResolution
function gives the entire screen's vertical dimensions. I now have to
subtract the space taken up by the Windows taskbar. This needs to be obtained
from the system as well (not hard-coded) because I use two rows for my
Windows Taskbar on my desktop but only one on my laptop. Do you have
code
to
determine the location and size of the Windows Taskbar?
What I can give you is the coordinates (in pixels) of the available screen
real-estate excluding the taskbar. (The following code is adapted from a
sample on Randy Birch's website http://vbnet.mvps.org - another very useful
resource for oddments of this kind). Put the following into a separate
module.
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Const SPI_GETWORKAREA& = 48
Private Declare Function SystemParametersInfo Lib "user32" _
Alias "SystemParametersInfoA" _
(ByVal uAction As Long, _
ByVal uParam As Long, _
lpvParam As Any, _
ByVal fuWinIni As Long) As Long
Public Function ScreenArea() As RECT
Dim rc As RECT
Call SystemParametersInfo(SPI_GETWORKAREA, 0&, rc, 0&)
ScreenArea = rc
End Function
You can't assume the taskbar is at the bottom, it could be at the top or the
side. Hence, this function gives you a user-defined type with 4 parameters,
Bottom, Top, Left & Right, giving the position in pixels of the desktop
area.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Jonathan West
2008-07-23 19:46:19 UTC
Permalink
Post by Peter
Thanks again for the excellent and speedy responses!
Dim l As Long, t As Long, b As Long, r As Long, ScrDim As RECT
Set ScrDim = ScreenArea
l = ScrDim.Left
t = ScrDim.Top
r = ScrDim.Right
b = ScrDim.Bottom
I get an error "Object Required" at the Set call.
What am I doing wrong?
You don't need Set. RECT is a type, not an object
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Jean-Guy Marcil
2008-07-23 19:36:06 UTC
Permalink
Post by Peter
Works great thank you! I have one more problem: The system.verticalResolution
function gives the entire screen's vertical dimensions. I now have to
subtract the space taken up by the Windows taskbar. This needs to be obtained
from the system as well (not hard-coded) because I use two rows for my
Windows Taskbar on my desktop but only one on my laptop. Do you have code to
determine the location and size of the Windows Taskbar?
I have never used it, but maybe that working with
Application.ActiveWindow.UsableHeight
will help.

This return a Point value and the active window has to be visible.
See the VBA help for more details (It states that you have to substract 1
from the returned value to get the actual value...).
Loading...