我有一个Windows窗体应用程序,在执行时,启动Firefox,抓取窗口的进程和句柄,并执行Firefox的屏幕截图,将其保存到磁盘(temp.bmp)并调用ProcessGetWindow。我基本上在MODI中使用MiSelectRects来捕捉我正在寻找的单词周围的矩形,然后我使用AutoIT鼠标点击该单词。
问题是我的坐标距离顶部约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 :(得分:1)
我使用相同的程序来查找位置。
int centerwidth = (intSelInfoRight - intSelInfoLeft)/2;
centerwidth = intSelInfoLeft + centerwidth;
int centerheight = (intSelInfoBottom - intSelInfoTop)/2;
centerheight = centerheight + intSelInfoTop;
你可以使用它找到文本的确切中间点。
但是这个程序总是给出单词第一次出现的位置,而不是下一次出现的位置。请让我知道如何在所有情况下找到文本的位置。
答案 1 :(得分: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);
答案 2 :(得分: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);
}