Click here to Skip to main content
15,891,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to understand how to set the background color of a button in win32.
I create my button with CreateWindow and tried using the API setbkcolor and setdcbrushcolor with all the necessary parameters but it does not work, then I tried to use FillRect but, not having found anything minimally commented, I cannot make the area of ​​the colored rectangle coincide with the surface of my button and then the text of the button is covered from the rectangle.
What can I do?
Thanks in advance for the help and advice
Below is the base code of my window

What I have tried:

C++
#include <windowsx.h>
#include <windows.h>

#include "resource.h"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#define IDC_BUTTON_START 0

HINSTANCE ghinstApp;
HWND ghwndApp;
HWND hStart;

BOOL Dlg_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
    HFONT hFont;

    ghwndApp = hwnd;
    SetClassLong(hwnd, GCLP_HICON, (LONG) LoadIcon(ghinstApp, MAKEINTRESOURCE(IDI_ICONA)));
    hFont = CreateFont(40, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Calibri");
    hStart = CreateWindow("Button", "PLAY", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 0, 0, 150, 114, hwnd, (HMENU)IDC_BUTTON_START, ghinstApp, NULL);
    SendDlgItemMessage(hwnd, IDC_BUTTON_START, WM_SETFONT, (WPARAM)hFont, TRUE);
    return TRUE;
}

void Dlg_OnClose()
{
    EndDialog(ghwndApp, 0);
}

BOOL CALLBACK Dlg_Proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        HANDLE_MSG(hDlg, WM_INITDIALOG, Dlg_OnInitDialog);
        HANDLE_MSG(hDlg, WM_CLOSE, Dlg_OnClose);
    default:
        break;
    }

    return FALSE;
}

int WINAPI WinMain(HINSTANCE hinstExe, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int nCmdShow)
{
    ghinstApp = hinstExe;

    DialogBox(hinstExe, MAKEINTRESOURCE(IDD_DIALOGMAIN), NULL, Dlg_Proc);

    return (0);
}
Posted
Updated 4-Sep-20 6:10am
v2

1 solution

That is a dialog which does not, by default, allow you to do your own painting. However, you can send messages to the button to control some aspects of it: Button Messages - Win32 apps | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900