Microsoft KB Archive/51139
Determining Display Metrics in an Excel Macro
The information in this article applies to:
- Microsoft Excel for Windows, versions 2.x, 3.0, 4.0, 5.0
IMPORTANT: The CALL and REGISTER functions are provided for advanced users only. If you use these functions incorrectly you could accidentally cause errors in your system's operation.
The following macro allows you to determine the metrics of your video display (that is, horizontal resolution, vertical resolution, number of colors) by using a macro. This information may be helpful in creating macros that create variable-sized custom dialog boxes based on display size or take into account the effect of display resolution on printed output.
A2 : =REGISTER("USER","GetActiveWindow","H") A3 : =REGISTER("GDI","GetDeviceCaps","HHH") A4 : =REGISTER("USER","GetDC","HH") A5 : =CALL(A2) hWnd A6 : =CALL(A4,A5) hDC A7 : =CALL(A3,A6,8) HORZRES A8 : =CALL(A3,A6,10) VERTRES A9 : =CALL(A3,A6,14) PLANES A10: =CALL(A3,A6,12) BITSPIXEL A11: =RETURN()
This macro returns the horizontal display resolution in pixels in cell A7 (HORZRES), the vertical resolution in cell A8 (VERTRES), the number of planes in cell A9 (PLANES), and the bits per pixel in cell A10 (BITSPIXEL).
The instructions in this macro do the following:
- Cell A2 uses REGISTER to obtain a text value to be used by the CALL function to activate the GetActiveWindow function in the Windows USER module. This function takes no argument and returns a handle to the current window (hWnd), which is Excel.
- Cell A3 uses REGISTER to obtain a text value to be used by CALL to activate the GetDeviceCaps function in the Windows GDI (graphics device interface) module. GetDeviceCaps accepts two parameters, a handle to the device context (hDC), obtained from the GetDC function, and a numeric parameter denoting the type of information requested, which is returned as an integer.
- Cell A4 uses REGISTER again to obtain a text value to be used by CALL to activate the GetDC function in the Windows USER module. This function takes a window handle (hWnd) as an argument (obtained from GetActiveWindow), and returns a handle to the device context (hDC).
- Cell A5 calls the GetActiveWindow function to return the handle to the current window (hWnd).
- Cell A6 calls the GetDC function, using the value returned in cell A5 (hWnd) to obtain a handle to the device context (hDC).
- Cells A7 through A10 call the GetDeviceCaps function using the handle to the device context (hDC) from cell A6, as well as integers denoting the type of information required, to obtain the display capabilities.
Note: the information above also applies to version 5.0 only when using version 4.0 macro language.
Additional query words:
Issue type :
Last Reviewed: March 15, 1999