clicking a button from an foreign app...

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kendrel.

I have tried to send a click event with Post_Message to a button from an foreign app, and it works when the button is not on a child window.

I used FindWindow to get the window and FindWindowEx to get the button, but i can only find buttons in the main window and not in childs.

The app i want to control has many child windows (i used spy++ to look at it), and i dunno how to loop through each child to get the handle of the button.

My idea is to loop through each window, and check the buttontext (getwindowtext) and compare it...

how would you do it if you want to find a specific button in an application? Some sample code would be very nice, since iam quite new to the WinApi... ;(

thx in advance!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Hi, try this:

Code: Select all

Global TextBuffer
Procedure EnumChildProc(hwnd, lParam)
  ; For any hwnd, you can 'nest' calls to EnumChildWindows and to other EnumChildProc
  result = #TRUE ; continue searching
  If GetWindowText_(hwnd, TextBuffer, 128)
    If PeekS(TextBuffer)="Text searched"
      result = #FALSE ; search succesful, stop callback, do whatever
    EndIf
  EndIf
  ProcedureReturn result
EndProcedure
TextBuffer = AllocateMemory(0, 128, 0)
hWndParent = FindWindow_(0, @"MyApp")
If hWndParent
  EnumChildWindows_(hWndParent, @EnumChildProc(), lParam) ; lParam just gets passed, you can put 0 there
EndIf
FreeMemory(0)
Hope it solves your problem. Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kendrel.

Funny, but i have tried it in vb and got it 2 work (got some hints from a friend). I also just tried your version, and it works aswell... and its even smaller as my vb version...
problem is that iam new to purebasic, and i dunno all the commands yes... whats that "@" exactly for, and do you really need to allocate memory and free it? some details would be nice... but anyway... good job m8... seems this is a nice community... small but nice... :)

here is my vb code :) tell me what you think




Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Sub main()
Handle = FindWindow(vbNullString, "Form1")
FindButtons (Handle)
End Sub
Public Sub FindButtons(ByVal hWndParent As Long)
EnumChildWindows hWndParent, AddressOf EnumChildWinProc, 0
End Sub
Public Function EnumChildWinProc(ByVal hwnd As Long, _
ByVal lParam As Long) As Long
Dim sClass As String, sText As String
Dim lRet As Long
sClass = String(256, 0)
lRet = GetClassName(hwnd, sClass, 255)
If lRet > 0 Then
sClass = Left$(sClass, lRet)
If InStr(1, sClass, "button", vbTextCompare) > 0 Then
sText = String(255, 0)
lRet = GetWindowText(hwnd, sText, 255)
If lRet > 0 Then
Debug.Print CStr(hwnd) & "; " & Left$(sText, lRet)
End If
End If
End If
EnumChildWinProc = 1
End Function
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kendrel.

I have another question, and probably you can help me a little.

So far its possible to find any Command Button by its text, but what if an application has 2 buttons with the same text?! I was also wondering how i could find a specific listbox to extract them, and there would be the same problem when an app has more than one listbox named like List1...

i think you could compare its control-id (you can retrieve it with spy++) when it doesnt change (thats what i heard)... but what would be the best solution to do this?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Hi. This is the code translated to PB (i think so):

Code: Select all

Declare FindButtons(hWndParent.l)
Declare EnumChildWinProc(hWndParent.l)

Procedure FindButtons(hWndParent)
  EnumChildWindows_(hWndParent, @EnumChildWinProc(), 0)
EndProcedure

Procedure EnumChildWinProc(hwnd, lParam)
  lRet = GetClassName_(hwnd, AllocateMemory(0, 255, 0), 255)
  If lRet>0
    sClass$ = PeekS(MemoryID())
    If FindString(sClass$, "button", 0)
      sText = AllocateMemory(1, 255, 0)
      lRet = GetWindowText_(hwnd, sText, 255)
      If lRet
        Debug Str(hwnd)+"; "+PeekS(sText)
      EndIf
      FreeMemory(1)
    EndIf
  EndIf
  FreeMemory(0)
  EnumChildWinProc = 1
  ProcedureReturn EnumChildWinProc
EndProcedure

main:

