简体   繁体   中英

How do I set the text and background color of an single static control c++?

I have managed to set the colors of static and edit controls when I set all of them to the same color, but I cant figure out how to set the color of just one without affecting the others. I've looked around on the internet but none of the things I find are about how to set color of just one static or edit control. Here is the code I am using at the moment to set the color of all static text fields:

#include <windows.h>

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

HWND staticTextField, staticTextFieldTwo;

char szClassName[ ] = "WindowsApp";

int WINAPI WinMain(HINSTANCE hThisInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpszArgument,
                   int nFunsterStil)

{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS; 
    wincl.cbSize = sizeof (WNDCLASSEX);

    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = CreateSolidBrush(RGB(0, 255, 255));

    if (!RegisterClassEx (&wincl))
        return 0;

    hwnd = CreateWindowEx (
       0,
       szClassName,
       "Windows app",
       WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
       CW_USEDEFAULT,
       CW_USEDEFAULT,
       544,
       375,
       HWND_DESKTOP,
       NULL,
       hThisInstance,
       NULL
    );

    ShowWindow (hwnd, nFunsterStil);

    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

    return messages.wParam;
}


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
    HDC hdcStatic = (HDC)wParam;

switch (message)
{
    case WM_DESTROY:
        PostQuitMessage (0);
        break;
    case WM_CREATE:

         staticTextField = CreateWindow("STATIC", "static 1", WS_VISIBLE 
         | WS_CHILD, 20, 20, 300, 25, hwnd, NULL, NULL, NULL);


         staticTextFieldTwo = CreateWindow("STATIC", "static2", WS_VISIBLE | 
         WS_CHILD | WS_BORDER, 100, 200, 300, 20, hwnd, NULL, NULL, NULL);

         break;
    case WM_CTLCOLORSTATIC:
        SetTextColor(hdcStatic, RGB(255, 0, 0));
        SetBkColor(hdcStatic, RGB(0, 255, 255));
        return (INT_PTR)CreateSolidBrush(RGB(0, 255, 255));
        break;
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
}

return 0;
}

When responding to WM_CTLCOLORSTATIC , check lParam which holds the handle of the control.

CreateSolidBrush creates a GDI object. It should be deleted with DeleteObject otherwise it causes a resource leak. You can declare the brush as static and delete it at the end. Example:

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HBRUSH hbrush = NULL;
    switch(message)
    {
    case WM_DESTROY:
        if (hbrush) DeleteObject(hbrush);
        hbrush = NULL;
        PostQuitMessage(0);
        break;
    case WM_CREATE:
        staticTextField = CreateWindow("STATIC", "static 1", WS_VISIBLE
            | WS_CHILD, 20, 20, 300, 25, hwnd, NULL, NULL, NULL);
        staticTextFieldTwo = CreateWindow("STATIC", "static2", WS_VISIBLE |
            WS_CHILD | WS_BORDER, 100, 200, 300, 20, hwnd, NULL, NULL, NULL);
        break;
    case WM_CTLCOLORSTATIC:
    {
        HDC hdcStatic = (HDC)wParam;
        if(lParam == (LPARAM)staticTextFieldTwo)
        {
            SetTextColor(hdcStatic, RGB(0, 255, 0));
            SetBkColor(hdcStatic, RGB(0, 255, 255));
            if (!hbrush)
                hbrush = CreateSolidBrush(RGB(0, 255, 255));
            return (LRESULT)hbrush;
        }
        break;
    }
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM