Page 1 of 2

Click a button in an other window

Posted: Wed Apr 29, 2009 7:53 pm
by codeman
Hello,

I have a question:
Can I click a button in an other window when i know the name of the EXE or the title of the program or the porcessID?
Can anybody give me a piece of code? It can be API, too.
Yours codeman!

PS: Sorry for my bad English.

Posted: Wed Apr 29, 2009 8:55 pm
by Derek
Try this for starters, no doubt there is a better way but it's a start.

Code: Select all

w$="test"

OpenWindow(0,0,0,160,100,w$)
ButtonGadget(0,10,10,140,20,"Click me")

Repeat
  handle = FindWindow_(0,w$)
Until handle>0

Repeat
  e=WaitWindowEvent()
  If e=#PB_Event_Gadget
    End
  EndIf
  If GetAsyncKeyState_(#VK_SPACE)
    mouse_event_(#MOUSEEVENTF_LEFTDOWN,0,0,0,0):Delay(10)
    mouse_event_(#MOUSEEVENTF_LEFTUP,0,0,0,0):Delay(10)
  EndIf
Until false
Position the cursor over the button and press space to send the mouse click.

Posted: Wed Apr 29, 2009 9:33 pm
by Joakim Christiansen
Maybe take a look here?
http://www.purebasic.fr/english/viewtopic.php?t=27548
Not exactly what you want but you can learn from it I think.

Btw, I found some code I once made to click a button in a Poker game:
http://www.copypastecode.com/codes/view/4929
Messy as hell, but I don't have time to rewrite it.

Posted: Thu Apr 30, 2009 2:06 pm
by codeman
Thx you both.

I will look and try and then I will tell you.

Posted: Fri May 01, 2009 12:29 pm
by codeman
Sorry but have anybody a solution where I can give the buttonname and windowname and the program click the button...
Thank you!
Yours, codeman!

Posted: Fri May 01, 2009 1:25 pm
by PB

Code: Select all

; Press Button On Window by PB. Free for any use. :)

; Launch Calc.exe and run this to press its "1/x" button.
; The result will be a "Cannot divide by zero" error. :)
; It doesn't matter if Calculator doesn't have the focus.

Global button$

Procedure PressButtonOnWindow(item,tmp)
  found$=Space(999) : GetWindowText_(item,found$,999)
  If found$=button$
    PostMessage_(item,#WM_KEYDOWN,#VK_SPACE,0)
    PostMessage_(item,#WM_KEYUP,#VK_SPACE,0)
  EndIf
  EnumChildWindows_(item,@PressButtonOnWindow(),0)
  ProcedureReturn #True
EndProcedure

hWnd=FindWindow_(0,"Calculator")

If hWnd
  button$="1/x"
  EnumChildWindows_(hWnd,@PressButtonOnWindow(),1)
EndIf

Posted: Fri May 01, 2009 3:01 pm
by Fluid Byte
As for the clicking the button ...

Theres an specific window message for this:

Code: Select all

RunProgram("calc.exe")

Delay(500)

hwndCalc = FindWindow_("SciCalc",0)

For i=0 To 8
	SendMessage_(GetDlgItem_(hwndCalc,125 + i),#BM_CLICK,0,0)
	Delay(500)
Next

Posted: Fri May 01, 2009 10:35 pm
by codeman
Thank you both! Both example help me! Thank you!

Re:

Posted: Wed Jul 27, 2016 12:40 pm
by Dude
The codes posted in this thread no longer work. Anyone know a way to update them?

Re: Click a button in an other window

Posted: Wed Jul 27, 2016 2:56 pm
by Shardik
Dude wrote:The codes posted in this thread no longer work.
That's not quite true. Fluid Byte's example is still working quite well on Windows XP SP3 and even on Windows 8.1 when you copy the old calculator version 5.1 from a Windows XP machine to Windows 8.1... :wink:

But the new calculator version 6.3 is indeed not working anymore on Windows 8.1 x64 because this calculator is a reprogrammed version which misses an Accelerators resource whereas in the old version 5.1 on Windows XP in Calc's Accelerators resource all keys like "0", "1" etc. and the virtual keys are defined. So "1" has the ID 125, hence the line
SendMessage_(GetDlgItem_(hwndCalc,125 + i),#BM_CLICK,0,0)
which doesn't work anymore in the Calc version 6.3 on Windows 8.1. The same holds true for Windows 7 SP1 x86 and Calc version 6.1.

Furthermore the class name was changed from SciCalc to CalcFrame...

Re: Click a button in an other window

Posted: Wed Jul 27, 2016 3:16 pm
by Thunder93
Windows 10 it's Windows.UI.Core.CoreWindow

Re: Click a button in an other window

Posted: Thu Jul 28, 2016 9:43 am
by Dude
Shardik wrote:That's not quite true. Fluid Byte's example is still working quite well on Windows XP SP3 and even on Windows 8.1 when you copy the old calculator version 5.1 from a Windows XP machine to Windows 8.1... :wink:
It's true for me: Fluid Byte's code doesn't work on Win 7 here at all. Calc just sits there with no changes; even when I try both code examples. :(

Let me ask the question another way, then: is there a method to iterate through all of a third-party window's elements? So I can (for example) get the hWnd of each element, to interact with it? My aim is to be able to "click" a button in a window based on its text, like AutoHotKey can.

I'm sure it's to do with EnumChildWindows_() but can't figure it out.

Re: Click a button in an other window

Posted: Thu Jul 28, 2016 10:51 am
by Shardik
Dude wrote:It's true for me: Fluid Byte's code doesn't work on Win 7 here at all. Calc just sits there with no changes; even when I try both code examples. :(
Fluid Bytes's example only works in Windows 7 and 8 if you copy Calc.exe from a Windows XP machine to your 7/8 machine and change

Code: Select all

RunProgram("calc.exe")
to

Code: Select all

RunProgram("YourPath\calc.exe")
I have verified it successfully on Windows 7 SP1 x86 and Windows 8.1 x64... :wink:

Re: Click a button in an other window

Posted: Thu Jul 28, 2016 11:30 am
by Dude
Shardik wrote:Fluid Bytes's example only works in Windows 7 and 8 if you copy Calc.exe from a Windows XP machine to your 7/8 machine
:lol: Well, that's no solution. Can't expect my users to do that. :)

Re: Click a button in an other window

Posted: Thu Jul 28, 2016 12:21 pm
by Bisonte
Dude wrote:
Shardik wrote:Fluid Bytes's example only works in Windows 7 and 8 if you copy Calc.exe from a Windows XP machine to your 7/8 machine
:lol: Well, that's no solution. Can't expect my users to do that. :)
BTW: why you need an extern calculator ? Is the new version of PB not able to calculate .... ?