Handle = FindWindow_(0, @"Form1")
FindButtons(Handle)
End
We allocate memory to have a buffer to pass its pointer to Windows functions (pointers to a variable are designated in PB by appending a '@' before its name). I bet VB does the same, but not transparently.

About getting ListView items' text:

Code: Select all

pitem.LV_ITEM
;iItem = index number of the item from which to retrieve its text
If SendMessage_(ListViewControlHandle, #LVM_GETITEMTEXT, iItem, @pitem)
  ItemText$ = PeekS(pitem\pszText)
EndIf
About the best method to locate the desired button, I don't know, it depends on what are you trying to do with your app. wIDs may be a good way, but they may change, it depends on the app. If you're coding for a known app, you can check that using Spy.

Hope this helps. Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kendrel.

thx again m8... your pb version does the same thing than mine, and yours is even smaller so i guess ill stick with it :)

ofcourse i want to code something for an specific application, and i think wIDs doesnt change on every start (or am i wrong?).
i simply want to know how i could compare its id then, or would you know a better way to check for a button or listbox for example?
listbox for example as always named like "List1" when i use spy++, so i will need something to id it by its wID... any other ideas?

and thx alot... iam glad you helped out...!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kendrel.

Ohwell, what can i say? I made it on my own and here is my solution... read further below the code...

Global TextBuffer
Procedure EnumChildProc(hwnd, lParam)
result = #TRUE
If GetWindowText_(hwnd, TextBuffer, 128)
id.l=GetWindowLong_(hwnd,-12)
If id.l=1
If PeekS(TextBuffer)="Command2"
result = #FALSE
MessageRequester("ID:",Str(id.l),0)
MessageRequester("Info!",Str(hwnd),0)
EndIf
EndIf
EndIf
ProcedureReturn result
EndProcedure

Procedure WindowLocate()
TextBuffer = AllocateMemory(0, 128, 0)
hWndParent = FindWindow_(0, @"Form1")
If hWndParent
EnumChildWindows_(hWndParent, @EnumChildProc(), lParam)
EndIf
FreeMemory(0)
EndProcedure

WindowLocate()


As you see i used FindWindowLong to get the id from the window, and it works...

The -12 is normally a const called GWL_ID. I tried to set it the way i understand to init constants, but however it doesnt work for me...

i was also wondering about purebasic that you first have to write all you procedures and then the main loop... this is somehow strange, but ok if iam not wrong again :)

could you tell me how to use a constant there and why my procedures have to be at the beginning of my code?!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

Constants work fine, and procedures have to be
declared if you use them before known by the compiler.

Code: Select all

;
; Procedure declarations
;
Declare EnumChildProc(hwnd, lParam)
Declare WindowLocate()


;
; Constants
;
#GWL_ID = -12


;
; Variables
;
Global TextBuffer


;
; Code
;
WindowLocate()




;
; Procedures
;
Procedure EnumChildProc(hwnd, lParam)
   result = #TRUE
   If GetWindowText_(hwnd, TextBuffer, 128)
      id.l=GetWindowLong_(hwnd,#GWL_ID)
      If id.l=1 
         If PeekS(TextBuffer)="Command2"
            result = #FALSE 
            MessageRequester("ID:",Str(id.l),0)
            MessageRequester("Info!",Str(hwnd),0)
         EndIf
      EndIf
   EndIf
ProcedureReturn result
EndProcedure


Procedure WindowLocate()
   TextBuffer = AllocateMemory(0, 128, 0)
   hWndParent = FindWindow_(0, @"Form1")
   If hWndParent
      EnumChildWindows_(hWndParent, @EnumChildProc(), lParam) 
   EndIf
   FreeMemory(0)
EndProcedure
cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kendrel.

Thx :) Shame on me... i missed a # when i used the constant b4 :)

What i couldnt find in the docs was that & where you point at the function... the docs arent the best... sad, but still good to have places like this one.

Danilo, what i always wanted to know...

it seems that you are an very experienced programmer, and i would like to know if this is your job or do you just do it in your spare time... i have read the german forums alot (yes, iam german), and i wonder that guys like you who seem to have a huge knowledge about programming, use a language like purebasic... dont get me wrong, but most ppl use c++ or something...
i have to learn vb and c++ because it is my job (iam becoming an application programmer), but i have to admit that i like languages like Purebasic (for apps) and Blitzbasic (for games) a little more... too sad the communitys are that small, and that one man cant comepete with M$... but thats business!

