Abrufen von Objektattributen und Auswählen neuer Objekte

Eine Anwendung kann die Attribute für Einen Stift, Pinsel, Palette, Schriftart oder Bitmap abrufen, indem sie die Funktionen GetCurrentObject und GetObject aufruft. Die GetCurrentObject-Funktion gibt ein Handle zurück, das das derzeit im DC ausgewählte Objekt identifiziert. Die GetObject-Funktion gibt eine Struktur zurück, die die Attribute des Objekts beschreibt.

Das folgende Beispiel zeigt, wie eine Anwendung die aktuellen Pinselattribute abrufen und die abgerufenen Daten verwenden kann, um zu bestimmen, ob ein neuer Pinsel ausgewählt werden muss.

    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 white 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); 

Hinweis

Die Anwendung hat das ursprüngliche Pinselhandle beim ersten Aufruf der SelectObject-Funktion gespeichert. Dieses Handle wird gespeichert, sodass der ursprüngliche Pinsel wieder auf dem DC ausgewählt werden kann, nachdem der letzte Malvorgang mit dem neuen Pinsel abgeschlossen wurde. Nachdem der ursprüngliche Pinsel wieder auf dem DC ausgewählt wurde, wird der neue Pinsel gelöscht, wodurch Arbeitsspeicher im GDI-Heap freigegeben wird.