Pelles C forum

C language => Beginner questions => Topic started by: CFred on July 14, 2023, 07:41:18 PM

Title: Keep toolbar button presed
Post by: CFred on July 14, 2023, 07:41:18 PM
I want to be able to programmatically press a toolbar button and keep it pressed. I am using the code

Code: [Select]
SendMessage(hTool, TB_PRESSBUTTON, ID_Button5W, MAKELPARAM(TRUE, 0));
to press the button by selecting an option in the menu for the window that contains the toolbar but it does not work.

(hTool is the handle of my toolbar and ID_Button5W is the ID of the button in my resource (*.h) file.)

What am I doing wrong?

I am attaching a zip file of the program.
Title: Re: Keep toolbar button presed
Post by: frankie on July 14, 2023, 09:20:54 PM
You copy&paste the code and forget to change the assignment:
Code: [Select]
void AddBitmapToToolbar(int bitmapId)
{
int btnIndex;

        ....

tbButton.iBitmap = btnIndex;
// tbButton.idCommand = mnuHelpAbout;    //This is probably from copy
tbButton.idCommand = bitmapId;            //This is what you want

        ....

// Add the button to the toolbar
SendMessage(hTool, TB_ADDBUTTONS, 1, (LPARAM)&tbButton);

}

Just for info, you can define variables inside a switch statement case, just you need to add braces to define a code block:
Code: [Select]
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// HWND hwndToolbar; //FALSE: Must declare here - not in the switch statement

switch(msg)
{
case WM_CREATE: //initialise window controls etc here if desired

                ....

case WM_SIZE:
{
    HWND hwndToolbar = GetDlgItem(hwnd, IDC_MAIN_TOOL); //You can declare variables inside a switch using braces
        SendMessage(hwndToolbar, TB_AUTOSIZE, 0, 0);
break;
}
case WM_CLOSE:
ImageList_Destroy(hImageList);

              ....
}

More general I suggest to take more care of details in assignment and casting (don't overlook the warnings!).
Title: Re: Keep toolbar button presed
Post by: CFred on July 15, 2023, 10:36:17 AM
@frankie: Thanks for resolving my issue. I spent hours trying to get the button to stay pressed in, visiting several sites on the internet for a solution, not realising that I had made such an elementary mistake.

Also, thanks for the tip about using braces to define variables inside a switch block.
Title: Re: Keep toolbar button presed
Post by: CFred on July 15, 2023, 11:46:18 AM
I have tried to solve the warning "Conversion from 'long long int' to 'int'; possible loss of data or unexpected result"  that this code generates in the line

Code: [Select]
    btnIndex = SendMessage(hTool, TB_ADDBITMAP, 8, (LPARAM)&tbab);
btnIndex  is used in TBBUTTON and I have declared btnIndex to be an integer because the iBitmap field of TBBUTTON  is an integer, according to the WIn32 Programmers reference. Where am I going wrong?



Title: Re: Keep toolbar button presed
Post by: frankie on July 15, 2023, 03:00:44 PM
I have tried to solve the warning "Conversion from 'long long int' to 'int'; possible loss of data or unexpected result"  that this code generates in the line

Code: [Select]
    btnIndex = SendMessage(hTool, TB_ADDBITMAP, 8, (LPARAM)&tbab);
btnIndex  is used in TBBUTTON and I have declared btnIndex to be an integer because the iBitmap field of TBBUTTON  is an integer, according to the WIn32 Programmers reference. Where am I going wrong?
The compiler doesn't know what is the very valid range for the function, but simply advice you that the conversion in case of exceeding value could give a wrong result.
Simply casting the result:
Code: [Select]
    btnIndex = (int)SendMessage(hTool, TB_ADDBITMAP, 8, (LPARAM)&tbab);Will tell the compiler that you are aware that the result could never exceed the max int for your variable, and that you are in control over it. In this case the warning will be omitted.

I forget:
Also, thanks for the tip about using braces to define variables inside a switch block.
Of course the lifespan of the variable is limited to the code inside the braces. For code outside the variable is undefined.
Title: Re: Keep toolbar button presed
Post by: CFred on July 15, 2023, 06:00:50 PM
@frankie: Thanks for the info.
Title: Re: Keep toolbar button presed
Post by: John Z on July 15, 2023, 09:55:19 PM
Hi CFred,

Be sure consider that some windows specific variables need to be destroyed or released before you exit a procedure, or in this case before the closing } if using { } to define variable scope. 

John Z
Title: Re: Keep toolbar button presed
Post by: CFred on July 16, 2023, 03:39:37 PM
@JohnZ: Thanks for the tip.