User Interface API Reference

This section provides detailed information on the Windows API functions and structures used for creating and managing the graphical user interface (GUI) of Windows applications.

Graphics Device Interface (GDI)

GDI is a core Windows component for drawing graphics and text on the screen and printers. It handles the rendering of windows, menus, icons, and other graphical elements.

GDI Functions

int BitBlt(HDC hdcDest, int xDest, int yDest, int width, int height, HDC hdcSrc, int xSrc, int ySrc, DWORD rop);

Copies a block of pixels from a source device context to a destination device context.

Parameters:

  • hdcDest: Handle to the destination device context.
  • xDest: X-coordinate, in logical units, of the upper-left corner of the destination rectangle.
  • yDest: Y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
  • width: Width, in logical units, of the destination rectangle.
  • height: Height, in logical units, of the destination rectangle.
  • hdcSrc: Handle to the source device context.
  • xSrc: X-coordinate, in logical units, of the lower-left corner of the source rectangle.
  • ySrc: Y-coordinate, in logical units, of the lower-left corner of the source rectangle.
  • rop: Raster operation code that specifies how the source and destination pixels are combined.

Return Value:

  • If the function succeeds, the return value is nonzero.
  • If the function fails, the return value is zero.
#include <windows.h>

BOOL success = BitBlt(hdcMem, 0, 0, 100, 100, hdcScreen, 10, 20, SRCCOPY);

GDI Structures

typedef struct tagPOINT { LONG x; LONG y; } POINT, *PPOINT, *LPPOINT;

Defines a point using its x and y coordinates.

Members:

  • x: Specifies the x-coordinate of the point.
  • y: Specifies the y-coordinate of the point.
#include <windows.h>

POINT pt = { 100, 200 };

User Interface Management (User32.dll)

The User32.dll library provides functions for creating and managing windows, handling user input (keyboard, mouse), and interacting with the operating system's user interface elements.

User32 Functions

HWND CreateWindowEx(DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);

Creates a top-level window with a specified class, window name, and style.

Parameters:

  • dwExStyle: Extended window styles.
  • lpClassName: Pointer to a null-terminated string that specifies the window class name.
  • lpWindowName: Pointer to a null-terminated string that specifies the text of the window.
  • dwStyle: Window styles.
  • x: The position of the window's left side, in screen coordinates.
  • y: The position of the window's top, in screen coordinates.
  • nWidth: The width, in device units, of the window.
  • nHeight: The height, in device units, of the window.
  • hWndParent: Handle to the parent window or owner window of the class being registered.
  • hMenu: Handle to a menu, or specifies a child-window identifier.
  • hInstance: Handle to the application instance.
  • lpParam: Pointer to a value to be passed to the window creation through the CREATESTRUCT structure (pcwstr).

Return Value:

  • If the function succeeds, the return value is a handle to the newly created window.
  • If the function fails, the return value is NULL.
#include <windows.h>

HWND hWnd = CreateWindowEx(0, L"MyWindowClass", L"My Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);

User32 Structures

typedef struct tagMSG { HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; } MSG, *LPMSG;

Defines the message structure used by the application to retrieve messages from the thread's message queue.

Members:

  • hwnd: Handle to the window whose property the message is for.
  • message: The message identifier.
  • wParam: Additional message information.
  • lParam: Additional message information.
  • time: The time when the message was posted.
  • pt: The cursor position, in screen coordinates, when the message was posted.
#include <windows.h>

MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

Shell Functions

Shell functions provide access to Windows shell features, such as file operations, shortcuts, and system information.

Shell Functions

BOOL ShellExecute(HWND hwnd, LPCTSTR lpOperation, LPCTSTR lpFile, LPCTSTR lpParameters, LPCTSTR lpDirectory, INT nShowCmd);

Performs an operation on a specified file.

Parameters:

  • hwnd: Handle to the parent window of any message boxes that the application displays.
  • lpOperation: Pointer to a null-terminated string that specifies the action to be performed.
  • lpFile: Pointer to a null-terminated string that specifies the file or object on which to perform the operation.
  • lpParameters: Pointer to a null-terminated string that specifies the null-separated parameters to a verb.
  • lpDirectory: Pointer to a null-terminated string that specifies the default directory.
  • nShowCmd: The flags that specify the window display state for the application.

Return Value:

  • If the function succeeds, it returns a value greater than 32.
  • If the function fails, it returns an error value that is less than or equal to 32.
#include <windows.h>
#include <shellapi.h>

ShellExecute(NULL, L"open", L"notepad.exe", NULL, NULL, SW_SHOWNORMAL);