TSWindowsAPI.cpp 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403
  1. #include "StdAfx.h"
  2. #include "TSWindowsAPI.h"
  3. #include <Tlhelp32.h>
  4. #include <psapi.h>
  5. #include "TSMyUser32DllFuntion.h"
  6. #include "TSMyKernel32DllFuntion.h"
  7. #include "TSRuntime.h"
  8. #pragma comment ( lib, "psapi.lib" )
  9. extern myOpenProcess my_OpenProcess;
  10. extern myScreenToClient my_ScreenToClient;
  11. extern myClientToScreen my_ClientToScreen;
  12. DWORD MyGetCursorPosRet = 0;
  13. __declspec(naked) BOOL WINAPI My_GetCursorPos(LPPOINT lpPoint)
  14. {
  15. _asm
  16. {
  17. mov edi, edi
  18. push ebp
  19. mov ebp, esp
  20. jmp MyGetCursorPosRet;
  21. }
  22. }
  23. TSWindowsAPI::TSWindowsAPI(void)
  24. {
  25. //if(TSRuntime::InitialWindowsVersion()!=4)//不是win7系统
  26. //{
  27. //TSRuntime::IsInitialmyUser32module=InitialMyUser32Dll();
  28. //TSRuntime::IsInitialmykernel32module=InitialMykernel32Dll();
  29. //if(TSRuntime::IsInitialmyUser32module==false||TSRuntime::IsInitialmykernel32module==false)
  30. // ::MessageBox(0,L"InitialMyUser32Dll失败",L"TS",0);
  31. //else
  32. // ::MessageBox(0,L"InitialMyUser32Dll成功",L"TS",0);
  33. //}
  34. //else
  35. // {
  36. // InitialMykernel32DllWIN7();
  37. // }
  38. if (MyGetCursorPosRet == 0)
  39. {
  40. MyGetCursorPosRet = (DWORD)::GetProcAddress(::GetModuleHandle(L"user32.dll"), "GetCursorPos");
  41. MyGetCursorPosRet += 5;
  42. }
  43. retstringlen = 0;
  44. WindowVerion = TSRuntime::InitialWindowsVersion();
  45. IsEuemprosuccess = 0;
  46. memset(npid, 0, MAX_PATH);
  47. }
  48. TSWindowsAPI::~TSWindowsAPI(void)
  49. {
  50. }
  51. BOOL TSWindowsAPI::EnumProcessbyName(DWORD dwPID, LPCWSTR ExeName, LONG type)
  52. {
  53. if (IsEuemprosuccess == 0)
  54. {
  55. int nItem = 0; // 项计数
  56. PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) };
  57. HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  58. if (hProcessSnap == INVALID_HANDLE_VALUE)
  59. return FALSE;
  60. if (::Process32First(hProcessSnap, &pe32))
  61. {
  62. do
  63. {
  64. if (type == 1)
  65. {
  66. if (wcsstr(pe32.szExeFile, ExeName) != NULL) //模糊匹配
  67. {
  68. npid[nItem] = pe32.th32ProcessID;
  69. IsEuemprosuccess++;
  70. nItem++;
  71. }
  72. }
  73. else
  74. {
  75. if (!wcsicmp(pe32.szExeFile, ExeName))
  76. {
  77. npid[nItem] = pe32.th32ProcessID;
  78. IsEuemprosuccess++;
  79. nItem++;
  80. }
  81. }
  82. } while (::Process32Next(hProcessSnap, &pe32));
  83. }
  84. ::CloseHandle(hProcessSnap);
  85. if (IsEuemprosuccess > 0)
  86. return TRUE;
  87. }
  88. else
  89. {
  90. for (int i = 0; i < IsEuemprosuccess; i++)
  91. {
  92. if (dwPID == npid[i])
  93. return TRUE;
  94. }
  95. }
  96. return FALSE;
  97. }
  98. DWORD TSWindowsAPI::FindChildWnd(HWND hchile, wchar_t* title, wchar_t* classname, wchar_t* retstring, bool isGW_OWNER, bool isVisible, wchar_t* process_name)
  99. {
  100. hchile = ::GetWindow(hchile, GW_HWNDFIRST);
  101. while (hchile != NULL)
  102. {
  103. if (isGW_OWNER) //判断是否要匹配所有者窗口为0的窗口,即顶级窗口
  104. if (::GetWindow(hchile, GW_OWNER) != 0)
  105. {
  106. hchile = ::GetWindow(hchile, GW_HWNDNEXT); //获取下一个窗口
  107. continue;
  108. }
  109. if (isVisible) //判断是否匹配可视窗口
  110. if (::IsWindowVisible(hchile) == false)
  111. {
  112. hchile = ::GetWindow(hchile, GW_HWNDNEXT); //获取下一个窗口
  113. continue;
  114. }
  115. if (title == NULL && classname == NULL)
  116. {
  117. if (process_name)
  118. {
  119. DWORD pid = 0;
  120. GetWindowThreadProcessId(hchile, &pid);
  121. if (EnumProcessbyName(pid, process_name))
  122. {
  123. if (retstring)
  124. {
  125. if (retstringlen == 0)
  126. retstringlen = wcslen(retstring);
  127. if (retstringlen > 1)
  128. swprintf(retstring, L"%s,%d", retstring, hchile);
  129. else
  130. swprintf(retstring, L"%d", hchile);
  131. }
  132. else
  133. return (DWORD)hchile;
  134. }
  135. }
  136. else
  137. {
  138. if (retstring)
  139. {
  140. int len = wcslen(retstring);
  141. if (len > 1)
  142. swprintf(retstring, L"%s,%d", retstring, hchile);
  143. else
  144. swprintf(retstring, L"%d", hchile);
  145. }
  146. else
  147. return (DWORD)hchile;
  148. }
  149. }
  150. else if (title != NULL && classname != NULL)
  151. {
  152. wchar_t WindowClassName[MAX_PATH] = { 0 };
  153. ::GetClassName(hchile, WindowClassName, MAX_PATH);
  154. wchar_t WindowTitle[MAX_PATH] = { 0 };
  155. ::GetWindowText(hchile, WindowTitle, MAX_PATH);
  156. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  157. {
  158. wchar_t* strfindclass = wcsstr(WindowClassName, classname); //模糊匹配
  159. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  160. if (strfindclass && strfindtitle)
  161. {
  162. if (process_name)//EnumWindowByProcess
  163. {
  164. DWORD pid = 0;
  165. GetWindowThreadProcessId(hchile, &pid);
  166. if (EnumProcessbyName(pid, process_name))
  167. {
  168. if (retstring)
  169. {
  170. if (retstringlen == 0)
  171. retstringlen = wcslen(retstring);
  172. if (retstringlen > 1)
  173. swprintf(retstring, L"%s,%d", retstring, hchile);
  174. else
  175. swprintf(retstring, L"%d", hchile);
  176. }
  177. else
  178. return (DWORD)hchile;
  179. }
  180. }
  181. else
  182. {
  183. if (retstring)
  184. {
  185. if (retstringlen == 0)
  186. retstringlen = wcslen(retstring);
  187. if (retstringlen > 1)
  188. swprintf(retstring, L"%s,%d", retstring, hchile);
  189. else
  190. swprintf(retstring, L"%d", hchile);
  191. }
  192. else
  193. return (DWORD)hchile;
  194. }
  195. }
  196. }
  197. }
  198. else if (title != NULL)
  199. {
  200. wchar_t WindowTitle[MAX_PATH] = { 0 };
  201. ::GetWindowText(hchile, WindowTitle, MAX_PATH);
  202. if (wcslen(WindowTitle) > 1)
  203. {
  204. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  205. if (strfind)
  206. {
  207. if (process_name)//EnumWindowByProcess
  208. {
  209. DWORD pid = 0;
  210. GetWindowThreadProcessId(hchile, &pid);
  211. if (EnumProcessbyName(pid, process_name))
  212. {
  213. if (retstring)
  214. {
  215. if (retstringlen == 0)
  216. retstringlen = wcslen(retstring);
  217. if (retstringlen > 1)
  218. swprintf(retstring, L"%s,%d", retstring, hchile);
  219. else
  220. swprintf(retstring, L"%d", hchile);
  221. }
  222. else
  223. return (DWORD)hchile;
  224. }
  225. }
  226. else
  227. {
  228. if (retstring)
  229. {
  230. if (retstringlen == 0)
  231. retstringlen = wcslen(retstring);
  232. if (retstringlen > 1)
  233. swprintf(retstring, L"%s,%d", retstring, hchile);
  234. else
  235. swprintf(retstring, L"%d", hchile);
  236. }
  237. else
  238. return (DWORD)hchile;
  239. }
  240. }
  241. }
  242. }
  243. else if (classname != NULL)
  244. {
  245. wchar_t WindowClassName[MAX_PATH] = { 0 };
  246. ::GetClassName(hchile, WindowClassName, MAX_PATH);
  247. if (wcslen(WindowClassName) > 1)
  248. {
  249. wchar_t* strfind = wcsstr(WindowClassName, classname); //模糊匹配
  250. if (strfind)
  251. {
  252. if (process_name)//EnumWindowByProcess
  253. {
  254. DWORD pid = 0;
  255. GetWindowThreadProcessId(hchile, &pid);
  256. if (EnumProcessbyName(pid, process_name))
  257. {
  258. if (retstring)
  259. {
  260. if (retstringlen == 0)
  261. retstringlen = wcslen(retstring);
  262. if (retstringlen > 1)
  263. swprintf(retstring, L"%s,%d", retstring, hchile);
  264. else
  265. swprintf(retstring, L"%d", hchile);
  266. }
  267. else
  268. return (DWORD)hchile;
  269. }
  270. }
  271. else
  272. {
  273. if (retstring)
  274. {
  275. if (retstringlen == 0)
  276. retstringlen = wcslen(retstring);
  277. if (retstringlen > 1)
  278. swprintf(retstring, L"%s,%d", retstring, hchile);
  279. else
  280. swprintf(retstring, L"%d", hchile);
  281. }
  282. else
  283. return (DWORD)hchile;
  284. }
  285. }
  286. }
  287. }
  288. HWND hchilechile = ::GetWindow(hchile, GW_CHILD);
  289. if (hchilechile != NULL)
  290. {
  291. DWORD dret = FindChildWnd(hchilechile, title, classname, retstring, isGW_OWNER, isVisible, process_name);
  292. if (dret > 0)
  293. break;
  294. }
  295. hchile = ::GetWindow(hchile, GW_HWNDNEXT); //获取下一个窗口
  296. }
  297. return 0;
  298. }
  299. //TSEnumWindow:filter整形数: 取值定义如下
  300. //
  301. //1 : 匹配窗口标题,参数title有效
  302. //
  303. //2 : 匹配窗口类名,参数class_name有效.
  304. //
  305. //4 : 只匹配指定父窗口的第一层孩子窗口
  306. //
  307. //8 : 匹配所有者窗口为0的窗口,即顶级窗口
  308. //
  309. //16 : 匹配可见的窗口
  310. //
  311. //32 : 匹配出的窗口按照窗口打开顺序依次排列
  312. bool TSWindowsAPI::TSEnumWindow(HWND parent, wchar_t* title, wchar_t* class_name, LONG filter, wchar_t* retstring, wchar_t* process_name)
  313. {
  314. bool bret = false;
  315. bool bZwindow = false;//匹配出的窗口按照窗口打开顺序依次排列
  316. if (parent == 0)
  317. {
  318. parent = GetDesktopWindow();
  319. }
  320. if (filter > 32)
  321. {
  322. bZwindow = true;//说明要排序窗口句柄
  323. filter = filter - 32;
  324. }
  325. DWORD procpids[MAX_PATH] = { 0 };
  326. int indexpid = 0;
  327. if (process_name)//EnumWindowByProcess
  328. {
  329. if (wcslen(process_name) < 1)
  330. return false;
  331. memset(npid, 0, MAX_PATH);
  332. IsEuemprosuccess = 0;
  333. if (EnumProcessbyName(0, process_name) == false)
  334. return false;
  335. }
  336. DWORD processpid = 0;
  337. retstringlen = 0;
  338. switch (filter)
  339. {
  340. case 0: //所有模式
  341. {
  342. if (process_name) //EnumWindowByProcess
  343. {
  344. return false;
  345. }
  346. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  347. if (p == NULL)
  348. return false;
  349. p = ::GetWindow(p, GW_HWNDFIRST);
  350. while (p != NULL)
  351. {
  352. if (retstringlen == 0)
  353. retstringlen = wcslen(retstring);
  354. if (retstringlen > 1)
  355. swprintf(retstring, L"%s,%d", retstring, p);
  356. else
  357. swprintf(retstring, L"%d", p);
  358. bret = true;
  359. HWND hchile = ::GetWindow(p, GW_CHILD);
  360. if (hchile != NULL)
  361. {
  362. FindChildWnd(hchile, NULL, NULL, retstring);
  363. }
  364. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  365. }
  366. break;
  367. }
  368. case 1://1 : 匹配窗口标题,参数title有效
  369. {
  370. if (wcslen(title) < 1)
  371. return false;
  372. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  373. if (p == NULL)
  374. return false;
  375. p = ::GetWindow(p, GW_HWNDFIRST);
  376. while (p != NULL)
  377. {
  378. wchar_t WindowTitle[MAX_PATH] = { 0 };
  379. ::GetWindowText(p, WindowTitle, MAX_PATH);
  380. if (wcslen(WindowTitle) > 1)
  381. {
  382. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  383. if (strfind)
  384. {
  385. if (process_name)//EnumWindowByProcess
  386. {
  387. DWORD pid = 0;
  388. GetWindowThreadProcessId(p, &pid);
  389. if (EnumProcessbyName(pid, process_name))
  390. {
  391. if (retstringlen == 0)
  392. retstringlen = wcslen(retstring);
  393. if (retstringlen > 1)
  394. swprintf(retstring, L"%s,%d", retstring, p);
  395. else
  396. swprintf(retstring, L"%d", p);
  397. bret = true;
  398. }
  399. }
  400. else
  401. {
  402. if (retstringlen == 0)
  403. retstringlen = wcslen(retstring);
  404. if (retstringlen > 1)
  405. swprintf(retstring, L"%s,%d", retstring, p);
  406. else
  407. swprintf(retstring, L"%d", p);
  408. bret = true;
  409. HWND hchile = ::GetWindow(p, GW_CHILD);
  410. if (hchile != NULL)
  411. {
  412. FindChildWnd(hchile, title, NULL, retstring);
  413. }
  414. }
  415. }
  416. }
  417. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  418. }
  419. break;
  420. }
  421. case 2://2 : 匹配窗口类名,参数class_name有效.
  422. {
  423. if (wcslen(class_name) < 1)
  424. return false;
  425. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  426. if (p == NULL)
  427. return false;
  428. p = ::GetWindow(p, GW_HWNDFIRST);
  429. while (p != NULL)
  430. {
  431. wchar_t WindowClassName[MAX_PATH] = { 0 };
  432. ::GetClassName(p, WindowClassName, MAX_PATH);
  433. if (wcslen(WindowClassName) > 1)
  434. {
  435. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  436. if (strfind)
  437. {
  438. if (process_name)//EnumWindowByProcess
  439. {
  440. DWORD pid = 0;
  441. GetWindowThreadProcessId(p, &pid);
  442. if (EnumProcessbyName(pid, process_name))
  443. {
  444. if (retstringlen == 0)
  445. retstringlen = wcslen(retstring);
  446. if (retstringlen > 1)
  447. swprintf(retstring, L"%s,%d", retstring, p);
  448. else
  449. swprintf(retstring, L"%d", p);
  450. bret = true;
  451. }
  452. }
  453. else
  454. {
  455. if (retstringlen == 0)
  456. retstringlen = wcslen(retstring);
  457. if (retstringlen > 1)
  458. swprintf(retstring, L"%s,%d", retstring, p);
  459. else
  460. swprintf(retstring, L"%d", p);
  461. bret = true;
  462. HWND hchile = ::GetWindow(p, GW_CHILD);
  463. if (hchile != NULL)
  464. {
  465. FindChildWnd(hchile, NULL, class_name, retstring);
  466. }
  467. }
  468. }
  469. }
  470. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  471. }
  472. break;
  473. }
  474. case 3: //1.窗口标题+2.窗口类名
  475. {
  476. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  477. return false;
  478. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  479. if (p == NULL)
  480. return false;
  481. p = ::GetWindow(p, GW_HWNDFIRST);
  482. while (p != NULL)
  483. {
  484. wchar_t WindowClassName[MAX_PATH] = { 0 };
  485. ::GetClassName(p, WindowClassName, MAX_PATH);
  486. wchar_t WindowTitle[MAX_PATH] = { 0 };
  487. ::GetWindowText(p, WindowTitle, MAX_PATH);
  488. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  489. {
  490. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  491. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  492. if (strfindclass && strfindtitle)
  493. {
  494. if (process_name)//EnumWindowByProcess
  495. {
  496. DWORD pid = 0;
  497. GetWindowThreadProcessId(p, &pid);
  498. if (EnumProcessbyName(pid, process_name))
  499. {
  500. if (retstringlen == 0)
  501. retstringlen = wcslen(retstring);
  502. if (retstringlen > 1)
  503. swprintf(retstring, L"%s,%d", retstring, p);
  504. else
  505. swprintf(retstring, L"%d", p);
  506. bret = true;
  507. }
  508. }
  509. else
  510. {
  511. if (retstringlen == 0)
  512. retstringlen = wcslen(retstring);
  513. if (retstringlen > 1)
  514. swprintf(retstring, L"%s,%d", retstring, p);
  515. else
  516. swprintf(retstring, L"%d", p);
  517. bret = true;
  518. HWND hchile = ::GetWindow(p, GW_CHILD);
  519. if (hchile != NULL)
  520. {
  521. FindChildWnd(hchile, title, class_name, retstring);
  522. }
  523. }
  524. }
  525. }
  526. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  527. }
  528. break;
  529. }
  530. case 4: //4 : 只匹配指定父窗口的第一层孩子窗口
  531. {
  532. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  533. if (p == NULL)
  534. return false;
  535. p = ::GetWindow(p, GW_HWNDFIRST);
  536. while (p != NULL)
  537. {
  538. if (process_name)//EnumWindowByProcess
  539. {
  540. DWORD pid = 0;
  541. GetWindowThreadProcessId(p, &pid);
  542. if (EnumProcessbyName(pid, process_name))
  543. {
  544. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  545. {
  546. if (indexpid < IsEuemprosuccess)
  547. {
  548. indexpid++;
  549. processpid = pid;
  550. memset(retstring, 0, retstringlen);//清空返回字符串
  551. retstringlen = 0;
  552. }
  553. }
  554. if (processpid == pid)
  555. {
  556. if (retstringlen == 0)
  557. retstringlen = wcslen(retstring);
  558. if (retstringlen > 1)
  559. swprintf(retstring, L"%s,%d", retstring, p);
  560. else
  561. swprintf(retstring, L"%d", p);
  562. bret = true;
  563. HWND hchile = ::GetWindow(p, GW_CHILD);
  564. if (hchile != NULL)
  565. {
  566. FindChildWnd(hchile, NULL, NULL, retstring, false, false, process_name);
  567. }
  568. }
  569. }
  570. }
  571. else
  572. {
  573. if (retstringlen == 0)
  574. retstringlen = wcslen(retstring);
  575. if (retstringlen > 1)
  576. swprintf(retstring, L"%s,%d", retstring, p);
  577. else
  578. swprintf(retstring, L"%d", p);
  579. bret = true;
  580. }
  581. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  582. }
  583. break;
  584. }
  585. case 5: //1.匹配窗口标题+//4 : 只匹配指定父窗口的第一层孩子窗口
  586. {
  587. if (wcslen(title) < 1)
  588. return false;
  589. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  590. if (p == NULL)
  591. return false;
  592. p = ::GetWindow(p, GW_HWNDFIRST);
  593. while (p != NULL)
  594. {
  595. if (process_name)//EnumWindowByProcess
  596. {
  597. DWORD pid = 0;
  598. GetWindowThreadProcessId(p, &pid);
  599. if (EnumProcessbyName(pid, process_name))
  600. {
  601. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  602. {
  603. if (indexpid < IsEuemprosuccess)
  604. {
  605. indexpid++;
  606. processpid = pid;
  607. memset(retstring, 0, retstringlen);//清空返回字符串
  608. retstringlen = 0;
  609. }
  610. }
  611. if (processpid == pid)
  612. {
  613. wchar_t WindowTitle[MAX_PATH] = { 0 };
  614. ::GetWindowText(p, WindowTitle, MAX_PATH);
  615. if (wcslen(WindowTitle) > 1)
  616. {
  617. if (wcsstr(WindowTitle, title))
  618. {
  619. if (retstringlen == 0)
  620. retstringlen = wcslen(retstring);
  621. if (retstringlen > 1)
  622. swprintf(retstring, L"%s,%d", retstring, p);
  623. else
  624. swprintf(retstring, L"%d", p);
  625. bret = true;
  626. }
  627. }
  628. HWND hchile = ::GetWindow(p, GW_CHILD);
  629. if (hchile != NULL)
  630. {
  631. FindChildWnd(hchile, title, NULL, retstring, false, false, process_name);
  632. }
  633. }
  634. }
  635. }
  636. else
  637. {
  638. wchar_t WindowTitle[MAX_PATH] = { 0 };
  639. ::GetWindowText(p, WindowTitle, MAX_PATH);
  640. if (wcslen(WindowTitle) > 1)
  641. {
  642. if (wcsstr(WindowTitle, title))
  643. {
  644. if (retstringlen == 0)
  645. retstringlen = wcslen(retstring);
  646. if (retstringlen > 1)
  647. swprintf(retstring, L"%s,%d", retstring, p);
  648. else
  649. swprintf(retstring, L"%d", p);
  650. bret = true;
  651. }
  652. }
  653. }
  654. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  655. }
  656. break;
  657. }
  658. case 6: //2 : 匹配窗口类名+4 : 只匹配指定父窗口的第一层孩子窗口
  659. {
  660. if (wcslen(class_name) < 1)
  661. return false;
  662. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  663. if (p == NULL)
  664. return false;
  665. p = ::GetWindow(p, GW_HWNDFIRST);
  666. while (p != NULL)
  667. {
  668. if (process_name)//EnumWindowByProcess
  669. {
  670. DWORD pid = 0;
  671. GetWindowThreadProcessId(p, &pid);
  672. if (EnumProcessbyName(pid, process_name))
  673. {
  674. if (indexpid < IsEuemprosuccess)
  675. {
  676. indexpid++;
  677. processpid = pid;
  678. memset(retstring, 0, retstringlen);//清空返回字符串
  679. retstringlen = 0;
  680. }
  681. }
  682. if (processpid == pid)
  683. {
  684. wchar_t WindowClassName[MAX_PATH] = { 0 };
  685. ::GetClassName(p, WindowClassName, MAX_PATH);
  686. if (wcslen(WindowClassName) > 1)
  687. {
  688. if (wcsstr(WindowClassName, class_name))
  689. {
  690. if (retstringlen == 0)
  691. retstringlen = wcslen(retstring);
  692. if (retstringlen > 1)
  693. swprintf(retstring, L"%s,%d", retstring, p);
  694. else
  695. swprintf(retstring, L"%d", p);
  696. bret = true;
  697. }
  698. }
  699. HWND hchile = ::GetWindow(p, GW_CHILD);
  700. if (hchile != NULL)
  701. {
  702. FindChildWnd(hchile, NULL, class_name, retstring, false, false, process_name);
  703. }
  704. }
  705. }
  706. else
  707. {
  708. wchar_t WindowClassName[MAX_PATH] = { 0 };
  709. ::GetClassName(p, WindowClassName, MAX_PATH);
  710. if (wcslen(WindowClassName) > 1)
  711. {
  712. if (wcsstr(WindowClassName, class_name))
  713. {
  714. if (retstringlen == 0)
  715. retstringlen = wcslen(retstring);
  716. if (retstringlen > 1)
  717. swprintf(retstring, L"%s,%d", retstring, p);
  718. else
  719. swprintf(retstring, L"%d", p);
  720. bret = true;
  721. }
  722. }
  723. }
  724. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  725. }
  726. break;
  727. }
  728. case 7: //1.窗口标题+2.窗口类名+4 : 只匹配指定父窗口的第一层孩子窗口
  729. {
  730. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  731. return false;
  732. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  733. if (p == NULL)
  734. return false;
  735. p = ::GetWindow(p, GW_HWNDFIRST);
  736. while (p != NULL)
  737. {
  738. if (process_name)//EnumWindowByProcess
  739. {
  740. DWORD pid = 0;
  741. GetWindowThreadProcessId(p, &pid);
  742. if (EnumProcessbyName(pid, process_name))
  743. {
  744. if (indexpid < IsEuemprosuccess)
  745. {
  746. indexpid++;
  747. processpid = pid;
  748. memset(retstring, 0, retstringlen);//清空返回字符串
  749. retstringlen = 0;
  750. }
  751. }
  752. if (processpid == pid)
  753. {
  754. wchar_t WindowClassName[MAX_PATH] = { 0 };
  755. ::GetClassName(p, WindowClassName, MAX_PATH);
  756. wchar_t WindowTitle[MAX_PATH] = { 0 };
  757. ::GetWindowText(p, WindowTitle, MAX_PATH);
  758. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  759. {
  760. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  761. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  762. if (strfindclass && strfindtitle)
  763. {
  764. if (retstringlen == 0)
  765. retstringlen = wcslen(retstring);
  766. if (retstringlen > 1)
  767. swprintf(retstring, L"%s,%d", retstring, p);
  768. else
  769. swprintf(retstring, L"%d", p);
  770. bret = true;
  771. }
  772. }
  773. HWND hchile = ::GetWindow(p, GW_CHILD);
  774. if (hchile != NULL)
  775. {
  776. FindChildWnd(hchile, title, class_name, retstring, false, false, process_name);
  777. }
  778. }
  779. }
  780. else
  781. {
  782. wchar_t WindowClassName[MAX_PATH] = { 0 };
  783. ::GetClassName(p, WindowClassName, MAX_PATH);
  784. wchar_t WindowTitle[MAX_PATH] = { 0 };
  785. ::GetWindowText(p, WindowTitle, MAX_PATH);
  786. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  787. {
  788. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  789. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  790. if (strfindclass && strfindtitle)
  791. {
  792. if (retstringlen == 0)
  793. retstringlen = wcslen(retstring);
  794. if (retstringlen > 1)
  795. swprintf(retstring, L"%s,%d", retstring, p);
  796. else
  797. swprintf(retstring, L"%d", p);
  798. bret = true;
  799. }
  800. }
  801. }
  802. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  803. }
  804. break;
  805. }
  806. case 8: //8 : 匹配所有者窗口为0的窗口,即顶级窗口
  807. {
  808. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  809. if (p == NULL)
  810. return false;
  811. p = ::GetWindow(p, GW_HWNDFIRST);
  812. while (p != NULL)
  813. {
  814. if (::GetWindow(p, GW_OWNER) == 0)
  815. {
  816. if (process_name)//EnumWindowByProcess
  817. {
  818. DWORD pid = 0;
  819. GetWindowThreadProcessId(p, &pid);
  820. if (EnumProcessbyName(pid, process_name))
  821. {
  822. if (retstringlen == 0)
  823. retstringlen = wcslen(retstring);
  824. if (retstringlen > 1)
  825. swprintf(retstring, L"%s,%d", retstring, p);
  826. else
  827. swprintf(retstring, L"%d", p);
  828. bret = true;
  829. HWND hchile = ::GetWindow(p, GW_CHILD);
  830. if (hchile != NULL)
  831. {
  832. FindChildWnd(hchile, NULL, NULL, retstring, true, false, process_name);
  833. }
  834. }
  835. }
  836. else
  837. {
  838. if (retstringlen == 0)
  839. retstringlen = wcslen(retstring);
  840. if (retstringlen > 1)
  841. swprintf(retstring, L"%s,%d", retstring, p);
  842. else
  843. swprintf(retstring, L"%d", p);
  844. bret = true;
  845. HWND hchile = ::GetWindow(p, GW_CHILD);
  846. if (hchile != NULL)
  847. {
  848. FindChildWnd(hchile, NULL, NULL, retstring, true);
  849. }
  850. }
  851. }
  852. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  853. }
  854. break;
  855. }
  856. case 9: //1.窗口标题+8 : 匹配所有者窗口为0的窗口,即顶级窗口
  857. {
  858. if (wcslen(title) < 1)
  859. return false;
  860. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  861. if (p == NULL)
  862. return false;
  863. p = ::GetWindow(p, GW_HWNDFIRST);
  864. while (p != NULL)
  865. {
  866. if (::GetWindow(p, GW_OWNER) == 0)
  867. {
  868. if (process_name)//EnumWindowByProcess
  869. {
  870. DWORD pid = 0;
  871. GetWindowThreadProcessId(p, &pid);
  872. if (EnumProcessbyName(pid, process_name))
  873. {
  874. wchar_t WindowTitle[MAX_PATH] = { 0 };
  875. ::GetWindowText(p, WindowTitle, MAX_PATH);
  876. if (wcslen(WindowTitle) > 1)
  877. {
  878. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  879. if (strfind)
  880. {
  881. if (retstringlen == 0)
  882. retstringlen = wcslen(retstring);
  883. if (retstringlen > 1)
  884. swprintf(retstring, L"%s,%d", retstring, p);
  885. else
  886. swprintf(retstring, L"%d", p);
  887. bret = true;
  888. }
  889. }
  890. HWND hchile = ::GetWindow(p, GW_CHILD);
  891. if (hchile != NULL)
  892. {
  893. FindChildWnd(hchile, title, NULL, retstring, true, false, process_name);
  894. }
  895. }
  896. }
  897. else
  898. {
  899. wchar_t WindowTitle[MAX_PATH] = { 0 };
  900. ::GetWindowText(p, WindowTitle, MAX_PATH);
  901. if (wcslen(WindowTitle) > 1)
  902. {
  903. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  904. if (strfind)
  905. {
  906. if (retstringlen == 0)
  907. retstringlen = wcslen(retstring);
  908. if (retstringlen > 1)
  909. swprintf(retstring, L"%s,%d", retstring, p);
  910. else
  911. swprintf(retstring, L"%d", p);
  912. bret = true;
  913. }
  914. }
  915. HWND hchile = ::GetWindow(p, GW_CHILD);
  916. if (hchile != NULL)
  917. {
  918. FindChildWnd(hchile, title, NULL, retstring, true);
  919. }
  920. }
  921. }
  922. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  923. }
  924. break;
  925. }
  926. case 10: //2.窗口类名+8 : 匹配所有者窗口为0的窗口,即顶级窗口
  927. {
  928. if (wcslen(class_name) < 1)
  929. return false;
  930. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  931. if (p == NULL)
  932. return false;
  933. p = ::GetWindow(p, GW_HWNDFIRST);
  934. while (p != NULL)
  935. {
  936. if (::GetWindow(p, GW_OWNER) == 0)
  937. {
  938. if (process_name)//EnumWindowByProcess
  939. {
  940. DWORD pid = 0;
  941. GetWindowThreadProcessId(p, &pid);
  942. if (EnumProcessbyName(pid, process_name))
  943. {
  944. wchar_t WindowClassName[MAX_PATH] = { 0 };
  945. ::GetClassName(p, WindowClassName, MAX_PATH);
  946. if (wcslen(WindowClassName) > 1)
  947. {
  948. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  949. if (strfind)
  950. {
  951. if (retstringlen == 0)
  952. retstringlen = wcslen(retstring);
  953. if (retstringlen > 1)
  954. swprintf(retstring, L"%s,%d", retstring, p);
  955. else
  956. swprintf(retstring, L"%d", p);
  957. bret = true;
  958. }
  959. }
  960. HWND hchile = ::GetWindow(p, GW_CHILD);
  961. if (hchile != NULL)
  962. {
  963. FindChildWnd(hchile, NULL, class_name, retstring, true, false, process_name);
  964. }
  965. }
  966. }
  967. else
  968. {
  969. wchar_t WindowClassName[MAX_PATH] = { 0 };
  970. ::GetClassName(p, WindowClassName, MAX_PATH);
  971. if (wcslen(WindowClassName) > 1)
  972. {
  973. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  974. if (strfind)
  975. {
  976. if (retstringlen == 0)
  977. retstringlen = wcslen(retstring);
  978. if (retstringlen > 1)
  979. swprintf(retstring, L"%s,%d", retstring, p);
  980. else
  981. swprintf(retstring, L"%d", p);
  982. bret = true;
  983. }
  984. }
  985. HWND hchile = ::GetWindow(p, GW_CHILD);
  986. if (hchile != NULL)
  987. {
  988. FindChildWnd(hchile, NULL, class_name, retstring, true);
  989. }
  990. }
  991. }
  992. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  993. }
  994. break;
  995. }
  996. case 11: ////1.窗口标题+2.窗口类名+8 : 匹配所有者窗口为0的窗口,即顶级窗口
  997. {
  998. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  999. return false;
  1000. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1001. if (p == NULL)
  1002. return false;
  1003. p = ::GetWindow(p, GW_HWNDFIRST);
  1004. while (p != NULL)
  1005. {
  1006. if (::GetWindow(p, GW_OWNER) == 0)
  1007. {
  1008. if (process_name)//EnumWindowByProcess
  1009. {
  1010. DWORD pid = 0;
  1011. GetWindowThreadProcessId(p, &pid);
  1012. if (EnumProcessbyName(pid, process_name))
  1013. {
  1014. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1015. ::GetClassName(p, WindowClassName, MAX_PATH);
  1016. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1017. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1018. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  1019. {
  1020. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  1021. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  1022. if (strfindclass && strfindtitle)
  1023. {
  1024. if (retstringlen == 0)
  1025. retstringlen = wcslen(retstring);
  1026. if (retstringlen > 1)
  1027. swprintf(retstring, L"%s,%d", retstring, p);
  1028. else
  1029. swprintf(retstring, L"%d", p);
  1030. bret = true;
  1031. }
  1032. }
  1033. HWND hchile = ::GetWindow(p, GW_CHILD);
  1034. if (hchile != NULL)
  1035. {
  1036. FindChildWnd(hchile, title, class_name, retstring, true, false, process_name);
  1037. }
  1038. }
  1039. }
  1040. else
  1041. {
  1042. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1043. ::GetClassName(p, WindowClassName, MAX_PATH);
  1044. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1045. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1046. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  1047. {
  1048. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  1049. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  1050. if (strfindclass && strfindtitle)
  1051. {
  1052. if (retstringlen == 0)
  1053. retstringlen = wcslen(retstring);
  1054. if (retstringlen > 1)
  1055. swprintf(retstring, L"%s,%d", retstring, p);
  1056. else
  1057. swprintf(retstring, L"%d", p);
  1058. bret = true;
  1059. }
  1060. }
  1061. HWND hchile = ::GetWindow(p, GW_CHILD);
  1062. if (hchile != NULL)
  1063. {
  1064. FindChildWnd(hchile, title, class_name, retstring, true);
  1065. }
  1066. }
  1067. }
  1068. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1069. }
  1070. break;
  1071. }
  1072. case 12: // //4 : 只匹配指定父窗口的第一层孩子窗口+8 : 匹配所有者窗口为0的窗口,即顶级窗口
  1073. {
  1074. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1075. p = ::GetWindow(p, GW_HWNDFIRST);
  1076. while (p != NULL)
  1077. {
  1078. if (::GetWindow(p, GW_OWNER) == 0)
  1079. {
  1080. if (process_name)//EnumWindowByProcess
  1081. {
  1082. DWORD pid = 0;
  1083. GetWindowThreadProcessId(p, &pid);
  1084. if (EnumProcessbyName(pid, process_name))
  1085. {
  1086. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  1087. {
  1088. if (indexpid < IsEuemprosuccess)
  1089. {
  1090. indexpid++;
  1091. processpid = pid;
  1092. memset(retstring, 0, retstringlen);//清空返回字符串
  1093. retstringlen = 0;
  1094. }
  1095. }
  1096. if (processpid == pid)
  1097. {
  1098. if (retstringlen == 0)
  1099. retstringlen = wcslen(retstring);
  1100. if (retstringlen > 1)
  1101. swprintf(retstring, L"%s,%d", retstring, p);
  1102. else
  1103. swprintf(retstring, L"%d", p);
  1104. bret = true;
  1105. HWND hchile = ::GetWindow(p, GW_CHILD);
  1106. if (hchile != NULL)
  1107. {
  1108. FindChildWnd(hchile, NULL, NULL, retstring, true, false, process_name);
  1109. }
  1110. }
  1111. }
  1112. }
  1113. else
  1114. {
  1115. if (retstringlen == 0)
  1116. retstringlen = wcslen(retstring);
  1117. if (retstringlen > 1)
  1118. swprintf(retstring, L"%s,%d", retstring, p);
  1119. else
  1120. swprintf(retstring, L"%d", p);
  1121. bret = true;
  1122. }
  1123. }
  1124. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1125. }
  1126. break;
  1127. }
  1128. case 13: //1.窗口标题+4 : 只匹配指定父窗口的第一层孩子窗口+8 : 匹配所有者窗口为0的窗口,即顶级窗口
  1129. {
  1130. if (wcslen(title) < 1)
  1131. return false;
  1132. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1133. p = ::GetWindow(p, GW_HWNDFIRST);
  1134. while (p != NULL)
  1135. {
  1136. if (::GetWindow(p, GW_OWNER) == 0)
  1137. {
  1138. if (process_name)//EnumWindowByProcess
  1139. {
  1140. DWORD pid = 0;
  1141. GetWindowThreadProcessId(p, &pid);
  1142. if (EnumProcessbyName(pid, process_name))
  1143. {
  1144. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  1145. {
  1146. if (indexpid < IsEuemprosuccess)
  1147. {
  1148. indexpid++;
  1149. processpid = pid;
  1150. memset(retstring, 0, retstringlen);//清空返回字符串
  1151. retstringlen = 0;
  1152. }
  1153. }
  1154. if (processpid == pid)
  1155. {
  1156. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1157. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1158. if (wcslen(WindowTitle) > 1)
  1159. {
  1160. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  1161. if (strfind)
  1162. {
  1163. if (retstringlen == 0)
  1164. retstringlen = wcslen(retstring);
  1165. if (retstringlen > 1)
  1166. swprintf(retstring, L"%s,%d", retstring, p);
  1167. else
  1168. swprintf(retstring, L"%d", p);
  1169. bret = true;
  1170. }
  1171. }
  1172. HWND hchile = ::GetWindow(p, GW_CHILD);
  1173. if (hchile != NULL)
  1174. {
  1175. FindChildWnd(hchile, title, NULL, retstring, true, false, process_name);
  1176. }
  1177. }
  1178. }
  1179. }
  1180. else
  1181. {
  1182. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1183. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1184. if (wcslen(WindowTitle) > 1)
  1185. {
  1186. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  1187. if (strfind)
  1188. {
  1189. if (retstringlen == 0)
  1190. retstringlen = wcslen(retstring);
  1191. if (retstringlen > 1)
  1192. swprintf(retstring, L"%s,%d", retstring, p);
  1193. else
  1194. swprintf(retstring, L"%d", p);
  1195. bret = true;
  1196. }
  1197. }
  1198. }
  1199. }
  1200. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1201. }
  1202. break;
  1203. }
  1204. case 14: //2.窗口类名+4 : 只匹配指定父窗口的第一层孩子窗口+8 : 匹配所有者窗口为0的窗口,即顶级窗口
  1205. {
  1206. if (wcslen(class_name) < 1)
  1207. return false;
  1208. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1209. p = ::GetWindow(p, GW_HWNDFIRST);
  1210. while (p != NULL)
  1211. {
  1212. if (::GetWindow(p, GW_OWNER) == 0)
  1213. {
  1214. if (process_name)//EnumWindowByProcess
  1215. {
  1216. DWORD pid = 0;
  1217. GetWindowThreadProcessId(p, &pid);
  1218. if (EnumProcessbyName(pid, process_name))
  1219. {
  1220. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  1221. {
  1222. if (indexpid < IsEuemprosuccess)
  1223. {
  1224. indexpid++;
  1225. processpid = pid;
  1226. memset(retstring, 0, retstringlen);//清空返回字符串
  1227. retstringlen = 0;
  1228. }
  1229. }
  1230. if (processpid == pid)
  1231. {
  1232. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1233. ::GetClassName(p, WindowClassName, MAX_PATH);
  1234. if (wcslen(WindowClassName) > 1)
  1235. {
  1236. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  1237. if (strfind)
  1238. {
  1239. if (retstringlen == 0)
  1240. retstringlen = wcslen(retstring);
  1241. if (retstringlen > 1)
  1242. swprintf(retstring, L"%s,%d", retstring, p);
  1243. else
  1244. swprintf(retstring, L"%d", p);
  1245. bret = true;
  1246. }
  1247. }
  1248. HWND hchile = ::GetWindow(p, GW_CHILD);
  1249. if (hchile != NULL)
  1250. {
  1251. FindChildWnd(hchile, NULL, class_name, retstring, true, false, process_name);
  1252. }
  1253. }
  1254. }
  1255. }
  1256. else
  1257. {
  1258. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1259. ::GetClassName(p, WindowClassName, MAX_PATH);
  1260. if (wcslen(WindowClassName) > 1)
  1261. {
  1262. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  1263. if (strfind)
  1264. {
  1265. if (retstringlen == 0)
  1266. retstringlen = wcslen(retstring);
  1267. if (retstringlen > 1)
  1268. swprintf(retstring, L"%s,%d", retstring, p);
  1269. else
  1270. swprintf(retstring, L"%d", p);
  1271. bret = true;
  1272. }
  1273. }
  1274. }
  1275. }
  1276. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1277. }
  1278. break;
  1279. }
  1280. case 15: ////1.窗口标题+2.窗口类名+4 : 只匹配指定父窗口的第一层孩子窗口+8 : 匹配所有者窗口为0的窗口,即顶级窗口
  1281. {
  1282. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  1283. return false;
  1284. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1285. p = ::GetWindow(p, GW_HWNDFIRST);
  1286. while (p != NULL)
  1287. {
  1288. if (::GetWindow(p, GW_OWNER) == 0)
  1289. {
  1290. if (process_name)//EnumWindowByProcess
  1291. {
  1292. DWORD pid = 0;
  1293. GetWindowThreadProcessId(p, &pid);
  1294. if (EnumProcessbyName(pid, process_name))
  1295. {
  1296. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  1297. {
  1298. if (indexpid < IsEuemprosuccess)
  1299. {
  1300. indexpid++;
  1301. processpid = pid;
  1302. memset(retstring, 0, retstringlen);//清空返回字符串
  1303. retstringlen = 0;
  1304. }
  1305. }
  1306. if (processpid == pid)
  1307. {
  1308. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1309. ::GetClassName(p, WindowClassName, MAX_PATH);
  1310. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1311. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1312. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  1313. {
  1314. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  1315. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  1316. if (strfindclass && strfindtitle)
  1317. {
  1318. if (retstringlen == 0)
  1319. retstringlen = wcslen(retstring);
  1320. if (retstringlen > 1)
  1321. swprintf(retstring, L"%s,%d", retstring, p);
  1322. else
  1323. swprintf(retstring, L"%d", p);
  1324. bret = true;
  1325. }
  1326. }
  1327. HWND hchile = ::GetWindow(p, GW_CHILD);
  1328. if (hchile != NULL)
  1329. {
  1330. FindChildWnd(hchile, title, class_name, retstring, true, false, process_name);
  1331. }
  1332. }
  1333. }
  1334. }
  1335. else
  1336. {
  1337. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1338. ::GetClassName(p, WindowClassName, MAX_PATH);
  1339. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1340. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1341. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  1342. {
  1343. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  1344. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  1345. if (strfindclass && strfindtitle)
  1346. {
  1347. if (retstringlen == 0)
  1348. retstringlen = wcslen(retstring);
  1349. if (retstringlen > 1)
  1350. swprintf(retstring, L"%s,%d", retstring, p);
  1351. else
  1352. swprintf(retstring, L"%d", p);
  1353. bret = true;
  1354. }
  1355. }
  1356. }
  1357. }
  1358. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1359. }
  1360. break;
  1361. }
  1362. case 16://匹配可见的窗口
  1363. {
  1364. parent = GetDesktopWindow();
  1365. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1366. p = ::GetWindow(p, GW_HWNDFIRST);
  1367. while (p != NULL)
  1368. {
  1369. if (::IsWindowVisible(p))
  1370. {
  1371. if (process_name)//EnumWindowByProcess
  1372. {
  1373. DWORD pid = 0;
  1374. GetWindowThreadProcessId(p, &pid);
  1375. if (EnumProcessbyName(pid, process_name))
  1376. {
  1377. if (retstringlen == 0)
  1378. retstringlen = wcslen(retstring);
  1379. if (retstringlen > 1)
  1380. swprintf(retstring, L"%s,%d", retstring, p);
  1381. else
  1382. swprintf(retstring, L"%d", p);
  1383. bret = true;
  1384. HWND hchile = ::GetWindow(p, GW_CHILD);
  1385. if (hchile != NULL)
  1386. {
  1387. FindChildWnd(hchile, NULL, NULL, retstring, false, true, process_name);
  1388. }
  1389. }
  1390. }
  1391. else
  1392. {
  1393. if (retstringlen == 0)
  1394. retstringlen = wcslen(retstring);
  1395. if (retstringlen > 1)
  1396. swprintf(retstring, L"%s,%d", retstring, p);
  1397. else
  1398. swprintf(retstring, L"%d", p);
  1399. bret = true;
  1400. HWND hchile = ::GetWindow(p, GW_CHILD);
  1401. if (hchile != NULL)
  1402. {
  1403. FindChildWnd(hchile, NULL, NULL, retstring, false, true);
  1404. }
  1405. }
  1406. }
  1407. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1408. }
  1409. break;
  1410. }
  1411. case 17: //1.窗口标题+//匹配可见的窗口
  1412. {
  1413. if (wcslen(title) < 1)
  1414. return false;
  1415. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1416. p = ::GetWindow(p, GW_HWNDFIRST);
  1417. while (p != NULL)
  1418. {
  1419. if (::IsWindowVisible(p))
  1420. {
  1421. if (process_name)//EnumWindowByProcess
  1422. {
  1423. DWORD pid = 0;
  1424. GetWindowThreadProcessId(p, &pid);
  1425. if (EnumProcessbyName(pid, process_name))
  1426. {
  1427. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1428. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1429. if (wcslen(WindowTitle) > 1)
  1430. {
  1431. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  1432. if (strfind)
  1433. {
  1434. if (retstringlen == 0)
  1435. retstringlen = wcslen(retstring);
  1436. if (retstringlen > 1)
  1437. swprintf(retstring, L"%s,%d", retstring, p);
  1438. else
  1439. swprintf(retstring, L"%d", p);
  1440. bret = true;
  1441. }
  1442. }
  1443. HWND hchile = ::GetWindow(p, GW_CHILD);
  1444. if (hchile != NULL)
  1445. {
  1446. FindChildWnd(hchile, title, NULL, retstring, false, true, process_name);
  1447. }
  1448. }
  1449. }
  1450. else
  1451. {
  1452. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1453. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1454. if (wcslen(WindowTitle) > 1)
  1455. {
  1456. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  1457. if (strfind)
  1458. {
  1459. if (retstringlen == 0)
  1460. retstringlen = wcslen(retstring);
  1461. if (retstringlen > 1)
  1462. swprintf(retstring, L"%s,%d", retstring, p);
  1463. else
  1464. swprintf(retstring, L"%d", p);
  1465. bret = true;
  1466. }
  1467. }
  1468. HWND hchile = ::GetWindow(p, GW_CHILD);
  1469. if (hchile != NULL)
  1470. {
  1471. FindChildWnd(hchile, title, NULL, retstring, false, true);
  1472. }
  1473. }
  1474. }
  1475. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1476. }
  1477. break;
  1478. }
  1479. case 18: //2.窗口类名+//匹配可见的窗口
  1480. {
  1481. if (wcslen(class_name) < 1)
  1482. return false;
  1483. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1484. p = ::GetWindow(p, GW_HWNDFIRST);
  1485. while (p != NULL)
  1486. {
  1487. if (::IsWindowVisible(p))
  1488. {
  1489. if (process_name)//EnumWindowByProcess
  1490. {
  1491. DWORD pid = 0;
  1492. GetWindowThreadProcessId(p, &pid);
  1493. if (EnumProcessbyName(pid, process_name))
  1494. {
  1495. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1496. ::GetClassName(p, WindowClassName, MAX_PATH);
  1497. if (wcslen(WindowClassName) > 1)
  1498. {
  1499. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  1500. if (strfind)
  1501. {
  1502. if (retstringlen == 0)
  1503. retstringlen = wcslen(retstring);
  1504. if (retstringlen > 1)
  1505. swprintf(retstring, L"%s,%d", retstring, p);
  1506. else
  1507. swprintf(retstring, L"%d", p);
  1508. bret = true;
  1509. }
  1510. }
  1511. HWND hchile = ::GetWindow(p, GW_CHILD);
  1512. if (hchile != NULL)
  1513. {
  1514. FindChildWnd(hchile, NULL, class_name, retstring, false, true, process_name);
  1515. }
  1516. }
  1517. }
  1518. else
  1519. {
  1520. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1521. ::GetClassName(p, WindowClassName, MAX_PATH);
  1522. if (wcslen(WindowClassName) > 1)
  1523. {
  1524. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  1525. if (strfind)
  1526. {
  1527. if (retstringlen == 0)
  1528. retstringlen = wcslen(retstring);
  1529. if (retstringlen > 1)
  1530. swprintf(retstring, L"%s,%d", retstring, p);
  1531. else
  1532. swprintf(retstring, L"%d", p);
  1533. bret = true;
  1534. }
  1535. }
  1536. HWND hchile = ::GetWindow(p, GW_CHILD);
  1537. if (hchile != NULL)
  1538. {
  1539. FindChildWnd(hchile, NULL, class_name, retstring, false, true);
  1540. }
  1541. }
  1542. }
  1543. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1544. }
  1545. break;
  1546. }
  1547. case 19: ////1.窗口标题+2.窗口类名+匹配可见的窗口
  1548. {
  1549. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  1550. return false;
  1551. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1552. p = ::GetWindow(p, GW_HWNDFIRST);
  1553. while (p != NULL)
  1554. {
  1555. if (::IsWindowVisible(p))
  1556. {
  1557. if (process_name)//EnumWindowByProcess
  1558. {
  1559. DWORD pid = 0;
  1560. GetWindowThreadProcessId(p, &pid);
  1561. if (EnumProcessbyName(pid, process_name))
  1562. {
  1563. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1564. ::GetClassName(p, WindowClassName, MAX_PATH);
  1565. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1566. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1567. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  1568. {
  1569. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  1570. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  1571. if (strfindclass && strfindtitle)
  1572. {
  1573. if (retstringlen == 0)
  1574. retstringlen = wcslen(retstring);
  1575. if (retstringlen > 1)
  1576. swprintf(retstring, L"%s,%d", retstring, p);
  1577. else
  1578. swprintf(retstring, L"%d", p);
  1579. bret = true;
  1580. }
  1581. }
  1582. HWND hchile = ::GetWindow(p, GW_CHILD);
  1583. if (hchile != NULL)
  1584. {
  1585. FindChildWnd(hchile, title, class_name, retstring, false, true, process_name);
  1586. }
  1587. }
  1588. }
  1589. else
  1590. {
  1591. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1592. ::GetClassName(p, WindowClassName, MAX_PATH);
  1593. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1594. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1595. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  1596. {
  1597. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  1598. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  1599. if (strfindclass && strfindtitle)
  1600. {
  1601. if (retstringlen == 0)
  1602. retstringlen = wcslen(retstring);
  1603. if (retstringlen > 1)
  1604. swprintf(retstring, L"%s,%d", retstring, p);
  1605. else
  1606. swprintf(retstring, L"%d", p);
  1607. bret = true;
  1608. }
  1609. }
  1610. HWND hchile = ::GetWindow(p, GW_CHILD);
  1611. if (hchile != NULL)
  1612. {
  1613. FindChildWnd(hchile, title, class_name, retstring, false, true);
  1614. }
  1615. }
  1616. }
  1617. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1618. }
  1619. break;
  1620. }
  1621. case 20: //4 : 只匹配指定父窗口的第一层孩子窗口+匹配可见的窗口
  1622. {
  1623. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1624. p = ::GetWindow(p, GW_HWNDFIRST);
  1625. while (p != NULL)
  1626. {
  1627. if (::IsWindowVisible(p))
  1628. {
  1629. if (process_name)//EnumWindowByProcess
  1630. {
  1631. DWORD pid = 0;
  1632. GetWindowThreadProcessId(p, &pid);
  1633. if (EnumProcessbyName(pid, process_name))
  1634. {
  1635. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  1636. {
  1637. if (indexpid < IsEuemprosuccess)
  1638. {
  1639. indexpid++;
  1640. processpid = pid;
  1641. memset(retstring, 0, retstringlen);//清空返回字符串
  1642. retstringlen = 0;
  1643. }
  1644. }
  1645. if (processpid == pid)
  1646. {
  1647. if (retstringlen == 0)
  1648. retstringlen = wcslen(retstring);
  1649. if (retstringlen > 1)
  1650. swprintf(retstring, L"%s,%d", retstring, p);
  1651. else
  1652. swprintf(retstring, L"%d", p);
  1653. bret = true;
  1654. HWND hchile = ::GetWindow(p, GW_CHILD);
  1655. if (hchile != NULL)
  1656. {
  1657. FindChildWnd(hchile, NULL, NULL, retstring, false, true, process_name);
  1658. }
  1659. }
  1660. }
  1661. }
  1662. else
  1663. {
  1664. if (retstringlen == 0)
  1665. retstringlen = wcslen(retstring);
  1666. if (retstringlen > 1)
  1667. swprintf(retstring, L"%s,%d", retstring, p);
  1668. else
  1669. swprintf(retstring, L"%d", p);
  1670. bret = true;
  1671. }
  1672. }
  1673. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1674. }
  1675. break;
  1676. }
  1677. case 21: //1.窗口标题+4 : 只匹配指定父窗口的第一层孩子窗口+匹配可见的窗口
  1678. {
  1679. if (wcslen(title) < 1)
  1680. return false;
  1681. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1682. p = ::GetWindow(p, GW_HWNDFIRST);
  1683. while (p != NULL)
  1684. {
  1685. if (::IsWindowVisible(p))
  1686. {
  1687. if (process_name)//EnumWindowByProcess
  1688. {
  1689. DWORD pid = 0;
  1690. GetWindowThreadProcessId(p, &pid);
  1691. if (EnumProcessbyName(pid, process_name))
  1692. {
  1693. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  1694. {
  1695. if (indexpid < IsEuemprosuccess)
  1696. {
  1697. indexpid++;
  1698. processpid = pid;
  1699. memset(retstring, 0, retstringlen);//清空返回字符串
  1700. retstringlen = 0;
  1701. }
  1702. }
  1703. if (processpid == pid)
  1704. {
  1705. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1706. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1707. if (wcslen(WindowTitle) > 1)
  1708. {
  1709. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  1710. if (strfind)
  1711. {
  1712. if (retstringlen == 0)
  1713. retstringlen = wcslen(retstring);
  1714. if (retstringlen > 1)
  1715. swprintf(retstring, L"%s,%d", retstring, p);
  1716. else
  1717. swprintf(retstring, L"%d", p);
  1718. bret = true;
  1719. }
  1720. }
  1721. HWND hchile = ::GetWindow(p, GW_CHILD);
  1722. if (hchile != NULL)
  1723. {
  1724. FindChildWnd(hchile, title, NULL, retstring, false, true, process_name);
  1725. }
  1726. }
  1727. }
  1728. }
  1729. else
  1730. {
  1731. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1732. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1733. if (wcslen(WindowTitle) > 1)
  1734. {
  1735. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  1736. if (strfind)
  1737. {
  1738. if (retstringlen == 0)
  1739. retstringlen = wcslen(retstring);
  1740. if (retstringlen > 1)
  1741. swprintf(retstring, L"%s,%d", retstring, p);
  1742. else
  1743. swprintf(retstring, L"%d", p);
  1744. bret = true;
  1745. }
  1746. }
  1747. }
  1748. }
  1749. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1750. }
  1751. break;
  1752. }
  1753. case 22://2.窗口类名+4 : 只匹配指定父窗口的第一层孩子窗口+匹配可见的窗口
  1754. {
  1755. if (wcslen(class_name) < 1)
  1756. return false;
  1757. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1758. p = ::GetWindow(p, GW_HWNDFIRST);
  1759. while (p != NULL)
  1760. {
  1761. if (::IsWindowVisible(p))
  1762. {
  1763. if (process_name)//EnumWindowByProcess
  1764. {
  1765. DWORD pid = 0;
  1766. GetWindowThreadProcessId(p, &pid);
  1767. if (EnumProcessbyName(pid, process_name))
  1768. {
  1769. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  1770. {
  1771. if (indexpid < IsEuemprosuccess)
  1772. {
  1773. indexpid++;
  1774. processpid = pid;
  1775. memset(retstring, 0, retstringlen);//清空返回字符串
  1776. retstringlen = 0;
  1777. }
  1778. }
  1779. if (processpid == pid)
  1780. {
  1781. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1782. ::GetClassName(p, WindowClassName, MAX_PATH);
  1783. if (wcslen(WindowClassName) > 1)
  1784. {
  1785. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  1786. if (strfind)
  1787. {
  1788. if (retstringlen == 0)
  1789. retstringlen = wcslen(retstring);
  1790. if (retstringlen > 1)
  1791. swprintf(retstring, L"%s,%d", retstring, p);
  1792. else
  1793. swprintf(retstring, L"%d", p);
  1794. bret = true;
  1795. }
  1796. }
  1797. HWND hchile = ::GetWindow(p, GW_CHILD);
  1798. if (hchile != NULL)
  1799. {
  1800. FindChildWnd(hchile, NULL, class_name, retstring, false, true, process_name);
  1801. }
  1802. }
  1803. }
  1804. }
  1805. else
  1806. {
  1807. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1808. ::GetClassName(p, WindowClassName, MAX_PATH);
  1809. if (wcslen(WindowClassName) > 1)
  1810. {
  1811. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  1812. if (strfind)
  1813. {
  1814. if (retstringlen == 0)
  1815. retstringlen = wcslen(retstring);
  1816. if (retstringlen > 1)
  1817. swprintf(retstring, L"%s,%d", retstring, p);
  1818. else
  1819. swprintf(retstring, L"%d", p);
  1820. bret = true;
  1821. }
  1822. }
  1823. }
  1824. }
  1825. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1826. }
  1827. break;
  1828. }
  1829. case 23://1.窗口标题+2.窗口类名+4 : 只匹配指定父窗口的第一层孩子窗口+匹配可见的窗口
  1830. {
  1831. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  1832. return false;
  1833. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1834. p = ::GetWindow(p, GW_HWNDFIRST);
  1835. while (p != NULL)
  1836. {
  1837. if (::IsWindowVisible(p))
  1838. {
  1839. if (process_name)//EnumWindowByProcess
  1840. {
  1841. DWORD pid = 0;
  1842. GetWindowThreadProcessId(p, &pid);
  1843. if (EnumProcessbyName(pid, process_name))
  1844. {
  1845. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  1846. {
  1847. if (indexpid < IsEuemprosuccess)
  1848. {
  1849. indexpid++;
  1850. processpid = pid;
  1851. memset(retstring, 0, retstringlen);//清空返回字符串
  1852. retstringlen = 0;
  1853. }
  1854. }
  1855. if (processpid == pid)
  1856. {
  1857. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1858. ::GetClassName(p, WindowClassName, MAX_PATH);
  1859. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1860. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1861. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  1862. {
  1863. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  1864. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  1865. if (strfindclass && strfindtitle)
  1866. {
  1867. if (retstringlen == 0)
  1868. retstringlen = wcslen(retstring);
  1869. if (retstringlen > 1)
  1870. swprintf(retstring, L"%s,%d", retstring, p);
  1871. else
  1872. swprintf(retstring, L"%d", p);
  1873. bret = true;
  1874. }
  1875. }
  1876. HWND hchile = ::GetWindow(p, GW_CHILD);
  1877. if (hchile != NULL)
  1878. {
  1879. FindChildWnd(hchile, title, class_name, retstring, false, true, process_name);
  1880. }
  1881. }
  1882. }
  1883. }
  1884. else
  1885. {
  1886. wchar_t WindowClassName[MAX_PATH] = { 0 };
  1887. ::GetClassName(p, WindowClassName, MAX_PATH);
  1888. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1889. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1890. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  1891. {
  1892. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  1893. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  1894. if (strfindclass && strfindtitle)
  1895. {
  1896. if (retstringlen == 0)
  1897. retstringlen = wcslen(retstring);
  1898. if (retstringlen > 1)
  1899. swprintf(retstring, L"%s,%d", retstring, p);
  1900. else
  1901. swprintf(retstring, L"%d", p);
  1902. bret = true;
  1903. }
  1904. }
  1905. }
  1906. }
  1907. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1908. }
  1909. break;
  1910. }
  1911. case 24://8 : 匹配所有者窗口为0的窗口,即顶级窗口+16.匹配可见的窗口
  1912. {
  1913. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1914. p = ::GetWindow(p, GW_HWNDFIRST);
  1915. while (p != NULL)
  1916. {
  1917. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  1918. {
  1919. if (process_name)//EnumWindowByProcess
  1920. {
  1921. DWORD pid = 0;
  1922. GetWindowThreadProcessId(p, &pid);
  1923. if (EnumProcessbyName(pid, process_name))
  1924. {
  1925. if (retstringlen == 0)
  1926. retstringlen = wcslen(retstring);
  1927. if (retstringlen > 1)
  1928. swprintf(retstring, L"%s,%d", retstring, p);
  1929. else
  1930. swprintf(retstring, L"%d", p);
  1931. bret = true;
  1932. HWND hchile = ::GetWindow(p, GW_CHILD);
  1933. if (hchile != NULL)
  1934. {
  1935. FindChildWnd(hchile, NULL, NULL, retstring, true, true, process_name);
  1936. }
  1937. }
  1938. }
  1939. else
  1940. {
  1941. if (retstringlen == 0)
  1942. retstringlen = wcslen(retstring);
  1943. if (retstringlen > 1)
  1944. swprintf(retstring, L"%s,%d", retstring, p);
  1945. else
  1946. swprintf(retstring, L"%d", p);
  1947. bret = true;
  1948. HWND hchile = ::GetWindow(p, GW_CHILD);
  1949. if (hchile != NULL)
  1950. {
  1951. FindChildWnd(hchile, NULL, NULL, retstring, true, true);
  1952. }
  1953. }
  1954. }
  1955. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  1956. }
  1957. break;
  1958. }
  1959. case 25: //1.窗口标题+ 8:匹配所有者窗口为0的窗口,即顶级窗口+16.匹配可见的窗口
  1960. {
  1961. if (wcslen(title) < 1)
  1962. return false;
  1963. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  1964. p = ::GetWindow(p, GW_HWNDFIRST);
  1965. while (p != NULL)
  1966. {
  1967. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  1968. {
  1969. if (process_name)//EnumWindowByProcess
  1970. {
  1971. DWORD pid = 0;
  1972. GetWindowThreadProcessId(p, &pid);
  1973. if (EnumProcessbyName(pid, process_name))
  1974. {
  1975. wchar_t WindowTitle[MAX_PATH] = { 0 };
  1976. ::GetWindowText(p, WindowTitle, MAX_PATH);
  1977. if (wcslen(WindowTitle) > 1)
  1978. {
  1979. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  1980. if (strfind)
  1981. {
  1982. if (retstringlen == 0)
  1983. retstringlen = wcslen(retstring);
  1984. if (retstringlen > 1)
  1985. swprintf(retstring, L"%s,%d", retstring, p);
  1986. else
  1987. swprintf(retstring, L"%d", p);
  1988. bret = true;
  1989. }
  1990. }
  1991. HWND hchile = ::GetWindow(p, GW_CHILD);
  1992. if (hchile != NULL)
  1993. {
  1994. FindChildWnd(hchile, title, NULL, retstring, true, true, process_name);
  1995. }
  1996. }
  1997. }
  1998. else
  1999. {
  2000. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2001. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2002. if (wcslen(WindowTitle) > 1)
  2003. {
  2004. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  2005. if (strfind)
  2006. {
  2007. if (retstringlen == 0)
  2008. retstringlen = wcslen(retstring);
  2009. if (retstringlen > 1)
  2010. swprintf(retstring, L"%s,%d", retstring, p);
  2011. else
  2012. swprintf(retstring, L"%d", p);
  2013. bret = true;
  2014. }
  2015. }
  2016. HWND hchile = ::GetWindow(p, GW_CHILD);
  2017. if (hchile != NULL)
  2018. {
  2019. FindChildWnd(hchile, title, NULL, retstring, true, true);
  2020. }
  2021. }
  2022. }
  2023. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2024. }
  2025. break;
  2026. }
  2027. case 26://2.窗口类名+ 8:匹配所有者窗口为0的窗口,即顶级窗口+16.匹配可见的窗口
  2028. {
  2029. if (wcslen(class_name) < 1)
  2030. return false;
  2031. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  2032. p = ::GetWindow(p, GW_HWNDFIRST);
  2033. while (p != NULL)
  2034. {
  2035. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  2036. {
  2037. if (process_name)//EnumWindowByProcess
  2038. {
  2039. DWORD pid = 0;
  2040. GetWindowThreadProcessId(p, &pid);
  2041. if (EnumProcessbyName(pid, process_name))
  2042. {
  2043. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2044. ::GetClassName(p, WindowClassName, MAX_PATH);
  2045. if (wcslen(WindowClassName) > 1)
  2046. {
  2047. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  2048. if (strfind)
  2049. {
  2050. if (retstringlen == 0)
  2051. retstringlen = wcslen(retstring);
  2052. if (retstringlen > 1)
  2053. swprintf(retstring, L"%s,%d", retstring, p);
  2054. else
  2055. swprintf(retstring, L"%d", p);
  2056. bret = true;
  2057. }
  2058. }
  2059. HWND hchile = ::GetWindow(p, GW_CHILD);
  2060. if (hchile != NULL)
  2061. {
  2062. FindChildWnd(hchile, NULL, class_name, retstring, true, true, process_name);
  2063. }
  2064. }
  2065. }
  2066. else
  2067. {
  2068. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2069. ::GetClassName(p, WindowClassName, MAX_PATH);
  2070. if (wcslen(WindowClassName) > 1)
  2071. {
  2072. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  2073. if (strfind)
  2074. {
  2075. if (retstringlen == 0)
  2076. retstringlen = wcslen(retstring);
  2077. if (retstringlen > 1)
  2078. swprintf(retstring, L"%s,%d", retstring, p);
  2079. else
  2080. swprintf(retstring, L"%d", p);
  2081. bret = true;
  2082. }
  2083. }
  2084. HWND hchile = ::GetWindow(p, GW_CHILD);
  2085. if (hchile != NULL)
  2086. {
  2087. FindChildWnd(hchile, NULL, class_name, retstring, true, true);
  2088. }
  2089. }
  2090. }
  2091. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2092. }
  2093. break;
  2094. }
  2095. case 27://1.窗口标题+2.窗口类名+8:匹配所有者窗口为0的窗口,即顶级窗口+16.匹配可见的窗口
  2096. {
  2097. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  2098. return false;
  2099. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  2100. p = ::GetWindow(p, GW_HWNDFIRST);
  2101. while (p != NULL)
  2102. {
  2103. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  2104. {
  2105. if (process_name)//EnumWindowByProcess
  2106. {
  2107. DWORD pid = 0;
  2108. GetWindowThreadProcessId(p, &pid);
  2109. if (EnumProcessbyName(pid, process_name))
  2110. {
  2111. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2112. ::GetClassName(p, WindowClassName, MAX_PATH);
  2113. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2114. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2115. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  2116. {
  2117. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  2118. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  2119. if (strfindclass && strfindtitle)
  2120. {
  2121. if (retstringlen == 0)
  2122. retstringlen = wcslen(retstring);
  2123. if (retstringlen > 1)
  2124. swprintf(retstring, L"%s,%d", retstring, p);
  2125. else
  2126. swprintf(retstring, L"%d", p);
  2127. bret = true;
  2128. }
  2129. }
  2130. HWND hchile = ::GetWindow(p, GW_CHILD);
  2131. if (hchile != NULL)
  2132. {
  2133. FindChildWnd(hchile, title, class_name, retstring, true, true, process_name);
  2134. }
  2135. }
  2136. }
  2137. else
  2138. {
  2139. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2140. ::GetClassName(p, WindowClassName, MAX_PATH);
  2141. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2142. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2143. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  2144. {
  2145. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  2146. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  2147. if (strfindclass && strfindtitle)
  2148. {
  2149. if (retstringlen == 0)
  2150. retstringlen = wcslen(retstring);
  2151. if (retstringlen > 1)
  2152. swprintf(retstring, L"%s,%d", retstring, p);
  2153. else
  2154. swprintf(retstring, L"%d", p);
  2155. bret = true;
  2156. }
  2157. }
  2158. HWND hchile = ::GetWindow(p, GW_CHILD);
  2159. if (hchile != NULL)
  2160. {
  2161. FindChildWnd(hchile, title, class_name, retstring, true, true);
  2162. }
  2163. }
  2164. }
  2165. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2166. }
  2167. break;
  2168. }
  2169. case 28: //4 : 只匹配指定父窗口的第一层孩子窗口+8:匹配所有者窗口为0的窗口,即顶级窗口+16.匹配可见的窗口
  2170. {
  2171. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  2172. p = ::GetWindow(p, GW_HWNDFIRST);
  2173. while (p != NULL)
  2174. {
  2175. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  2176. {
  2177. if (process_name)//EnumWindowByProcess
  2178. {
  2179. DWORD pid = 0;
  2180. GetWindowThreadProcessId(p, &pid);
  2181. if (EnumProcessbyName(pid, process_name))
  2182. {
  2183. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  2184. {
  2185. if (indexpid < IsEuemprosuccess)
  2186. {
  2187. indexpid++;
  2188. processpid = pid;
  2189. memset(retstring, 0, retstringlen);//清空返回字符串
  2190. retstringlen = 0;
  2191. }
  2192. }
  2193. if (processpid == pid)
  2194. {
  2195. if (retstringlen == 0)
  2196. retstringlen = wcslen(retstring);
  2197. if (retstringlen > 1)
  2198. swprintf(retstring, L"%s,%d", retstring, p);
  2199. else
  2200. swprintf(retstring, L"%d", p);
  2201. bret = true;
  2202. HWND hchile = ::GetWindow(p, GW_CHILD);
  2203. if (hchile != NULL)
  2204. {
  2205. FindChildWnd(hchile, NULL, NULL, retstring, true, true, process_name);
  2206. }
  2207. }
  2208. }
  2209. }
  2210. else
  2211. {
  2212. if (retstringlen == 0)
  2213. retstringlen = wcslen(retstring);
  2214. if (retstringlen > 1)
  2215. swprintf(retstring, L"%s,%d", retstring, p);
  2216. else
  2217. swprintf(retstring, L"%d", p);
  2218. bret = true;
  2219. }
  2220. }
  2221. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2222. }
  2223. break;
  2224. }
  2225. case 29: ////1.窗口标题+4 : 只匹配指定父窗口的第一层孩子窗口+8:匹配所有者窗口为0的窗口,即顶级窗口+16.匹配可见的窗口
  2226. {
  2227. if (wcslen(title) < 1)
  2228. return false;
  2229. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  2230. p = ::GetWindow(p, GW_HWNDFIRST);
  2231. while (p != NULL)
  2232. {
  2233. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  2234. {
  2235. if (process_name)//EnumWindowByProcess
  2236. {
  2237. DWORD pid = 0;
  2238. GetWindowThreadProcessId(p, &pid);
  2239. if (EnumProcessbyName(pid, process_name))
  2240. {
  2241. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  2242. {
  2243. if (indexpid < IsEuemprosuccess)
  2244. {
  2245. indexpid++;
  2246. processpid = pid;
  2247. memset(retstring, 0, retstringlen);//清空返回字符串
  2248. retstringlen = 0;
  2249. }
  2250. }
  2251. if (processpid == pid)
  2252. {
  2253. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2254. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2255. if (wcslen(WindowTitle) > 1)
  2256. {
  2257. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  2258. if (strfind)
  2259. {
  2260. if (retstringlen == 0)
  2261. retstringlen = wcslen(retstring);
  2262. if (retstringlen > 1)
  2263. swprintf(retstring, L"%s,%d", retstring, p);
  2264. else
  2265. swprintf(retstring, L"%d", p);
  2266. bret = true;
  2267. }
  2268. }
  2269. HWND hchile = ::GetWindow(p, GW_CHILD);
  2270. if (hchile != NULL)
  2271. {
  2272. FindChildWnd(hchile, title, NULL, retstring, true, true, process_name);
  2273. }
  2274. }
  2275. }
  2276. }
  2277. else
  2278. {
  2279. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2280. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2281. if (wcslen(WindowTitle) > 1)
  2282. {
  2283. wchar_t* strfind = wcsstr(WindowTitle, title); //模糊匹配
  2284. if (strfind)
  2285. {
  2286. if (retstringlen == 0)
  2287. retstringlen = wcslen(retstring);
  2288. if (retstringlen > 1)
  2289. swprintf(retstring, L"%s,%d", retstring, p);
  2290. else
  2291. swprintf(retstring, L"%d", p);
  2292. bret = true;
  2293. }
  2294. }
  2295. }
  2296. }
  2297. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2298. }
  2299. break;
  2300. }
  2301. case 30://2.窗口类名+4 : 只匹配指定父窗口的第一层孩子窗口+8:匹配所有者窗口为0的窗口,即顶级窗口+16.匹配可见的窗口
  2302. {
  2303. if (wcslen(class_name) < 1)
  2304. return false;
  2305. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  2306. p = ::GetWindow(p, GW_HWNDFIRST);
  2307. while (p != NULL)
  2308. {
  2309. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  2310. {
  2311. if (process_name)//EnumWindowByProcess
  2312. {
  2313. DWORD pid = 0;
  2314. GetWindowThreadProcessId(p, &pid);
  2315. if (EnumProcessbyName(pid, process_name))
  2316. {
  2317. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  2318. {
  2319. if (indexpid < IsEuemprosuccess)
  2320. {
  2321. indexpid++;
  2322. processpid = pid;
  2323. memset(retstring, 0, retstringlen);//清空返回字符串
  2324. retstringlen = 0;
  2325. }
  2326. }
  2327. if (processpid == pid)
  2328. {
  2329. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2330. ::GetClassName(p, WindowClassName, MAX_PATH);
  2331. if (wcslen(WindowClassName) > 1)
  2332. {
  2333. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  2334. if (strfind)
  2335. {
  2336. if (retstringlen == 0)
  2337. retstringlen = wcslen(retstring);
  2338. if (retstringlen > 1)
  2339. swprintf(retstring, L"%s,%d", retstring, p);
  2340. else
  2341. swprintf(retstring, L"%d", p);
  2342. bret = true;
  2343. }
  2344. }
  2345. HWND hchile = ::GetWindow(p, GW_CHILD);
  2346. if (hchile != NULL)
  2347. {
  2348. FindChildWnd(hchile, NULL, class_name, retstring, true, true, process_name);
  2349. }
  2350. }
  2351. }
  2352. }
  2353. else
  2354. {
  2355. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2356. ::GetClassName(p, WindowClassName, MAX_PATH);
  2357. if (wcslen(WindowClassName) > 1)
  2358. {
  2359. wchar_t* strfind = wcsstr(WindowClassName, class_name); //模糊匹配
  2360. if (strfind)
  2361. {
  2362. if (retstringlen == 0)
  2363. retstringlen = wcslen(retstring);
  2364. if (retstringlen > 1)
  2365. swprintf(retstring, L"%s,%d", retstring, p);
  2366. else
  2367. swprintf(retstring, L"%d", p);
  2368. bret = true;
  2369. }
  2370. }
  2371. }
  2372. }
  2373. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2374. }
  2375. break;
  2376. }
  2377. case 31: //1.窗口标题+2.窗口类名+4 : 只匹配指定父窗口的第一层孩子窗口+8:匹配所有者窗口为0的窗口,即顶级窗口+16.匹配可见的窗口
  2378. {
  2379. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  2380. return false;
  2381. HWND p = ::GetWindow(parent, GW_CHILD); //获取桌面窗口的子窗口
  2382. p = ::GetWindow(p, GW_HWNDFIRST);
  2383. while (p != NULL)
  2384. {
  2385. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  2386. {
  2387. if (process_name)//EnumWindowByProcess
  2388. {
  2389. DWORD pid = 0;
  2390. GetWindowThreadProcessId(p, &pid);
  2391. if (EnumProcessbyName(pid, process_name))
  2392. {
  2393. if (processpid != pid) //只匹配指定映像的所对应的第一个进程. 可能有很多同映像名的进程,只匹配第一个进程的.
  2394. {
  2395. if (indexpid < IsEuemprosuccess)
  2396. {
  2397. indexpid++;
  2398. processpid = pid;
  2399. memset(retstring, 0, retstringlen);//清空返回字符串
  2400. retstringlen = 0;
  2401. }
  2402. }
  2403. if (processpid == pid)
  2404. {
  2405. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2406. ::GetClassName(p, WindowClassName, MAX_PATH);
  2407. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2408. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2409. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  2410. {
  2411. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  2412. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  2413. if (strfindclass && strfindtitle)
  2414. {
  2415. if (retstringlen == 0)
  2416. retstringlen = wcslen(retstring);
  2417. if (retstringlen > 1)
  2418. swprintf(retstring, L"%s,%d", retstring, p);
  2419. else
  2420. swprintf(retstring, L"%d", p);
  2421. bret = true;
  2422. }
  2423. }
  2424. HWND hchile = ::GetWindow(p, GW_CHILD);
  2425. if (hchile != NULL)
  2426. {
  2427. FindChildWnd(hchile, title, class_name, retstring, true, true, process_name);
  2428. }
  2429. }
  2430. }
  2431. }
  2432. else
  2433. {
  2434. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2435. ::GetClassName(p, WindowClassName, MAX_PATH);
  2436. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2437. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2438. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  2439. {
  2440. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  2441. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  2442. if (strfindclass && strfindtitle)
  2443. {
  2444. if (retstringlen == 0)
  2445. retstringlen = wcslen(retstring);
  2446. if (retstringlen > 1)
  2447. swprintf(retstring, L"%s,%d", retstring, p);
  2448. else
  2449. swprintf(retstring, L"%d", p);
  2450. bret = true;
  2451. }
  2452. }
  2453. }
  2454. }
  2455. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2456. }
  2457. break;
  2458. }
  2459. default:
  2460. return bret;
  2461. }
  2462. return bret;
  2463. }
  2464. bool TSWindowsAPI::TSEnumWindowSuper(wchar_t* spec1, LONG flag1, LONG type1, wchar_t* spec2, LONG flag2, LONG type2, LONG sort, wchar_t* retstring)
  2465. {
  2466. bool bret = false;
  2467. wchar_t findhwnd1[MAX_PATH * 100] = { 0 };
  2468. wchar_t findhwnd2[MAX_PATH * 100] = { 0 };
  2469. bool bfindhwnd1 = false;
  2470. bool bfindhwnd2 = false;
  2471. retstringlen = 0;
  2472. HWND parent = GetDesktopWindow();
  2473. HWND p = ::GetWindow(parent, GW_CHILD);
  2474. p = ::GetWindow(p, GW_HWNDFIRST);
  2475. while (p != NULL)
  2476. {
  2477. if (flag1 == 0)//0表示spec1的内容是标题
  2478. {
  2479. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2480. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2481. if (wcslen(WindowTitle) > 0)
  2482. {
  2483. if (type1 == 0)//0精确判断,1模糊判断
  2484. {
  2485. if (wcscmp(spec1, WindowTitle) == 0)
  2486. bfindhwnd1 = true;
  2487. }
  2488. else if (type1 == 1)
  2489. {
  2490. if (wcsstr(WindowTitle, spec1) != NULL)
  2491. bfindhwnd1 = true;
  2492. }
  2493. }
  2494. }
  2495. else if (flag1 == 1)//1表示spec1的内容是程序名字
  2496. {
  2497. DWORD pid = 0;
  2498. ::GetWindowThreadProcessId(p, &pid);
  2499. wchar_t proname[MAX_PATH] = { 0 };
  2500. GetProcesspath(pid, proname);
  2501. }
  2502. else if (flag1 == 2)//2表示spec1的内容是类名
  2503. {
  2504. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2505. ::GetClassName(p, WindowClassName, MAX_PATH);
  2506. if (wcslen(WindowClassName) > 0)
  2507. {
  2508. if (type1 == 0)//0精确判断,1模糊判断
  2509. {
  2510. if (wcscmp(spec1, WindowClassName) == 0)
  2511. bfindhwnd1 = true;
  2512. }
  2513. else
  2514. {
  2515. if (wcsstr(WindowClassName, spec1) != NULL)
  2516. bfindhwnd1 = true;
  2517. }
  2518. }
  2519. }
  2520. if (bfindhwnd1)
  2521. {
  2522. if (retstringlen == 0)
  2523. retstringlen = wcslen(retstring);
  2524. if (retstringlen > 1)
  2525. swprintf(retstring, L"%s,%d", retstring, p);
  2526. else
  2527. swprintf(retstring, L"%d", p);
  2528. bfindhwnd1 = false;
  2529. }
  2530. HWND hchile = ::GetWindow(p, GW_CHILD);
  2531. if (hchile != NULL)
  2532. {
  2533. FindChildWnd(hchile, NULL, NULL, findhwnd1);
  2534. }
  2535. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2536. }
  2537. return bret;
  2538. }
  2539. bool TSWindowsAPI::TSEnumProcess(wchar_t* name, wchar_t* retstring)
  2540. {
  2541. bool bret = false;
  2542. retstringlen = 0;
  2543. if (wcslen(name) < 1)
  2544. return false;
  2545. IsEuemprosuccess = 0;
  2546. if (EnumProcessbyName(0, name) == true)
  2547. {
  2548. bret = true;
  2549. for (int i = 0; i < IsEuemprosuccess; i++)
  2550. {
  2551. if (retstringlen == 0)
  2552. retstringlen = wcslen(retstring);
  2553. if (retstringlen > 1)
  2554. swprintf(retstring, L"%s,%d", retstring, npid[i]);
  2555. else
  2556. swprintf(retstring, L"%d", npid[i]);
  2557. }
  2558. }
  2559. return bret;
  2560. }
  2561. bool TSWindowsAPI::TSClientToScreen(LONG hwnd, LONG& x, LONG& y)
  2562. {
  2563. POINT point;
  2564. if (my_ClientToScreen)
  2565. {
  2566. my_ClientToScreen((HWND)hwnd, &point);
  2567. }
  2568. else
  2569. ::ClientToScreen((HWND)hwnd, &point);
  2570. x = point.x;
  2571. y = point.y;
  2572. return true;
  2573. }
  2574. bool TSWindowsAPI::TSFindWindow(wchar_t* class_name, wchar_t* title, LONG& rethwnd, DWORD parent)
  2575. {
  2576. bool bret = false;
  2577. rethwnd = 0;
  2578. HWND p = NULL;
  2579. if (parent == 0)
  2580. p = ::GetWindow(GetDesktopWindow(), GW_CHILD); //获取桌面窗口的子窗口
  2581. else
  2582. p = ::GetWindow((HWND)parent, GW_CHILD); //获取桌面窗口的子窗口
  2583. if (p == NULL)
  2584. return bret;
  2585. p = ::GetWindow(p, GW_HWNDFIRST);
  2586. while (p != NULL)
  2587. {
  2588. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  2589. {
  2590. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  2591. {
  2592. rethwnd = (LONG)p;
  2593. bret = true;
  2594. break;
  2595. }
  2596. else
  2597. {
  2598. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2599. ::GetClassName(p, WindowClassName, MAX_PATH);
  2600. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2601. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2602. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  2603. {
  2604. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  2605. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  2606. if ((wcslen(class_name) >= 1 && strfindclass) || (wcslen(title) >= 1 && strfindtitle))
  2607. {
  2608. rethwnd = (LONG)p;
  2609. bret = true;
  2610. break;
  2611. }
  2612. }
  2613. }
  2614. }
  2615. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2616. }
  2617. return bret;
  2618. }
  2619. bool TSWindowsAPI::TSFindWindowByProcess(wchar_t* class_name, wchar_t* title, LONG& rethwnd, wchar_t* process_name, DWORD Pid)
  2620. {
  2621. bool bret = false;
  2622. rethwnd = 0;
  2623. if (process_name)
  2624. {
  2625. if (wcslen(process_name) < 1)
  2626. return false;
  2627. memset(npid, 0, MAX_PATH);
  2628. IsEuemprosuccess = 0;
  2629. if (EnumProcessbyName(0, process_name) == false)
  2630. return false;
  2631. HWND p = ::GetWindow(GetDesktopWindow(), GW_CHILD); //获取桌面窗口的子窗口
  2632. p = ::GetWindow(p, GW_HWNDFIRST);
  2633. while (p != NULL)
  2634. {
  2635. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  2636. {
  2637. DWORD pid = 0;
  2638. GetWindowThreadProcessId(p, &pid);
  2639. if (EnumProcessbyName(pid, process_name))
  2640. {
  2641. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  2642. {
  2643. rethwnd = (LONG)p;
  2644. bret = true;
  2645. break;
  2646. }
  2647. else
  2648. {
  2649. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2650. ::GetClassName(p, WindowClassName, MAX_PATH);
  2651. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2652. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2653. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  2654. {
  2655. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  2656. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  2657. if ((wcslen(class_name) >= 1 && strfindclass) || (wcslen(title) >= 1 && strfindtitle))
  2658. {
  2659. rethwnd = (LONG)p;
  2660. bret = true;
  2661. break;
  2662. }
  2663. }
  2664. HWND hchile = ::GetWindow(p, GW_CHILD);
  2665. if (hchile != NULL)
  2666. {
  2667. wchar_t* classname = NULL;
  2668. wchar_t* titles = NULL;
  2669. if (wcslen(class_name) > 0)
  2670. classname = class_name;
  2671. if (wcslen(title) > 0)
  2672. titles = titles;
  2673. DWORD dret = FindChildWnd(hchile, titles, classname, NULL, false, false, process_name);
  2674. if (dret > 0)
  2675. {
  2676. rethwnd = (LONG)dret;
  2677. bret = true;
  2678. break;
  2679. }
  2680. }
  2681. }
  2682. }
  2683. }
  2684. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2685. }
  2686. }
  2687. else if (Pid > 0)
  2688. {
  2689. HWND p = ::GetWindow(GetDesktopWindow(), GW_CHILD); //获取桌面窗口的子窗口
  2690. p = ::GetWindow(p, GW_HWNDFIRST);
  2691. while (p != NULL)
  2692. {
  2693. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  2694. {
  2695. DWORD npid = 0;
  2696. GetWindowThreadProcessId(p, &npid);
  2697. if (Pid == npid)
  2698. {
  2699. if (wcslen(class_name) < 1 && wcslen(title) < 1)
  2700. {
  2701. rethwnd = (LONG)p;
  2702. bret = true;
  2703. break;
  2704. }
  2705. else
  2706. {
  2707. wchar_t WindowClassName[MAX_PATH] = { 0 };
  2708. ::GetClassName(p, WindowClassName, MAX_PATH);
  2709. wchar_t WindowTitle[MAX_PATH] = { 0 };
  2710. ::GetWindowText(p, WindowTitle, MAX_PATH);
  2711. if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1)
  2712. {
  2713. wchar_t* strfindclass = wcsstr(WindowClassName, class_name); //模糊匹配
  2714. wchar_t* strfindtitle = wcsstr(WindowTitle, title); //模糊匹配
  2715. if ((wcslen(class_name) >= 1 && strfindclass) || (wcslen(title) >= 1 && strfindtitle))
  2716. {
  2717. rethwnd = (LONG)p;
  2718. bret = true;
  2719. break;
  2720. }
  2721. }
  2722. HWND hchile = ::GetWindow(p, GW_CHILD);
  2723. if (hchile != NULL)
  2724. {
  2725. wchar_t* classname = NULL;
  2726. wchar_t* titles = NULL;
  2727. if (wcslen(class_name) > 0)
  2728. classname = class_name;
  2729. if (wcslen(title) > 0)
  2730. titles = titles;
  2731. DWORD dret = FindChildWnd(hchile, titles, classname, NULL, false, false, process_name);
  2732. if (dret > 0)
  2733. {
  2734. rethwnd = (LONG)dret;
  2735. bret = true;
  2736. break;
  2737. }
  2738. }
  2739. }
  2740. }
  2741. }
  2742. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2743. }
  2744. }
  2745. return bret;
  2746. }
  2747. bool TSWindowsAPI::TSGetClientRect(LONG hwnd, LONG& x, LONG& y, LONG& x1, LONG& y1)
  2748. {
  2749. bool bret = false;
  2750. RECT clientrect;
  2751. if (IsWindow((HWND)hwnd))
  2752. {
  2753. ::GetClientRect((HWND)hwnd, &clientrect);
  2754. POINT point;
  2755. point.x = clientrect.left;
  2756. point.y = clientrect.top;
  2757. ::ClientToScreen((HWND)hwnd, &point);
  2758. x = point.x;
  2759. y = point.y;
  2760. point.x = clientrect.right;
  2761. point.y = clientrect.bottom;
  2762. ::ClientToScreen((HWND)hwnd, &point);
  2763. x1 = point.x;
  2764. y1 = point.y;
  2765. bret = true;
  2766. }
  2767. return bret;
  2768. }
  2769. bool TSWindowsAPI::TSGetClientSize(LONG hwnd, LONG& width, LONG& height)
  2770. {
  2771. bool bret = false;
  2772. RECT clientrect;
  2773. if (IsWindow((HWND)hwnd))
  2774. {
  2775. ::GetClientRect((HWND)hwnd, &clientrect);
  2776. width = clientrect.right - clientrect.left;
  2777. height = clientrect.bottom - clientrect.top;
  2778. bret = true;
  2779. }
  2780. return bret;
  2781. }
  2782. bool TSWindowsAPI::TSGetMousePointWindow(LONG& rethwnd, LONG x, LONG y)
  2783. {
  2784. bool bret = false;
  2785. rethwnd = 0;
  2786. POINT point;
  2787. if ((x != -1 && y != -1))
  2788. {
  2789. point.x = x;
  2790. point.y = y;
  2791. }
  2792. else
  2793. //::GetCursorPos(&point);
  2794. My_GetCursorPos(&point);
  2795. rethwnd = (DWORD)::WindowFromPoint(point);
  2796. if (rethwnd == NULL)
  2797. {
  2798. HWND p = ::GetWindow(GetDesktopWindow(), GW_CHILD); //获取桌面窗口的子窗口
  2799. p = ::GetWindow(p, GW_HWNDFIRST);
  2800. while (p != NULL)
  2801. {
  2802. if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0)
  2803. {
  2804. RECT rc;
  2805. ::GetWindowRect(p, &rc);
  2806. if ((rc.top <= point.y) && (rc.left <= point.x) && (rc.right >= (point.x - rc.left)) && (rc.bottom >= (point.y - rc.top)))
  2807. {
  2808. wchar_t WindowClass[MAX_PATH] = { 0 };
  2809. ::GetClassName(p, WindowClass, MAX_PATH);
  2810. //if((windowpoint.x==0||windowpoint.x<rc.left)&&wcscmp(WindowClass,L"CabinetWClass")!=0) //IE框窗体排除在外
  2811. if (wcscmp(WindowClass, L"CabinetWClass") != 0) //IE框窗体排除在外
  2812. {
  2813. rethwnd = (DWORD)p;
  2814. bret = true;
  2815. break;
  2816. }
  2817. }
  2818. }
  2819. p = ::GetWindow(p, GW_HWNDNEXT); //获取下一个窗口
  2820. }
  2821. }
  2822. else
  2823. bret = true;
  2824. return bret;
  2825. }
  2826. int TSWindowsAPI::GetProcessNumber()//获取CPU个数
  2827. {
  2828. SYSTEM_INFO info;
  2829. GetSystemInfo(&info);
  2830. return (int)info.dwNumberOfProcessors;
  2831. }
  2832. // 时间格式转换
  2833. __int64 TSWindowsAPI::FileTimeToInt64(const FILETIME& time)
  2834. {
  2835. ULARGE_INTEGER tt;
  2836. tt.LowPart = time.dwLowDateTime;
  2837. tt.HighPart = time.dwHighDateTime;
  2838. return(tt.QuadPart);
  2839. }
  2840. double TSWindowsAPI::get_cpu_usage(DWORD ProcessID) //获取指定进程CPU使用率
  2841. {
  2842. //cpu数量
  2843. static int processor_count_ = -1;
  2844. //上一次的时间
  2845. static __int64 last_time_ = 0;
  2846. static __int64 last_system_time_ = 0;
  2847. FILETIME now;
  2848. FILETIME creation_time;
  2849. FILETIME exit_time;
  2850. FILETIME kernel_time;
  2851. FILETIME user_time;
  2852. __int64 system_time;
  2853. __int64 time;
  2854. // __int64 system_time_delta;
  2855. // __int64 time_delta;
  2856. double cpu = -1;
  2857. if (processor_count_ == -1)
  2858. {
  2859. processor_count_ = GetProcessNumber();
  2860. }
  2861. GetSystemTimeAsFileTime(&now);
  2862. //HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION/*PROCESS_ALL_ACCESS*/, false, ProcessID);
  2863. HANDLE hProcess = NULL;
  2864. if (my_OpenProcess)
  2865. {
  2866. hProcess = my_OpenProcess(
  2867. PROCESS_QUERY_INFORMATION |
  2868. PROCESS_VM_READ,
  2869. FALSE, ProcessID);
  2870. }
  2871. else
  2872. {
  2873. hProcess = OpenProcess(
  2874. PROCESS_QUERY_INFORMATION |
  2875. PROCESS_VM_READ,
  2876. FALSE, ProcessID);
  2877. }
  2878. if (!hProcess)
  2879. {
  2880. return -1;
  2881. }
  2882. if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))
  2883. {
  2884. return -1;
  2885. }
  2886. system_time = (FileTimeToInt64(kernel_time) + FileTimeToInt64(user_time)) / processor_count_; //CPU使用时间
  2887. time = FileTimeToInt64(now); //现在的时间
  2888. last_system_time_ = system_time;
  2889. last_time_ = time;
  2890. CloseHandle(hProcess);
  2891. Sleep(1000);
  2892. //hProcess = OpenProcess(PROCESS_QUERY_INFORMATION/*PROCESS_ALL_ACCESS*/, false, ProcessID);
  2893. if (my_OpenProcess)
  2894. {
  2895. hProcess = my_OpenProcess(
  2896. PROCESS_QUERY_INFORMATION |
  2897. PROCESS_VM_READ,
  2898. FALSE, ProcessID);
  2899. }
  2900. else
  2901. {
  2902. hProcess = OpenProcess(
  2903. PROCESS_QUERY_INFORMATION |
  2904. PROCESS_VM_READ,
  2905. FALSE, ProcessID);
  2906. }
  2907. if (!hProcess)
  2908. {
  2909. return -1;
  2910. }
  2911. if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))
  2912. {
  2913. return -1;
  2914. }
  2915. GetSystemTimeAsFileTime(&now);
  2916. system_time = (FileTimeToInt64(kernel_time) + FileTimeToInt64(user_time)) / processor_count_; //CPU使用时间
  2917. time = FileTimeToInt64(now); //现在的时间
  2918. CloseHandle(hProcess);
  2919. cpu = ((double)(system_time - last_system_time_) / (double)(time - last_time_)) * 100;
  2920. return cpu;
  2921. }
  2922. //或者指定进程内存使用率
  2923. DWORD TSWindowsAPI::GetMemoryInfo(DWORD ProcessID)
  2924. {
  2925. PROCESS_MEMORY_COUNTERS pmc;
  2926. DWORD memoryInK = 0;
  2927. HANDLE hProcess = NULL;
  2928. if (my_OpenProcess)
  2929. {
  2930. hProcess = my_OpenProcess(
  2931. PROCESS_QUERY_INFORMATION |
  2932. PROCESS_VM_READ,
  2933. FALSE, ProcessID);
  2934. }
  2935. else
  2936. {
  2937. hProcess = OpenProcess(
  2938. PROCESS_QUERY_INFORMATION |
  2939. PROCESS_VM_READ,
  2940. FALSE, ProcessID);
  2941. }
  2942. if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)))
  2943. {
  2944. //memoryInK = pmc.WorkingSetSize/1024; //单位为k
  2945. memoryInK = pmc.WorkingSetSize;
  2946. }
  2947. CloseHandle(hProcess);
  2948. return memoryInK;
  2949. }
  2950. bool TSWindowsAPI::TSGetProcessInfo(LONG pid, wchar_t* retstring)
  2951. {
  2952. bool bret = false;
  2953. wchar_t process_name[MAX_PATH] = { 0 };
  2954. wchar_t process_path[MAX_PATH] = { 0 };
  2955. DWORD cpu = 0;
  2956. DWORD meminfo = 0;
  2957. int nItem = 0; // 项计数
  2958. PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) };
  2959. HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  2960. if (hProcessSnap == INVALID_HANDLE_VALUE)
  2961. return FALSE;
  2962. if (::Process32First(hProcessSnap, &pe32))
  2963. {
  2964. do
  2965. {
  2966. if (pe32.th32ProcessID == pid)
  2967. {
  2968. wcscpy(process_name, pe32.szExeFile);
  2969. break;
  2970. }
  2971. } while (::Process32Next(hProcessSnap, &pe32));
  2972. }
  2973. ::CloseHandle(hProcessSnap);
  2974. if (wcslen(process_name) < 1)
  2975. return bret;
  2976. TSRuntime::GetRemoteModulePath(process_name, pid, process_path);
  2977. cpu = (DWORD)get_cpu_usage(pid);
  2978. meminfo = (DWORD)GetMemoryInfo(pid);
  2979. swprintf(retstring, L"%s|%s|%d|%d", process_name, process_path, cpu, meminfo);
  2980. return bret;
  2981. }
  2982. bool TSWindowsAPI::GetProcesspath(DWORD ProcessID, wchar_t* process_path)
  2983. {
  2984. HANDLE hProcess = NULL;
  2985. if (my_OpenProcess)
  2986. {
  2987. hProcess = my_OpenProcess(
  2988. PROCESS_QUERY_INFORMATION |
  2989. PROCESS_VM_READ,
  2990. FALSE, ProcessID);
  2991. }
  2992. else
  2993. {
  2994. hProcess = OpenProcess(
  2995. PROCESS_QUERY_INFORMATION |
  2996. PROCESS_VM_READ,
  2997. FALSE, ProcessID);
  2998. }
  2999. HMODULE hMods = NULL;
  3000. DWORD cbNeededModule = 0;
  3001. EnumProcessModules(hProcess, &hMods, sizeof(hMods), &cbNeededModule);
  3002. GetModuleFileNameEx(hProcess, hMods, process_path, MAX_PATH);
  3003. return true;
  3004. }
  3005. bool TSWindowsAPI::TSGetWindow(LONG hwnd, LONG flag, LONG& rethwnd)
  3006. {
  3007. bool bret = false;
  3008. rethwnd = 0;
  3009. HWND wnd = (HWND)hwnd;
  3010. if (IsWindow(wnd) == false)
  3011. return bret;
  3012. DWORD type = -1;
  3013. if (flag == 0) //0:获取父窗口
  3014. rethwnd = (LONG)::GetParent(wnd);
  3015. else if (flag == 1)//获取第一个儿子窗口
  3016. type = GW_CHILD;
  3017. else if (flag == 2)//获取First 窗口
  3018. type = GW_HWNDFIRST;
  3019. else if (flag == 3)//获取Last窗口
  3020. type = GW_HWNDLAST;
  3021. else if (flag == 4)//获取下一个窗口
  3022. type = GW_HWNDNEXT;
  3023. else if (flag == 5)//获取上一个窗口
  3024. type = GW_HWNDPREV;
  3025. else if (flag == 6) //获取拥有者窗口
  3026. type = GW_OWNER;
  3027. else if (flag == 7) //获取顶层窗口
  3028. rethwnd = (LONG)::GetForegroundWindow();
  3029. if (type != -1)
  3030. rethwnd = (LONG)::GetWindow(wnd, (UINT)type);
  3031. if (rethwnd != 0)
  3032. bret = true;
  3033. return bret;
  3034. }
  3035. bool TSWindowsAPI::TSGetWindowState(LONG hwnd, LONG flag)
  3036. {
  3037. bool bret = false;
  3038. HWND wnd = (HWND)hwnd;
  3039. if (flag == 0) //0://判断窗口是否存在
  3040. bret = ::IsWindow(wnd);
  3041. else if (flag == 1)//判断窗口是否处于激活
  3042. {
  3043. if (::GetActiveWindow() == wnd)
  3044. bret = true;
  3045. }
  3046. else if (flag == 2)//2 : 判断窗口是否可见
  3047. bret = ::IsWindowVisible(wnd);
  3048. else if (flag == 3)//3 : 判断窗口是否最小化
  3049. bret = ::IsIconic(wnd);
  3050. else if (flag == 4)//4 : 判断窗口是否最大化
  3051. bret = ::IsZoomed(wnd);
  3052. else if (flag == 5)//5 : 判断窗口是否置顶
  3053. {
  3054. if (::GetForegroundWindow() == wnd)
  3055. bret = true;
  3056. }
  3057. else if (flag == 6) //6 : 判断窗口是否无响应
  3058. bret = ::IsHungAppWindow(wnd);
  3059. else if (flag == 7) //判断窗口是否可用(灰色为不可用)
  3060. bret = ::IsWindowEnabled(wnd);
  3061. return bret;
  3062. }
  3063. bool TSWindowsAPI::TSSendPaste(LONG hwnd)
  3064. {
  3065. bool bret = true;
  3066. HANDLE hClip;
  3067. char* chBuffer = NULL;
  3068. if (OpenClipboard(NULL))
  3069. {
  3070. //从剪贴板中取出一个内存的句柄
  3071. hClip = GetClipboardData(CF_TEXT);
  3072. //定义字符型指针变量用来保存内存块中的数据
  3073. //对内存块进行加锁,将内存句柄值转化为一个指针,并将内存块的引用计数器加一,内存中的数据也返回到指针型变量中
  3074. chBuffer = (char*)GlobalLock(hClip);
  3075. //将数据保存到字符型变量中
  3076. //将内存块的引用计数器减一
  3077. GlobalUnlock(hClip);
  3078. //关闭剪贴板,释放剪贴板资源的占用权
  3079. CloseClipboard();
  3080. }
  3081. //anscii 转 unicode
  3082. DWORD num = MultiByteToWideChar(CP_ACP, 0, chBuffer, -1, NULL, 0);
  3083. wchar_t* wword = new wchar_t[num + 1];//动态的申请空间存字
  3084. memset(wword, 0, (num + 1) * sizeof(wchar_t)); //初始化动作
  3085. MultiByteToWideChar(CP_ACP, 0, chBuffer, -1, wword, num);
  3086. int len = wcslen(wword);
  3087. //MessageBoxA(NULL,tts,tts,NULL);
  3088. for (int i = 0; i < len; i++)
  3089. {
  3090. ::SendMessage((HWND)hwnd, WM_CHAR, (WPARAM)wword[i], (LPARAM)1);
  3091. Sleep(10);
  3092. }
  3093. delete[] wword;
  3094. return bret;
  3095. }
  3096. bool TSWindowsAPI::TSSetWindowSize(LONG hwnd, LONG width, LONG hight, int type)
  3097. {
  3098. bool bret = false;
  3099. if (type == 0)//SetClientSize
  3100. {
  3101. RECT rectProgram, rectClient;
  3102. HWND hWnd = (HWND)hwnd;
  3103. ::GetWindowRect(hWnd, &rectProgram); //获得程序窗口位于屏幕坐标
  3104. ::GetClientRect(hWnd, &rectClient); //获得客户区坐标
  3105. //非客户区宽,高
  3106. int nWidth = rectProgram.right - rectProgram.left - (rectClient.right - rectClient.left);
  3107. int nHeiht = rectProgram.bottom - rectProgram.top - (rectClient.bottom - rectClient.top);
  3108. nWidth += width;
  3109. nHeiht += hight;
  3110. rectProgram.right = nWidth;
  3111. rectProgram.bottom = nHeiht;
  3112. int showToScreenx = GetSystemMetrics(SM_CXSCREEN) / 2 - nWidth / 2; //居中处理
  3113. int showToScreeny = GetSystemMetrics(SM_CYSCREEN) / 2 - nHeiht / 2;
  3114. bret = ::MoveWindow(hWnd, showToScreenx, showToScreeny, rectProgram.right, rectProgram.bottom, false);
  3115. }
  3116. else //SetWindowSize
  3117. {
  3118. RECT rectClient;
  3119. HWND hWnd = (HWND)hwnd;
  3120. ::GetWindowRect(hWnd, &rectClient); //获得程序窗口位于屏幕坐标
  3121. bret = ::MoveWindow(hWnd, rectClient.left, rectClient.top, width, hight, false);
  3122. }
  3123. return bret;
  3124. }
  3125. bool TSWindowsAPI::TSSetWindowState(LONG hwnd, LONG flag, LONG rethwnd)
  3126. {
  3127. bool bret = false;
  3128. HWND hWnd = (HWND)hwnd;
  3129. if (IsWindow(hWnd) == false)
  3130. return bret;
  3131. int type = -1;
  3132. type = flag;
  3133. if (flag == 0)//关闭指定窗口
  3134. ::SendMessage(hWnd, WM_CLOSE, 0, 0);
  3135. else if (flag == 1)//激活指定窗口
  3136. {
  3137. ::ShowWindow(hWnd, SW_SHOW);
  3138. ::SetForegroundWindow(hWnd);
  3139. }
  3140. else if (flag == 2)//最小化指定窗口,但不激活
  3141. ::ShowWindow(hWnd, SW_SHOWMINNOACTIVE);
  3142. else if (flag == 3)//最小化指定窗口,并释放内存,但同时也会激活窗口
  3143. ::ShowWindow(hWnd, SW_SHOWMINIMIZED);
  3144. else if (flag == 4)//最大化指定窗口,同时激活窗口.
  3145. ::ShowWindow(hWnd, SW_SHOWMAXIMIZED);
  3146. else if (flag == 5)//恢复指定窗口 ,但不激活
  3147. ::ShowWindow(hWnd, SW_SHOWNOACTIVATE);
  3148. else if (flag == 6)//隐藏指定窗口
  3149. ::ShowWindow(hWnd, SW_HIDE);
  3150. else if (flag == 7)//显示指定窗口
  3151. {
  3152. ::ShowWindow(hWnd, SW_SHOW);
  3153. ::SetForegroundWindow(hWnd);
  3154. }
  3155. else if (flag == 8)//置顶指定窗口
  3156. ::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);//窗口置顶
  3157. else if (flag == 9)//9 : 取消置顶指定窗口
  3158. ::SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  3159. else if (flag == 10)//禁止指定窗口
  3160. ::EnableWindow(hWnd, false);
  3161. else if (flag == 11)//取消禁止指定窗口
  3162. ::EnableWindow(hWnd, true);
  3163. else if (flag == 12)//12 : 恢复并激活指定窗口
  3164. ::ShowWindow(hWnd, SW_RESTORE);
  3165. else if (flag == 13)//13 : 强制结束窗口所在进程.
  3166. {
  3167. DWORD pid = 0;
  3168. ::GetWindowThreadProcessId(hWnd, &pid);
  3169. TSRuntime::EnablePrivilege(L"SeDebugPrivilege", true);
  3170. HANDLE hprocess = NULL;
  3171. if (my_OpenProcess)
  3172. hprocess = my_OpenProcess(PROCESS_ALL_ACCESS, false, pid);
  3173. else
  3174. hprocess = ::OpenProcess(PROCESS_ALL_ACCESS, false, pid);
  3175. ::TerminateProcess(hprocess, 0);
  3176. }
  3177. else if (flag == 14)//14 : 闪烁指定的窗口
  3178. {
  3179. FLASHWINFO fInfo;
  3180. fInfo.cbSize = sizeof(FLASHWINFO);
  3181. fInfo.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG;//这里是闪动窗标题和任务栏按钮,直到用户激活窗体
  3182. fInfo.dwTimeout = 0;
  3183. fInfo.hwnd = hWnd;
  3184. fInfo.uCount = 0xffffff;
  3185. FlashWindowEx(&fInfo);
  3186. }
  3187. else if (flag == 15)//使指定的窗口获取输入焦点
  3188. {
  3189. ::ShowWindow(hWnd, SW_SHOW);
  3190. ::SetFocus(hWnd);
  3191. }
  3192. if (type >= 0 && type < 16)
  3193. bret = true;
  3194. return bret;
  3195. }
  3196. bool TSWindowsAPI::TSSetWindowTransparent(LONG hwnd, LONG trans)
  3197. {
  3198. bool bret = false;
  3199. COLORREF crKey = NULL;
  3200. DWORD dwFlags = 0;
  3201. BYTE bAlpha = 0;
  3202. if (trans < 0)
  3203. trans = 0;
  3204. if (trans > 255)
  3205. trans = 255;
  3206. typedef bool(__stdcall* mySetLayeredWindowAttributes)(
  3207. HWND hwnd,
  3208. COLORREF pcrKey,
  3209. BYTE pbAlpha,
  3210. DWORD pdwFlags);
  3211. mySetLayeredWindowAttributes obj_SetLayeredWindowAttributes = NULL;
  3212. HINSTANCE hlibrary;
  3213. hlibrary = LoadLibrary(_T("user32.dll"));
  3214. obj_SetLayeredWindowAttributes = (mySetLayeredWindowAttributes)GetProcAddress(hlibrary, "SetLayeredWindowAttributes");
  3215. SetWindowLong((HWND)hwnd, GWL_EXSTYLE, 0x80001);
  3216. bret = obj_SetLayeredWindowAttributes((HWND)hwnd, crKey, trans, 2);
  3217. return bret;
  3218. }
  3219. bool TSWindowsAPI::TSSetClipboard(wchar_t* values)
  3220. {
  3221. bool bret = false;
  3222. int n = ::WideCharToMultiByte(CP_ACP, 0, values, -1, NULL, 0, NULL, NULL);
  3223. char* chcontent = new char[n + 1];
  3224. memset(chcontent, 0, sizeof(char) * n + 1);
  3225. WideCharToMultiByte(CP_ACP, 0, values, -1, chcontent, n, NULL, NULL);
  3226. if (OpenClipboard(NULL))
  3227. {
  3228. //将剪贴板内容清空
  3229. EmptyClipboard();
  3230. //字节长度
  3231. int leng = strlen(chcontent) + 1;
  3232. //在堆上分配可移动的内存块,程序返回一个内存句柄
  3233. HANDLE hClip = GlobalAlloc(GHND | GMEM_SHARE, leng);
  3234. //定义指向字符型的指针变量
  3235. char* buff;
  3236. //对分配的内存块进行加锁,将内存块句柄转化成一个指针,并将相应的引用计数器加一
  3237. buff = (char*)GlobalLock(hClip);
  3238. //将用户输入的数据拷贝到指针变量中,实际上就是拷贝到分配的内存块中
  3239. memcpy(buff, chcontent, leng);
  3240. buff[leng - 1] = 0;
  3241. //数据写入完毕,进行解锁操作,并将引用计数器数字减一
  3242. GlobalUnlock(hClip);
  3243. //将存放有数据的内存块放入剪贴板的资源管理中
  3244. HANDLE help = SetClipboardData(CF_TEXT, hClip);
  3245. //关闭剪贴板,释放剪贴板资源的占用权
  3246. CloseClipboard();
  3247. //MessageBox(0,L"已将数据存入剪贴板",L"剪切扳",0);
  3248. if (help != NULL)
  3249. {
  3250. bret = true;
  3251. }
  3252. else
  3253. {
  3254. bret = false;
  3255. }
  3256. }
  3257. delete[] chcontent;
  3258. return bret;
  3259. }
  3260. bool TSWindowsAPI::TSGetClipboard(wchar_t* retstr)
  3261. {
  3262. bool bret = false;
  3263. HANDLE hClip;
  3264. char* chBuffer = NULL;
  3265. if (OpenClipboard(NULL))
  3266. {
  3267. //从剪贴板中取出一个内存的句柄
  3268. hClip = GetClipboardData(CF_TEXT);
  3269. //定义字符型指针变量用来保存内存块中的数据
  3270. //对内存块进行加锁,将内存句柄值转化为一个指针,并将内存块的引用计数器加一,内存中的数据也返回到指针型变量中
  3271. chBuffer = (char*)GlobalLock(hClip);
  3272. //将数据保存到字符型变量中
  3273. //将内存块的引用计数器减一
  3274. GlobalUnlock(hClip);
  3275. //关闭剪贴板,释放剪贴板资源的占用权
  3276. CloseClipboard();
  3277. }
  3278. DWORD num = MultiByteToWideChar(CP_ACP, 0, chBuffer, -1, NULL, 0);
  3279. wchar_t* wword = new wchar_t[num + 1];//动态的申请空间存字
  3280. memset(wword, 0, (num + 1) * sizeof(wchar_t)); //初始化动作
  3281. MultiByteToWideChar(CP_ACP, 0, chBuffer, -1, wword, num);
  3282. if (num < MAX_PATH * 4 - 1)
  3283. wcscpy(retstr, wword);
  3284. delete[] wword;
  3285. return bret;
  3286. }