admin
2023-03-07 8b06b1cbf112d55307ea8a6efe711db4e7506d89
ConsoleApplication/THSActionUtil.cpp
@@ -230,6 +230,33 @@
   return 0;
}
HWND THSActionUtil::getDXJLWindow()
{
   HWND hwnd = getDXJLMainWindow();
   hwnd = FindWindowExA(hwnd, NULL, "#32770", NULL);
   if (Win32Util::isWindowShow(hwnd)) {
      return hwnd;
   }
   return 0;
}
HWND THSActionUtil::getDXJLMainWindow()
{
   list<HWND> wlist = Win32Util::searchWindow("短线精灵");
   list<HWND>::iterator ele;
   for (ele = wlist.begin(); ele != wlist.end(); ele++) {
      HWND hwnd = *ele;
      if (Win32Util::isWindowShow(hwnd)) {
         return hwnd;
      }
   }
   return 0;
}
HWND THSActionUtil::getTradeSuccessWindow() {
   list<HWND> wlist = Win32Util::searchWindow("网上股票交易系统");
@@ -297,7 +324,7 @@
               content_count++;
            }
         }
         if (content_count >= 16)
         if (content_count >= L2_CHENNL_COUNT*2)
         {
            return hwnd;
         }
@@ -591,9 +618,26 @@
   SendMessage(hwnd, WM_LBUTTONUP, 0x00000000, 0x00E70047);
}
void THSActionUtil::openDXJLScreen()
{
   HWND hw = getL2Win();
   if (hw <= 0) {
      throw string("L2未打开");
   }
   Win32Util::focus(hw);
   Win32Util::keyboardNum("DXJL", 300);
   Win32Util::keyboard(13, 10);
   Sleep(1000);
   DEVMODE device = Win32Util::getL2ScreenInfo();
   int device_width = device.dmPelsWidth;
   int device_height = device.dmPelsHeight;
   POINTL device_pos = device.dmPosition;
   Win32Util::moveWin(THSActionUtil::getDXJLMainWindow(), device_pos.x + device_width / 2 + 30, device_pos.y + device_height / 2, 1120, 900);
}
//添加股票
bool THSActionUtil::setGP(std::string quickCode, list<std::string> codeList, RecognitionManager* recognitionManager) {
bool THSActionUtil::setGP(std::string quickCode, list<std::string> codeList, RecognitionManager* recognitionManager, MatOcrFun ocrFun) {
   list<std::string> tempCodeList;
   for (std::list<string>::iterator e = codeList.begin();e != codeList.end();e++) {
@@ -688,7 +732,7 @@
   for (std::list<string>::iterator ele = tempCodeList.begin();ele != tempCodeList.end();++ele) {
      if ((*ele).length() > 0)
      {
         addGP(*ele);
         addGP(*ele, ocrFun);
         Sleep(100);
      }
   }
@@ -722,7 +766,8 @@
}
void THSActionUtil::addGP(string code) {
bool THSActionUtil::addGP(string code, MatOcrFun ocrFun) {
   clock_t start_time = clock();
   //打开副屏
   HWND sw = getSecondWindow();
   if (sw <= 0)
@@ -742,6 +787,8 @@
      throw string("板块截屏内容为空(15)");
   }
   std::list<GPCodeArea>  areaList = recognitionGPArea(img);
   for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();++ele) {
      GPCodeArea codeArea = *ele;
      if (codeArea.type == IMG_TYPE_ADD) {
@@ -750,21 +797,79 @@
         GetWindowRect(content, &rc);
         int y = rc.top + codeArea.starty + (codeArea.endy - codeArea.starty) / 2;
         int x = rc.left + codeArea.startx + (codeArea.endx - codeArea.startx) / 2;
         //截图
         HWND addGPWin = 0;
         for (int i = 0; i < 6; i++) {
         Win32Util::click(x, y, 50);
         Sleep(10);
         Win32Util::click(x, y, 50);
         Sleep(10);
         Win32Util::click(x, y, 50);
         //输入股票代码
         Win32Util::keyboardNum(code, 1000);
         Win32Util::keyboard(VK_RETURN, 1500);
         //关闭按钮
         Sleep(100);
         HWND close = FindWindowA(0, "添加股票");
         PostMessage(close, WM_CLOSE, 0, 0);
            for (int j = 0; j < 20; j++) {
               list<HWND> winList = Win32Util::searchWindow("添加股票");
               if (winList.size() > 0) {
                  for (list<HWND>::iterator ele = winList.begin(); ele != winList.end(); ele++) {
                     if (Win32Util::isWindowShow(*ele)) {
                        addGPWin = *ele;
         break;
      }
                     else {
                        cout << "代码框没显示" << endl;
   }
                  }
               }
               else {
                  cout << "没有获取到代码框" << endl;
                  Sleep(50);
               }
            }
            if (addGPWin > 0) {
               break;
            }
            else {
               Sleep(50);
            }
         }
         if (addGPWin <= 0) {
            throw string("未打开添加代码Window(16)");
         }
         //输入股票代码
         Win32Util::keyboardNum(code, 50);
         // 识别第一个位置的内容
         bool set_success = FALSE;
         for (int i = 0; i < 10; i++) {
            cv::Mat img_ = CaptureUtil::capture(addGPWin);
            cv::Mat grayImg_ = ImgUtil::grayImage(img_);
            cv::Mat rectImg = cv::Mat(grayImg_, cv::Rect(40, 145, 50, 26));
            cv::Mat targetImg = rectImg.clone();
            rectImg.copyTo(targetImg);
            try {
               list<OCRResult> resultList = ocrFun(code, targetImg);
               if (resultList.size() > 0) {
                  set_success = TRUE;
                  break;
               }
               else {
                  Sleep(100);
               }
            }
            catch (...) {
               Sleep(100);
            }
         }
         if (set_success)
         {
            Win32Util::keyboard(VK_RETURN, 50);
         }
         //关闭按钮
         Sleep(50);
         HWND close = FindWindowA(0, "添加股票");
         PostMessage(close, WM_CLOSE, 0, 0);
         return set_success;
      }
   }
   return FALSE;
}
@@ -1879,6 +1984,12 @@
         {
            loginBtn =   FindWindowExA(win, NULL, "Button", "登 录");
            if (loginBtn > 0) {
               // 优先选择电信
               HWND dianXin = FindWindowExA(win, NULL, "Button", "中国电信");
               if (dianXin > 0) {
                  Win32Util::visualClick(dianXin, MAKELONG(2, 2));
                  Sleep(100);
               }
               //点击登录按钮
               Win32Util::visualClick(loginBtn, MAKELONG(2, 2));
               break;
@@ -1892,6 +2003,54 @@
}
bool THSActionUtil::screenL2Money(HWND l2_hwnd, string money, MatOcrFun ocrFun)
{
   if (!Win32Util::isWindowShow(l2_hwnd)) {
      throw string("L2窗口句柄未展示");
   }
   HWND win = FindWindowExA(l2_hwnd, NULL, "#32770", NULL);
   win = FindWindowExA(win, NULL, "stock_trade_info_dlg", "stock_trade_info");
   if (win <= 0) {
      throw string("未找到L2窗口的内容");
   }
   RECT rect;
   Win32Util::getWindowRect(win, &rect);
   //重试5次
   for (int i = 0; i < 5; i++) {
      Win32Util::postMessage(win, WM_LBUTTONDOWN, MAKELONG(1, 0), MAKELONG(0x007E, 0x000A));
      Win32Util::sendMessage(win, WM_PARENTNOTIFY, MAKELONG(1, 0), MAKELONG(0x2FBC, 0x000B));
      Win32Util::postMessage(win, WM_LBUTTONUP, MAKELONG(1, 0), MAKELONG(0x007E, 0x000A));
      Sleep(10);
      HWND child = FindWindowExA(win, NULL, "EditWnd", NULL);
      if (child > 0)
      {
         RECT editRect;
         Win32Util::getWindowRect(child, &editRect);
         //Win32Util::focus(child);
         Win32Util::input_num(child, money);
         Win32Util::focus(child);
         Sleep(50);
         cv::Mat mat = CaptureUtil::capture(win);
         cv::Mat grayImg = ImgUtil::grayImage(mat);
         cv::Mat rectMat = cv::Mat(grayImg, cv::Rect(editRect.left - rect.left, editRect.top - rect.top, editRect.right - editRect.left, editRect.bottom - editRect.top));
         //ImgUtil::markMat(grayImg, cv::Rect(editRect.left - rect.left, editRect.top - rect.top, editRect.right - editRect.left, editRect.bottom - editRect.top),255,1);
         cv::Mat targetMat = rectMat.clone();
         rectMat.copyTo(targetMat);
         list<OCRResult>  results = ocrFun(".", targetMat);
         string newmoney = "";
         for (list<OCRResult>::iterator ele = results.begin(); ele != results.end(); ++ele) {
            newmoney.append((*ele).text);
         }
         if (newmoney == money) {
            return TRUE;
         }
      }
   }
   return FALSE;
}
void THSActionUtil::clearPlateCodes(HWND hwnd)
{
   HWND content = FindWindowExA(hwnd, NULL, "AfxFrameOrView100s", NULL);