Setting the colour of a checkbox (Win32 API NOT MFC) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Setting the colour of a checkbox (Win32 API NOT MFC)

I've created a check box on a window using the following call to CreateWindowEx()..
(note: h_check is a window handle(HWND), h_mainwindow is a handle to the parent window which has been created, CBID_CW is a constant and is used as an id number, app_instance is the hInstance passed to WinMain())

h_check = CreateWindowEx(NULL, "BUTTON", "Blah", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX | BS_TEXT, 0, 0, 100, 17, h_mainwindow, (HMENU)(CBID_CW), app_instance, NULL);

My problem lies here.. The main window background is white but the check box has a grey background behind the text. I want to change the colour of the grey area behind the check boxes text to white so it matches the rest of the window.

Can anyone give me an example of how to do this without drawing the check box myself?
If i have to draw the checkbox myself that doesn't matter but it would be easier if i could just get windows to draw it a different colour.

Thanks :)

Comments

  • pingpongpingpong Posts: 937Member
    You dont need to draw it yourself.

    In your main window, handle WM_CTLCOLORSTATIC like this:
    [code]
    switch(message)
    {
    case WM_CTLCOLORSTATIC:
    if((HWND)lParam == h_check) // lParam is the HWND for the control
    return (LONG)GetStockObject(WHITE_BRUSH); // white
    else
    return DefWindowProc(hWnd, message, wParam, lParam); // default
    [/code]
  • DarknezzDarknezz Posts: 245Member
    Thanks a lot dude :) I read about a WM_CTLCOLOR message in the Win32 API reference but i tried and got confused. I also searched on google but all i could find was MFC tutorials.
    Here on the other hand, i found you and your code fixed my problem :) Thanks!

    : You dont need to draw it yourself.
    :
    : In your main window, handle WM_CTLCOLORSTATIC like this:
    : [code]
    : switch(message)
    : {
    : case WM_CTLCOLORSTATIC:
    : if((HWND)lParam == h_check) // lParam is the HWND for the control
    : return (LONG)GetStockObject(WHITE_BRUSH); // white
    : else
    : return DefWindowProc(hWnd, message, wParam, lParam); // default
    : [/code]
    :

Sign In or Register to comment.