Click a button in an other window
Click a button in an other window
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.
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.
Try this for starters, no doubt there is a better way but it's a start.
Position the cursor over the button and press space to send the mouse click.
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
- Joakim Christiansen
- Addict
- Posts: 2452
- Joined: Wed Dec 22, 2004 4:12 pm
- Location: Norway
- Contact:
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.
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.
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.
"PureBasic won't be object oriented, period" - Fred.
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
As for the clicking the button ...
Theres an specific window message for this:
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?
Re: Click a button in an other window
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...Dude wrote:The codes posted in this thread no longer work.

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
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.SendMessage_(GetDlgItem_(hwndCalc,125 + i),#BM_CLICK,0,0)
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.
Re: Click a button in an other window
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
Re: Click a button in an other window
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.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...

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
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 changeDude 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.
Code: Select all
RunProgram("calc.exe")
Code: Select all
RunProgram("YourPath\calc.exe")

Re: Click a button in an other window
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


Re: Click a button in an other window
BTW: why you need an extern calculator ? Is the new version of PB not able to calculate .... ?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 machineWell, that's no solution. Can't expect my users to do that.