How to use CMFCRibbonEdit-derived class
-
Sunday, August 12, 2012 6:10 PMNewbie question: I know I can derive a class from CMFCRibbonEdit and override some members. But how do I get the ribbon to use the derived class?
All Replies
-
Wednesday, August 15, 2012 10:47 AMModerator
Hi Johnsssl,
Sorry for delay.
You can create an instance of the derived class. For example, the derived class is named CMyRibbonEdit:
CMyRibbonEdit* pEditIndentLeft = new CMyRibbonEdit(ID_PAGELAYOUT_INDENT_LEFT, 72, _T("Left:\nil"), 13); // specify the min and max value of the spin button control pEditIndentLeft->EnableSpinButtons(0, 1000); // set the text of the edit control pEditIndentLeft->SetEditText(_T("0"));MSDN archive provides a sample of using ribbon control: http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=vcsamplesmfc&DownloadId=9816
You can get it as a reference.
Regards,
Damon
Damon Zheng [MSFT]
MSDN Community Support | Feedback to us
- Edited by Damon ZhengMicrosoft Contingent Staff, Moderator Wednesday, August 15, 2012 10:48 AM
- Edited by Damon ZhengMicrosoft Contingent Staff, Moderator Wednesday, August 15, 2012 10:49 AM
-
Sunday, August 19, 2012 4:16 AM
I guess my problem is that I am using the ribbon designer - the resource editor - rather than building the ribbon in code. So I get no chance to code something like:
pPanelWindow->Add(new CMFCMyRibbonEdit(ID_SOMETHING, _T("Something")));
I wonder if there is a way for me to continue to use the ribbon designer, but change just the few controls that I need my derived class for.
Does that make sense? Should I drop the ribbon designer?
-
Sunday, August 19, 2012 4:36 AMActually the real drag is that the ribbon designer's Spin Edit control gives me just about everything I need. I set up a COMMAND event handler, and it gets called at all the right places - when the spin buttons are pressed, and when focus leave the control. I can set the text of the control in the event handler. Easy. But, and this is a big but, I can't tell in the COMMAND event handler what particular event fired the handler. Was it a UDN_DELTAPOS notification, a WM_KILLFOCUS notification, or something else. The ON_NOTIFY handler would give me the notification code, but CMFCRibbonEdit notifications come through the ON_COMMAND handler, so I do not see how to get the notification code.
-
Sunday, August 19, 2012 7:04 PM
But, and this is a big but, I can't tell in the COMMAND event handler what particular event fired the handler.
I can answer this question: none. Ribbon control does not fire any events. It is window that only draws most of the controls, with the exception of a few. Since it is a window (MFC wrapper) it sends messages that are generated either base on hit test or relay messages from real windows controls hosted by the ribbon.
Anyway, there are not too many choices you have, since designers of the new MFC classes did not leave too much room for innovations by implementing certain functions as virtual to make our lives easier.
There is no function in CMFCRibbonEdit returning pointer or reference to the edit control and allowing for customization.
CMFCRibbonEdit class is not derived from CWnd, hence you cannot subclass it, since this is not a window. However, it hosts and controls rich edit window.
1 choice:
Override CMFCRibbonEdit and add to the ribbon from the code instead of ribbon editor.
Than you can subclass edit window and handle all messages in your class.2-nd choice would be to enumerate child windows after ribbon is created and subclass edit control.
3-rd choice (similar to the 2-nd) would be to create hook (WH_CBT) and subclass edit window when created.
Each one has pros and cons and depending on the level of control you want to gain.
Two last approaches give you most control, since you can handle all messages.
You can do the same with spinner control.
JohnCz
- Marked As Answer by Johnsssl Monday, August 20, 2012 3:43 AM
-
Wednesday, August 22, 2012 8:33 PMDon't know about etiquette here, but to JohnCz: Thank you so much. Works like a charm.
-
Wednesday, August 22, 2012 9:30 PMYou are very welcome, glad to be of help.
JohnCz Please consider voting if you find this post helpful.

