Intercept WndProc (without interrupting it)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Intercept WndProc (without interrupting it)

    I'm trying to watch WndProc as it creates an application window. I want to see all the messages that it sends, but I don't want to interrupt the sending of these messages. This seems to be happening. For example, if I use this code:

    Code:
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m.Msg == 0x84)
                {
                    backgroundWorker1.RunWorkerAsync(m);
                    
                }
            }
    
            private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
            {
                Message m = (Message)e.Argument;
                ListViewItem lvi = new ListViewItem(m.HWnd.ToString());
                lvi.SubItems.Add(m.LParam.ToString());
                lvi.SubItems.Add(m.Msg.ToString());
                lvi.SubItems.Add(m.Result.ToString());
                lvi.SubItems.Add(m.WParam.ToString());
    
                e.Result = lvi;            
            }
    That just adds the message's contents to a listview so I can see it, but now it won't let me resize my form. It adds the message to the list, but apparently it doesn't send it to the program. Is there a way I can accomplish this?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    As I understood it, calling the base class WndProc allows it to be processed. I'm not sure why that's stopping it... does taking out the code where you call the background worker make it work again?

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      When I tried your code, I got an exception saying the background worker was too busy. Message 0x84 comes very frequently I guess.

      Maybe the list view stuff slows it down too much? I did the following and seemed to have no issues.

      Code:
              protected override void WndProc(ref Message m)
              {
                  base.WndProc(ref m);
      
                  if (m.Msg == 0x84)
                  {
                      string output = String.Format("{0} - {1}, {2}, {3}, {4}",
                          m.Msg.ToString("x"),
                          m.HWnd.ToString(),
                          m.LParam.ToString(),
                          m.Result.ToString(),
                          m.WParam.ToString());
      
                      Console.WriteLine(output);
                  }
              }

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        0x84 is only sent when you put your mouse in the sizing area of the form. Left, Right, Top, Bottom, Top-left, Top-right, Bottom-left, and Bottom-right. If your mouse never enters the form (or does so fast enough that it never _technically_ touches the border, then 0x84 is never sent.

        And I did remove the background worker and replaced it with just a regular method call and it started to work. Weird...

        It's probably because I never handled the RunWorkerComple te event, so the WndProc override never got the Message object back. Maybe...

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Ah, gotcha. Yea that makes sense now that you point it out...

          Yea I can't say, I don't know much about the BackgroundWorke r... I haven't had occasion to use it much.

          Comment

          Working...