VC 递归调用GetWindow查找控件

HWND g_TargetMainWnd = NULL ;
BOOL WINAPI   EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
        if (:: GetWindowLong(hWnd , GWL_STYLE ) & WS_VISIBLE )
       {
               char szWindowText[128] = { 0 };
               if (:: GetWindowText(hWnd , szWindowText, 128) != 0)
              {
                      if (strstr(szWindowText, "职业技能实训平台" ) != NULL )
                     {
                           g_TargetMainWnd = hWnd;
                            return FALSE;
                     }
              }

       }
        return TRUE;
}

void CAnswerGetDlg ::Work(HWND hSubWnd )
{
        if ( hSubWnd != NULL)
       {             
               while ( hSubWnd != NULL)
              {
                      char szWindowText[128] = { 0 };
                     :: GetWindowText(hSubWnd , szWindowText, 128);
                      if (strstr(szWindowText, "[A]"))
                     {
                            RECT rc;
                            BOOL bret = ::GetWindowRect( hSubWnd, &rc);                                 
                            SetDlgItemText(IDC_EDIT1 , szWindowText + 3);
                            char szCoordinate[16] = { 0 };
                           sprintf(szCoordinate, "%d %d", rc.left, rc.top);
                            SetDlgItemText(IDC_EDIT5 , szCoordinate);
                     }
                      if (strstr(szWindowText, "[B]"))
                     {
                            RECT rc;
                           ::GetWindowRect( hSubWnd, &rc);
                            SetDlgItemText(IDC_EDIT2 , szWindowText + 3);
                            char szCoordinate[16] = { 0 };
                           sprintf(szCoordinate, "%d %d", rc.left, rc.top);
                            SetDlgItemText(IDC_EDIT6 , szCoordinate);
                     }
                      if (strstr(szWindowText, "[C]"))
                     {
                            RECT rc;
                           ::GetWindowRect( hSubWnd, &rc);
                            SetDlgItemText(IDC_EDIT3 , szWindowText + 3);
                            char szCoordinate[16] = { 0 };
                           sprintf(szCoordinate, "%d %d", rc.left, rc.top);
                            SetDlgItemText(IDC_EDIT7 , szCoordinate);
                     }
                      if (strstr(szWindowText, "[D]"))
                     {
                            RECT rc;
                           ::GetWindowRect( hSubWnd, &rc);
                            SetDlgItemText(IDC_EDIT4 , szWindowText + 3);
                            char szCoordinate[16] = { 0 };
                           sprintf(szCoordinate, "%d %d", rc.left, rc.top);
                            SetDlgItemText(IDC_EDIT8 , szCoordinate);
                     }

                      HWND hSubWnd2 = ::GetWindow( hSubWnd, GW_CHILD);
                      if(hSubWnd2 != NULL)
                     {
                           Work(hSubWnd2);
                     }

                      hSubWnd = ::GetWindow( hSubWnd, GW_HWNDNEXT);
              }
       }
}


void CAnswerGetDlg ::OnBnClickedOk()
{
        for ( int i = 0; i < 8; i++)
       {
               SetDlgItemText(1000 + i, "" );
       }
       ::EnumWindows(( WNDENUMPROC)EnumWindowsProc, NULL );
        if (g_TargetMainWnd != NULL)
       {
               HWND hSubWnd = ::GetWindow(g_TargetMainWnd, GW_CHILD);
              Work(hSubWnd);
       }
}
本文为“技术点滴”的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注