C language > Beginner questions

Keep toolbar button presed

(1/2) > >>

CFred:
I want to be able to programmatically press a toolbar button and keep it pressed. I am using the code


--- Code: ---SendMessage(hTool, TB_PRESSBUTTON, ID_Button5W, MAKELPARAM(TRUE, 0));
--- End code ---

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.

frankie:
You copy&paste the code and forget to change the assignment:

--- Code: ---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);

}

--- End code ---

Just for info, you can define variables inside a switch statement case, just you need to add braces to define a code block:

--- Code: ---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);

              ....
}

--- End code ---

More general I suggest to take more care of details in assignment and casting (don't overlook the warnings!).

CFred:
@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.

CFred:
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: ---    btnIndex = SendMessage(hTool, TB_ADDBITMAP, 8, (LPARAM)&tbab);
--- End code ---

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?



frankie:

--- Quote from: 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: ---    btnIndex = SendMessage(hTool, TB_ADDBITMAP, 8, (LPARAM)&tbab);
--- End code ---

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?

--- End quote ---
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: ---    btnIndex = (int)SendMessage(hTool, TB_ADDBITMAP, 8, (LPARAM)&tbab);
--- End code ---
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:

--- Quote from: CFred on July 15, 2023, 10:36:17 AM ---Also, thanks for the tip about using braces to define variables inside a switch block.

--- End quote ---
Of course the lifespan of the variable is limited to the code inside the braces. For code outside the variable is undefined.

Navigation

[0] Message Index

[#] Next page

Go to full version