Wednesday, April 18, 2007

VC++.NET SKILL : Message Filtering

Currently, I' looking for How to message capture.
I found two ways.

First overriding an WndProc.
But, I also found that WndProc can't distinguish "WM_KEYUP from WM_LBUTTONUP.
Now I'm under investigation.

Second, using an IMessageFilter.
This interface allows an application to capture a message before it is dispatched to a control or form.
(reference page : http://msdn2.microsoft.com/en-us/library/system.windows.forms.imessagefilter.aspx)

Now, I'll show how to..

//Declare a class dreeived from IMessageFilter

ref class TestMessageFilter: public IMessageFilter

{

public:

[SecurityPermission(SecurityAction::LinkDemand, Flags =


SecurityPermissionFlag::UnmanagedCode)]

virtual bool PreFilterMessage( Message % m )


{


if ( m.Msg ==0x100 ) //WM_KEYDOWN

{

Int32 key = m.WParam.ToInt32();

KeysConverter tc; //Converter object that converting Integer keyvalue to string.

tc.ConvertToString(key);
return true;

}

else return false;

}

.....................................

TestMessageFilter^ MsgFilter; //Declare as class's member variable

sgFilter = gcnew TestMessageFilter();

Application::AddMessageFilter(MsgFilter); //Add Message Filter

No comments:

Powered By Blogger