Windows Develop Bookmark and Share   
 index > Windows Forms General > Issues with WndProc and SendMessage
 

Issues with WndProc and SendMessage

Hi everyone,

We are having issues with getting a "paste" event being sent up a hierarchy to the parent form.

Using WinXP Sp3,VS 2005 Professional, and .NET 2.0 for development.

We have an application that comprised of multiple solution files which we've inherited. Solution #2 houses all our custom controls. When built, the dll is used as a reference in Solution #4 so those controls can be placed on our multiple forms.

We have a special DoPaste Method in solution #4 which we call whenever a "paste" event happens from a form within Solution #4. This functions perfectly whenever we select "Edit | Paste" from the menu, or use CTRL-V (we just call the menu's paste event).

However, there's also a context menu which allows a user to right-click and paste text into the custom controls. Unfortunately, it's pasting "blanks".

What we want to do is when a user right-clicks to paste, have that bubble up to the forms in Solution #4.

In the custom textbox control in Solution #2, we have the following code:

private const int WM_PASTE = 0x0302;
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int SendMessage(
        int hWnd, // handle to destination window
        uint Msg, // message
        int wParam, // first message parameter
        int lParam // second message parameter
        );

        /// <summary>
        /// Override WndProc Method : Capture pasting
        /// </summary>
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_PASTE:
                    {
                        SendMessage((int)Parent.Handle, Convert.ToUInt32(WM_PASTE), WM_PASTE, WM_PASTE);
                        //Have also tried sending to this.Handle instead of Parent.Handle
                        SendMessage((int)Parent.Handle, Convert.ToUInt32(WM_PASTE), WM_PASTE, WM_PASTE);
                        break;
                    }
                default:
                    {
                        base.WndProc(ref m);
                        break;
                    }
            }
        }

This seems to work as tracing through it the SendMessage routine is called.

In Solution #4, we have the following code, which doesn't seem to ever receive the WM_PASTE message if we step through it message by message:

// Pasting
        private const int WM_PASTE = 0x0302;
        /// <summary>
        /// Override WndProc Method : Capture Pasting into ABC123 Text Box
        /// </summary>
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_PASTE:
                    {
                        DoPaste();
                        break;
                    }
                default:
                    {
                        base.WndProc(ref m);
                        break;
                    }
            }

        }

Questions are:

1) Is this the correct way to do it?

2) If not, what is the correct (or better) way?

3) If so, why wouldthis not be working?

Thanks!

FireMyst  Wednesday, September 16, 2009 12:37 AM
I've figured out how to send a message to the parent form. I've had to alter the control in SOlution #2 to call send message as follows:

SendMessage((

int)this.TopLevelControl.Handle, WM_PASTE, 0, 0);

Now I need to figure out how to do it if that top level control has a child that I need to paste into.
  • Marked As Answer byFireMyst Wednesday, September 16, 2009 5:52 AM
  •  
FireMyst  Wednesday, September 16, 2009 3:32 AM
I've figured out how to send a message to the parent form. I've had to alter the control in SOlution #2 to call send message as follows:

SendMessage((

int)this.TopLevelControl.Handle, WM_PASTE, 0, 0);

Now I need to figure out how to do it if that top level control has a child that I need to paste into.
  • Marked As Answer byFireMyst Wednesday, September 16, 2009 5:52 AM
  •  
FireMyst  Wednesday, September 16, 2009 3:32 AM

You can use google to search for other answers

Custom Search

More Threads

• Crystal Report ignoring parameters - can't work out why...
• Get selected indices from ListView control
• How to Disable default tooltip for a button...
• Switching forms in Visual C++
• Save As using File Stream Object
• Tree View Folder Icon Insert in C#
• Hiding properties on a property grid
• Give inactive form an active appearance
• Panels and Focus() (C#)
• Comparing strings