User message
-
My existing code is below. A global function which is not a member of any class is calling a function which is a member of class CNewDlg. This code works, but I don't like it. I'm using it because it's the best I have been able to do. I think it can be done better using a user message, but I don't know how user messages work and haven't been able to find any examples. How would I code this using a user message?
void WINAPI HostChange(LinkPtr data)
<---global; not in any class. A callback provides ptr data.{ ((CNewDlg*)AfxGetApp()->m_pMainWnd)->LogonChange(data); } void CNewDlg::LogonChange(LinkPtr data)
<---function in a dialog class{ if(data->SomeField == 3){Do something...} }
-
My existing code is below. A global function which is not a member of any class is calling a function which is a member of class CNewDlg. This code works, but I don't like it. I'm using it because it's the best I have been able to do. I think it can be done better using a user message, but I don't know how user messages work and haven't been able to find any examples. How would I code this using a user message?
void WINAPI HostChange(LinkPtr data)
<---global; not in any class. A callback provides ptr data.{ ((CNewDlg*)AfxGetApp()->m_pMainWnd)->LogonChange(data); } void CNewDlg::LogonChange(LinkPtr data)
<---function in a dialog class{ if(data->SomeField == 3){Do something...} }
Oliver123 wrote:
This code works, but I don't like it.
I'm with you on this! :) Here's an ultra-easy alternative, which while not perfect, has its advantages.
-
Add 2 buttons (
IDC_HOST_CHANGE
andIDC_LOGON_CHANGE
) toCNewDlg
and make them invisible. -
Write a handler for each - the one for
IDC_HOST_CHANGE
should callHostChange()
and the one forIDC_LOGON_CHANGE
should callLogonChange()
. -
HostChange()
andLogonChange()
should get a pointer to an (perhaps the) instance ofLinkPtr
via a singleton access method exposed by your app class. -
Your static callbacks should post the appropriate message to your app's
AfxGetMainWnd()
. No casting toCNewDlg
is required. For example:AfxGetMainWnd()->PostMessage (WM_COMMAND, IDC_HOST_CHANGE);
In lieu of a registered message, this provides a clean separation between event generation by your application's innards and event handling by your application's user interface. /ravi
This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com
-
-
My existing code is below. A global function which is not a member of any class is calling a function which is a member of class CNewDlg. This code works, but I don't like it. I'm using it because it's the best I have been able to do. I think it can be done better using a user message, but I don't know how user messages work and haven't been able to find any examples. How would I code this using a user message?
void WINAPI HostChange(LinkPtr data)
<---global; not in any class. A callback provides ptr data.{ ((CNewDlg*)AfxGetApp()->m_pMainWnd)->LogonChange(data); } void CNewDlg::LogonChange(LinkPtr data)
<---function in a dialog class{ if(data->SomeField == 3){Do something...} }
Here's a quick, general introduction to user-defined messages. I'm assuming you're using MFC here. 1. Define a message identifier. You can base it off of
WM_USER
orWM_APP
:#define WM_MY_MESSAGE (WM_APP + 1)
2. Declare a message handler in your dialog class:
afx_msg LRESULT OnMyMessage(WPARAM wParam,LPARAM lParam);
3. Add an entry to the message map for your dialog class:
BEGIN_MESSAGE_MAP(...)
// ...
ON_MESSAGE(WM_MY_MESSAGE,OnMyMessage)
// ...
END_MESSAGE_MAP()4. Define your message handler:
LRESULT MyDialogClass::OnMyMessage(WPARAM wParam,LPARAM lParam)
{
// ...
}5. Finally, send the message when needed:
my_dialog->SendMessage(WM_MY_MESSAGE,
(WPARAM)first_parameter,
(LPARAM)second_parameter);where
first_parameter
andsecond_parameter
are whatever values you want to send with the message.
Software Zen:
delete this;