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.
Thanks.
ASKER
Any example?
Or something to get started with...?
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
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.InteropServ ices;
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
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.InteropServ
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,
text = title.ToString().Trim();
MessageBox.Show("Title of dialog box: " + text);
}
}
}
__________________________
_______________
Nayer Naguib
ASKER
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!...
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.InteropServ ices;
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(childHand le, 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
__________________________
using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServ
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(childHand
}
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
ASKER
The last code you have provided me it just doesn't do anything, even with the Notepad example, but anyway thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Forced accept.
Computer101
EE Admin
Computer101
EE Admin
_______________
Nayer Naguib