Compartilhar via


Recuperar atributos de objeto, selecionar novos objetos

Um aplicativo pode recuperar os atributos de uma caneta, pincel, paleta, fonte ou bitmap chamando as funções GetCurrentObject e GetObject . A função GetCurrentObject retorna um identificador que identifica o objeto atualmente selecionado no DC; a função GetObject retorna uma estrutura que descreve os atributos do objeto .

O exemplo a seguir mostra como um aplicativo pode recuperar os atributos de pincel atuais e usar os dados recuperados para determinar se é necessário selecionar um novo pincel.

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

Observação

O aplicativo salvou o identificador de pincel original ao chamar a função SelectObject pela primeira vez. Esse identificador é salvo para que o pincel original possa ser selecionado novamente no DC depois que a última operação de pintura tiver sido concluída com o novo pincel. Depois que o pincel original é selecionado novamente no DC, o novo pincel é excluído, liberando memória no heap de GDI.