admin
2022-08-25 14db1514fec89db599766d4996b4bb1fead0eff0
ConsoleApplication/THSActionUtil.cpp
@@ -1,6 +1,8 @@
#include "THSActionUtil.h"
#include "ImgDivider.h"
#include "Win32Util.h"
#include <fstream>
bool isL2Screen(string str) {
   if (str.find("同花顺(") != string::npos) {
@@ -49,7 +51,7 @@
      std::string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\";
      path.append(std::to_string(rand())).append("_listen.jpg");
      imwrite(path, img);
      throw string("代码数字分隔出错");
      throw string("代码数字分隔出错(32)");
   }
   return   nums;
}
@@ -181,19 +183,36 @@
   }
   if (!zp) {
      throw string("同花顺主屏L2窗口未打开");
      throw string("同花顺主屏L2窗口未打开(01)");
   }
   if (!fp) {
      throw string("同花顺副屏1没有打开");
      throw string("同花顺副屏1没有打开(02)");
   }
   if (!fp2) {
      throw string("同花顺副屏2没有打开");
      throw string("同花顺副屏2没有打开(03)");
   }
   return true;
}
void THSActionUtil::openL2Screen() {
   HWND l2 = getL2Win();
   if (l2 <= 0) {
      //尝试打开主屏
      HWND main = THSActionUtil::getMainWin();
      if (main > 0)
      {
         Win32Util::focus(main);
         Win32Util::keyboardNum(".00", 200);
         Win32Util::keyboard(13, 10);
         Sleep(1000);
      }
   }
}
void THSActionUtil::openSecondScreen() {
@@ -230,7 +249,7 @@
   if (!open) {
      if (mainPage <= 0) {
         throw string("未找到首页");
         throw string("未找到首页(04)");
      }
      Win32Util::focus(mainPage);
@@ -290,7 +309,7 @@
   if (!open) {
      if (mainPage <= 0) {
         throw string("未找到首页");
         throw string("未找到首页(05)");
      }
      Win32Util::focus(mainPage);
@@ -315,6 +334,26 @@
   }
}
void THSActionUtil::openTradeSuccessScreen()
{
   HWND hwnd = getTradeSuccessWindow();
   if (hwnd <= 0) {
      throw string("交易窗口未打开(06)");
   }
   hwnd = FindWindowExA(hwnd, NULL, "AfxMDIFrame140s", NULL);
   hwnd = FindWindowExA(hwnd, NULL, "AfxWnd140s", NULL);
   hwnd = FindWindowExA(hwnd, NULL, NULL, "HexinScrollWnd");
   hwnd = FindWindowExA(hwnd, NULL, NULL, "HexinScrollWnd2");
   hwnd = FindWindowExA(hwnd, NULL, "SysTreeView32", NULL);
   if (hwnd <= 0) {
      throw string("未找到交易窗口菜单(07)");
   }
   //点击当日委托
   SendMessage(hwnd, WM_LBUTTONDOWN, 0x00000001, 0x00E70047);
   SendMessage(hwnd, WM_LBUTTONUP, 0x00000000, 0x00E70047);
}
//添加股票
bool THSActionUtil::setGP(std::string quickCode, list<std::string> codeList, RecognitionManager* recognitionManager) {
@@ -333,7 +372,7 @@
      sw = getSecondWindow();
   }
   if (sw <= 0) {
      throw string("未打开副屏");
      throw string("未打开副屏(11)");
   }
   //打开板块
@@ -354,7 +393,7 @@
   cout << "副屏截图:" << oimg.rows << "-" << oimg.cols << endl;
   if (oimg.cols <= 0 || oimg.rows <= 0) {
      throw string("板块截屏内容为空");
      throw string("板块截屏内容为空(12)");
   }
   cv::Mat img = ImgUtil::grayImage(oimg);
@@ -419,7 +458,7 @@
   oimg = CaptureUtil::capture(content);
   if (oimg.cols <= 0 || oimg.rows <= 0) {
      throw string("板块截屏内容为空");
      throw string("板块截屏内容为空(13)");
   }
   img = ImgUtil::grayImage(oimg);
   areaList = recognitionGPArea(img);
@@ -456,14 +495,14 @@
      sw = getSecondWindow();
   }
   if (sw <= 0) {
      throw string("未打开副屏");
      throw string("未打开副屏(14)");
   }
   HWND content = FindWindowExA(sw, NULL, "AfxFrameOrView100s", NULL);
   cv::Mat oimg = CaptureUtil::capture(content);
   cv::Mat img = ImgUtil::grayImage(oimg);
   if (img.cols <= 0 || img.rows <= 0) {
      throw string("板块截屏内容为空");
      throw string("板块截屏内容为空(15)");
   }
   std::list<GPCodeArea>  areaList = recognitionGPArea(img);
   for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();++ele) {
@@ -502,7 +541,7 @@
   std::list<int*> dataItemList;
   for (int i = start_row;i < end_row;i++) {
      bool empty = ImgDivider::isRowEmpty(img, i, start_col, start_col + 50, 1, _IMG_BINARY_THRESHOLD)&& ImgDivider::isRowEmpty(img, i, start_col+50, start_col + 150, 2, _IMG_BINARY_THRESHOLD) && ImgDivider::isRowEmpty(img, i, start_col + (end_col - start_col) / 2 - 40, start_col + (end_col - start_col) / 2 + 40, 1, _IMG_BINARY_THRESHOLD);
      bool empty = ImgDivider::isRowEmpty(img, i, start_col, start_col + 50, 1, _IMG_BINARY_THRESHOLD) && ImgDivider::isRowEmpty(img, i, start_col + 50, start_col + 150, 2, _IMG_BINARY_THRESHOLD) && ImgDivider::isRowEmpty(img, i, start_col + (end_col - start_col) / 2 - 40, start_col + (end_col - start_col) / 2 + 40, 1, _IMG_BINARY_THRESHOLD);
      if (empty) {
         if (emptyStartRow < 0) {
            emptyStartRow = i;
@@ -584,8 +623,8 @@
      contentEndRow = img.rows - 1;
   }
   if (contentStartRow < 0|| contentEndRow<0) {
      throw string("起始行分隔出错");
   if (contentStartRow < 0 || contentEndRow < 0) {
      throw string("起始行分隔出错(16)");
   }
   //分隔列
@@ -601,7 +640,7 @@
   }
   if (startC < 0) {
      throw string("内容框分隔出错");
      throw string("内容框分隔出错(17)");
   }
   GPCodeArea area = GPCodeArea();
   area.startx = 0;
@@ -629,7 +668,7 @@
   }
   if (contentStartRow < 0) {
      throw string("起始行分隔出错");
      throw string("起始行分隔出错(18)");
   }
   //分隔列
@@ -654,7 +693,7 @@
   }
   if (startC < 0 || endC < 0) {
      throw string("内容框分隔出错");
      throw string("内容框分隔出错(19)");
   }
   //再次分隔内容框,排除干扰
@@ -864,7 +903,7 @@
   }
   if (contentStartRow < 0) {
      throw string("起始行分隔出错");
      throw string("起始行分隔出错(20)");
   }
   //分隔列
