MSDN MSDN Documentation

CreateDC

Creates a device context (DC) for a specified device.

Syntax

HDC CreateDC(
    LPCSTR   pwszDriver,
    LPCSTR   pwszDevice,
    LPCSTR   pwszOutput,
    const DEVMODE *pDevMode
);

Parameters

pwszDriverPointer to a string that specifies the name of the display driver. This is the same string that was passed to the RegisterClass function.
pwszDevicePointer to a string that specifies the device name. For most display drivers this can be NULL to use the default device.
pwszOutputPointer to a string that specifies the output port. If NULL, the default output is used.
pDevModePointer to a DEVMODE structure that contains device initialization data. This parameter can be NULL to use the default settings.

Return Value

If the function succeeds, the return value is a handle to a device context for the specified device. If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

Example

#include <windows.h>
#include <stdio.h>

int main()
{
    // Create a DC for the display driver
    HDC hdc = CreateDC("DISPLAY", NULL, NULL, NULL);
    if (hdc == NULL) {
        printf("CreateDC failed. Error: %lu\\n", GetLastError());
        return 1;
    }

    // Use the DC – draw a simple rectangle
    Rectangle(hdc, 50, 50, 200, 150);

    // Pause so the window stays visible
    system("pause");

    // Release the DC
    DeleteDC(hdc);
    return 0;
}

← Back to Functions List