2012-01-19 38 views
1

我有一个Windows窗体应用程序,它在执行时启动Firefox,抓取窗口的进程和句柄,并执行Firefox的屏幕截图,将其保存到磁盘(temp.bmp)并调用ProcessGetWindow。我基本上使用MODI中的MiSelectRects来捕获我正在寻找的单词周围的矩形,然后使用AutoIT鼠标单击该单词。MODI MiSelectRects获取坐标错误

问题是我的坐标从顶部偏离了大约10个像素。

任何想法可能是错误的?这是处理的功能。我已经将AutoIT处理注释掉了,我只是用一个MessageBox进行调试来显示实际的坐标。然后我用AutoIT的窗口信息工具确认,这绝对是关闭的...我做错了什么,或者是有什么东西搞砸MODI?

public void ProcessGetWindow(Bitmap image) 
     {   
      Document modiDoc = null; 
      MiDocSearch modiSearch = null; 
      IMiSelectableItem modiTextSel = null; 
      MiSelectRects modiSelectRects = null; 
      MiSelectRect modiSelectRect = null; 
      MiRects modiRects = null; 
      int intSelInfoPN; 
      string intSelInfoTop; 
      int intSelInfoBottom; 
      string intSelInfoLeft; 
      int intSelInfoRight;    

      // Load an existing image file. 
      modiDoc = new Document(); 
      modiDoc.Create(@"C:\\temp.bmp"); 

      // Perform OCR. 
      modiDoc.Images[0].OCR(); 

      // Search for the selected word. 
      modiSearch = new MiDocSearch(); 
      modiSearch.Initialize(modiDoc, "Click Me", 0, 0, false, false); 
      modiSearch.Search(null, ref modiTextSel);  

      try 
      {   
       modiSelectRects = modiTextSel.GetSelectRects(); 
      } 
      catch (COMException) 
      { 
       MessageBox.Show("Me thinks that the OCR didn't work right!"); 
      } 

      foreach (MiSelectRect mr in modiSelectRects) 
      { 
       //intSelInfoPN = mr.PageNumber.ToString(); 
       intSelInfoTop = mr.Top.ToString(); 
       //intSelInfoBottom = mr.Bottom; 
       intSelInfoLeft = mr.Left.ToString(); 
       //intSelInfoRight = mr.Right; 

       /*AutoItX3 auto = new AutoItX3(); 
       auto.AutoItSetOption("MouseCoordMode", 2); 
       auto.MouseClick("", intSelInfoLeft, intSelInfoTop, 1, 80);*/ 

       MessageBox.Show("Coordinates: " + intSelInfoLeft + ", " + intSelInfoTop, "Coordinates", MessageBoxButtons.OK);    
      } 

      //string textResult = modiTextSel.Text; 

      //MessageBox.Show(textResult, "Search Results", MessageBoxButtons.OK); 

      // Close this dialog. 
      Application.Exit(); 
     } 
+0

仅供参考,微软已弃用MODI。当用户升级到Office 2010时,您将遇到问题。 – TrueWill

回答

0

我不熟悉,介绍的工具,但是从我读GetSelectRects函数返回一个边框,这是一个包含整个选择,在这种情况下,你搜索词的最小矩形。我相信发生的事情是,你点击边界矩形的角落,而不是在单词所在的中间。

计算坐标为矩形的中心,并尝试一下的是:

int height = mr.Bottom - mr.Top; 
int width = mr.Right - mr.Left; 

AutoItX3 auto = new AutoItX3(); 
auto.AutoItSetOption("MouseCoordMode", 2); 
auto.MouseClick("", width/2, height/2, 1, 80); 
1

我使用同样的程序,找到位置。

int centerwidth = (intSelInfoRight - intSelInfoLeft)/2; 
       centerwidth = intSelInfoLeft + centerwidth; 
       int centerheight = (intSelInfoBottom - intSelInfoTop)/2; 
       centerheight = centerheight + intSelInfoTop; 

你可以找到使用它的文本的确切中间点。

但是这个程序总是给出第一次出现的单词的位置,而不是下一次出现的位置。请让我知道如何在所有事件中找到文本的位置。

0
MODI.Document modiDoc = null; 
    MODI.MiDocSearch modiSearch = null; 
    MODI.IMiSelectableItem modiTextSel = null; 
    MODI.MiSelectRects modiSelectRects = null; 
    MODI.MiSelectRect modiSelectRect = null; 
    MODI.MiRects modiRects = null; 
    int intSelInfoPN; 
    int intSelInfoTop; 
    int intSelInfoBottom; 
    int intSelInfoLeft; 
    int intSelInfoRight; 

    // Load an existing image file. 
    modiDoc = new MODI.Document(); 
    modiDoc.Create(@"C:\Users\h117953\Desktop\OCR\1.jpg"); 

    // Perform OCR. 
    //modiDoc.Images[0].OCR(); 
    //MODI.Image image = (MODI.Image)modiDoc.Images[0]; 
    modiDoc.OCR(MiLANGUAGES.miLANG_ENGLISH); 
    MODI.Image modiImage = (modiDoc.Images[0] as MODI.Image); 


    //string ocrtext = @"C:\Users\h117953\Desktop\OCR\Sample.txt"; 

    //File.WriteAllText(ocrtext, modiImage.Layout.Text); 

    // Search for the selected word. 
    //int wordindex 
    modiSearch = new MODI.MiDocSearch(); 
    //date to search 
    modiSearch.Initialize(modiDoc, "Deer", 0, 2, false, false); 
    modiSearch.Search(null, ref modiTextSel); 
    if (modiTextSel == null) 
    { 
     Response.Write("\nText not found \n"); 


    } 
    else 
    { 
     Response.Write("\nText is found \n"); 
     try 
     { 
      modiSelectRects = modiTextSel.GetSelectRects(); 
     } 
     catch (Exception) 
     { 
      Response.Write("Me thinks that the OCR didn't work right!"); 
     } 

     int centerwidth = 0; 
     int centerheight = 0; 

     foreach (MODI.MiSelectRect mr in modiSelectRects) 
     { 
      //intSelInfoPN = mr.PageNumber.ToString(); 
      intSelInfoTop = mr.Top; 
      intSelInfoBottom = mr.Bottom; 
      intSelInfoLeft = mr.Left; 
      intSelInfoRight = mr.Right; 


      // MessageBox.Show("Coordinates: " + intSelInfoLeft + ", " + intSelInfoTop, "Coordinates", MessageBoxButtons.OK); 
      // MessageBox.Show("Coordinates: " + intSelInfoRight + ", " + intSelInfoBottom, "Coordinates", MessageBoxButtons.OK); 
      centerwidth = (intSelInfoRight - intSelInfoLeft)/2; 
      centerwidth = intSelInfoLeft + centerwidth; 
      centerwidth = (intSelInfoBottom - intSelInfoTop)/2; 
      centerheight = centerheight + intSelInfoTop; 

      //MessageBox.Show("Coordinates: " + centerwidth + ", " + centerheight, "Coordinates", MessageBoxButtons.OK); 
      Response.Write("the Widht and Height co-ordinates are (Width,Height)= ({0},{1})" + centerwidth + ","+ centerheight); 



     }