send/post a message to a window

M

Mark

In a standard unmanaged win32 application I can send/post a message to a
window as a communication mean.
Can it be down also in a dot net application?

Regards
Mark
 
P

Peter Duniho

Mark said:
In a standard unmanaged win32 application I can send/post a message to a
window as a communication mean.
Can it be down also in a dot net application?

Yes. A .NET Control-based object still has an underlying window that
receives messages and a window proc that handles these messages. Your
own code can override the WndProc method in the class if it has a need
to deal with custom messages.

That said, the kinds of things that may have been done using SendMessage
or PostMessage are often better done using some other mechanism in .NET.
Occasionally you may indeed want to use the "old-style" mechanisms
you're used to, but it would be a mistake to generally try to do
Win32-style programming in .NET (even if .NET is filled with various
behaviors it's inherited from the native Win32 API :) ). It's better to
think of .NET as a whole new platform, and focus on implementing
behaviors in a way that use the .NET mechanisms.

Pete
 
M

MArk

Thanks,
Following your advise, can you indicate what is the similar mechanism for
sending Windows messages in dot net?

Regards

Mark
 
P

Peter Duniho

MArk said:
Following your advise, can you indicate what is the similar mechanism for
sending Windows messages in dot net?

That depends on what you really want to do. If you literally want to
send a message, you will have to use the regular Win32 Send/PostMessage
mechanism.

My point is that for the kinds of things that messages are often used
for, you just don't do that any more. For example, you might have used
something messages like EM_SETSEL and EM_REPLACESEL to modify the text
in an edit control. In .NET, the TextBox control has instance methods
and properties to do work like that (for example, the Select() method to
change the selection and the SelectedText property to change the text in
the selection).

You mention using messages "as a communication means", but that's not
very descriptive. In a certain sense, ALL window messages are "a
communication means". So saying that's what you're using messages for
is pretty vague.

If you have some specific "communications" that you are trying to
accomplish, there is likely in .NET a better way to do it that sending a
window message. But absent some specific information about what you're
really trying to accomplish, it's not really possible to answer the
question.

Pete
 
B

Ben Voigt [C++ MVP]

MArk said:
Thanks,
Following your advise, can you indicate what is the similar mechanism for
sending Windows messages in dot net?

SendMessage becomes Control.Invoke(Delegate)
PostMessage becomes Control.BeginInvoke(Delegate)
 
P

Peter Duniho

Ben said:
SendMessage becomes Control.Invoke(Delegate)
PostMessage becomes Control.BeginInvoke(Delegate)

That's only partly true.

Many examples of where you might use SendMessage in particular are
simply replaced by calling a method directly. You'd only use Invoke()
in situations when you'd have used a cross-thread SendMessage() in Win32.

IMHO, PostMessage() has a much closer correlation to BeginInvoke(), but
even there I wouldn't say there's a 1-to-1 correlation. At least some
of the things one might have used PostMessage() for are, again, better
handled just using normal .NET calls.

Pete
 
B

Ben Voigt [C++ MVP]

Peter Duniho said:
That's only partly true.

Many examples of where you might use SendMessage in particular are simply
replaced by calling a method directly. You'd only use Invoke() in
situations when you'd have used a cross-thread SendMessage() in Win32.

IMHO, PostMessage() has a much closer correlation to BeginInvoke(), but
even there I wouldn't say there's a 1-to-1 correlation. At least some of
the things one might have used PostMessage() for are, again, better
handled just using normal .NET calls.

The original post asked about SendMessage and PostMessage as a means of
communication. Which to my small little brain means "between threads",
because I'd have used a direct function call in native code as well if that
was what I wanted.
 
P

Peter Duniho

Ben said:
The original post asked about SendMessage and PostMessage as a means of
communication. Which to my small little brain means "between threads",
because I'd have used a direct function call in native code as well if that
was what I wanted.

I don't disagree with that application. But I do think that's an overly
narrow interpretation of "communication". As I mentioned to the OP, the
problem description, such as it was, was very vague and it's very
difficult to answer vague questions like that in a really useful way.

But I would not limit "communication" to be inter-thread communication.
I would say that sending messages like WM_SETTEXT, EM_REPLACESEL, etc.
are forms of communication, and they often occur in a single thread.
Likewise, something like WSAASyncSelect() that uses window messages to
handle Winsock communications; again, usually all in the same thread.

I also think that while direct function calls are best where possible,
there are viable means of inter-thread communication that don't use
Send/Post/Invoke/etc. as the mechanism.

I have tried to encourage the OP to clarify what it is exactly he's
trying to replicate in .NET. With better details, it's a lot easier to
provide a useful reply. And it may well be that using Control.Invoke()
or Control.BeginInvoke() are in fact the things he's looking for. But
those apply to very specific needs, and it could just as easily be that
he doesn't need those at all.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top