Click background window programmatically

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Click background window programmatically

Post by Dude »

Hi all, I want my app to click a button control on my Winamp window when I press a hotkey, to go to the next track. Let's ignore the fact that Winamp may have a hotkey to do this, because the end goal is to adapt the code to work with any background window. :)

So, without bringing Winamp to the front, how can I simulate a mouse click on it, without bringing it to the foreground, and at a specific X/Y mouse position without moving the mouse so obviously to the user?

I found this StackOverflow link:

https://stackoverflow.com/questions/123 ... ck-message

And it appears that I can perhaps do it with these two lines of code:

Code: Select all

Macro MAKELPARAM(loWord,hiWord) : (hiWord << 16 | loWord) : EndMacro
SendMessage_(Winamp_hWnd,#WM_LBUTTONDOWN,#MK_LBUTTON,MAKELPARAM(x,y))
But it doesn't work. :( According to this post, it should:

viewtopic.php?p=465341#p465341

Alternatively, how can I convert the following to PureBasic (from that same link), which also appears to be what I want:

Code: Select all

{
    INPUT input;
    input.type=INPUT_MOUSE;
    input.mi.dx=0;
    input.mi.dy=0;
    input.mi.dwFlags=(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE|MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP);
    input.mi.mouseData=0;
    input.mi.dwExtraInfo=NULL;
    input.mi.time=0;
    SendInput(1,&input,sizeof(INPUT));
    return 0;
}
Thanks!
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Click background window programmatically

Post by infratec »

viewtopic.php?p=363502#p363502

You can modify this for mouse instead of keyboard.

But I don't think that you can use this.
You need the xy coordinates of the button.

https://docs.microsoft.com/en-us/window ... -sendinput
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Click background window programmatically

Post by infratec »

Code: Select all

Define MouseInput.Input

MouseInput\type = #INPUT_MOUSE
MouseInput\mi\dx = 0
MouseInput\mi\dy = 0
MouseInput\mi\dwFlags =(#MOUSEEVENTF_ABSOLUTE|#MOUSEEVENTF_MOVE|#MOUSEEVENTF_LEFTDOWN|#MOUSEEVENTF_LEFTUP)
MouseInput\mi\mouseData = 0
MouseInput\mi\dwExtraInfo = #Null
MouseInput\mi\time = 0
SendInput_(1, @MouseInput, SizeOf(Input))
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Click background window programmatically

Post by Dude »

Thanks for your replies, infratec, but I couldn't make them do what I want. :(

The following thread apparently shows how to do it, if someone can look? It's the reply that starts with: "I found in the past, a way to send message to Windows Media Player":

https://stackoverflow.com/questions/103 ... her-window

Thanks!
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Click background window programmatically

Post by infratec »

As I told you, you need the coordinates of the Button.
Else you click on coordinates 0,0. Where definately no button is located.
Or, if yuo send only WM_LBUTTONDOWN you may select/activate the button before you send something.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Click background window programmatically

Post by RASHAD »

You can do it IF you know the handle of the Button
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Click background window programmatically

Post by Dude »

infratec wrote:As I told you, you need the coordinates of the Button.
Of course I changed the co-ords to other than 0,0. :P

Rashad: The handle is actually irrelevant. The goal is just to click whatever's there at the X/Y position; not to click a specific control. I didn't make that properly clear in my first post. The button in my first post was where the calculated X/Y positon was.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Click background window programmatically

Post by RASHAD »

In your case you must
- Find first the handle of winAMP
- Get it's rectangle (to get x & y for the origin)
- Calculate your aim x & y relative to the origin
- Call winAMP to top
- Set the cursor to x & y
- Simulate mouse click
- Send winAmp to it's Z order again
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Click background window programmatically

Post by Dude »

Rashad, what you posted is literally exactly how I'm already doing it. :wink: But the part I'd like to avoid is the bringing of Winamp to the top to do the click. It looks ugly to flash to the front like that, so I was hoping there'd be a background way.
Post Reply