@@ -889,7 +928,7 @@
   }
   if (startC < 0 || endC < 0) {
      throw string("内容框分隔出错");
      throw string("内容框分隔出错(21)");
   }
   //cv::imshow("内容", cv::Mat(img, cv::Rect(startC, contentStartRow, endC - startC, rows - contentStartRow)));
@@ -900,7 +939,7 @@
      resultList = splitGPCodeArea(img, contentStartRow + 25, startC, rows, endC);
   }
   catch (...) {
      throw string("分隔代码区域出错");
      throw string("分隔代码区域出错(22)");
   }
   return resultList;
}
@@ -996,7 +1035,7 @@
   }
   //contentStartRow < 0 || contentEndRow < 0
   if (contentStartRow < 0) {
      throw string("起始行或结束行分隔出错");
      throw string("起始行或结束行分隔出错(23)");
   }
   contentEndRow = contentStartRow + 50;
@@ -1115,7 +1154,7 @@
   }
   //contentStartRow < 0 || contentEndRow < 0
   if (contentStartRow < 0) {
      throw string("起始行或结束行分隔出错");
      throw string("起始行或结束行分隔出错(24)");
   }
   contentEndRow = contentStartRow + 50;
@@ -1231,6 +1270,61 @@
}
void THSActionUtil::betchAddGP(HWND addDialog, list<string> codeList)
{
   //将代码写入txt文件
   string path = "D:\\gpcode\\gp.txt";
   ofstream ofs;
   ofs.open(path, ios::trunc);
   int index = 0;
   for (list<string>::iterator ele = codeList.begin();ele != codeList.end();++ele)
   {
      index++;
      ofs << *ele;
      if (index < codeList.size())
         ofs << "\n";
   }
   Sleep(200);
   addDialog = FindWindowExA(addDialog, NULL,"AfxWnd100s","CEFBrowser" );
   addDialog = FindWindowExA(addDialog, NULL, "CefBrowserWindow", NULL);
   addDialog = FindWindowExA(addDialog, NULL, "Chrome_WidgetWin_0", NULL);
   addDialog = FindWindowExA(addDialog, NULL, "Chrome_RenderWidgetHostHWND", NULL);
   cout << "添加股票弹框:"<< addDialog << endl;
   Win32Util::focus(addDialog);
   //图片选择按钮
   SendMessage(addDialog, WM_LBUTTONDOWN, 0x00000001, 0x00370141);
   SendMessage(addDialog, WM_LBUTTONUP, 0x00000000, 0x00370141);
   Sleep(100);
   //上传文件按钮
   SendMessage(addDialog, WM_LBUTTONDOWN, 0x00000001, 0x016000B1);
   SendMessage(addDialog, WM_LBUTTONUP, 0x00000000, 0x016000B1);
   HWND hwnd = HWND(0);
   for (int i = 0;i < 10;i++)
   {
      HWND temp = FindWindowA("#32770", "打开");
      if (temp > 0) {
         hwnd = temp;
         break;
      }
      Sleep(1000);
   }
   Win32Util::selectTexFileWin10(hwnd, "D:\\gpcode\\");
   Sleep(500);
   Win32Util::focus(addDialog);
   //点击添加全部按钮
   SendMessage(addDialog, WM_LBUTTONDOWN, 0x00000001, 0x00570139);
   SendMessage(addDialog, WM_LBUTTONUP, 0x00000000, 0x00570139);
   //关闭弹框
   HWND close = FindWindowA(0, "添加股票");
   SendMessage(close, WM_CLOSE, 0, 0);
}
std::list<GPCodeArea> THSActionUtil::getListenL2GPAreaAndCode(RecognitionManager* recognitionManager) {
   //截图当前有哪些股票代码
   list<HWND> wlist = Win32Util::searchWindow("同花顺(");
@@ -1251,7 +1345,7 @@
      }
   }
   if (mainPage <= 0) {
      throw string("L2监听未打开");
      throw string("L2监听未打开(25)");
   }
   HWND content = FindWindowExA(mainPage, NULL, "AfxFrameOrView100s", NULL);