thx for help...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

> and i would like to know if this is your job
> or do you just do it in your spare time...

Its NOT my job.

> i have read the german forums alot (yes, iam german),
> and i wonder that guys like you who seem to have a huge
> knowledge about programming, use a language like purebasic...
> dont get me wrong, but most ppl use c++ or something...

Right.
I´m coming from Assembly only and i dont like
the syntax of C/C++.
If you look for a HighLevelCompiler, you have
to know what you want.

VB is out of Question, because its plain sh*t.
And other languages like C/C++ or Delphi use
a syntax which i dont like.
";", "{", "}" ...etc...

All this special chars are not required for
programming and you have to type more.
You have to look everywhere that you dont
forget a "}" or ";" - who likes that ??

So something like PureBasic would be the
best thing.
Its the best compiler i´ve seen lately.
(Syntax, ASM-output, !ASM, small EXE-Size, make own Libraries, ...)

But PureBasic is still in the beginning
and has too much bugs (Gadget-Events for example) -
and that after 2 years of development !!

And dont think PB will be bugfree after v3.30...
...thats the annoying part.
I know every language has bugs, but in PB
its endlessly... or can you see the end yet ??

I want full control over everything
(like ASM, no hidden stuff like in VB),
i want speed and i want optimized Executables.
Everything like in ASM, but easier
string-handling and calculations.

So i´m in a personal conflict here.
PB is good, but still not good enough...
...so i should do my own thing or not !?
Who knows...

Have fun,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kendrel.

I did not know that Purebasic still has that many bugs, and i wonder if there is something like a known bug list.

I think you like Purebasic alot since you did so much for the community, but still you are disapointed about alot of things... but still you are here using purebasic :)

I have to Learn C++ and VB at School (i personally dont like the { and ; ****), because these are languages you get a job with... nobody would give me a job when i would only know a language like purebasic...

whatever, there are always things that you like/dislike in a programming language, and i guess we have to live with that...

but here is one question. dont you think its possible to develope something in purebasic at the moment? too many bugs? missing features? and what about oop?!

Regards, Marcel

p.s. Ich kenn keinen der mit assembler angefangen hat.. :) ist doch wohl nen tacken schwieriger als c++ oder andere sprachen...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

> I think you like Purebasic alot since you did so much for the community, but still you
> are disapointed about alot of things... but still you are here using purebasic :)

Yep, thats 100% right.

Cant believe somebody understood what
i wanted to say.

> but here is one question. dont you think its possible to develope
> something in purebasic at the moment? too many bugs? missing features?
> and what about oop?!

You can write enough stuff with PureBasic.
Most time i need API too, but thats not a problem for me.
The people that dont know WinAPI have a big
problem very often.

Everytime i write something, i find a bug
or after the next version it doesnt work anymore.
Maybe i´m doing too advanced stuff. I dont think so.

The Gadget-Handling doesnt work right, so some
time ago i though i look at game-development.
I wrote a AnimSprite-Library and found a bug
in the SpriteCollision. Fred fixed that, but
now there is another bug in SpriteCollision.
See:

Code: Select all

[url]http://forums.purebasic.com/english/viewtopic.php?t=1651[/url]
So somebody has to wait until it is fixed.

I begin the next tool in the meanwhile and
shortly after that i find the next bug...
This is very demotivating, because you cant
just finish a little tool in a few days
(like it should be).

But you got it... i´m still here.

Let´s hope for the best, maybe its much better in 1 year...
(do you believe?)

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> All this special chars are not required for programming and you have to
> type more. You have to look everywhere that you dont forget a "}" or
> ";" - who likes that ??

I hate that, too. That's why I chose PureBasic over C/++ when I gave up VB.

> But PureBasic is still in the beginning and has too much bugs (Gadget-Events
> for example)

What gadget event problems are you having? I haven't seen any... but I also
haven't used all gadgets yet in my apps.

> and that after 2 years of development !!

Two years is still young for a language written by mainly one person. I think
you're expecting way too much from Fred. It sounds like you expect PureBasic
to be as professional as Visual Basic, which is not going to happen any time soon.

