Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Windows Message Handling

Windows Message Handling

Scheduled Pinned Locked Moved C#
csharpc++question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    ke5in
    wrote on last edited by
    #1

    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!

    L J 3 Replies Last reply
    0
    • K ke5in

      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!

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K ke5in

        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!

        J Offline
        J Offline
        James T Johnson
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K ke5in

          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!

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          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 members

          public 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
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups