-2

I want to use SendMessage to complete part of my remote control program. Case I think it will control the computer program without make it get focus.

Here is my demo. I use windows mspaint to test it. It works when I post "Left button down", so I am sure that my HWND is right(The drawing area handle got by spy++ or some similar tools). It draws a point in my "drawing area" in mspaint. But when I select a color and run the folling code. It doesn't work. Nothing happens in my drawing area on the same mspaint program.

Here is my code:

    #include <stdio.h>
    #include <Windows.h>

    int main(){
        HWND hwnd;
        printf("Please input your handle\n");
        scanf("%d", &hwnd); 
        int x = 100, y = 100;
        //int right = SendMessageA(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y)); //It works
        int wrong = SendMessageA(hwnd, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(x, y));   //It doesn't work

        printf("%d", wrong);
        printf("%d", GetLastError());
        return 0;
    }

I am confused because the two lines are so similar. Is there any bugs in mspaint?

Lyronx L
  • 13
  • 5
  • I'm sure that I can draw something in the drawing area with the right button, instead of setting wrong white color. – Lyronx L Mar 09 '20 at 15:12
  • 2
    You can't fake input this way. If you want to fake input, call `SendInput`. – David Heffernan Mar 09 '20 at 15:13
  • And the Sendmessage function returns zero, which means it complete successfully. – Lyronx L Mar 09 '20 at 15:13
  • @David Heffernan But why? And will SendInput get focus? – Lyronx L Mar 09 '20 at 15:14
  • You will have to move the input focus to the target app, and then call `SendInput`, if you want to fake input. – David Heffernan Mar 09 '20 at 15:34
  • @David Heffernan I do not want to set any focus. Case I do not want to disturb user. – Lyronx L Mar 09 '20 at 15:46
  • 1
    1. If you want to fake input, you must use `SendInput`. 2. `SendInput` places messages in the queue of the thread that has input focus. Once you appreciate the consequences of these two statements, you might be ready to reconsider your goals. This question is asked many times here every week. With a little research you will see us giving the same answer again and again. Perhaps that will help you. – David Heffernan Mar 09 '20 at 15:49
  • It sounds like you do not even care about faking input, so why not just skip that part altogether and move on to automating your UI? [UI Automation](https://learn.microsoft.com/en-us/windows/win32/winauto/entry-uiauto-win32) explains how to do this. – IInspectable Mar 09 '20 at 16:01
  • By default, click or drag right mouse button cause nothing happen at drawing area of Paint. Did you [Swap Mouse Button](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-swapmousebutton)? – Rita Han Mar 10 '20 at 07:00
  • @Rita Han - MSFT It seems like that there are "Color 1" and "Color 2" in color palette of mspaint and the "Color 1" refers to the color of the link I click and drag by left mouse button and the "Color 2" refers to the right mouse button. Isn't it? I am using Windows 7 Professional. – Lyronx L Mar 10 '20 at 09:15
  • @LyronxL I am using Windows 10 1903: "**Color 2 (background color) - Click here and then select a color from the color palette. This color is used with the eraser and for shape fills.**" So it is not for right mouse button. – Rita Han Mar 11 '20 at 02:29
  • @Rita Han - MSFT You are right but when you try to drag your right button after selecting the color 2, you can draw a line with selected color by brush tool. Isn't it? – Lyronx L Mar 12 '20 at 02:10
  • @LyronxL Yes, I can reproduce this issue on Windows 10. For check purpose, I create a win32 window application and send mouse message to it. It can receive both left and right button down messages. If there is any issue in Paint or not need a further investigation. Do you have any concern about using [SendInput](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput) API? – Rita Han Mar 12 '20 at 08:18
  • @Rita Han - MSFT I just cannot understand why there are differences between mouse event of left button and right one. Moreover the `SendInput` function needs an absolute coordinate so that I have to distribute user and I cannot control more than one program concurrently. – Lyronx L Mar 12 '20 at 12:28

1 Answers1

0

I am confused because the two lines are so similar. Is there any bugs in mspaint?

These two code lines can be executed successfully and they do send WM_LBUTTONDOWN and WM_RBUTTONDOWN messages to the target window. I can confirm this via target a own created window application and monitor mouse message in it. So maybe the difference depends on how does the mspaint application handle left and right mouse button click.

The following code works for me for both left and right mouse buttons. You can have a try.

int x = 100, y = 100;

// Left mouse button click
int lResult = SendMessageA(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y)); 
Sleep(500);
lResult = SendMessageA(hwnd, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(x, y));

x += 10;
// Right mouse button click
int rResult = SendMessageA(hwnd, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(x, y));   
Sleep(500);
rResult = SendMessageA(hwnd, WM_RBUTTONUP, MK_RBUTTON, MAKELPARAM(x, y));

printf("Done.");

The test result of above code:

enter image description here

Note: As other communities already pointed out, using SendInput instead of SendMessage to Synthesizes keystrokes, mouse motions, and button clicks.

Rita Han
  • 9,574
  • 1
  • 11
  • 24
  • Yeah, it's a problem of mspaint. I tried to add the WM_BUTTONUP function and it works. – Lyronx L Mar 13 '20 at 07:57
  • 1
    @lyr: No. The issue is *your* code and your *assumptions* about how the system should behave. You are doing it wrong. Any properly implemented Windows application is allowed to fail to respond to your crude attempts at faking input. Because [you can't simulate keyboard input with PostMessage](https://devblogs.microsoft.com/oldnewthing/20050530-11/?p=35513). Your approach is *fundamentally* flawed. Choose an approach that isn't, like UI Automation. – IInspectable Mar 20 '20 at 16:29