Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

Clicking a button by code (send message / event ?)

Hi you all,

Some system forms ask the user to confirm an action.

If I don't like to be asked, because my unattended app as an Nt4.0 service started that system function. I would like to "click" that confirmation by code.

Once I saw some snippets, searching something like the window title, collecting the captures of buttons and sending something like a click event to the desired one.
Can you help?

Link posting welcomed too.

Thanx a lot.

C.U.,
swdld

0
swdld
Asked:
swdld
1 Solution
 
VKCommented:
Hello swdld !

Usage:

1. AppActivate title[, wait]
2. SendKeys string[, wait]

V.K.
0
 
gramireCommented:
'this function will do what you want.....

Sub ClickButton()
    Dim msgWindow As Long
    Dim okButton As Long
   
    okButton = 0
   
    'the loop is here in case the window is not present when the sub is called
    Do
        DoEvents
        msgWindow = FindWindow("#32770", "TITLE BAR TEXT")
        'TITLE BAR TEXT = the text from the title bar of the window with the button
       
        okButton = FindWindowEx(msgWindow, 0, "Button", "OK")
        'OK = the text on the button. if the button's text has an underlined letter,
        '   use & before that letter. for example if you have "Yes" with the "Y"
        '   underlined, you'd use "&Yes"
    Loop Until okButton <> 0


    Call SendMessage(okButton, WM_KEYDOWN, VK_SPACE, 0&)
    Call SendMessage(okButton, WM_KEYUP, VK_SPACE, 0&)
End Sub





'these are the function and constant declarations you need:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Const VK_SPACE = &H20

Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101


These, and many other API functions *WERE* found at www.vbapi.com but the owner discontinued the project. I remember he offered his entire site for download on a single zip. I have that file. It's about 1.39MB. If anybody needs it, let me know. I believe the site was updated at least once after saved the zip, so I may not have the latest version.
0
 
ArkCommented:
Private Const BM_CLICK = &HF5
SendMessage hButton, BM_CLICK, 0, ByVal 0&
0
 
MoondancerCommented:
Greetings.

This question has been locked with a Proposed Answer, and remains open today.

If the Proposed Answer did not serve your needs, please reject it and comment with an update.  If the Proposed Answer helped you, please accept it to grade and close this question.  If you need help splitting points between multiple experts, please comment here with details so we can help you.

EXPERTS ->  Please guide me here in terms of closing recommendations if the Asker does not respond in 4 days.

Thanks to all,
Moondancer - EE Moderator
0
 
swdldAuthor Commented:
Sorry for not corresponding such a long time,
I moved.
Thanx a lot anyway !

C.U.,
swdld
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now