Click here to Skip to main content
15,067,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I have the following code:
C#
private void button1_Click(object sender, EventArgs e)
 {
    Process[] plist = Process.GetProcesses();
    foreach (Process p in plist)
     {
      if (p.ProcessName == "notepad")
       {
         AutomationElement ae = AutomationElement.FromHandle(p.MainWindowHandle);

         AutomationElement npEdit = ae.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Edit"));

         TextPattern tp = npEdit.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
         TextPatternRange[] trs;

         if (tp.SupportedTextSelection == SupportedTextSelection.None)
          {
             return;
          }
          else
          {
            trs = tp.GetSelection();
            label1.Text = trs[0].GetText(-1);
          }
       }
     }
  }

The above code is used to get Selected Text form any window but it is not working in some browsers (IE, Chrome, Firefox), The main problem in this line
AutomationElement ae = AutomationElement.FromHandle(p.MainWindowHandle);


The P.MainWindowHandle always return 0 in the case of browser. Please help.

Thanks.
Posted
Updated 13-Dec-10 6:16am
v2
Comments
TweakBird 13-Dec-10 12:17pm
   
Edited for code blocks; please use <pre> tag for code.
Saksida Bojan 13-Dec-10 13:38pm
   
So for IE, crome or FF what do you put for if (p.ProcessName == "??")
And can you try p.MainModule.ProcessName or something. Since i am not in front of VS i can not garante accuarty but MainModule is always a startup module of an exe. MainWindowHandle always return 0 can mean a few thing, It is a system process, windows isn't created but exsist only in process as a modul or your app does not have PROCESS_QUERY_INFORMATION permision. Try ranning as administrator. But this answer could be inacurate.

1 solution

When I replace p.ProcessName == "iexplore" I do get a p.MainWindowHandle different of 0

Then the problem I get is that it does not find a ClassNameProperty with name Edit.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900