2
votes

I'm completely new to MFC and have been working on this issue for a couple of days now and can't find any solution that works.

Problem:

I have a dialog class (Modal Dialog Box) with a style defined in an .rc file (Code below) and get the resource id of the icon (int m_icon is the same as IDR_MAINFRAME) from another class (OtherClass.rc). All the text information inside the Dialog Box is set dynamically (Code below) but the same doesn't work with the Icon. The marked Icon in the image below is what I'm trying to set.

The Icon Resource is defined in another .rc file and the LoadImage seems to work as I can set the small Icon in the top left of the window. The only problem is setting the big icon in this image. (Not shown at all, just an empty space)

enter image description here

OtherClass.rc

IDR_MAINFRAME           ICON                    "res\\MyIcon.ico"

Dialog.rc

ABOUTBOX DIALOGEX 0, 0, 285, 77
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "<<Aboutbox>>"
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
    ICON            ABOUT_ICON, ABOUT_ICON, 11, 10, 21, 20
    LTEXT           "", IDC_STATIC, 40, 10, 163, 8, SS_NOPREFIX
    LTEXT           "<<Package Name 1.00>>", ABOUT_NAME, 40, 20, 163, 8, SS_NOPREFIX
    LTEXT           "<<FileName>>", ABOUT_FILENAME, 40, 30, 163, 8, SS_NOPREFIX

    DEFPUSHBUTTON   "OK", IDOK, 217, 7, 60, 14, WS_GROUP
END

Dialog.cpp

BOOL AboutDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Window Title
    SetWindowText(L"About " + m_title);

    // Set Icon
    HICON hIcon = (HICON)LoadImage(GetModuleHandleW(NULL), MAKEINTRESOURCE(m_icon), IMAGE_ICON, 96, 96, LR_DEFAULTCOLOR);
    
    SetIcon(hIcon, TRUE);
    SetIcon(hIcon, FALSE);
    
    // Text
    SetDlgItemText(ABOUT_NAME, m_name);
    SetDlgItemText(ABOUT_FILENAME, m_filename);
    
    return TRUE;
}

What I've tried doing is:

1. GetDlgItem(ABOUT_ICON)->SetIcon(hIcon, TRUE);
2. SendMessage(WM_SETICON, ICON_BIG, (LPARAM)hIcon);

and many more things along those lines but the icon space just remains empty. Neither the LoadImage nor the GetDlgItem(ABOUT_ICON) returns a nullptr (already checked that).

1

1 Answers

2
votes

You shouldn't have to do any thing like SetIcon or SendMessage to get an icon to show. What's wrong is your RC file is wrong. It should look like this:

ABOUTBOX DIALOGEX 0, 0, 285, 77
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "<<Aboutbox>>"
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
    ICON            ABOUT_ICON, IDC_STATIC, 11, 10, 21, 20
    LTEXT           "", IDC_STATIC, 40, 10, 163, 8, SS_NOPREFIX
    LTEXT           "<<Package Name 1.00>>", ABOUT_NAME, 40, 20, 163, 8, SS_NOPREFIX
    LTEXT           "<<FileName>>", ABOUT_FILENAME, 40, 30, 163, 8, SS_NOPREFIX

    DEFPUSHBUTTON   "OK", IDOK, 217, 7, 60, 14, WS_GROUP
END

Take a look at the ICON line after the BEGIN line.

You had it as ABOUT_ICON, ABOUT_ICON, 11, 10, 21, 20, but the 2nd parameter tells the framework what the ID of the control is. Since you want a Static control, you use the ID of IDC_STATIC which tells the framework there is no ID, but instead it should create a generic Static control.

If this doesn't fix it, I would investigate if ABOUT_ICON is wired up correctly, that it actually is pointing at an icon.

Also, why are you working in the RC file? MFC gives you a great GUI editor. For example, you could modify it using the UI:

MFC