Intercept Messages with MessageWindow?

Z

Zanna

Hi, I'm trying to intercept windows messages of a control, so I saw I have
to use a MessageWindow derived class.
I made this simple example, that I thinked would intercept all messages to
my form and changed Form1.Text into the message code.

Obviously it does nothing :(

Why?

' You can do a Copy-and-paste on a blank .vb file
'
' Form ---------------------------

Imports Microsoft.WindowsCE.Forms
Imports System.Windows.Forms

Public Class Form1
Inherits System.Windows.Forms.Form

Public Sub New()
MyBase.New()
InitializeComponent()
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub

Private Sub InitializeComponent()
Me.Text = "Form1"
End Sub

Private wm As myMW

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
wm = New myMW(DataGrid1)
End Sub

End Class

' Message Window Class---------------------------

Public Class myMW
Inherits MessageWindow

Private pc As Control

Public Sub New(ByVal c As Control)
pc = c
End Sub

Protected Overrides Sub WndProc(ByRef msg As Message)
pc.Text = msg.Msg.ToString
End Sub
End Class
 
A

Alex Feinman [MVP]

I really don't see how this could possibly intercept control's messages. If
you need to do something like that, take a look at ApplicationEx class in
OpenNETCF SDF
 
Z

Zanna

I really don't see how this could possibly intercept control's messages.

Me too :)

I just asked for some code :)
If
you need to do something like that, take a look at ApplicationEx class in
OpenNETCF SDF

I hope its code will be really simple to understand.

Thanks
 
Z

Zanna

I hope its code will be really simple to understand.

No... there is something wrong...

It seems that the ApplicationEx class do a replacement for the main loop so
it can process (and dispatch) each message.

This is a too drastic way in my opinion, also because I prefer to not
replace the System.Windows.Forms.Application class.

Really I haven't another way to trap messages?

Thaks angain!
 
P

Paul G. Tobey [eMVP]

You could install a keyboard hook to capture *only* the keyboard messages,
or write native code to subclass the control, but otherwise, no. The
ApplicationEx class is far and away the best you can do for this
application.

Paul T.
 
C

Chris Tacke, eMVP

I don't think it's too drastic. Implement a filter and ignore everything
but your keystrokes. It's by far easier than anything else we could
suggest.
 
W

William Ryan eMVP

Zanna:

You are passing the grid control to your messagewindow class. I'm not sure
what the ultimate goal is but I don't think you're going to get there with
the usage you posted. Also, from your usage, it appears that you are trying
to find out what message is being sent when something happens with/to the
grid. You can access most of this stuff directly, but assuming you are sure
you want to use the MessageWindow class...

In your form, you could change the constructor to something like...

Private _form as Form

Public Sub New(ByVal frm as Forms)
_form = frm
End Sub

Then in your WndProc you'll probably want to look for specific things
although in your case you mention you want to set the text no matter what
happens:

Protected Overrides Sub WndProc(ByRef msg As Message)
_form.MessageText = msg.ToString
MyBase.WndProc(msg)
End Sub


I'd actually set a property in your Form for MessageText
Private _messageText as String
Public Property MessageText() as String
Get
Return _messageText
End Get

Set(ByVal Value as String)

_messageText = value
'Instead of setting _messageText (or in addition to
' you could set the textbox's test to value
End Set

End Property
 

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

Similar Threads


Top