SendMessage – C# 21

//DllImport using System.Runtime.InteropServices;
//Left Button - Mouse Down
public const int WM_LBUTTONDOWN = 0x0201;
//Left Button - Mouse Up
public const int WM_LBUTTONUP = 0x0202;
/// <summary>
/// The SendMessage function sends the specified message to a window or windows. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message. The PostMessage function, in contrast, posts a message to a thread’s message queue and returns immediately.
/// </summary>
///<param name="hWnd"></param>Identifies the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
///<param name="wMsg"></param>Specifies the message to be sent.
///<param name="wParam"></param>Specifies additional message-specific information.
///<param name="lParam"></param>Specifies additional message-specific information. ///<returns>The return value specifies the result of the message processing and depends on the message sent. </returns>

private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
long lngResult = SendMessage(btn1.Handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
lngResult = SendMessage(btn1.Handle, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);

21 thoughts on “SendMessage – C#

  1. Pingback: Communication between classes in C# at Neonlabs Innovations

  2. Reply SEan Strand Nov 28,2006 4:57 pm

    Hi-Ho, I am SeanS, from the AJX Course.

    Can U Please send me / tell me How I can down load or even link into the ZenGardens master.html
    so that I am able to complete the homework ?

    At present I am running round in circles.

    All regards SEanS

  3. Reply Michael Sync Nov 29,2006 1:08 pm

    ZenGardens master.html?

    maybe. you can download from CSS Zengarden

    just open any page that you wanna get CSS. Click to “View Source”. then you will see the CSS link in source..

    I think, you dont need to use the CSS file of CSS Zengarden for Homework #11.

    you can use your owned css file since Sangsing is just checking how we understand about CSS..

  4. Reply Waheed Dec 29,2006 12:39 am

    Very nice piece of code and explains very well, but this code calls the button down and button up events of whatever windows handle is passed, is there any way to call th click event, I know this should form a click but it doesnt.


  5. Reply Michael Sync Dec 30,2006 3:12 pm

    Yeah. click event should fire when you sent a msg to button… I will chk in details on coming monday.. and will let you know…

    can u show me the code that you hav done so far? maybe. i can point out some points in ur code….

    aniway, i will definetely let u know on coming monday..

  6. Reply GramBorder Mar 20,2007 1:26 am

    Hi all!

    I want to all of you know, World is mine, and yoursite good


  7. Reply Michael Sync Mar 20,2007 2:14 am

    >>World is mine,
    Oh. really..

    >>and yoursite good

  8. Reply kazim Jun 28,2007 9:28 am

    Above Send message code is fine as i have written it in the thread and want to send message whenever some data is receieved

    How can i make equivlant of C++ in C#
    C++ code is given:
    ON_MESSAGE(Msg_UpdateM, OnUpdateM)
    Msg_updateM is my custom Message
    OnUpdate is my function to update some value taken from thread and want to write in User interface

    Thanks in advance

  9. Reply Michael Sync Sep 21,2007 1:26 am

    Thanks. Dopler.

    Kazim, I’m not very familiar with C++ (I know only C++ 3.0 dos version that I learnt at collage.) I will find out about ON_MESSAGE and will let u know..

  10. Reply irfan Nov 26,2007 11:08 pm

    I am using combobox.can any body please explain in how to send message using combo in C#

  11. Reply Michael Sync Nov 26,2007 11:57 pm

    I am using combobox.can any body please explain in how to send message using combo in C#

    Have you tried this code?
    SendMessage(combobox1.Handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);

    Note: combobox1 is the name of your combobox.

  12. Reply rajan Apr 19,2008 5:21 am

    i want to know how to send a message through c#

  13. Reply Michael Sync Apr 19,2008 6:00 am

    >>>>i want to know how to send a message through c#

    hum? I already wrote in this post, isn’t it?

  14. Reply S. Geier Apr 26,2008 6:47 am


    this is some really spiffy code fragment but I am wondering how to send a Keystroke like “Shift + B” to another application.

    I am aware of the fact that 3 messages have to be send WM_KEYDOWN, WM_CHAR and finally WM_KEYUP to make the application recognize a keystroke… the thing is I dont know how to implement all the virtual codes like ing VK_F5 = 0×74, int VK_E = 0×45 into the SendMessage methode ;(
    I have succeeded in sending keys by using C#’s Sendkeys.Send() methode but its kinda limited in serveral ways. For example you cannot broadcast NUMPAD keys because its just not supported (correct me if I am wrong).

    Thanks for your help

  15. Reply kookimebux Feb 1,2009 10:44 am

    Hello. And Bye. :)

  16. Pingback: Communication between classes in C# at Neonlabs

  17. Reply soundaryaroja May 15,2011 7:55 am

    C languages are very intersting& its very simple

  18. Reply Ravi Bhanushali Nov 29,2011 8:17 am

    hi my name is ravi
    i want to knw that how tond an SMS on any mobile no that we want by using the C# language
    IF anyone has the solution please mail me on my gmail account

  19. Reply Burhan Dec 1,2012 3:10 am

    well i also need the demo of sending a message using c# as a language .. Kindly send me if anyone has .. please ..
    My Id is

Leave a Reply