Sahibi çizilmiş düğmelerini kullanma

Sahibi çizilmiş düğmesinin üst pencere genellikle düğme için en az üç ileti yanıt: WM_INITDIALOG, wm_commandve WM_DRAWITEM. WM_MEASUREITEM mesajın sahibi çizilmiş düğmeleri işlemek için gerekli değildir.

Sahibi çizilmiş bir düğme boya gerekir sistem üst pencere olan lParam DRAWITEMSTRUCT yapısına işaret WM_DRAWITEM ileti gönderir. Bu yapı sahibi çizilmiş tüm kontrolleri ile bilgileriyle denetimi boya uygulama sağlamak için kullanın. DRAWITEMSTRUCT yapısı itemAction ve itemState üyeleri sahibi çizilmiş bir düğme boyamak nasıl tanımlamak.

Aşağıdaki örnek WM_INITDIALOG, WM_DRAWITEM ve wm_command mesaj sahibi çizilmiş düğmelerin nasıl gösterir. Bu örnek, bir olup denetimin seçili bağlı bir denetim için iki adet bit eşlem çizmek gösterilmiştir. Genellikle wParam parametre WM_DRAWITEM ileti denetimi tanımlamak için kullanacağınız; Bu örnekte, tek bir denetim kabul edilir.

BOOL CALLBACK OwnDrawProc(HWND hDlg, UINT message, WPARAM wParam, 
                          LPARAM lParam) 
{ 
    HDC hdcMem; 
    LPDRAWITEMSTRUCT lpdis; 
 
    switch (message) 
    { 
        case WM_INITDIALOG: 
 
            // hinst, hbm1 and hbm2 are defined globally. 
            hbm1 = LoadBitmap((HANDLE) hinst, "OwnBit1"); 
            hbm2 = LoadBitmap((HANDLE) hinst, "OwnBit2"); 
            return TRUE; 
 
        case WM_DRAWITEM: 
            lpdis = (LPDRAWITEMSTRUCT) lParam; 
            hdcMem = CreateCompatibleDC(lpdis->hDC); 
 
            if (lpdis->itemState & ODS_SELECTED)  // if selected 
                SelectObject(hdcMem, hbm2); 
            else 
                SelectObject(hdcMem, hbm1); 
 
            // Destination 
            StretchBlt( 
                lpdis->hDC,         // destination DC 
                lpdis->rcItem.left, // x upper left 
                lpdis->rcItem.top,  // y upper left 
 
                // The next two lines specify the width and 
                // height. 
                lpdis->rcItem.right - lpdis->rcItem.left, 
                lpdis->rcItem.bottom - lpdis->rcItem.top, 
                hdcMem,    // source device context 
                0, 0,      // x and y upper left 
                32,        // source bitmap width 
                32,        // source bitmap height 
                SRCCOPY);  // raster operation 
 
            DeleteDC(hdcMem); 
            return TRUE; 
 
        case WM_COMMAND: 
            if (wParam == IDOK 
                || wParam == IDCANCEL) 
            { 
                EndDialog(hDlg, TRUE); 
                return TRUE; 
            } 
            if (HIWORD(wParam) == BN_CLICKED) 
            { 
                switch (LOWORD(wParam)) 
                { 
                    case IDC_OWNERDRAW: 
 
                        // application-defined processing 
 
                        break; 
                } 
            } 
            break; 
 
        case WM_DESTROY: 
            DeleteObject(hbm1);  // delete bitmaps 
            DeleteObject(hbm2); 
 
            break; 
 
    } 
    return FALSE; 
        UNREFERENCED_PARAMETER(lParam); 
} 
 

Index