How can I change the background color of a button WinAPI C++

后端 未结 4 1417
时光说笑
时光说笑 2020-12-05 08:49

I have searched this many times, but everything I find is MFC. I want it in C++ WinAPI. I know how to change the style of a button control, but I cannot find out how to ma

4条回答
  •  忘掉有多难
    2020-12-05 09:06

    You can edit a button(which has the flag BS_OWNERDRAW) in the message WM_DRAWITEM on the DialogProc(MSDN About WM_DRAWITEM), heres a simple example of how to draw a simple button:

    LPDRAWITEMSTRUCT Item;
        Item = (LPDRAWITEMSTRUCT)lParam;
        SelectObject(Item->hDC, CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial Black"));
        FillRect(Item->hDC, &Item->rcItem, CreateSolidBrush(0));
        SelectObject(Item->hDC, CreateSolidBrush(0));
        if (Item->itemState & ODS_SELECTED)
        {
            SetTextColor(Item->hDC, 0);
            SelectObject(Item->hDC, CreateSolidBrush(0xFF00));
            SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0xFF00));
        }
        else
        {
            SetTextColor(Item->hDC, 0x00FF00);  
            SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0x00FF00));
    
        }
        SetBkMode(Item->hDC, TRANSPARENT);
        RoundRect(Item->hDC, Item->rcItem.left, Item->rcItem.top, Item->rcItem.right, Item->rcItem.bottom, 20, 20);
        int len;
        len = GetWindowTextLength(Item->hwndItem);
        LPSTR lpBuff;
        lpBuff = new char[len+1];
        GetWindowTextA(Item->hwndItem, lpBuff, len+1);
        DrawTextA(Item->hDC, lpBuff, len, &Item->rcItem, DT_CENTER);
    

提交回复
热议问题