> And dont think PB will be bugfree after v3.30...

You shouldn't keep expecting it to be bugfree so soon. Personally, I expect the
program to be 99% bug-free by version 10.0, or even later! And I'm perfectly
fine with that, and don't mind using workarounds in the meantime.

A programmer is supposed to enjoy the problems that come with coding.
Don't take it all so seriously, Danilo -- have fun killing the bugs!


PB - Registered PureBasic Coder

Edited by - PB on 29 July 2002 22:25:41
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.
p.s. Ich kenn keinen der mit assembler angefangen hat.. :) ist doch wohl nen tacken schwieriger als c++ oder andere sprachen...
The first computerlanguage should be ASM of course
ASM was my second, after C64Basic....

Its a long way to the top if you wanna .....CodeGuru

Edited by - Rings on 30 July 2002 08:11:02
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

> What gadget event problems are you having? I haven't seen any...
> but I also haven't used all gadgets yet in my apps.

You havent seen any ?? LOL... (sorry)

Code: Select all

PanelGadgets:
   > PB wrote:
   > BUT... this is the same bug that I reported in March in the
   > following post, and Paul reported last October (!),
   >
   [url]http://forums.purebasic.com/english/viewtopic.php?t=1585[/url]


TreeGadget:
   [url]http://forums.purebasic.com/english/viewtopic.php?t=1642[/url]


ComboBox (fixed for 3.30):
   [url]http://forums.purebasic.com/english/viewtopic.php?t=1571[/url]


Combobox:
   Selecting a new item gives RightClick Event (very bad, IMO)


SpinGadget:
   Buttons UP and DOWN are reversed
   [url]http://forums.purebasic.com/english/viewtopic.php?t=1721[/url]


Window Repainting:
   [url]http://forums.purebasic.com/english/viewtopic.php?t=1648[/url]


StatusBar Problem:
   [url]http://forums.purebasic.com/english/viewtopic.php?t=1677[/url]


EventType() doesnt return #PB_EventType_LeftClick etc..
for the main window:
[url]http://forums.purebasic.com/english/viewtopic.php?t=1742[/url]


and so on...
...there is more if you search
the forum or use the gadgets.

Best thing is to use no gadgets or GUI stuff at all.

PureBasic DirectX (gaming stuff) looks OK so far
(only small problems were there so far), and the
console should also work like exprected.
But the whole GUI-stuff isnt correct atm.

You think i should wait for v10.0 or longer -
thats round about 7 years or so !!

This things are not SUCH a big problem, that
you cant get it correct after 2 years of development.
In PowerBasic or C/C++ you do the EventHandling
by hand everytime. And it works.

You can program the GUI-stuff 100% API in PureBasic
and make everything yourself (I would like BASIC MACROs
for 100% API coding).
But why are all this commands and events included in PB ??

They are there for the user to use it, so they should be bugfree.
I think when Fred is sitting down and looks at the whole
Gadget and Event stuff for a while, this things could be
99% bugfree in the NEAR future (v3.30/v3.40) - not in 7 years.

I dont know how Fred works, but sometimes it looks funny.
2 Bugs removed, 3 added.
And all of a sudden a whole command is missing after
a new release. Just missing, dunno why (PeekF()).
How can THIS happen ?? I dont know...

I dont lick someones backside and i dont give out free
bl*wj*bs only because somebody is writing a compiler.
(looks like some ppl here like that way).

I say what i think and i want to talk about that stuff -
and i think Fred knows what i mean with this.
The Event-Handling isnt the easiest thing in Windows
and you have to take care about MUCH stuff - but its possible
to do it right (look at some of the billion appz out there).

Especially in a programming language that many people use,
this stuff MUST work at 99% (at least).

If Fred gets dismotivated when i talk about this stuff here,
he can tell me so. I will shaddup then and care more about
my own things.
Some people dont like the truth, but i think Frederic
can live with it (maybe i´m just wrong here).
When nobody says something about it, Fred thinks everything
is OK and he doesnt fix all the VERY IMPORTANT stuff now
because he has other priorities.

And now enjoy the BUGs, PB...

cya,
...Danilo
"I hate it, but i love it"

(registered PureBasic user)
Post Reply