For some reason, as soon as I try to separate certain blocks of code into different functions, adding buttons (bitmaps included) to a toolbar isn’t working anymore. Having them put together in one place however works like a charm, however.
Yet I can’t figure out the reason for this. Maybe a pointer isn’t working as expected…
The expected output is this:

The relevant code:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
CreateUserInterface(hwnd);
break;
...
}
return 0;
}
void CreateUserInterface(HWND hwnd)
{
HFONT hfDefault;
HWND hEdit;
HWND hTool;
TBADDBITMAP tbab;
TBBUTTON tbb[TBBSIZE];
HWND hStatus;
int statWidths[] = {100, -1};
// create edit control
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL |
ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0, 100, 100,
hwnd, (HMENU) IDC_MAIN_EDIT,
GetModuleHandle(NULL), NULL);
if(hEdit == NULL)
{
MessageBox(hwnd, "Could not create edit box!", "Error!",
MB_OK | MB_ICONERROR);
}
hfDefault = GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM) hfDefault, MAKELPARAM(FALSE, 0));
// create toolbar
hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0,
hwnd, (HMENU) IDC_MAIN_TOOL,
GetModuleHandle(NULL), NULL);
if(hTool == NULL)
{
MessageBox(hwnd, "Could not create tool bar!", "Error!",
MB_OK | MB_ICONERROR);
}
SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
tbab.hInst = HINST_COMMCTRL;
tbab.nID = IDB_STD_SMALL_COLOR;
SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM) &tbab);
ZeroMemory(tbb, sizeof tbb);
tbb[0].iBitmap = STD_FILENEW;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[0].idCommand = ID_FILE_NEW;
tbb[1].iBitmap = STD_FILEOPEN;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
tbb[1].idCommand = ID_FILE_OPEN;
tbb[2].iBitmap = STD_FILESAVE;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_BUTTON;
tbb[2].idCommand = ID_FILE_SAVE_AS;
SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM) &tbb);
}
What I get is:

The relevant code:
#define TBBSIZE 1
void CreateUserInterface(HWND hwnd)
{
...
HWND hTool;
TBADDBITMAP tbab;
TBBUTTON tbb[TBBSIZE];
...
CreateToolbar(hwnd, hTool);
InitializeBitmap(hTool, &tbab);
InitializeButtons(htool, tbb, TBBSIZE);
...
}
void CreateToolbar(HWND hwnd, HWND hTool)
{
hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0,
hwnd, (HMENU) IDC_MAIN_TOOL,
GetModuleHandle(NULL), NULL);
if(hTool == NULL)
{
MessageBox(hwnd, "Could not create tool bar!", "Error!",
MB_OK | MB_ICONERROR);
}
SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
}
void InitializeBitmap(HWND hTool, TBADDBITMAP *tbab)
{
(*tbab).hInst = HINST_COMMCTRL;
(*tbab).nID = IDB_STD_SMALL_COLOR;
SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM) tbab);
}
void InitializeButtons(HWND hTool, TBBUTTON *tbb, int size)
{
ZeroMemory(tbb, sizeof(*tbb) * size);
tbb[size-size].iBitmap = STD_FILENEW;
tbb[size-size].fsState = TBSTATE_ENABLED;
tbb[size-size].fsStyle = TBSTYLE_BUTTON;
tbb[size-size].idCommand = ID_FILE_NEW;
SendMessage(hTool, TB_ADDBUTTONS, size, (LPARAM) tbb);
...
}
(Don’t mind the status bar to the bottom right, I forgot to include it in the code in the first example)
For obvious reasons the problem must lie somewhere in the bit that handles adding the bitmaps and buttons. But what it is I don’t know…what am I missing?
Edit1: To make it read easier I have removed the two additional buttons (less code). It works the same way still, i.e. not working at all. 😉
EDIT2: Thanks to HostileFork I found out that a windows handle doesn’t work quite the same way as a normal raw pointer in C. The solution was to pass the address of hTool to the CreateToolbar function:
#define TBBSIZE 1
void CreateUserInterface(HWND hwnd)
{
...
HWND hTool;
...
CreateToolbar(hwnd, &hTool);
...
}
void CreateToolbar(HWND hwnd, HWND *hTool)
{
*hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0,
hwnd, (HMENU) IDC_MAIN_TOOL,
GetModuleHandle(NULL), NULL);
... // and so on
}
It looks like you’re assigning to an argument, which will only affect it for the duration of the function:
You need to either make hTool a return value, or pass it by reference or pointer so that its value can be bubbled up to the caller and used in the other functions…