Capturing text in Windows UI controls with C#

  • Thread starter Thread starter TomTom
  • Start date Start date
T

TomTom

Hi,

With C#, is it possible to hover a mouse pointer on the Windows UI controls
and show the UI text? For example, can I hover my mouse over "File" in the
menu bar and show the text "File" on a label?

I've seen a program like this but not sure if it can be done with C#.

Thanks!
TomTom
 
Sorry, I didn't describe what I wanted to do in more details.

I need to capture the text that's in other programs. The scenario looks
like:

1. I activate MyApp.
2. I hover mouse pointer over some menu text, command bar text, etc. in MS
Word.
3. The pointed text show up in MyApp.

Is this possible?

Thanks,
TomTom
 
The MenuItem has a Select event that is called when the mouse is over it.
Use this to set the label text.

Chris
 
I don't know of any way to do it using .NET.
You would probably need functionality similar to Spy++, I tried to select a
MenuItem, but could only get the whole menu.
If it is possible, it would require some interop, try asking in the some of
the win32 api groups, if there are APIs you can use,
these can be wrapped in DllImport for use in .NET.

Chris
 
I had a little progress on my search on how I can do this. Below is the step
I gathered on the web, but I am yet to find how I could use this.

1. In Visual Studio by going to Project->Add Reference. In the .NET tab,
choose the Accessibility.dll..

2. Add the following code in the C# program.

[DllImport("oleacc.dll", EntryPoint="AccessibleObjectFromPoint",
SetLastError=true, CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern int AccessibleObjectFromPoint(Point pt, ref
Accessibility.IAccessible ppacc,ref object pvarChild);//(IntPtr hWnd);

[DllImport("oleacc.dll", EntryPoint="AccessibleObjectFromEvent",
SetLastError=true, CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern int AccessibleObjectFromEvent(int hwnd, int objid,
int childid, ref Accessibility.IAccessible ppacc, ref object pvarChild);

[DllImport("user32.dll", EntryPoint="SetWinEventHook", SetLastError=true,
CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern int SetWinEventHook(int eventMin, int eventMax, int
hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int
idThread, int dwflags);

public delegate int WinEventProc(int hWinEventHook, int idEvent, int hwnd,
int idObject, int idChild, int dwEventThread, int dwmsEventTime);


Then, I am stuck.... Basically I don't know how I can create the
IAccessible object in the C# and call the ac.get_accName function as the
mouse pointer moves... I crafted up an experimental code, but this does not
work.

Accessibility.IAccessible ac = new Accessibility.IAccessible();
Point pt = new Point(50,50);
string test = ac.get_accName(pt);


If you know a way to do this, can you please let me know? Even the general
pseudo-code would help.

Thanks.
Tomtom
 
Back
Top