CreateDC
Creates a device context (DC) for a specified device.
Syntax
HDC CreateDC(
LPCSTR pwszDriver,
LPCSTR pwszDevice,
LPCSTR pwszOutput,
const DEVMODE *pDevMode
);
Parameters
| pwszDriver | Pointer to a string that specifies the name of the display driver. This is the same string that was passed to the RegisterClass function. |
|---|---|
| pwszDevice | Pointer to a string that specifies the device name. For most display drivers this can be NULL to use the default device. |
| pwszOutput | Pointer to a string that specifies the output port. If NULL, the default output is used. |
| pDevMode | Pointer 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
- The created DC can be used with any GDI function that requires a device context.
- When you are finished with the DC, call
DeleteDCto release it. - For printers, the
pwszDriveris typically\\.\PRINTERandpwszDeviceis the printer name.
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;
}
MSDN Documentation