Drawing graphic objects requires a presentation space and a device context to direct output to a specific instance of an output device, such as a display window or a printer. This association enables the device context to identify the output device for that presentation space. Further, the device context identifies the particular instance of the output device, such as a printer or display window.
A presentation space can be associated with only one device context at a time. The reverse is also true: a device context can be associated with only one presentation space at a time.
The following figure shows how a presentation space is associated with a window device context. It is then disassociated from the window device context and associated with a printer device context. It cannot be associated with both device contexts simultaneously.
WM_Create:
hdcScreen = WinOpenWindowsDC (hwnd);
phs = GpiCreatePS (...GPIA.Assoc);
.
.
.
WM_COMMAND:
Case IDM_File PRINT: /* Device selection */
hdcPrinter = DevOpenDC (...);
GpiAssociate (hps, NULL); /* Disconnect from screen */
GpiAssociate (hps, hdcPrinter); /* Connect to printer */
.
. /* Output */
.
GpiAssociate (hps, NULL); /* Disconnect from print */
GpiAssociate (hps, hdcScreen); /* Reconnect to screen */
.
.
.
WM_PAINT:
WinBeginPaint (hwnd, hps, NULL);
.
. /* Output */
.
The following figure shows how to open a window device context and associate it with a normal presentation space.
HDC hdcWin; /* Window device-context handle */
HPS hpsWin; /* Normal-presentation-space handle */
HWND hwndClient; /* Client-window handle */
HAB hab; /* Anchor-block handle */
SIZEL sizlPage; /* Presentation page */
hdcWin = WinOpenWindowDC(hwndClient);
hpsWin = GpiCreatePS(hab, hdcWin, &sizlPage,
PU_LOENGLISH | GPIA_ASSOC);
Note: This type of code is used when the device context is defined before the presentation space.
WinOpenWindowDC can be called only once for a particular window and returns an error if called a second time. WinQueryWindowDC can be used to obtain a window device context previously allocated using WinOpenWindowDC. The following figure shows how to create a presentation space with page units of 0.01 inch (PU_LOENGLISH) and associate it with a printer device context. As input to GpiCreatePS, you supply the height and width of the presentation page.
HAB hab; /* Anchor-block handle */
HPS hpsPrinter; /* Presentation-space handle */
HDC hdcPrinter; /* Device-context handle */
SIZEL sizlPage; /* Page structure */
.
.
.
hpsPrinter = GpiCreatePS(hab, hdcPrinter, &sizlPage,
PU_LOENGLISH | GPIA_ASSOC);