• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

Sending Windows messages to objects

Hi

I think I understand how to send messages in MFC.

However, suppose I want to send a message but I only one particular object to receive it or do something with it. How would I do that?

John
0
IssacJones
Asked:
IssacJones
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
IssacJonesAuthor Commented:
For example, if I had three objects A, B and C (all of the same type), how could I send a message but only B receives it?
0
 
IgorNedCommented:
Set the first parameter to the message target window:
::SendMessage(HWND hWnd, UINT ...
..or if you're using MFC:
yourObject.SendMessage(...
0
 
IgorNedCommented:
Using your example.
::SendMessage( B->GetSafeHwnd(), ...
or MFC way
B->SendMessage()
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Janusz CzopowikCommented:
You have to define object. Wht is it?

If your objects are not windows attached to MFC class’ object (derived from CWnd) or just Windows window having valid windows hadle, you cannot send a message.
However, you have ability to send a command message to any MFC object derived from CCmdTarget.
0
 
IssacJonesAuthor Commented:
I wasn't considering windows dervied objects initially. However, from what you say, it looks as if I just need to re-derive them from CCmdTarget?
0
 
sarabandeCommented:
the CCmdTarget is baseclass of all mfc classes that could handle messages but itself has no member PostMessage, SendMessage, ... .

instead it has virtual member function CCmdTarget::OnCmdMsg which is overridable but where the baseclass implementation does the lookup into the message maps which were used to call member functions of mfc derived classes.

Sara
0
 
Janusz CzopowikCommented:
Well, CWinApp does not have Send/Post message either.
As I have mentioned:
"you have ability to send a command message to any MFC object derived from CCmdTarget"
This was just a general remark.
I was not sure what OOP meant by objects. Once I know, I can elaborate.
The CCmdTarget derived class (as I mention CWinApp or CDocument for example) has own message map.
However since this class does not wrap windows handle, messages cannot be Send/Post directly. CWinApp receives command messages because this is a part of the command routing implemented by MFC.
If you want to use CCmdTarget ocjects, you would have to insert them into a command routing chain.
Using object’s owner (must be CWnd derived) and add following:

BOOL CSomeView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
	if(m_MsgReceiver.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
	{
		return TRUE;
	}

	return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

Open in new window


I have a question: why are you trying to communicate with your objects using message sending mechanism.
Did you consider simply calling public member functions implemented by the object?
0
 
IssacJonesAuthor Commented:
Hi John

I have indeed been using public member functions in the past. However, I'm learning MFC and C++ and it just occurred to me that sending messages to objects could be possible. Maybe I'm making life too complicated. I'm just curious I guess!

John
0
 
Janusz CzopowikCommented:
You are not making it too complicated. Everything depends on your project design.
If you want your object to be able to respond directly to user commands, then use CCmdTarget.
For example: CDocument is derived from CCmdTarget. Save command could be handled by a view. Since by design, document keeps all necessary data it is logical to communicate Save command directly to the document object.
If you do not need user interaction but need serialization, use CObject as a base class and call public members.
If you do not need any of the above, do not use MFC class as a base and call public members.

Everything depends on you to decise. If you want to learn more about MFC and C++ it is a good exercise.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now