Click a button in an other window

Just starting out? Need help? Post your questions and find answers here.
codeman
User
User
Posts: 27
Joined: Thu Feb 19, 2009 7:12 am

Click a button in an other window

Post 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.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post 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.
I like logic, hence I dislike humans but love computers.
codeman
User
User
Posts: 27
Joined: Thu Feb 19, 2009 7:12 am

Post by codeman »

Thx you both.

I will look and try and then I will tell you.
codeman
User
User
Posts: 27
Joined: Thu Feb 19, 2009 7:12 am

Post 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!
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
codeman
User
User
Posts: 27
Joined: Thu Feb 19, 2009 7:12 am

Post by codeman »

Thank you both! Both example help me! Thank you!
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re:

Post by Dude »

The codes posted in this thread no longer work. Anyone know a way to update them?
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Click a button in an other window

Post 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...
Last edited by Shardik on Wed Jul 27, 2016 3:45 pm, edited 1 time in total.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Click a button in an other window

Post by Thunder93 »

Windows 10 it's Windows.UI.Core.CoreWindow
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Click a button in an other window

Post 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.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Click a button in an other window

Post 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:
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Click a button in an other window

Post 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. :)
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Click a button in an other window

Post 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 .... ?
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Post Reply