top of page

Get graphics / display capabilities for a device using the GetDeviceCaps Windows API Function

  • Writer: John
    John
  • 4 minutes ago
  • 2 min read
This post is one of a series providing implementation examples of Windows API Functions, Types, Enums and Consts using VBA. The code in this post can be used as-is, however, if you regularly (or even just occasionally) work with Windows API declarations in VBA, you may want to see the posts Automatically add Windows API declaration(s) and Using 'F1' to view Windows API web pages which explain some of the functionality that can be added to the VBE by VBE_Extras.

The GetDeviceCaps Function provides information on the graphics / display capabilities (hence "Caps") of a device ... information such as:


  • Widths and heights of screens in millimetres or pixels

  • Monitor resolution

  • Colour information

  • Numbers of available brushes, pens, colours and fonts

  • Other detailed information relating to the GDI library (that this Function is part of) such as its ability to perform shading and its ability to draw certain types of curves


The key thing that GetDeviceCaps is, then, typically used for from VBA code is to get the screen size (in the code below, the screen size is obtained in millimetres, inches, pixels, points and TWIPS) and to get the number of points per pixel (which allows the calculation of the number of TWIPS per pixel) and, from that, allows points to be converted to pixels (again, as in the code below).


However, before you get information on the device capabilities, you first need to get a handle to a Device Context. For this, we use the GetDC Windows API Function. A Device Context must always be released (otherwise it will continue to exist in memory once you have done with it ... a memory leak) which you do using ReleaseDC. All of this is encapsulated in the GetDeviceCapsValue Function in the below VBA code so you don't have to worry about it!


So here is some example code. This first block of code includes the Declarations for the Windows API Functions along with a number of Consts that can be passed to GetDeviceCaps to get the information you might require, and a series of short helper Functions (e.g. GetScreenSizeInMm) to encapsulate getting useful information (not all of the Consts have an associated helper Function in the below code ... but adding one for the information you need should not be difficult).



Here's a runnable Function to demonstrate using each of the helper Functions and send the result to the Immediate window.



bottom of page