0
votes

Hello Everyone, I am making an application in Visual C++ 2008 Professional Edition which uses a background image for a dialog box. The problem is that I can't get radio buttons to be transparent so that the image is in the backdrop and only the caption of the radio button is visible. enter image description here

Please check the image. The radio button should be transparent and only the text of the control should be visible. I have already checked the following link-: Dialog box Background image

I am using the following code-:

    #include <Windows.h>
#include <CommCtrl.h>
#include "resource.h"

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

LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DialogFunc(HWND, UINT, WPARAM, LPARAM);

int controlsLoaded=0;
char szWinName[]="Test";
HWND hDlg=NULL;
HINSTANCE hInst;

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
                   LPSTR lpszArgs, int nWinMode)
{
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wndclass;

    wndclass.cbSize=sizeof(WNDCLASSEX);

    wndclass.hInstance=hThisInst;
    wndclass.lpszClassName=szWinName;
    wndclass.lpfnWndProc=WindowFunc;
    wndclass.style=0;

    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hIconSm=NULL;
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);

    wndclass.lpszMenuName=NULL;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;

    wndclass.hbrBackground=(HBRUSH) GetStockObject(LTGRAY_BRUSH);

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

    /*Initialize the common controls for WinXP look and feel*/
    InitCommonControls();

    hInst=hThisInst;

    hwnd=CreateWindow(
        szWinName,
        "Auto Timer (Work in progress)",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hThisInst,
        NULL
        );


    while(GetMessage(&msg, NULL, 0, 0)>0)
    { if (!hDlg||!IsDialogMessage(hDlg,&msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    }
    return msg.wParam;

}

LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wparam, 
                            LPARAM lparam)
{ 
    switch(message){
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_CREATE:
            hDlg=CreateDialog(hInst,MAKEINTRESOURCE(IDD_FORMVIEW),
                hwnd,(DLGPROC)DialogFunc);
            break;
        default:
            return DefWindowProc(hwnd,message,wparam,lparam);
    }
    return 0;
}
BOOL CALLBACK DialogFunc(HWND hwnd, UINT message, 
                         WPARAM wparam, LPARAM lparam)
{ 
  switch(message)
    { 
    case WM_CLOSE:
        DestroyWindow(hwnd);
        hDlg=NULL;
        PostQuitMessage(0);
        return TRUE;
    }
    return FALSE;
}

The following is my resource file-:

    // Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE 
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE 
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE 
BEGIN
    "\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO 
BEGIN
    IDD_FORMVIEW, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 251
        TOPMARGIN, 7
        BOTTOMMARGIN, 83
    END
END
#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_FORMVIEW DIALOGEX 0, 0, 259, 91
STYLE DS_SETFONT | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_OVERLAPPEDWINDOW | WS_EX_APPWINDOW
CAPTION "Test"
FONT 8, "MS Shell Dlg", 400, 0, 0x0
BEGIN
    CONTROL         "Radio1",IDC_RADIO1,"Button",BS_AUTORADIOBUTTON,103,48,94,20,WS_EX_TRANSPARENT
    CONTROL         102,IDC_STATIC,"Static",SS_BITMAP,119,44,33,31,WS_EX_TRANSPARENT
END


/////////////////////////////////////////////////////////////////////////////
//
// Bitmap
//

IDB_BITMAP1             BITMAP                  "1.bmp"
#endif    // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED

I have tried returning the brush to the bitmap image with WM_CTLCOLORSTATIC message. But it doesn't work. Is there any way to send the message for drawing the controls manually? Because I think I can avoid this problem if the picture is drawn first and then radio button. For example WM_CTLCOLORSTATIC message is sent every time a static control is to be drawn is it the same message that is sent when a radio button is to be drawn because radio buttons are not static controls. Please do not give me msdn links and I am using pure Win32 and not MFC. Please I need help so far whatever I have tried is not helping and I have heard that there is no limitation to what the Win32 API can do. Oh and I have also posted this topic on dreamincode.net Check Here

1
A radio button control already supports this with the BS_BITMAP and BS_ICON style flags. Trying to achieve transparency effects like you do isn't impossible but usually leads to disappointment. - Hans Passant

1 Answers

0
votes

Add to your DialogFunc:

case WM_CTLCOLORBTN:
//.....
int nCtrlId = GetDlgCtrlID((HWND)lParam);   
if(nCtrlId == RadiobuttonID)
{
   SetBkMode((HDC)wParam), TRANSPARENT);
   return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
}