遞歸方式+FindWindow 遍歷窗口句柄。查找已知句柄下的所有句柄。按節點方式保存

//使用軟件爲C++builder

////GW_CHILD GW_HWNDNEXT
//int HandCount 定義的全局變量
//HWND Handled[1024]    //定義的全局變量
//DebugInfo(AnsiString str) //自定義的字符串顯示函數

//遞歸函數
void __fastcall TForm1::Gethh(HWND hendle)
{
        HWND Group[1024];
        HWND hh = GetWindow(hendle,GW_CHILD);
        int count = 0;
        if(hh != NULL)
        {
          int i=0;                    
          Group[i] = hh;
          do
          {
            Handled[HandCount++] = Group[i];
            Group[i+1] = GetWindow(Group[i],GW_HWNDNEXT);
            DebugInfo("Group["+AnsiString(i)+"] Hendle is 0x"+IntToHex((__int32)Group[i],8));
            count = i+1;
          }while(i++,NULL != Group[i]);
        }
        else        ////爲了區別分支點,用以下隔開
        {
          Handled[HandCount++] = NULL;  //爲了區別分支點,用NULL隔開
          DebugInfo("");        
        }
        for(int j=0;jLines->Add("Handled["+AnsiString(i)+"] is 0x"+IntToHex((__int32)Handled[i],8));
        }
}
//---------------------------------------------------------------------------

////GW_CHILD GW_HWNDNEXT
//int HandCount 定義的全局變量
//HWND Handled[1024]    //定義的全局變量
//DebugInfo(AnsiString str) //自定義的字符串顯示函數
//遞歸函數
void __fastcall TForm1::Gethh(HWND hendle)
{
        HWND Group[1024];
        HWND hh = GetWindow(hendle,GW_CHILD);
        int count = 0;
        if(hh != NULL)
        {
          int i=0;                   
          Group[i] = hh;
          do
          {
            Handled[HandCount++] = Group[i];
            Group[i+1] = GetWindow(Group[i],GW_HWNDNEXT);
            DebugInfo("Group["+AnsiString(i)+"] Hendle is 0x"+IntToHex((__int32)Group[i],8));
            count = i+1;
          }while(i++,NULL != Group[i]);
        }
        else        ////爲了區別分支點,用以下隔開
        {
          Handled[HandCount++] = NULL;  //爲了區別分支點,用NULL隔開
          DebugInfo("");       
        }
        for(int j=0;j<count;j++)
        {
          Gethh(Group[j]);
        }
}
void __fastcall TForm1::Button6Click(TObject *Sender)   //舉例
{
        HandCount=0;    //初始化全局變量
        //HWND hh1 = FindWindowA("#32769 (??)",NULL);     //"Cypress USB Console");
        HWND hh1 = FindWindowA(NULL,"Cypress USB Console"); //獲取一個窗口句柄
        Gethh(hh1); //調用遞歸函數得到所有句柄
        for(int i=0;i<HandCount;i++)
        {
          Report->Lines->Add("Handled["+AnsiString(i)+"] is 0x"+IntToHex((__int32)Handled[i],8));
        }
}
//---------------------------------------------------------------------------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章