@@ -1308,7 +1402,66 @@
   return fresultList;
}void THSActionUtil::clearPlateCodes(HWND hwnd)
{
   HWND content = FindWindowExA(hwnd, NULL, "AfxFrameOrView100s", NULL);
   for (int i = 0;i < 15;i++)
   {
      //截图识别是否还有代码
      cv::Mat oimg = CaptureUtil::capture(content);
      if (oimg.cols <= 0 || oimg.rows <= 0) {
         throw string("板块截屏内容为空(26)");
      }
      cv::Mat img = ImgUtil::grayImage(oimg);
      oimg.release();
      std::list<GPCodeArea>  areaList = recognitionGPArea(img);
      cout << "数据数量:" << areaList.size() << endl;
      if (areaList.size() <= 2) {
         //执行最后一次删除
         Win32Util::focus(hwnd);
         Win32Util::keyboard(VK_DELETE, 30);
         break;
      }
      //执行删除事件
      for (int j = 0;j < 20;j++) {
         Win32Util::focus(hwnd);
         Win32Util::keyboard(VK_DELETE, 30);
      }
   }
}
RECT THSActionUtil::getPlateAddCodeBtnPostion(HWND win)
{
   //获取添加按钮位置
   HWND content = FindWindowExA(win, NULL, "AfxFrameOrView100s", NULL);
   RECT contentRect;
   GetWindowRect(content, &contentRect);
   cv::Mat oimg = CaptureUtil::capture(content);
   if (oimg.cols <= 0 || oimg.rows <= 0) {
      throw string("板块截屏内容为空(27)");
   }
   cv::Mat img = ImgUtil::grayImage(oimg);
   oimg.release();
   std::list<GPCodeArea>  areaList = recognitionGPArea(img);
   for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();ele++) {
      cout << "添加按钮位置:" << (*ele).startx << "," << (*ele).starty << "," << (*ele).endx << "," << (*ele).endy << endl;
      if ((*ele).type == IMG_TYPE_ADD) {
         RECT rect;
         rect.left = contentRect.left + (*ele).startx;
         rect.right = contentRect.left + (*ele).endx;
         rect.top = contentRect.top + (*ele).starty;
         rect.bottom = contentRect.top + (*ele).endy;
         return rect;
      }
   }
   throw string("尚未找到添加按钮(28)");
}
bool THSActionUtil::setListenL2GP(int p, string code, RecognitionManager* recognitionManager) {
@@ -1352,7 +1505,7 @@
   }
   if (contentStartRow < 0) {
      throw string("起始行分隔出错");
      throw string("起始行分隔出错(29)");
   }
   //分隔列
