Link to home
Start Free TrialLog in
Avatar of TSTechNA
TSTechNA

asked on

Read the caption text from a programs dialog box

I want to know if it is possible to get the text caption of an emerging dialog box of a program by using the Windows API or something else, the only constant data that I know it has is the "Class" value (#32770 (Dialog): is the value obtained on the Microsoft Spy++) and the caption of the Main Program Window of course, the control handle value of the dialog box is changing every time the program loads, if the Spy++ is able to read this value there should be a way to automate this, I would like to do this using C#... Any help would be greatly appreciated.

Thanks.
Avatar of nayernaguib
nayernaguib
Flag of Egypt image

There is more that one way to do that. For example, you can use a Windows API call to enumerate all top-level windows (EnumWindows), find the window that contains the previously known caption (GetWindowText), find the child of this window (EnumChildWindows), and then read the window text (GetWindowText).

_______________

  Nayer Naguib
Avatar of TSTechNA
TSTechNA

ASKER

Any example?

Or something to get started with...?
Here is an example using C#. The example uses FindWindow to locate an app and then closes the associated Window. Once you the dialog, instead of closing it, you can use GetWindowText.

http://www.codeproject.com/csharp/cskillapp.asp project with same C# code that demonstrates using FindWindow.

http://www.csharphelp.com/archives2/archive301.html another example demonstrating the GetWindowText function
Looks like EnumChildWindows does not enumerate dialog boxes, and we do not also need EnumWindows.

The example below will display a message box containing the title of the warning dialog box that is displayed when you want to exit Notepad without saving text.

Notes:

1. The dialog box should be already open when the code runs.
2. You need to replace "Untitled - Notepad" with the caption of the parent window.

___________________________________________________________

using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TextCapture
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int GetWindow(int handle, int cmd);
        [DllImport("User32.Dll")]
        public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);

        const int GW_HWNDPREV = 3;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int handle = FindWindow(null, "Untitled - Notepad");
            int childHandle = GetWindow(handle, GW_HWNDPREV);
            StringBuilder title = new StringBuilder(256);
            string text = "";
            GetWindowText(childHandle, title, 256);
            text = title.ToString().Trim();
            MessageBox.Show("Title of dialog box: " + text);
        }
    }
}

___________________________________________________________

_______________

  Nayer Naguib
Maybe I'm not clear enough, read the message (the caption from a label inside the DialogBox, not the DialogBox title).. I want to be able to read the message from the DialogBox (Just like the MessageBox) the message box will show some crucial data to me in this message...

Anyway thanks for your answers!...
The following piece of code will capture and display the text of every control on the dialog box window. You will need to do some changes to the code in order to have the application only process the required label text.

________________________________________________________

using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TextCapture
{
      public partial class Form1 : Form
      {
            [DllImport("user32.dll")]
            public static extern int FindWindow(string lpClassName, string lpWindowName);
            [DllImport("user32.dll")]
            public static extern int GetWindow(int handle, int cmd);
            [DllImport("User32.Dll")]
            public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);
            [DllImport("User32.dll")]
            public static extern Boolean EnumChildWindows(int hWndParent, Delegate lpEnumFunc, int lParam);

            const int GW_HWNDPREV = 3;

            public delegate void Callback(int hWnd,int lParam);

            public Form1()
            {
                  InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                  int handle = FindWindow(null, "Untitled - Notepad");
                  int childHandle = GetWindow(handle, GW_HWNDPREV);
                  Callback myCallBack = new Callback(GetMessage);
                  EnumChildWindows(childHandle, myCallBack, 0);
            }

            public void GetMessage(int hWnd, int lParam)
            {
                  StringBuilder contents = new StringBuilder(256);
                  string txt="";
                  GetWindowText(hWnd, contents, 256);
                  txt = contents.ToString().Trim();
                  MessageBox.Show("Text shown on control: "+ txt);
            }
      }
}

________________________________________________________

_______________

  Nayer Naguib
The last code you have provided me it just doesn't do anything, even with the Notepad example, but anyway thanks.

ASKER CERTIFIED SOLUTION
Avatar of nayernaguib
nayernaguib
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin