C++ Win API SendMessage Function

This is a discussion on C++ Win API SendMessage Function within the Windows Programming forums, part of the Platform Specific Boards category; I use Borland C++ Builder 3, Win 200 Pro. From within my program I use SendMessage to click a command ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    24

    C++ Win API SendMessage Function

    I use Borland C++ Builder 3, Win 200 Pro.

    From within my program I use SendMessage to click a command button in another application. This works and another window pops up, this new window has focus, from within this new window i wont to get the handle for an edit box.

    The problem is after the new window pops up my program pauses, is there any way i can return conrol to my program to continue searching for this control, am i missing a function call?

    code below..

    Thanks.

    Code:
    //---------------------------------------------------------------------------
    
    #include <stdlib.h>
    #include <vcl.h>
    #include <Windows.h>
    #pragma hdrstop
    
    #include "my_IP_suite.h"
    
    //---------------------------------------------------------------------------
    
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    
    //---------------------------------------------------------------------------
    
    __fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
    {
    HWND hWnd; //window handle
    HWND hBut; //control handle
    
    // get handle of window
    hWnd = FindWindow(NULL, "Reminder");
    
    if(! hWnd) exit(0); //if window don't found, exit program
    
    //move through controls, get handle of command button
    hBut = FindWindowEx(hWnd, NULL, "TButton", NULL);
    hBut = FindWindowEx(hWnd, hBut, "TButton", NULL);
    
    //click command button
    SendMessage(hBut, BM_CLICK, NULL, NULL);
    
    /*program pauses here, "Registration" window has focus, but program wont
    continue */
    
    //title of new window
    hWnd = FindWindow(NULL, "Registration");
    
    if(! hWnd) exit(0); //if window don't found, exit program
    
    //move through controls, get handle of edit box
    hBut = FindWindowEx(hWnd, NULL, "TButton", NULL);
    hBut = FindWindowEx(hWnd, hBut, "TButton", NULL);
    hBut = FindWindowEx(hWnd, hBut, "TButton", NULL);
    hBut = FindWindowEx(hWnd, hBut, "TButton", NULL);
    }
    
    //---------------------------------------------------------------------------

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Use PostMessage in place of SendMessage, then maybe use a timer function to setup a small delay

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    24
    Thanks that worked heaps better.
    That opened the new window, but now generated another error.

    The new window contains two comamnd buttons and 2 edit boxes

    This is where i try to assgin the handles:
    Code:
    //move through controls, get handle of LAST edit box
    
    hBut = FindWindowEx(hWnd, NULL, "TButton", NULL);
    hBut = FindWindowEx(hWnd, hBut, "TButton", NULL);
    hBut = FindWindowEx(hWnd, hBut, "TEdit", NULL);  //returns NULL
    hBut = FindWindowEx(hWnd, hBut, "TEdit", NULL);
    The handles return correctly for the 2 buttons, but the first call to FindWindowEx returns NULL.

    The order of the controls on form :
    starting a z - TButton, TButton, TEdit, TEdit

    Any idea on why the call isn't picking up the edit boxes?

    Thanks.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    try setting a SetTimer with a second or two interval and on the WM_TIMER message proccess the FindWindowEx functions. The reason you are getting an error is because your function reaches the FindWindowEx function before your other window is fully created.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Win API wildcard function??
    By Echidna in forum Windows Programming
    Replies: 5
    Last Post: 11-18-2001, 11:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21