SendMessage vs. WndProc

Posted by Poma on Stack Overflow See other posts from Stack Overflow or by Poma
Published on 2010-03-29T17:24:45Z Indexed on 2010/03/29 17:53 UTC
Read the original article Hit count: 756

Filed under:
|
|

I'm trying to extend TextBox control to add watermarking functionality. The example I've found on CodeProject is using imported SendMessage function.

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);

void SetWatermark()
{
    SendMessage(this.Handle, 0x1501, 0, "Sample");
}

I'm wondering why not use protected WndProc instead

void SetWatermark()
{
    var m =new Message() { HWnd = this.Handle, Msg = 0x1501, WParam = (IntPtr)0, LParam = Marshal.StringToHGlobalUni("Sample") };
    WndProc(ref m);
}

Both seem to work fine. Almost all examples I've seen on internet use SendMessagefunction. Why is that? Isn't WndProc function designed to replace SendMessage?

P.S. I don't know right to convert string to IntPtr and found that Marshal.StringToHGlobalUni works ok. Is it right function to do this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about pinvoke