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

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.