Windows Message Handling
-
Consider this C++ code: //Thread 1 HWND target = ::SendMessage(hWnd, UWM_HERE_I_AM, (WPARAM)m_hWnd); //Thread 2 LRESULT CTheOtherApp::OnHereIAm(WPARAM wParam, LPARAM) { other = new CWnd; other.Attach((HWND)wParam); return (LRESULT)m_hWnd; } Q1: How would I "register" the OnHereIam handler in a C# Program? (I read about AddMessageFilter in MSDN but its rather vague to me.) Q2: What would the "return" statement look like? I mean how do you "cast" LRESULT in C#? Q3: I guess if there is a way to register the OnHereIam message handler I won’t need to write a custom WndProc. But if I have too write one how is this usually done? Thanks!
-
Consider this C++ code: //Thread 1 HWND target = ::SendMessage(hWnd, UWM_HERE_I_AM, (WPARAM)m_hWnd); //Thread 2 LRESULT CTheOtherApp::OnHereIAm(WPARAM wParam, LPARAM) { other = new CWnd; other.Attach((HWND)wParam); return (LRESULT)m_hWnd; } Q1: How would I "register" the OnHereIam handler in a C# Program? (I read about AddMessageFilter in MSDN but its rather vague to me.) Q2: What would the "return" statement look like? I mean how do you "cast" LRESULT in C#? Q3: I guess if there is a way to register the OnHereIam message handler I won’t need to write a custom WndProc. But if I have too write one how is this usually done? Thanks!
Hi , its 6:40 am here and I didnt just wake up :) Look here , should answer a couple q's. X| http://www.syncfusion.com/FAQ/WinForms/default.asp[^] MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D
-
Consider this C++ code: //Thread 1 HWND target = ::SendMessage(hWnd, UWM_HERE_I_AM, (WPARAM)m_hWnd); //Thread 2 LRESULT CTheOtherApp::OnHereIAm(WPARAM wParam, LPARAM) { other = new CWnd; other.Attach((HWND)wParam); return (LRESULT)m_hWnd; } Q1: How would I "register" the OnHereIam handler in a C# Program? (I read about AddMessageFilter in MSDN but its rather vague to me.) Q2: What would the "return" statement look like? I mean how do you "cast" LRESULT in C#? Q3: I guess if there is a way to register the OnHereIam message handler I won’t need to write a custom WndProc. But if I have too write one how is this usually done? Thanks!
ke5in wrote: Q1: How would I "register" the OnHereIam handler in a C# Program? (I read about AddMessageFilter in MSDN but its rather vague to me.) You are better off overriding WndProc for this. AddMessageFilter should be the last option because of the overhead involved of calling the added message filters for *every* message passed to the UI thread, where-as WndProc only gets called for messages passed to that particular window. The On* methods are called by the WndProc, not through any sort of magic but by putting something such as a switch statement in the WndProc and test for each message then calling the appropriate On* method. ke5in wrote: Q2: What would the "return" statement look like? I mean how do you "cast" LRESULT in C#? The return type should be IntPtr. ke5in wrote: Q3: I guess if there is a way to register the OnHereIam message handler I won’t need to write a custom WndProc. But if I have too write one how is this usually done?
protected override void WndProc( ref Message m )
{
const int UWM_HERE_I_AM = 0x1234;switch( m.Msg )
{
case UWM_HERE_I_AM:
m.Result = OnHereIAm( m.WParam, m.LParam );
return;
}base.WndProc( ref m );
}Normally you would follow the event architecture that .NET already provides (event, delegate, and protected On* method for inheritors to override). HTH, James "And we are all men; apart from the females." - Colin Davies
-
Consider this C++ code: //Thread 1 HWND target = ::SendMessage(hWnd, UWM_HERE_I_AM, (WPARAM)m_hWnd); //Thread 2 LRESULT CTheOtherApp::OnHereIAm(WPARAM wParam, LPARAM) { other = new CWnd; other.Attach((HWND)wParam); return (LRESULT)m_hWnd; } Q1: How would I "register" the OnHereIam handler in a C# Program? (I read about AddMessageFilter in MSDN but its rather vague to me.) Q2: What would the "return" statement look like? I mean how do you "cast" LRESULT in C#? Q3: I guess if there is a way to register the OnHereIam message handler I won’t need to write a custom WndProc. But if I have too write one how is this usually done? Thanks!
I tried emailing this but just got a bounce-back in return: > In C# I can create a class for a thread and get it running. > But the thing that has me confused is the “event” which > occurs in the application. How do I notify the thread that > this “event” has occurred using “the event architecture that > .NET already provides”? Here is how I would set it up (ignore capitalization, I'm typing in outlook so it likes to 'correct' me). There are two different ways of doing this, the first is used if its just a notification that something happened ("I was clicked", "I finished processing the task you already know about", etc), the second is used if you need to supply some data to the listener. First the notification method:
class MyThread : ....
{
// other memberspublic event EventHandler MyEvent;
protected virtual void OnMyEvent(EventArgs e)
{
FireMyEvent(e);
}private void FireMyEvent(EventArgs e)
{
if( MyEvent != null )
{
MyEvent(this, e);
}
}private void SomeCode()
{
SomeLengthyProcessing();SomeMoreProcessing(); // MyEvent occurred let everyone know OnMyEvent(EventArgs.Empty); EvenMoreProcessing();
}
}Now in the code that needs to receive that event:
.....
MyThread mt = new MyThread();// attach event listner
mt.MyEvent += new EventHandler(myMethodToExecuteWhenMyEventHappens);
.....private void myMethodToExecuteWhenMyEventHappens(object sender, EventArgs e) {
// Code to execute when MyEvent occurs
}In the cases where you have data to be returned you use the same pattern, but the type names change a bit
class MyEventEventArgs : EventArgs
{
// data members
private int myValue;public MyEventEventArgs( /* arguments */ int myValue )
{
// set data members
this.myValue = myValue;
}public int MyValue
{
get { return myValue; }
}
}class MyThread : ....
{
// other members
public delegate void MyEventEventHandler(object sender, MyEventEventArgs e);public event MyEventEventHandler MyEvent;
protected virtual void OnMyEvent(MyEventEventArgs e)
{
FireMyEvent(e);
}private void FireMyEvent(MyEventEventArgs e)
{
if( MyEvent != null )
{
MyEvent(this, e);
}
}private void SomeCode()
{
SomeLengthyProcessing();SomeMoreProcessing(); // MyEvent occurred let everyone know MyEventEventArgs e = n