|
I would like to display a text status in MFC main frame from anywhere from my code.
I am using standard MFC CStatusBar indicators
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_STATUS_00,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
and in main frame message map
ON_UPDATE_COMMAND_UI_RANGE(ID_STATUS_00,ID_STATUS_99, OnUpdateRange)
The processing function is defined , per MSDN
void CMainFrame::OnUpdateRange(CCmdUI *pCmdUI)
When I test post the last message in range
PostMessage(ID_STATUS_99);
the OnUpdateRange gets executed , however, I do not know how to retrieve the correct range message from CcmdUI.
Any help will be greatly appreciated.
Cheers Vaclav
|
|
|
|
|
pCmdUI->m_nID will give you the ID
|
|
|
|
|
That is what I thought ( found in documentation) , but it is always the ID of the first message in range.
What is odd - the ON_UPDATE_COMMAND_UI_RANGE function gets executed initially and so far I do not know why.
As long as this is how it suppose to work I'll just keep on debugging to find out what I missed.
Thanks for your help.
|
|
|
|
|
Vaclav_Sal wrote: PostMessage(ID_STATUS_99);
Should that not be
PostMessage (WM_COMMAND, ID_STATUS_99) ?
I presume that you are updating some sort of progress report, in which case I'd recommend just posting a registered window message to your main frame, and letting a message handler there deal with it, rather than handling 100 different messages.
ie:
PostMessage (CMainFrame::g_rwm_UpdateStatus, nProgress, nProgressMax);
CMainFrame
{
....
static UINT g_rwm_UpdateStatus;
...
UINT CMainFrame::g_rwm_UpdateStatus = ::RegisterWindowsMessage (_T("CMainFrame::g_rwm_UpdateStatus"));
...
ON_REGISTERED_MESSAGE(g_rwm_UpdateStatus, OnUpdateStatus)
...
and so on. You don't have to have the message number in the header - you can have many calls to RegisterWindowsMessage in many parts of your code, you just have to use the same string name each time. I use the class/member variable, but you could also use a GUID. Just make sure to comment for your future self.
In another note, when you're debugging Update handler, you can always walk up the call stack to see why you're getting parameters that you do.
Good luck,
Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
|
|
|
|
|
Thanks Iain.
It just bugged me, I kept thinking there got to be some “index” somewhere to keep track of the range.
So – I made three major mistakes -
I have no clue why I thought PostMessage(ID_whatever) would work.
Second mistake and third – I thought the the MFC will route the message thru main frame first.
So if I do PostMessage(WM_COMMAND, ID_STATUS_99) it works only when posted in main frame.
I'll admit – most of the time I just do not understand how MFC routes the messages!
In this “case” I have a Cview in splitter and maybe that is the “problem”.
I guess I need to look it up somewhere , again!
I was planning to have the actual text in the function which updates the status text.
Anyway – I'll take a look at registered message as you suggested.
Cheers
Vaclav
In the mean time I cheated this way
AfxGetMainWnd()->PostMessage(WM_COMMAND ,ID_STATUS_99);
but there is still a problem - the status update is called when anything is updated ( duh) and the text goes back to the first index in range.
Working on that. Perhaps static text variable will do.
-- modified 24-Jan-13 11:47am.
|
|
|
|
|
I would recommend against using WM_COMMAND for your status update message, and go for the registered window message:
1/ It saves command collision
2/ MFC does a LOT of work routing command messages about. This is one of its great strengths, and makes many things nice and elegant, but it can be confusing too.
3/ As long as you use the same string id for the message, you don't have to share header files, making code independence a bit cleaner.
Doing AfxGetMainWnd()->PostMessage is not cheating at all! Just change from WM_COMMAND and I'll be happy with you.
As you why the status bit is being reset? No idea, that will depend on the details of your code. I bet your command handler is being called from places you didn't expect. (See point 1 above).
Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
|
|
|
|