Thread: Difference between SendMessage and SendInput for faking a mouse click

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    16

    Difference between SendMessage and SendInput for faking a mouse click

    I used to use the following code to fake mouse click on a check box:
    GetWindowRect(bHandle, &rect);
    SetCursorPos(rect.left + 0xA, rect.top + 0x5);
    SendMessage(bHandle, WM_LBUTTONDOWN, 0, 0x000A0005);
    SendMessage(bHandle, WM_LBUTTONUP, 0, 0x000A0005);

    The problem is that the result is random --- sometimes the code successfully check/uncheck the box, sometime it fails (I can see the cursor is moved on top of the box, but the click doesn't happen).

    I searched the board, and see someone mentioned using SendInput function to generate mouse click. I will try it but, does anyone know what's the difference between the two approaches?

    Thank you in advance!

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Why do you need to 'fake' a mouse click?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    ♥Sexy Coding Hunk♥ CartoonLarry's Avatar
    Join Date
    Dec 2003
    Location
    Michigan
    Posts
    50
    This is what I have done in the past when I wanted to check and/or uncheck a check box.

    // Check the check box
    SetForegroundWindow(ParentHwnd); // Bring window to the front
    SetFocus(ChildHwnd); // Make sure you have focus
    SendMessage(ChildHwnd, BM_SETCHECK, BST_CHECKED, 0); // Check

    // Uncheck the check box
    SetForegroundWindow(ParentHwnd); // Bring window to the front
    SetFocus(ChildHwnd); // Make sure you have focus
    SendMessage(ChildHwnd, BM_SETCHECK, BST_UNCHECKED, 0); // Uncheck

    Hope this helps

Popular pages Recent additions subscribe to a feed