CreateDC function
Creates a device context (DC) for a device using the specified driver.
Syntax
Parameters
Return Value
Remarks
Example
HDC CreateDC(
LPCTSTR pwszDriver,
LPCTSTR pwszDevice,
LPCTSTR pwszOutput,
const DEVMODE *pdevmode
);
| Parameter | Type | Description |
|---|---|---|
| pwszDriver | LPCTSTR | Name of the device driver (e.g., "DISPLAY"). |
| pwszDevice | LPCTSTR | Device name; can be NULL for the default device. |
| pwszOutput | LPCTSTR | Port name (e.g., "COM1:"); can be NULL. |
| pdevmode | const DEVMODE* | Pointer to a DEVMODE structure that defines the device initialization parameters; can be NULL for defaults. |
Returns a handle to the newly created device context. If the function fails, the return value is NULL. Call GetLastError for extended error information.
- The created DC can be used with any GDI functions that accept an
HDC. - When you are finished with the DC, release it with
DeleteDC. - For printer devices, specify the printer driver name and device name obtained from
EnumPrinters. - Passing
NULLforpwszDriverandpwszDevicecreates a screen DC for the primary monitor.
#include <windows.h>
#include <stdio.h>
int main() {
HDC hdc = CreateDC(L"DISPLAY", NULL, NULL, NULL);
if (!hdc) {
printf("CreateDC failed: %ld\n", GetLastError());
return 1;
}
// Draw a simple rectangle
RECT rc = {50, 50, 300, 200};
FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW+1));
// Clean up
DeleteDC(hdc);
return 0;
}