Determining Display Metrics in an Excel Macro

Last reviewed: November 2, 1994
Article ID: Q51139
The information in this article applies to:
  • Microsoft Excel for Windows, versions 2.x, 3.0, 4.0, 5.0

SUMMARY

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).

MORE INFORMATION

The instructions in this macro do the following:

  1. 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.

  2. 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.

  3. 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).

  4. Cell A5 calls the GetActiveWindow function to return the handle to the current window (hWnd).

  5. Cell A6 calls the GetDC function, using the value returned in cell A5 (hWnd) to obtain a handle to the device context (hDC).

  6. 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.


KBCategory: kbusage
KBSubcategory:

Additional words: display resolution high res mapping dialog box
screen driver worksheet formatting hi res position 2.0 2.00 2.1 2.10 3.0
3.00 4.0 4.00


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1994
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.