NO

Author Topic: Keep toolbar button presed  (Read 3597 times)

Offline CFred

  • Member
  • *
  • Posts: 36
Keep toolbar button presed
« 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.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Keep toolbar button presed
« Reply #1 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!).
« Last Edit: July 14, 2023, 09:35:00 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline CFred

  • Member
  • *
  • Posts: 36
Re: Keep toolbar button presed
« Reply #2 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.

Offline CFred

  • Member
  • *
  • Posts: 36
Re: Keep toolbar button presed
« Reply #3 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?



« Last Edit: July 15, 2023, 12:24:16 PM by CFred »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Keep toolbar button presed
« Reply #4 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.
« Last Edit: July 15, 2023, 03:06:38 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline CFred

  • Member
  • *
  • Posts: 36
Re: Keep toolbar button presed
« Reply #5 on: July 15, 2023, 06:00:50 PM »
@frankie: Thanks for the info.

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Keep toolbar button presed
« Reply #6 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

Offline CFred

  • Member
  • *
  • Posts: 36
Re: Keep toolbar button presed
« Reply #7 on: July 16, 2023, 03:39:37 PM »
@JohnZ: Thanks for the tip.