Načtení atributů objektu, výběr nových objektů
Aplikace může načíst atributy pera, štětce, palety, písma nebo bitmapy zavoláním funkcí GetCurrentObject a GetObject. Funkce GetCurrentObject vrátí popisovač identifikující objekt aktuálně vybraný do řadiče domény; funkce GetObject vrátí strukturu, která popisuje atributy objektu.
Následující příklad ukazuje, jak aplikace může načíst aktuální atributy štětce a použít načtená data k určení, zda je nutné vybrat nový štětec.
HDC hdc; // display DC handle
HBRUSH hbrushNew, hbrushOld; // brush handles
HBRUSH hbrush; // brush handle
LOGBRUSH lb; // logical-brush structure
// Retrieve a handle identifying the current brush.
hbrush = GetCurrentObject(hdc, OBJ_BRUSH);
// Retrieve a LOGBRUSH structure that contains the
// current brush attributes.
GetObject(hbrush, sizeof(LOGBRUSH), &lb);
// If the current brush is not a solid-black brush,
// replace it with the solid-black stock brush.
if ((lb.lbStyle != BS_SOLID)
|| (lb.lbColor != 0x000000))
{
hbrushNew = GetStockObject(BLACK_BRUSH);
hbrushOld = SelectObject(hdc, hbrushNew);
}
// Perform painting operations with the solid-black brush.
// After completing the last painting operation with the new
// brush, the application should select the original brush back
// into the device context and delete the new brush.
// In this example, hbrushNew contains a handle to a stock object.
// It is not necessary (but it is not harmful) to call
// DeleteObject on a stock object. If hbrushNew contained a handle
// to a brush created by a function such as CreateBrushIndirect,
// it would be necessary to call DeleteObject.
SelectObject(hdc, hbrushOld);
DeleteObject(hbrushNew);
Poznámka
Aplikace uložila původní popisovač štětce při prvním volání SelectObject funkce. Tento úchyt se uloží tak, aby byl původní štětec vybrán zpět do řadiče domény po dokončení poslední operace malování s novým kartáčem. Po výběru původního štětce zpět do řadiče domény se nový štětec odstraní a uvolní paměť v haldě GDI.