WinApi.cpp 94 KB

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