Class module encapsulating Window API functions

This page discusses detailed procedure for the task encapsulating multiple Windows application programming interface (API) functions in a class module to execute a single procedure. This is similar to creating a standard module encapsulating the Window API functions. However, It is recommended to use the class module rather than the standard module.
Visit the page below for overview of class module.
Class Module Overview

1. Instantiate the Class into an object

It is necessary to make an object based on the class module as the very first essential step. The very first step involves a process called Instantiating to create an object. The instantiation is accomplished following the steps below

Description Example
Instantiate a class into an object using New keyword in the standard module. Dim cl_window As New C_Window

2. Standard module

The standard module requires properties defined by Window API functions. For example, GetSystemMetrics function, one Window API function, returns ScreenHeight.
The Window API functions are located in a class module C_Window and it is instantiated into the Object cl_window before using it as an object.

Option Explicit
Sub WindowMetrices()
    Dim cl_window As New C_Window
    Dim msg As String
    With cl_window
        msg = "Screen Info: " & vbNewLine
        msg = msg & "Height in pixel :" & .ScreenHeight & vbNewLine
        msg = msg & "Width in pixel :" & .ScreenWidth & vbNewLine
        msg = msg & "Color depth :" & .ColorDepth & vbNewLine & vbNewLine
        msg = msg & "Window dimensions:" & vbNewLine
        msg = msg & " Left  : " & .WindowLeft & vbNewLine
        msg = msg & " Right : " & .WindowRight & vbNewLine
        msg = msg & " Top   : " & .WindowTop & vbNewLine
        msg = msg & " Bottom: " & .WindowBottom & vbNewLine
        msg = msg & " Width : " & .WindowWidth & vbNewLine
        msg = msg & " Height: " & .WindowHeight
    End With
    MsgBox msg
End Sub
		

3. Encapsulating the required API functions and properties

The Window API functions required to return the properties in the Procedure WindowMetrices of the standard module are described below and encapsulated in the class module named C_Winodw.

'window API to return the handle(hwnd) of window
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
 ByVal lpClassName As String, _
 ByVal lpWindowName As String) As Long
 
'Window API to return window dimension of the hwnd of window
Private Declare Function GetWindowRect Lib "user32" ( _
 ByVal hwnd As Long, _
 lpRect As RECT) As Long

'API Call to retrieve system information
Private Declare Function GetSystemMetrics Lib "user32" ( _
ByVal nIndex As Long) As Long

'GetDC returns the device context (DC) of a window or other device, given the object's handle.
'When you are finished using the device context, you should use ReleaseDC to free up system resources.
Private Declare Function GetDC Lib "user32" ( _
ByVal hwnd As Long) As Long

Private Declare Function GetDeviceCaps Lib "Gdi32" ( _
ByVal hDC As Long, _
ByVal nIndex As Long) As Long

Private Declare Function ReleaseDC Lib "user32" ( _
ByVal hwnd As Long, _
ByVal hDC As Long) As Long
		

4. The underlying web Page

Visit the page below and download the open-source file.

Window Coordinate and Metrics

Dated on:12-NOvember-2019