@@ -1377,7 +1530,7 @@
   }
   if (startC < 0 || endC < 0) {
      throw string("内容框分隔出错");
      throw string("内容框分隔出错(30)");
   }
   GPCodeArea area = GPCodeArea();
@@ -1387,4 +1540,56 @@
   area.endy = rows - 1;
   return area;
}
}
void THSActionUtil::addTargetCodes(list<std::string> codeList, RecognitionManager* recognitionManager)
{
   //获取同花顺主页
   HWND win = getMainWin();
   Win32Util::focus(win);
   //快捷键打开板块
   Win32Util::keyboardNum("51", 200);
   Win32Util::keyboard(13, 10);
   Sleep(1000);
   //删除板块内的所有内容
   clearPlateCodes(win);
   //获取添加按钮位置
   RECT codeArea = getPlateAddCodeBtnPostion(win);
   //点击添加按钮
   Win32Util::click(codeArea.left + 5, codeArea.top + 3);
   //批量添加代码
   HWND addDLG;
   for (int i = 0;i < 10;i++) {
      addDLG = FindWindowA("#32770", "添加股票");
      if (addDLG > 0) {
         break;
      }
      Sleep(1000);
   }
   if (addDLG <= 0) {
      throw string("添加股票窗口打开失败(31)");
   }
   if (codeList.size() >= 100)
   {
      betchAddGP(addDLG, codeList);
   }
   else {
      //逐个添加
      for (list<std::string>::iterator ele = codeList.begin();ele != codeList.end();++ele) {
         string code = *ele;
         Win32Util::focus(addDLG);
         for (int i = 0;i < 6;i++)
         {
            Win32Util::keyboard(VK_BACK, 10);
         }
         Win32Util::focus(addDLG);
         Win32Util::keyboardNum(code, 1000);
         Win32Util::focus(addDLG);
         Win32Util::keyboard(VK_RETURN, 1500);
      }
      Sleep(100);
      SendMessage(addDLG, WM_CLOSE, 0, 0);
   }
   //TODO 校验代码的正确性
}