SendMessage – C#

//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);

27 Replies to “SendMessage – C#”

  1. 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

  2. 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..

  3. 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.


  4. 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..

  5. 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

  6. 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..

  7. 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.

  8. Hi,

    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 = 0x74, int VK_E = 0x45 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

  9. Superb blog! Do you have any hints for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost on everything.

    Would you suggest starting with a free platform like WordPress or go for a
    paid option? There are so many options out there that I’m completely confused ..
    Any recommendations? Many thanks!

  10. А ответ прост как всё гениальное. Не стремитесь взрослеть. Не развращайтесь. Не убивайте в себе естественность. Позвольте себе быть чуть слабее, чтобы о вас хотелось заботиться. И тогда, клянусь вам, демографическая проблема в России останется в далеком страшном прошлом. Мало того, мы станем самой счастливой нацией в мире. А значит, самой лучшей. И всех мы победим!

  11. Unter Erster Hilfe versteht man lebensrettende und gesundheitserhaltende Sofortmaßnahmen, die von jedermann erlernt und bei Notfällen angewendet werden können. In der Rettungskette übernehmen Ersthelfer die Alarmierung, die Absicherung der Unfallstelle und die Betreuung der Patienten, bis professionelle Hilfe eintrifft Erste Hilfe Kurs in Muenchen

Leave a Reply