更改按钮颜色

时间:2018-12-16 11:57:05

标签: c++ user-interface winapi button colors

我想在用户单击按钮时更改按钮的颜色。

主要计划是:每个月的第10个工人的按钮变为红色,如果该工人完成了工作,则单击该按钮,该按钮将变为绿色。

我不知道该怎么办。

我已经有了此代码。我刚刚创建了一个窗口,并向其中添加了一个按钮:

    switch (msg) {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_CREATE:
        AddButton(hWnd);
        break;
    default:
        return DefWindowProcW(hWnd, msg, wp, lp);
    }
}

void AddButton(HWND hWnd)
{
    CreateWindowW(L"Button", L"Change colors", WS_VISIBLE | WS_CHILD,
        350, 200,
        100, 100,
        hWnd,
        NULL,
        NULL,
        NULL);
}

所以我尝试了WM_LBUTTONDOWN。我认为这是当用户单击按钮时程序将执行的操作。我放入了switch(msg) case WM_LBUTTONDOWN:

但不知道下一步是什么。

1 个答案:

答案 0 :(得分:1)

这是一个简单的演示,用于演示如何检测按钮的单击以及如何在单击按钮时更改按钮的颜色。

#pragma comment(linker, "/manifestdependency:\"type='win32' \
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
    processorArchitecture='*' \
    publicKeyToken='6595b64144ccf1df' language='*'\"")

#pragma comment(lib, "comctl32.lib")
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
static BOOL flag = false;
ATOM RegisterWndClass(HINSTANCE hInst);

BOOL CreateMainWnd(HINSTANCE hInstance, int nCmdShow);

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

HINSTANCE hInst;

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hInstPrev, LPWSTR lpszCmdLine,
    int nCmdShow)
{
    INITCOMMONCONTROLSEX icex = { 0 };
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_USEREX_CLASSES | ICC_BAR_CLASSES |
        ICC_COOL_CLASSES | ICC_TAB_CLASSES | ICC_WIN95_CLASSES |
        ICC_PROGRESS_CLASS | ICC_PAGESCROLLER_CLASS;

    InitCommonControlsEx(&icex);

    MSG msg;

    hInst = hInstance;

    if (!RegisterWndClass(hInstance))
        return NULL;

    if (!CreateMainWnd(hInstance, nCmdShow))
        return NULL;

    while (GetMessage(&msg, NULL, NULL, NULL))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
};

ATOM RegisterWndClass(HINSTANCE hInstance)
{

    WNDCLASS wndClass = { 0 };
    wndClass.style = CS_DBLCLKS;
    wndClass.lpfnWndProc = MainWndProc;
    wndClass.hInstance = hInstance;
    wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClass.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = L"MainClass";
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;

    return RegisterClass(&wndClass);
}

BOOL CreateMainWnd(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd = CreateWindow(L"MainClass", L"Buttons sample",
        WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
        GetSystemMetrics(SM_CXSCREEN) / 2 - 115,
        GetSystemMetrics(SM_CYSCREEN) / 2 - 50,
        230, 100, NULL, NULL, hInstance, NULL);

    if (!hWnd)
        return FALSE;

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
}

HBITMAP hBitmap = NULL;

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_CREATE:
    {
        // Owner draw button

        CreateWindow(L"BUTTON", L"", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON |
            BS_OWNERDRAW, 10, 10, 60, 30, hWnd,
            (HMENU)10001, hInst, NULL);

        // Custom draw button

        CreateWindow(L"BUTTON", L"", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 80,
            10, 60, 30, hWnd, (HMENU)10002, hInst, NULL);

        // Bitmap button

        HWND hBitmapButton = CreateWindow(L"BUTTON", L"", WS_CHILD | WS_VISIBLE
            | BS_PUSHBUTTON | BS_BITMAP,
            150, 10, 60, 30, hWnd,
            (HMENU)10003, hInst, NULL);

        HDC hDC = GetDC(hWnd);

        HDC hMemDC = CreateCompatibleDC(hDC);

        hBitmap = CreateCompatibleBitmap(hDC, 55, 25);

        SelectObject(hMemDC, hBitmap);

        SetDCBrushColor(hMemDC, RGB(0, 0, 255));

        RECT r = { 0 };
        r.left = 0;
        r.right = 55;
        r.top = 0;
        r.bottom = 25;

        FillRect(hMemDC, &r, (HBRUSH)GetStockObject(DC_BRUSH));

        DeleteDC(hMemDC);
        ReleaseDC(hWnd, hDC);

        SendMessage(hBitmapButton, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP,
            (LPARAM)hBitmap);

        return 0;
    }

    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case 10001:
            MessageBox(hWnd, L"Owner draw button clicked", L"Message", NULL);
            return 0;
        case 10002:
            MessageBox(hWnd, L"Custom draw button clicked", L"Message", NULL);
            return 0;
        case 10003:
            MessageBox(hWnd, L"Bitmap button clicked", L"Message", NULL);
            return 0;
        }
        break;

        // Owner draw button

    case WM_DRAWITEM:
        if (wParam == 10001)
        {
            if (flag == false)
            {
                LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)lParam;

                SetDCBrushColor(lpDIS->hDC, RGB(255, 0, 0));

                SelectObject(lpDIS->hDC, GetStockObject(DC_BRUSH));

                RoundRect(lpDIS->hDC, lpDIS->rcItem.left, lpDIS->rcItem.top,
                    lpDIS->rcItem.right, lpDIS->rcItem.bottom, 5, 5);
            }
            else 
            {
                LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)lParam;

                SetDCBrushColor(lpDIS->hDC, RGB(0, 255, 0));

                SelectObject(lpDIS->hDC, GetStockObject(DC_BRUSH));

                RoundRect(lpDIS->hDC, lpDIS->rcItem.left, lpDIS->rcItem.top,
                    lpDIS->rcItem.right, lpDIS->rcItem.bottom, 5, 5);
            }
            flag = !flag;
            return TRUE;
        }
        break;

        // Custom draw button

    case WM_NOTIFY:
        switch (((LPNMHDR)lParam)->code)
        {
        case NM_CUSTOMDRAW:
            if (((LPNMHDR)lParam)->idFrom == 10002)
            {
                LPNMCUSTOMDRAW lpnmCD = (LPNMCUSTOMDRAW)lParam;

                switch (lpnmCD->dwDrawStage)
                {
                case CDDS_PREPAINT:

                    SetDCBrushColor(lpnmCD->hdc, RGB(0, 255, 0));
                    SetDCPenColor(lpnmCD->hdc, RGB(0, 255, 0));
                    SelectObject(lpnmCD->hdc, GetStockObject(DC_BRUSH));
                    SelectObject(lpnmCD->hdc, GetStockObject(DC_PEN));

                    RoundRect(lpnmCD->hdc, lpnmCD->rc.left + 3,
                        lpnmCD->rc.top + 3,
                        lpnmCD->rc.right - 3,
                        lpnmCD->rc.bottom - 3, 5, 5);

                    return TRUE;
                }
            }
            break;
        }
        break;

    case WM_DESTROY:
        if (hBitmap != NULL)
            DeleteObject((HBITMAP)hBitmap);
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

尝试对“如果”条件进行一些修改,以满足您的需求。