jeudi 12 octobre 2023

I'm getting issue with owner-draw checkbox [closed]

I'm trying to create an interface for my application, and I'm using owner-draw checkboxes and button styles.

Normally when I select the button, info will go to WM_COMMAND for my button name, and only the selected button codes will work. But when I click the button and if the checkbox is checked, it goes to unchecked, or if the checkbox is unchecked, it goes to checked. But I didn't click the checkbox.

If I include the MessageBox function in cases (you can see below, in WM_COMMAND) it's working.

I can't understand if it's a bug or my code is just broken.

switch (uMsg) {
    case WM_CREATE:
    {
        hwnd2 = CreateWindow(L"BUTTON", L"", WS_VISIBLE | WS_CHILD | BS_OWNERDRAW, 150, 150, 20, 20, hwnd, (HMENU)but1, NULL, NULL);
        CreateWindow(L"EDIT", L"", WS_VISIBLE | WS_CHILDWINDOW | WS_BORDER | BS_CENTER, 350, 250, 50, 20, hwnd, NULL, NULL, NULL);
        Check = CreateWindow(L"BUTTON", L"", WS_VISIBLE | WS_CHILD | BS_OWNERDRAW, 200, 200, 15, 15, hwnd, (HMENU)checkbox, NULL, NULL);
        Check2 = CreateWindow(L"BUTTON", L"", WS_VISIBLE | WS_CHILD | BS_OWNERDRAW, 250, 250, 15, 15, hwnd, (HMENU)checkbox2, NULL, NULL);
        break;
    }
    case WM_COMMAND:
    {
        switch (HIWORD(wParam)) {
            case BN_CLICKED:
            {
                if (wParam == but1) {
                    MessageBox(NULL, L"working", L"window", MB_ICONINFORMATION);
                }
                if (wParam == checkbox) {
                    if (check == FALSE) {
                        check = TRUE;
                        MessageBox(NULL, L"1", L"window", MB_ICONINFORMATION);
                    }
                    else {
                        check = FALSE;
                        MessageBox(NULL, L"2", L"window", MB_ICONINFORMATION);
                    }
                }
                if (wParam == checkbox2) {
                    if (check2 == FALSE) {
                        check2 = TRUE;
                        MessageBox(NULL, L"3", L"window", MB_ICONINFORMATION);
                    }
                    else {
                        check2 = FALSE;
                        MessageBox(NULL, L"4", L"window", MB_ICONINFORMATION);
                    }
                }
            }
        }
    
        break;
    }
    case WM_DRAWITEM:
    {
        LPDRAWITEMSTRUCT bh = (LPDRAWITEMSTRUCT)lParam;
        //LPDRAWITEMSTRUCT bk = (LPDRAWITEMSTRUCT)lParam;

        if (bh->hwndItem == hwnd2) {
            SetBkMode(bh->hDC, TRANSPARENT);
            FillRect(bh->hDC, &bh->rcItem, brush2);
            HRGN first = CreateRoundRectRgn(bh->rcItem.left, bh->rcItem.top, bh->rcItem.right, bh->rcItem.bottom, 5, 5);
            if (bh->itemState & ODS_SELECTED) {
                FillRgn(bh->hDC, first, RGN);
            }
            else {
                FillRgn(bh->hDC, first, border);
            }
            SelectObject(bh->hDC, font2);
            TextOut(bh->hDC, 3, 0, lk, _tcslen(lk));
        }
        if (bh->hwndItem == Check) {
            SetBkMode(bh->hDC, TRANSPARENT);
            FillRect(bh->hDC, &rect3, brush2);
            HRGN second = CreateRoundRectRgn(bh->rcItem.left, bh->rcItem.top, bh->rcItem.right, bh->rcItem.bottom, 2, 2);
            if (bh->itemState & ODS_SELECTED) {
                FillRgn(bh->hDC, second, RGN);
            }
            else {
                FillRgn(bh->hDC, second, border);
            }
            if (check == TRUE) {
                TextOut(bh->hDC, 2, 0, box, _tcslen(box));
            }
            else {
                TextOut(bh->hDC, 2, 0, boxf, _tcslen(boxf));
            }
        }
        if (bh->hwndItem == Check2) {
            SetBkMode(bh->hDC, TRANSPARENT);
            FillRect(bh->hDC, &rect3, brush2);
            HRGN second = CreateRoundRectRgn(bh->rcItem.left, bh->rcItem.top, bh->rcItem.right, bh->rcItem.bottom, 2, 2);
            if (bh->itemState & ODS_SELECTED) {
                FillRgn(bh->hDC, second, RGN);
            }
            else {
                FillRgn(bh->hDC, second, border);
            }
            if (check2 == TRUE) {
                TextOut(bh->hDC, 2, 0, box, _tcslen(box));
            }
            else {
                TextOut(bh->hDC, 2, 0, boxf, _tcslen(boxf));
            }
        }
        break;
    }
    case WM_PAINT:
    {
        hdc = BeginPaint(hwnd, &ps);
        FillRect(hdc, &rect, brush);
        SetTextColor(hdc, color);
        SelectObject(hdc, hfont);
        SetBkMode(hdc, TRANSPARENT);
        TextOut(hdc, 100, 100, pen, _tcslen(pen));

        EndPaint(hwnd, &ps);
        //DeleteObject(brush); ---> Eğer HBRUSH değişkenini windowproc içine yazarsan pencere donacaktır.O yüzden Global değişken olarak ayarla ve objeyi silme (DeleteObject()) .
        //DeleteObject(hfont); ---> Eğer HFONT değişkenini windowproc içine yazarsan pencere donacaktır.O yüzden Global değişken olarak ayarla ve objeyi silme (DeleteObject()) .
        DeleteDC(hdc);
        break;
    }
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        break;
    }
    default:
    {
        return DefWindowProcA(hwnd, uMsg, wParam, lParam);
    }
}



Aucun commentaire:

Enregistrer un commentaire