How can I "unclick" a button in C++ Builder 6 - c++

I have a button in C++ Builder 6 that I need to activate (and stay activated). But when I don't need it anymore, I want to click again on that button, and it goes back to the UP state.
Thank you for your help.

if you're working with VCL controls I think you can use a TSpeedButton and implement the toggle logic with its TSpeedButton.Down property. It stays pressed while Down is true and unpressed when Down is false. Check this
there are these properties for this:
TSpeedButton::Down indicates clicked button (you can also programaticaly set it to true/false on runtime)
TSpeedButton::AllowUp enable unclicking on second click
TSpeedButton::GroupIndex if non zero all buttons with the same index are goruped together and only one can be down at time so when you click on one all others are unclicked.
So for single button set AllowUp=true And GroupIndex to unique non zero number and for multiple buttons just set GroupIndex to the same unique non zero number for all buttons.
Cheers!

Related

MFC DDX_Radio causes debug assertion failure when DoDataExchange is called (dlgdata.cpp Line 286)

I have a dialog with radio button with groups of 4 buttons.
DDX_Radio(pDX, D_RADIO_GROUPLEAD, intToStore) in DoDataExchange is there for saving and loading.
It works perfectly fine.
Selection changed, DoDataExchange called, stores and loads data no problem.
Problem occurs when I hide one of the radio button (hide it via ShowWindow(SW_HIDE). Let's just call it 3rd button for reference. And previously, the selection was this 3rd button.
I have logic that will automatically select the default one (one with Group flagged as true in the editor). I call the button's SetCheck(1).
Visually everything seems to be working.
However, when I click on 2nd button, then try to call DoDataExchange (hence DDX_Radio), it will causes debug assertion failure. (dlgdata.cpp Line 286)
AND
the data is not properly populated back in intToStore.
Why could this be and how can I avoid this issue?
Thank you.
The problem is, that the auto radio button stuff in Windows skips buttons that are disabled. In detail. You click on button 2 while button 3 is selected and disabled. Button 2 gets selected but button 3 is not unchecked.
The next problem occurs when DoDataExchange runs. It doesn't check if a button is enabled or disabled. DDX_Radio just loops over all radio buttons, and it find 2 buttons in the group are enabled. This causes the ASSERT. DDX_Radio don't care if a button is enabled or disabled.
My advice: Use a custom OnClick handle by yourself, and disable all other buttons manually.

MFC radio button group message handler

I've got two little questions at Visual Studio MFC-GUI programming technique regarding the handling of a group of radio buttons in VS2015 CE.
I have a little dialog based application. This application draws some lines on my dialog with a specified pen.
Now I made a group of radio buttons to be able do use different colors for the pen. So I created a group box, placed the radio buttons into this group box, enabled the group property of the first radio button and checked the tab sequence so that all of the radio buttons are properly in sequence.
That's ok - testing the GUI I'm able to select just one of the radio button as planned, because I wouldn't be able to draw a line in to different colors at the same time. Now I added an handler for the BN_CLICKED Message of the first radio button object in the ClassWizard to add the selection of different colored pens.
Now comes the interesting part. This handler is executed only when i click the first radio button. The one with group property enabled. I thought it should be executed whenever I click any of the radio buttons in this group. The next thing I tried was to add an BN_CLICKED-Messagehandler to all of those radio buttons, but the ClassWizard does not shows the BN_CLICKED Message for the other radio buttons - only for the one with group proerty enabled.
After googling around I read a post that one would be able to add an BN_CLICKED-Messagehandler for an button by simply double clicking it in the Dialog. So I double clicked all of my radio buttons and added an invocation of the BN_CLICKED-Messagehandler of the first radio button. That's working as a workaround so that now my line is drawn in the different colors whenever I click on any of those radio buttons (black, blue, red , and so on).
I have two questions - is this behaviour intended? I suppose the reason why I'm only able to add a BN_CLICKED-Messagehandler in the ClassWizard for the first radio button is, that it would be invoked by any of the radio buttons in my group, wouldn't it?
Another question is, I would have preferred to write the handler once and be able to tell the class wizard to use this particular handler instead of creating a unique handler for each of the buttons. That's independent of this radio buttons. I tested this with a simple application which consists of an assembly of several buttons - which can have their own BN_CLICKED-Messagehandlers attached by the class wizard. When I didn't accept the automatically generated functionname from the wizard and enter the name of my own handlerfunction it simply states that it wouldn't be able to overwrite it. How can I select an existing handler for an object message in the class wizard or is that not possible and I have to do this manually in code for myself?
Hope there's an VS2015 MFC Guy out there be able to tell me ..
This is indeed by design. You could have wanted different behaviour for each button click - if you don;t simply associate all of the BN_CLICKED to the same message handler and delete the ones that were automatically generated.
You can also use ON_CONTROL_RANGE(BN_CLICKED, id1, id2, memberFxn ) - make sure your buttons are in the same range in your resource.h
Hope that helps!

How do I add a Checkbox to a tool bar in MFC with a custom bitmap?

I have a C++ MFC MDI application. I have a tool bar with some buttons on it. I need to add some check boxes to this toolbar and i need them to have custom bitmaps just as my buttons do. Thanks
EDIT:
By bitmpas i refer to the pixel images that can be created using the tool bar editor in visual stuidos 2008. I would like a picture (of my creation) instead of the usual tick box.
You don't use checkboxes on toolbars.
You should rather use regular buttons in Check mode. That means that the button stays pressed when user releases it. Clicking it a second time releases the button. This is the same behaviour as a checkbox.
You can either set a toolbar button as checkable by code:
m_ToolBar.SetButtonStyle(nButtonId, TBBS_CHECKBOX);
Or by enabling the corresponding property in the resource editor.
If you want to modify the image displayed when the button is pressed, in your ON_UPDATE_COMMAND_UI handler, use m_ToolBar.GetButtonInfo() to check if the image matches the state. If not, change it using m_ToolBar.SetButtonInfo() and specify the index of an extra image added to the image list of the toolbar.
The following is a link which might help you
http://www.ucancode.net/Visual_C_Control/Place-Combo-Edit-Box-Progress-Control-On-ToolBar-CToolBar-VC-Example.htm

how to make slew key in mfc

i have a dialog box in which a edit control and a button is presentfuncionality is that when i cliked on button edit control changes values from 0 to 30.for single click functionality is working fine but when i hold that button then it should quickly go on increasing 0 to 30 but its not going that way.it not taking left click down event.how to achecve this functionality
Try Spin control, it should work as you expect.
Edit: maybe you can use AutoRepeat button: http://www.codeproject.com/KB/buttons/autorepeat.aspx

"Sticky" MFC popup menu

I currently have some toolbar buttons with a small arrow on the side (TBSTYLE_EX_DRAWDDARROWS) that, when clicked, result in a popup context menu being displayed under the button. This is done by constructing a custom popup menu and calling TrackPopupMenu.
The client now wants to be able to select multiple options from the menu before it closes, so that multiple options can be be modified without the need to re-open the menu and wait for an intermediate redraw between each change.
For example:
User clicks dropdown button
Dropdown menu appears (modal, waits indefinitely for user action)
User clicks some item (e.g., toggle a checkmark)
Timer (e.g., 500ms) starts
If timer expires, the menu is closed and all selected actions are executed.
User clicks another item before the timer expires, go back to 4.
The best I can come up with is to redisplay the menu by calling TrackPopupMenu multiple times. This makes the menu "flicker" when you select an item, and will probably require me to start a thread in order to do the timeouts, which I would rather avoid.
Rather than a menu, put up a dialog box with the options on it. A dialog can easily do all that is required.
A menu that doesn't close when you click it will just seem wrong. A dialog that closes by itself will seem wrong too, but it's probably the least of two evils.
Edit: If there's anything I've learned with Microsoft, it's don't try to fight the default behavior. You're asking for trouble if you do.
If you're building your menu dynamically I can see how automatic sizing can be handy, but it's not hard to do in a dialog either - make the dialog really big and before it becomes visible, enumerate the children and take a union of all their rectangles, then resize to that. Checking the boundaries to make sure they're on-screen is just a few if statements with OffsetRect. Checkboxes are trivial; icons less so, but still not bad.
One additional enhancement that would be easy to add is to dismiss the dialog immediately on a double-click.
Following #Mark Ransom's answer, you should put up a dialog box. But you can make the dialog modeless and make it close itself when you click outside of it (i.e., the dialog loses focus). That way it could behave more like a menu.
Notice that normal menus never go away by themselves, you always have to click somewhere outside the menu (or one of its options) to make it disappear.