Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6728289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:09:09+00:00 2026-05-26T10:09:09+00:00

For some reason, as soon as I try to separate certain blocks of code

  • 0

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:
Working version with buttons in the toolbar

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:

Here the buttons are missing

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:09:10+00:00Added an answer on May 26, 2026 at 10:09 am

    It looks like you’re assigning to an argument, which will only affect it for the duration of the function:

    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);
        ...
    }
    

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, when I try to install SQL Server 2008 Express , I
I use the code below to insert data into my sqlite-database but for some
For some reason my mapview and annotations work fine without dequeueReusableAnnotationViewWithIdentifier, but as soon
For some reason I never see this done. Is there a reason why not?
For some reason when I attempt to make a request to an Ajax.net web
For some reason when I create a new namespace in Visual Studio 2008 its
For some reason, lately the *.UDL files on many of my client systems are
For some reason, Section 1 works but Section 2 does not. When run in
For some reason beyond me I can't access the mysql server on a machine.
For some reason the Windows command prompt is special in that you have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.