HWND FindWindowByCaption(HWND hParentWin, char * caption){ // hParentWin为0,则取desktop HWND hChild,hNext,hResult = 0 ; char cTmp[ 100 ] ; string pattern = caption ; if (hParentWin == 0 ) hParentWin = GetDesktopWindow(); // (0) 每次的处理:比较窗口标题 GetWindowText(hParentWin,cTmp, 100 ); string s = cTmp; // (1) 退出条件:找到 包含caption的窗口 if ( s.find(pattern, 0 ) != 0xffffffff ) return hParentWin; // (2) 退出条件: 无子窗口 if ( (hChild = GetWindow(hParentWin,GW_CHILD) ) == 0 ) return hResult; // (3) 进入方法:列举本窗口下的所有子窗口 hNext = hChild ; while ( hNext != 0 ) { if ( (hResult = FindWindowByCaption(hNext,caption)) != 0 ) return hResult; hNext = GetWindow(hNext,GW_HWNDNEXT); } return hResult;} void gnFindWindow(ParamBlk FAR * parm){ // char cCaption, int hParent HWND hParent = parm -> pCount == 1 ? 0 :(HWND) parm -> p[ 1 ].val.ev_long ; NullTerminate( & parm -> p[ 0 ].val); char * cCaption = ( char * ) _HandToPtr(parm -> p[ 0 ].val.ev_handle); _RetInt( ( long ) FindWindowByCaption(hParent, cCaption), 30 );}
写C++,真是不容易呵,过后再看C#,小菜一碟