Two-state button that mimics a checkbox in MFC - c++

I want to make a button on an MFC tool bar that can be pressed and retain it's state until pressed again (like a check box) but I don't want a tick box. I'd ideally like a button that changes between two icons and uses DDV to change the value of a boolean on being pressed. How would I go about this.
As a side question I could really do with a good book on MFC. I've got Programming Windows with MFC 2nd edition, but was wondering if there was anything that went into a bit more detail?

Use ON_UPDATE_COMMAND_UI to set the state of the button. e.g. a typical handler will look like this:
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_UPDATE_COMMAND_UI(ID_MY_BUTTON, OnUpdateMyButton)
END_MESSAGE_MAP()
...
void CMyView::OnUpdateMyButton(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck( isButtonPressed() );
}
With regard to good MFC documentation I recommend Professional MFC (Mike Blaszczak) - it's a little old but very good.

From the documentation of the CToolBar Class:
To create a check-box button, assign it the style TBBS_CHECKBOX or use a CCmdUI object's SetCheck member function in an ON_UPDATE_COMMAND_UI handler.

Draw a Radio Button on Dialog box.
Select Radio button & go to button Properties page.
Set Push Like property & set as "True"
Add event handler for the same control & take Boolean variable.
Use API -> "CheckDlgButton(IDC_RADIO1, 1);" for set status as per Boolean variable value.
for more details. visit below link.
https://www.codeproject.com/Articles/530/A-Better-Bitmap-Button-Class

Related

Embedding dialogs in main dialog and switching them with button click in MFC

I have a design like below:
So basically, I want to embed three dialogs in the application main dialog and switch between them, for each button click i.e., button 1 will show dialog one , button 2 will hide dialog 1 and show dialog 2 .. and so on.
Each dialog will be having a different design and functions.
I tried using CPropertySheet class to Add pages but its GUI is different. It has either option for navigating the dialogs using next / back button , or from a tab control.
None of which is as per my requirement.
So I want to know is it possible to have a design like this in MFC ? If yes how? Which Class/ control should I use.
Any help will be appreciated.
What you can do is use a normal CDialog class, add your buttons to it and also create a frame/rect as a placeholder for where your embedded dialogs are to appear. The following piece of code will create and position your embedded dialog.
CRect rect;
CWnd *pHost = GetDlgItem(ID_OF_YOUR_FRAME_RECT);
pHost->GetWindowRect(&rect);
ScreenToClient(&rect);
pDialog->Create(ID_OF_YOUR_DIALOG, this);
pDialog->MoveWindow(&rect);
pDialog->ShowWindow(SW_SHOW);
On button clicks, you hide the previously shown dialog (SW_HIDE) and show your selected dialog(SW_SHOW) with ShowWindow(...).
If you create your embedded dialogs with IDD_FORMVIEW style in the add resource editor it'll have the proper styles for embedding.
Another option is probably to use an embedded PropertySheet and hide the tab row and programatically change the tabs on the button clicks. I just find it to be too much fuzz with borders, positioning, validation and such for my liking.
If you have the MFC Feature Pack, that first came with VS2008 SP1 and is in all later versions, you might like to consider CMFCPropertySheet. There are a number of examples on the linked page, that are very similar to your design.
For example, this:
What worked for me just using dialog based application is SetParent() method. Dont know why nobody mentioned it. It seems to work fine.
I am doing like below:
VERIFY(pDlg1.Create(PanelDlg::IDD, this));
VERIFY(pDlg2.Create(PanelDlg2::IDD, this));
VERIFY(pDlg3.Create(PanelDlg2::IDD, this));
::SetParent(pDlg1.GetSafeHwnd(), this->m_hWnd);
::SetParent(pDlg2.GetSafeHwnd(), this->m_hWnd);
::SetParent(pDlg3.GetSafeHwnd(), this->m_hWnd);
Now I can show or hide a child dialog at will (button clicks) as below:
pDlg1.ShowWindow(SW_SHOW);
pDlg2.ShowWindow(SW_HIDE);
pDlg3.ShowWindow(SW_HIDE);

OnLButtonDown() is not fired if I click an item in my dialog (ListBox, CheckBox etc) but it fires ok if I click background or Static Text

If I click anywhere on the dialog window, like in the "background", or on a Static Text, the function OnLButtonDown() is fired. But if I click a ListBox/EditBox/Combo/Calendar/Checkbox etc is not fired.
I thought that because these have control variables atached to it, and the Static Text don't. But adding a new Listbox to my Dialog and testing, I see that it doesn't fire either, so now I am confused...
I added OnLButtonDown() with the Class Wizard, it appears in:
BEGIN_MESSAGE_MAP(CMFCTesting2Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
// other handlers, etc
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
My function:
void CMFCTesting2Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
AfxMessageBox(CString("BUTTON DOWN"));
CDialogEx::OnLButtonDown(nFlags, point);
}
I tried calling CDialogEx:: ... before AfxMessageBox... but same result.
The CListBox item, has the Notify option set to True, as I seen some advices in other posts.
I suspect that the WM notification message is captured somehow by the ListBox, and not sent "forward" to my CMFCTesting2Dlg, but I understood that this should happen with the Notify option set to True, on the ListBox, no? (well, obviously, not...)
Sorry, I am new to dealing with WM in MFC.
BTW, I use Visual Studio 2010 Ultimate, and this is a Visual C++ - MFC- MFC Application - Dialog Based project.
How can I capture this mouse down event if clicked on a listbox / combo / etc?
On the LONG STORY, I am actually trying to accomplish this issue:
I have two listboxes (lets say), and I want to scroll them synchronously, when user scrolls one of them, others must scroll the same (or update in the next moment). And I thought of using on mouse down to track the position of every Listbox (with the GetTopIndex), and on mouse up, to GetTopIndex again and compare with the previous ones. If a change was made, then a listbox was scrolled and then update all listboxes with SetTopIndex. The unfriendly solution for the user, but simpler for me, would be clicking a button that does this verification / update, but its not elegant at all, and it can only set them at the same level, but can't determine which one was scrolled last time. This automatically scrolling of the listboxes should be made just for displaying, it is not important the selections in the listboxes. I tried using the Message Type in the Add Event Handler for the Listbox, but none that are displayed work for my problem, KillFocus and SetFocus, are not fired if the scroll-bar is dragged, only if an item in the listbox is clicked... and I didn't succeed on the others message type either, from the Add Event Handler.
Once a window handles a message, it stops being sent to the other windows beneath it. A static window doesn't handle a mouse down, so it goes to the dialog. The other controls all handle it in some fashion (setting focus at least) so it never gets to the dialog.
You can override the OnLButtonDown handler in each of the controls to do something else once they've finished their default handling in the base class. You might also get to see the message in the PreTranslateMessage method of the dialog.
As far as getting the List Controls to sync scrolling goes, your best bet would be to intercept WM_VSCROLL by subclassing CListCtrl or perhaps by PreTranslateMessage, and then calling SetScrollInfo on the other list. If the number of items in the lists are the same, it should be fairly simple.

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

c++ win32 create a text only button

I would like to add a simple text button to my c++ win32 application. I'm creating the button using CreateWindowEx function, but can't figure out the correct style to do so. I would like to display a text only button and be able to recive messages when the user clicks on it. The style i would like to get is identical to the text button in windows 7 system volume control (where it says "Mixer"). If possible i would like to display a tooltip also.
That mixer control looks more like a hyperlink control than a button. I'd go for the SysLink control if that's what you need.
You could create a "Button" class window with the BS_OWNERDRAW style and handle the WM_DRAWITEM messages. In your WM_DRAWITEM message handler you can simply display the text.
Actually that button is an owner draw button - it listens to mouse move messages and when you hover over it, it underlines the text (the syslink control doesn't have this behavior). Otherwise it's a stock button.

How to right button drag with Win32 API?

I implemented the IDropTarget interface and the drag & drop (file from explorer) works well.
When I drag & drop a file with right mouse button, the context menu does not popup.
So I think it's my responsibility to show the context menu,
But in IDropTarget::Drop method, (grfKeyState & MK_RBUTTON) is always 0.
How can I know if the drag mouse button was right?
And is there a special method to show menu and receive the selection after the drop?
(I prefer the Win32 API way, I don't use MFC.)
I guess (grfKeyState & MK_RBUTTON) is always 0 because when the Drop() method is called, the mouse button is already released (you've just made the drop) - so naturally the keyboard/mouse state flags would indicate that it is indeed released.
What you can (and should, according to the documentation) do, is to decide ahead the course of action you take on Drop() when you're handling DropEnter(). I haven't tried it myself, but I'm quite sure that the MK_RBUTTON flag should be set there. You can check that flag when your drop target's DropEnter() method is being called, save it and then open the context menu yourself when Drop() is called. This is not the cleanest solution, but I don't know if there's a way to make Windows show a context menu for you.