SysControl.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. #include "StdAfx.h"
  2. #include <map>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm> // std::reverse
  6. using namespace std;
  7. #include <commctrl.h>
  8. #include "SysControl.h"
  9. #include "AutoFunc.h"
  10. SysControl::SysControl(void)
  11. {
  12. }
  13. SysControl::~SysControl(void)
  14. {
  15. }
  16. void SysControl::Sethand(HWND Hand)
  17. {
  18. m_ResHwnd = Hand;
  19. }
  20. bool SysControl::HitMenu(WORD MenuId)
  21. {
  22. DWORD wparam = MenuId;
  23. LRESULT lResult = PostMessage( // returns LRESULT in lResult
  24. m_ResHwnd, // handle to destination control
  25. (UINT)WM_COMMAND, // message ID
  26. (WPARAM)wparam, // = (WPARAM) () wParam;
  27. (LPARAM)0 // = 0; not used, must be zero
  28. );
  29. if (lResult == CB_ERR)
  30. {
  31. return false;
  32. }
  33. return true;
  34. }
  35. bool SysControl::GetHwndTitle(TCHAR *pszTitle, DWORD size)
  36. {
  37. AttachThreadInput(
  38. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  39. ::GetCurrentThreadId(), //自己的线程ID
  40. TRUE);
  41. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  42. m_ResHwnd, // handle to destination control
  43. (UINT)WM_GETTEXT, // message ID
  44. (WPARAM)size, // = (WPARAM) () wParam;
  45. (LPARAM)pszTitle // = 0; not used, must be zero
  46. );
  47. return (lResult != CB_ERR);
  48. }
  49. bool SysControl::GetCheckBoxStr(TCHAR *pszTitle, DWORD size)
  50. {
  51. AttachThreadInput(
  52. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  53. ::GetCurrentThreadId(), //自己的线程ID
  54. TRUE);
  55. int ret = GetHwndTitle(pszTitle, size);
  56. if (ret > 0)
  57. {
  58. return true;
  59. }
  60. return false;
  61. }
  62. bool SysControl::SetCheckBox(INT Set)
  63. {
  64. AttachThreadInput(
  65. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  66. ::GetCurrentThreadId(), //自己的线程ID
  67. TRUE);
  68. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  69. m_ResHwnd, // handle to destination control
  70. (UINT)BM_SETCHECK, // message ID
  71. (WPARAM)Set, // = (WPARAM) () wParam;
  72. (LPARAM)0 // = 0; not used, must be zero
  73. );
  74. return (lResult != CB_ERR);
  75. }
  76. // <0:failed,0:Gray,1:not sel,2:sel
  77. INT SysControl::GetCheckBoxStatus()
  78. {
  79. AttachThreadInput(
  80. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  81. ::GetCurrentThreadId(), //自己的线程ID
  82. TRUE);
  83. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  84. m_ResHwnd, // handle to destination control
  85. (UINT)BM_GETCHECK, // message ID
  86. (WPARAM)0, // = (WPARAM) () wParam;
  87. (LPARAM)0 // = 0; not used, must be zero
  88. );
  89. if (lResult == BST_CHECKED)
  90. {
  91. return 2;
  92. }
  93. else if (lResult == BST_UNCHECKED)
  94. {
  95. return 1;
  96. }
  97. else if (lResult == BST_INDETERMINATE)
  98. {
  99. return 0;
  100. }
  101. return -1;
  102. }
  103. INT SysControl::GetListCount()
  104. {
  105. AttachThreadInput(
  106. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  107. ::GetCurrentThreadId(), //自己的线程ID
  108. TRUE);
  109. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  110. m_ResHwnd, // handle to destination control
  111. (UINT)LB_GETCOUNT, // message ID
  112. (WPARAM)0, // = (WPARAM) () wParam;
  113. (LPARAM)0 // = 0; not used, must be zero
  114. );
  115. if (lResult == CB_ERR)
  116. {
  117. return -1;
  118. }
  119. return (INT)lResult;
  120. }
  121. bool SysControl::GetListBoxTitle(DWORD Idx, TCHAR *pszTitle)
  122. {
  123. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  124. m_ResHwnd, // handle to destination control
  125. (UINT)LB_GETTEXT, // message ID
  126. (WPARAM)Idx, // = (WPARAM) () wParam;
  127. (LPARAM)pszTitle // = 0; not used, must be zero
  128. );
  129. if (lResult == CB_ERR)
  130. {
  131. return false;
  132. }
  133. return true;
  134. }
  135. bool SysControl::AddStringListBox(TCHAR *pszTitle)
  136. {
  137. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  138. m_ResHwnd, // handle to destination control
  139. (UINT)LB_ADDSTRING, // message ID
  140. (WPARAM)0, // = (WPARAM) () wParam;
  141. (LPARAM)pszTitle // = 0; not used, must be zero
  142. );
  143. if (lResult == CB_ERR)
  144. {
  145. return false;
  146. }
  147. return true;
  148. }
  149. INT SysControl::GetComboCount()
  150. {
  151. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  152. m_ResHwnd, // handle to destination control
  153. (UINT)CB_GETCOUNT, // message ID
  154. (WPARAM)0, // = (WPARAM) () wParam;
  155. (LPARAM)0 // = 0; not used, must be zero
  156. );
  157. if (lResult == CB_ERR)
  158. {
  159. return -1;
  160. }
  161. return (INT)lResult;
  162. }
  163. INT SysControl::GetComboCurSelIdx()
  164. {
  165. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  166. m_ResHwnd, // handle to destination control
  167. (UINT)CB_GETCURSEL, // message ID
  168. (WPARAM)0, // = (WPARAM) () wParam;
  169. (LPARAM)0 // = 0; not used, must be zero
  170. );
  171. if (lResult == CB_ERR)
  172. {
  173. return -1;
  174. }
  175. return (INT)lResult;
  176. }
  177. bool SysControl::GetComboCurSelStr(TCHAR *pszTitle, DWORD size)
  178. {
  179. INT CurSel = GetComboCurSelIdx();
  180. if (CurSel >= 0)
  181. {
  182. AttachThreadInput(
  183. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  184. ::GetCurrentThreadId(), //自己的线程ID
  185. TRUE);
  186. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  187. m_ResHwnd, // handle to destination control
  188. (UINT)CB_GETLBTEXT, // message ID
  189. (WPARAM)CurSel, // = (WPARAM) () wParam;
  190. (LPARAM)pszTitle // = 0; not used, must be zero
  191. );
  192. if (lResult == CB_ERR)
  193. {
  194. return false;
  195. }
  196. return true;
  197. }
  198. return false;
  199. }
  200. INT SysControl::SetComboCurSel(const char* pKey)
  201. {
  202. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  203. m_ResHwnd, // handle to destination control
  204. (UINT)CB_SELECTSTRING, // message ID
  205. (WPARAM)-1, // = (WPARAM) () wParam;
  206. (LPARAM)pKey // = 0; not used, must be zero
  207. );
  208. if (lResult == CB_ERR)
  209. {
  210. return -1;
  211. }
  212. return (INT)lResult;
  213. }
  214. bool SysControl::SetComboCurSel(DWORD Idx)
  215. {
  216. LRESULT lResult = SendMessage( // returns LRESULT in lResult
  217. m_ResHwnd, // handle to destination control
  218. (UINT)CB_SETCURSEL, // message ID
  219. (WPARAM)Idx, // = (WPARAM) () wParam;
  220. (LPARAM)0 // = 0; not used, must be zero
  221. );
  222. return (lResult != CB_ERR);
  223. }
  224. BOOL SysControl::IsObjectValid()
  225. {
  226. if(m_ResHwnd == NULL)
  227. {
  228. return FALSE;
  229. }
  230. return IsWindow(m_ResHwnd);
  231. }
  232. void SysControl::GetTabControlSelect(int &sel)
  233. {
  234. if(m_ResHwnd)
  235. {
  236. sel = TabCtrl_GetCurSel(m_ResHwnd);
  237. }
  238. }
  239. BOOL SysControl::SetTabControlSelect(DWORD sel,BOOL real)
  240. {
  241. SIZE_T dwBytesWrite, dwBytesRead;
  242. DWORD dwProcessID;
  243. HANDLE hProcess;
  244. BOOL bSuccess,bWriteOK;
  245. RECT rect = {0};
  246. int curSelection = -1;
  247. GetTabControlSelect(curSelection);
  248. if(curSelection == sel)
  249. {
  250. return TRUE;
  251. }
  252. GetWindowThreadProcessId(m_ResHwnd,&dwProcessID);
  253. hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessID);
  254. if(!hProcess) //得不到指定进程的句柄
  255. return FALSE;
  256. LPVOID lpHeadItemRemote=VirtualAllocEx(hProcess,NULL,sizeof(RECT),MEM_COMMIT,PAGE_READWRITE);
  257. bWriteOK=::WriteProcessMemory(hProcess,lpHeadItemRemote,(LPVOID)&rect,sizeof(RECT),(SIZE_T*)&dwBytesWrite);
  258. if(!bWriteOK) //写内存错误
  259. return FALSE;
  260. TabCtrl_GetItemRect(m_ResHwnd,sel,lpHeadItemRemote);
  261. bSuccess=ReadProcessMemory(hProcess,lpHeadItemRemote,&rect,sizeof(RECT),(SIZE_T*)&dwBytesRead);
  262. if(!bSuccess) //不能在指定进程内读取文本
  263. return FALSE;
  264. VirtualFreeEx(hProcess,lpHeadItemRemote,0,MEM_RELEASE);
  265. CloseHandle(hProcess);
  266. RECT ParentRect = {0,0,0,0};
  267. int CurSel = -1;
  268. int trytime = 10;
  269. while(CurSel != sel && trytime > 0)
  270. {
  271. InputCmds::HitMouse(m_ResHwnd, WM_LBUTTONDOWN, MK_LBUTTON, rect.left + ParentRect.left + 3, rect.top + ParentRect.top + 3, real);
  272. InputCmds::HitMouse(m_ResHwnd, WM_LBUTTONUP, MK_LBUTTON, rect.left + ParentRect.left + 3, rect.top + ParentRect.top + 3, real);
  273. Sleep(500);
  274. GetTabControlSelect(CurSel);
  275. --trytime;
  276. }
  277. return (CurSel == sel);
  278. }
  279. INT SysControl::GetTabControlCount()
  280. {
  281. return TabCtrl_GetItemCount(m_ResHwnd);;
  282. }
  283. bool SysControl::GetTabControlTitles(DWORD Idx,TCHAR *pszTitle,DWORD size)
  284. {
  285. SIZE_T dwBytesWrite, dwBytesRead;
  286. DWORD dwProcessID;
  287. HANDLE hProcess;
  288. BOOL bSuccess,bWriteOK;
  289. int count1 = TabCtrl_GetItemCount(m_ResHwnd);
  290. if(count1 <= 0)
  291. {
  292. return false;
  293. }
  294. TCITEM Item = { 0 };
  295. Item.mask = TCIF_TEXT;//text
  296. Item.cchTextMax = MAX_PATH;//size
  297. GetWindowThreadProcessId(m_ResHwnd,&dwProcessID);
  298. hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessID);
  299. if (!hProcess)
  300. {
  301. return false;
  302. }
  303. LPVOID lpHeadItemRemote=VirtualAllocEx(hProcess,NULL,sizeof(TCITEM),MEM_COMMIT,PAGE_READWRITE);
  304. LPVOID lpBuffRemote=VirtualAllocEx(hProcess,NULL,MAX_PATH*sizeof(TCHAR),MEM_COMMIT,PAGE_READWRITE);
  305. Item.pszText = (LPSTR)lpBuffRemote;//buff
  306. bWriteOK=::WriteProcessMemory(hProcess,lpHeadItemRemote,(LPVOID)&Item,sizeof(TCITEM),(SIZE_T*)&dwBytesWrite);
  307. if (!bWriteOK)
  308. {
  309. return false;
  310. }
  311. bool ret = false;
  312. if(Idx < (DWORD)count1)
  313. {
  314. memset(pszTitle, 0, sizeof(TCHAR)*size);
  315. if(TabCtrl_GetItem(m_ResHwnd,Idx,lpHeadItemRemote))
  316. {
  317. bSuccess = ReadProcessMemory(hProcess, lpBuffRemote, pszTitle, size, (SIZE_T*)&dwBytesRead);
  318. if (bSuccess)
  319. {
  320. ret = true;
  321. }
  322. }
  323. }
  324. VirtualFreeEx(hProcess,lpHeadItemRemote,0,MEM_RELEASE);
  325. VirtualFreeEx(hProcess,lpBuffRemote,0,MEM_RELEASE);
  326. CloseHandle(hProcess);
  327. return ret;
  328. }
  329. void SysControl::GetListViewControlSize(INT &Count, INT &Column)
  330. {
  331. HWND hHeaderCtrl = ListView_GetHeader(m_ResHwnd);
  332. Column = Header_GetItemCount(hHeaderCtrl);
  333. Count = ListView_GetItemCount(m_ResHwnd);
  334. }
  335. DWORD SysControl::GetListViewControlSelect(INT *pList, DWORD size)
  336. {
  337. DWORD Count = 0;
  338. int Idx = ListView_GetNextItem(m_ResHwnd,-1,LVNI_SELECTED);
  339. while(Idx >= 0)
  340. {
  341. if (Count < size)
  342. {
  343. pList[Count++] = Idx;
  344. }
  345. else
  346. {
  347. return Count;
  348. }
  349. Idx = ListView_GetNextItem(m_ResHwnd,Idx,LVNI_SELECTED);
  350. }
  351. return Count;
  352. }
  353. INT SysControl::ListViewMakeSelection(TCHAR *pszName)
  354. {
  355. INT ret = -1;
  356. INT Count = 0;
  357. INT Column = 0;
  358. DWORD dwProcessID;
  359. HANDLE hProcess;
  360. SIZE_T dwBytesWrite;
  361. BOOL bWriteOK;
  362. AttachThreadInput(
  363. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  364. ::GetCurrentThreadId(), //自己的线程ID
  365. TRUE);
  366. GetListViewControlSize(Count, Column);
  367. for (INT i = 0; i < Count; i++)
  368. {
  369. for (INT j = 0; j < Column; j++)
  370. {
  371. TCHAR szBuf[MAX_PATH] = { 0 };
  372. if (GetListViewItemContext(i, j, szBuf, MAX_PATH))
  373. {
  374. if (_stricmp(szBuf, pszName) == 0)
  375. {
  376. //hit line
  377. LVITEM lvitem;
  378. GetWindowThreadProcessId(m_ResHwnd, &dwProcessID);
  379. hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
  380. if (!hProcess) //得不到指定进程的句柄
  381. {
  382. return ret;
  383. }
  384. //在指定进程内分配存储空间
  385. LPVOID lpListItemRemote = VirtualAllocEx(hProcess, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
  386. lvitem.state = LVIS_SELECTED | LVIS_FOCUSED;
  387. lvitem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  388. dwBytesWrite = 0;
  389. bWriteOK = ::WriteProcessMemory(hProcess, lpListItemRemote, (LPVOID)&lvitem, sizeof(LVITEM), (SIZE_T*)&dwBytesWrite);
  390. if (!bWriteOK) //写内存错误
  391. {
  392. CloseHandle(hProcess);
  393. return ret;
  394. }
  395. InputCmds::MakeFocus(m_ResHwnd, TRUE);
  396. SendMessage(m_ResHwnd, LVM_SETITEMSTATE, (WPARAM)i, (LPARAM)lpListItemRemote);
  397. VirtualFreeEx(hProcess, lpListItemRemote, 0, MEM_RELEASE);
  398. CloseHandle(hProcess);
  399. return i;
  400. }
  401. }
  402. }
  403. }
  404. return ret;
  405. }
  406. bool SysControl::ListViewMakeChecked(TCHAR *pszName, BOOL Checked)
  407. {
  408. INT Idx = ListViewMakeSelection(pszName);
  409. if (Idx >= 0)
  410. {
  411. //ListView_SetCheckState(m_ResHwnd, Idx, Checked);
  412. //ListView_SetItemState(m_ResHwnd, Idx, INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1), LVIS_STATEIMAGEMASK);
  413. DWORD dwProcessID;
  414. HANDLE hProcess;
  415. SIZE_T dwBytesWrite;
  416. BOOL bWriteOK;
  417. LVITEM lvitem;
  418. GetWindowThreadProcessId(m_ResHwnd, &dwProcessID);
  419. hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
  420. if (!hProcess) //得不到指定进程的句柄
  421. {
  422. return false;
  423. }
  424. //在指定进程内分配存储空间
  425. LPVOID lpListItemRemote = VirtualAllocEx(hProcess, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
  426. lvitem.state = INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1);
  427. lvitem.stateMask = (LVIS_STATEIMAGEMASK);
  428. dwBytesWrite = 0;
  429. bWriteOK = ::WriteProcessMemory(hProcess, lpListItemRemote, (LPVOID)&lvitem, sizeof(LVITEM), (SIZE_T*)&dwBytesWrite);
  430. if (!bWriteOK) //写内存错误
  431. {
  432. CloseHandle(hProcess);
  433. return false;
  434. }
  435. SendMessage(m_ResHwnd, LVM_SETITEMSTATE, (WPARAM)Idx, (LPARAM)lpListItemRemote);
  436. VirtualFreeEx(hProcess, lpListItemRemote, 0, MEM_RELEASE);
  437. CloseHandle(hProcess);
  438. return true;
  439. //LV_ITEM _macro_lvi;
  440. //_macro_lvi.state = INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1);
  441. //_macro_lvi.stateMask = (LVIS_STATEIMAGEMASK);
  442. //SNDMSG((m_ResHwnd), LVM_SETITEMSTATE, (WPARAM)(Idx), (LPARAM)(LV_ITEM *)&_macro_lvi);
  443. //return true;
  444. }
  445. return false;
  446. }
  447. bool SysControl::ListViewControlHit(BOOL dbClick, DWORD hang, DWORD lie, BOOL real)
  448. {
  449. SIZE_T dwBytesWrite, dwBytesRead;
  450. DWORD dwProcessID;
  451. HANDLE hProcess;
  452. BOOL bSuccess,bWriteOK;
  453. //AttachThreadInput(
  454. // ::GetWindowThreadProcessId((m_ResHwnd),NULL), //当前焦点窗口的线程ID
  455. // ::GetCurrentThreadId(), //自己的线程ID
  456. // TRUE);
  457. GetWindowThreadProcessId(m_ResHwnd,&dwProcessID);
  458. hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessID);
  459. if (!hProcess) //得不到指定进程的句柄
  460. {
  461. return false;
  462. }
  463. POINT pt = {0};
  464. LPVOID lpHeadItemRemote=VirtualAllocEx(hProcess,NULL,sizeof(POINT),MEM_COMMIT,PAGE_READWRITE);
  465. if (lpHeadItemRemote == NULL)
  466. {
  467. CloseHandle(hProcess);
  468. return false;
  469. }
  470. bWriteOK=::WriteProcessMemory(hProcess,lpHeadItemRemote,(LPVOID)&pt,sizeof(POINT),(SIZE_T*)&dwBytesWrite);
  471. if(!bWriteOK) //写内存错误
  472. {
  473. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  474. CloseHandle(hProcess);
  475. return false;
  476. }
  477. ListView_GetItemPosition(m_ResHwnd,hang,lpHeadItemRemote);
  478. bSuccess=ReadProcessMemory(hProcess,lpHeadItemRemote,&pt,sizeof(POINT),(SIZE_T*)&dwBytesRead);
  479. if(!bSuccess) //不能在指定进程内读取文本
  480. {
  481. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  482. CloseHandle(hProcess);
  483. return false;
  484. }
  485. for (DWORD i = 0; i < lie; i++)
  486. {
  487. pt.x += ListView_GetColumnWidth(m_ResHwnd, i);
  488. }
  489. VirtualFreeEx(hProcess,lpHeadItemRemote,0,MEM_RELEASE);
  490. CloseHandle(hProcess);
  491. RECT rect = { 0, 0, 0, 0 };
  492. //GetWindowRect(m_ResHwnd,&rect);
  493. if(dbClick & real)
  494. {
  495. InputCmds::HitMouse(m_ResHwnd,WM_LBUTTONDBLCLK,MK_LBUTTON,rect.left + pt.x + 3,rect.top + pt.y + 3,real);
  496. }
  497. else if(dbClick)
  498. {
  499. InputCmds::HitMouse(m_ResHwnd,WM_LBUTTONDOWN,MK_LBUTTON,rect.left + pt.x + 3,rect.top + pt.y + 3,real);
  500. InputCmds::HitMouse(m_ResHwnd,WM_LBUTTONUP,MK_LBUTTON,rect.left + pt.x + 3,rect.top + pt.y + 3,real);
  501. InputCmds::HitMouse(m_ResHwnd,WM_LBUTTONDBLCLK,MK_LBUTTON,rect.left + pt.x + 3,rect.top + pt.y + 3,real);
  502. }
  503. else
  504. {
  505. InputCmds::HitMouse(m_ResHwnd,WM_LBUTTONDOWN,MK_LBUTTON,rect.left + pt.x + 3,rect.top + pt.y + 3,real);
  506. InputCmds::HitMouse(m_ResHwnd,WM_LBUTTONUP,MK_LBUTTON,rect.left + pt.x + 3,rect.top + pt.y + 3,real);
  507. }
  508. return true;
  509. }
  510. bool SysControl::GetListViewItemTitle(DWORD Column, TCHAR *pszTitle, DWORD size)
  511. {
  512. const DWORD nMaxLen = 1023;
  513. TCHAR szBuf[nMaxLen + 1];
  514. int nLVItemCount;
  515. int nColumns;
  516. DWORD dwProcessID;
  517. HANDLE hProcess;
  518. HWND hHeaderCtrl;
  519. HDITEM hdItemLocal;
  520. SIZE_T dwBytesRead, dwBytesWrite;
  521. BOOL bSuccess, bWriteOK;
  522. nLVItemCount = ListView_GetItemCount(m_ResHwnd);
  523. hHeaderCtrl = ListView_GetHeader(m_ResHwnd);
  524. nColumns = Header_GetItemCount(hHeaderCtrl);
  525. if (nColumns <= 0 || (INT)Column >= nColumns) {
  526. return false;
  527. }
  528. GetWindowThreadProcessId(m_ResHwnd, &dwProcessID);
  529. hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
  530. if (!hProcess) //得不到指定进程的句柄
  531. {
  532. return false;
  533. }
  534. //在指定进程内分配存储空间
  535. LPVOID lpTextRemote = VirtualAllocEx(hProcess, NULL, nMaxLen + 1, MEM_COMMIT, PAGE_READWRITE);
  536. LPVOID lpHeadItemRemote = VirtualAllocEx(hProcess, NULL, sizeof(HDITEM), MEM_COMMIT, PAGE_READWRITE);
  537. if ((!lpTextRemote) || (!lpHeadItemRemote)) //不能在指定进程内分配存储空间
  538. {
  539. CloseHandle(hProcess);
  540. return false;
  541. }
  542. ZeroMemory(szBuf, nMaxLen + 1);
  543. bWriteOK = ::WriteProcessMemory(hProcess, lpTextRemote, (LPVOID)szBuf, nMaxLen + 1, (SIZE_T*)&dwBytesWrite);
  544. if (!bWriteOK) //写内存错误
  545. {
  546. VirtualFreeEx(hProcess, lpTextRemote, 0, MEM_RELEASE);
  547. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  548. //关闭指定进程句柄
  549. CloseHandle(hProcess);
  550. return false;
  551. }
  552. hdItemLocal.mask = HDI_TEXT;
  553. hdItemLocal.cchTextMax = nMaxLen;
  554. hdItemLocal.pszText = (LPTSTR)lpTextRemote;
  555. dwBytesWrite = 0;
  556. bWriteOK = ::WriteProcessMemory(hProcess, lpHeadItemRemote, (LPVOID)&hdItemLocal, sizeof(HDITEM), (SIZE_T*)&dwBytesWrite);
  557. if (!bWriteOK) //写内存错误
  558. {
  559. VirtualFreeEx(hProcess, lpTextRemote, 0, MEM_RELEASE);
  560. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  561. //关闭指定进程句柄
  562. CloseHandle(hProcess);
  563. return false;
  564. }
  565. SendMessage(hHeaderCtrl, HDM_GETITEM, (WPARAM)Column, (LPARAM)lpHeadItemRemote);
  566. bSuccess = ReadProcessMemory(hProcess, lpTextRemote, szBuf, nMaxLen + 1, (SIZE_T*)&dwBytesRead);
  567. //从指定进程存储空间读取文本
  568. if (!bSuccess) //不能在指定进程内读取文本
  569. {
  570. VirtualFreeEx(hProcess, lpTextRemote, 0, MEM_RELEASE);
  571. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  572. //关闭指定进程句柄
  573. CloseHandle(hProcess);
  574. return false;
  575. }
  576. strcpy_s(pszTitle, size, szBuf);
  577. VirtualFreeEx(hProcess, lpTextRemote, 0, MEM_RELEASE);
  578. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  579. //关闭指定进程句柄
  580. CloseHandle(hProcess);
  581. return true;
  582. }
  583. bool SysControl::GetListViewItemContext(DWORD hang, DWORD lie, TCHAR *pszContext, DWORD size)
  584. //void SysControl::GetAllListViewItems(vector<vector<wstring>> &strlist)
  585. {
  586. const DWORD nMaxLen=1023;
  587. TCHAR szBuf[nMaxLen+1];
  588. int nLVItemCount;
  589. int nColumns;
  590. DWORD dwProcessID;
  591. HANDLE hProcess;
  592. HWND hHeaderCtrl;
  593. LVITEM lvItemLocal;
  594. SIZE_T dwBytesRead, dwBytesWrite;
  595. BOOL bSuccess,bWriteOK;
  596. nLVItemCount = ListView_GetItemCount(m_ResHwnd);
  597. hHeaderCtrl = ListView_GetHeader(m_ResHwnd);
  598. nColumns = Header_GetItemCount(hHeaderCtrl);
  599. if ((INT)hang >= nLVItemCount || (nColumns <= 0 || (INT)lie >= nColumns))
  600. {
  601. return false;
  602. }
  603. GetWindowThreadProcessId(m_ResHwnd, &dwProcessID);
  604. hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessID);
  605. if (!hProcess) //得不到指定进程的句柄
  606. {
  607. return false;
  608. }
  609. //在指定进程内分配存储空间
  610. LPVOID lpTextRemote=VirtualAllocEx(hProcess,NULL,nMaxLen+1,MEM_COMMIT,PAGE_READWRITE);
  611. LPVOID lpListItemRemote=VirtualAllocEx(hProcess,NULL,sizeof(LVITEM),MEM_COMMIT,PAGE_READWRITE);
  612. if ((!lpTextRemote) || (!lpListItemRemote)) //不能在指定进程内分配存储空间
  613. {
  614. CloseHandle(hProcess);
  615. return false;
  616. }
  617. bool ret = false;
  618. do{
  619. ZeroMemory(szBuf,nMaxLen+1);
  620. bWriteOK= ::WriteProcessMemory(hProcess,lpTextRemote,(LPVOID)szBuf,nMaxLen+1,(SIZE_T*)&dwBytesWrite);
  621. if (!bWriteOK) //写内存错误
  622. {
  623. break;
  624. }
  625. lvItemLocal.iItem = hang;
  626. lvItemLocal.iSubItem = lie;
  627. lvItemLocal.mask=LVIF_TEXT;
  628. lvItemLocal.cchTextMax=nMaxLen;
  629. lvItemLocal.pszText=(LPTSTR)lpTextRemote;
  630. dwBytesWrite=0;
  631. bWriteOK=::WriteProcessMemory(hProcess,lpListItemRemote,(LPVOID)&lvItemLocal,sizeof(LVITEM),(SIZE_T*)&dwBytesWrite);
  632. if (!bWriteOK) //写内存错误
  633. {
  634. break;
  635. }
  636. SendMessage(m_ResHwnd, LVM_GETITEMTEXT, (WPARAM)hang, (LPARAM)lpListItemRemote);
  637. bSuccess=ReadProcessMemory(hProcess,lpTextRemote,szBuf,nMaxLen+1,(SIZE_T*)&dwBytesRead);
  638. //从指定进程存储空间读取文本
  639. if (!bSuccess) //不能在指定进程内读取文本
  640. {
  641. break;
  642. }
  643. strcpy_s(pszContext, size, szBuf);
  644. ret = true;
  645. } while (0);
  646. //在指定进程内释放存储空间
  647. VirtualFreeEx(hProcess,lpListItemRemote,0,MEM_RELEASE);
  648. VirtualFreeEx(hProcess,lpTextRemote,0,MEM_RELEASE);
  649. //关闭指定进程句柄
  650. CloseHandle(hProcess);
  651. return ret;
  652. }
  653. bool SysControl::GetTreeViewNodeName(PVOID hItem,TCHAR *pszBuff)
  654. {
  655. SIZE_T dwBytesWrite, dwBytesRead;
  656. DWORD dwProcessID;
  657. HANDLE hProcess;
  658. BOOL bSuccess,bWriteOK;
  659. memset(pszBuff,0,MAX_PATH*sizeof(TCHAR));
  660. GetWindowThreadProcessId(m_ResHwnd,&dwProcessID);
  661. hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessID);
  662. if (!hProcess) //得不到指定进程的句柄
  663. {
  664. return false;
  665. }
  666. TVITEM itemattr = { 0 };
  667. itemattr.hItem = (HTREEITEM)hItem;
  668. itemattr.mask = TVIF_TEXT;
  669. itemattr.cchTextMax = MAX_PATH;
  670. LPVOID lpHeadItemBuff=VirtualAllocEx(hProcess,NULL,sizeof(TCHAR)*MAX_PATH,MEM_COMMIT,PAGE_READWRITE);
  671. LPVOID lpHeadItemRemote=VirtualAllocEx(hProcess,NULL,sizeof(TVITEM),MEM_COMMIT,PAGE_READWRITE);
  672. if (lpHeadItemBuff == NULL || lpHeadItemRemote == NULL)
  673. {
  674. CloseHandle(hProcess);
  675. return false;
  676. }
  677. itemattr.pszText = (LPSTR)lpHeadItemBuff;
  678. bWriteOK=::WriteProcessMemory(hProcess,lpHeadItemRemote,(LPVOID)&itemattr,sizeof(TVITEM),(SIZE_T*)&dwBytesWrite);
  679. if (!bWriteOK) //写内存错误
  680. {
  681. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  682. VirtualFreeEx(hProcess, lpHeadItemBuff, 0, MEM_RELEASE);
  683. CloseHandle(hProcess);
  684. return false;
  685. }
  686. //TreeView_GetItem(m_ResHwnd, lpHeadItemRemote);
  687. BOOL ret = (BOOL)SNDMSG((m_ResHwnd), TVM_GETITEM, 0, (LPARAM)(TV_ITEM *)(lpHeadItemRemote));
  688. bSuccess=ReadProcessMemory(hProcess,lpHeadItemBuff,pszBuff,sizeof(TCHAR)*MAX_PATH,(SIZE_T*)&dwBytesRead);
  689. if (!bSuccess) //不能在指定进程内读取文本
  690. {
  691. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  692. VirtualFreeEx(hProcess, lpHeadItemBuff, 0, MEM_RELEASE);
  693. CloseHandle(hProcess);
  694. return false;
  695. }
  696. VirtualFreeEx(hProcess,lpHeadItemRemote,0,MEM_RELEASE);
  697. VirtualFreeEx(hProcess,lpHeadItemBuff,0,MEM_RELEASE);
  698. CloseHandle(hProcess);
  699. return true;
  700. }
  701. PVOID SysControl::FindSpecificTreeNode(PVOID hItem,TCHAR *pszNodeName)
  702. {
  703. BOOL ret = FALSE;
  704. HTREEITEM hFound = NULL;
  705. while(hItem)
  706. {
  707. TCHAR szBuff[MAX_PATH] = { 0 };
  708. GetTreeViewNodeName(hItem, szBuff);
  709. if (_strnicmp(szBuff, pszNodeName,strlen(pszNodeName)) == 0)
  710. {
  711. //got one
  712. return hItem;
  713. }
  714. //child
  715. HTREEITEM hChildItem = TreeView_GetChild(m_ResHwnd, ((HTREEITEM)hItem));
  716. if(hChildItem)
  717. {
  718. hFound = (HTREEITEM)FindSpecificTreeNode(hChildItem, pszNodeName);
  719. if(hFound)
  720. {
  721. return hFound;
  722. }
  723. }
  724. hItem = TreeView_GetNextItem(m_ResHwnd, ((HTREEITEM)hItem), TVGN_NEXT);
  725. }
  726. return NULL;
  727. }
  728. bool SysControl::TreeViewMakeSelection(TCHAR *pszName)
  729. {
  730. AttachThreadInput(
  731. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  732. ::GetCurrentThreadId(), //自己的线程ID
  733. TRUE);
  734. HTREEITEM hItem = TreeView_GetRoot(m_ResHwnd);
  735. HTREEITEM hFound = (HTREEITEM)FindSpecificTreeNode(hItem, pszName);
  736. if (hFound)
  737. {
  738. InputCmds::MakeFocus(m_ResHwnd, TRUE);
  739. TreeView_Select(m_ResHwnd, hFound, TVGN_CARET);
  740. return true;
  741. }
  742. return false;
  743. }
  744. bool SysControl::TreeViewMakeIdxChecked(DWORD Idx, BOOL Checked)
  745. {
  746. SIZE_T dwBytesWrite;
  747. DWORD dwProcessID;
  748. HANDLE hProcess;
  749. BOOL bWriteOK;
  750. AttachThreadInput(
  751. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  752. ::GetCurrentThreadId(), //自己的线程ID
  753. TRUE);
  754. //InputCmds::MakeFocus(m_ResHwnd, TRUE);
  755. //UINT count1 = TreeView_GetCount(m_ResHwnd);
  756. HTREEITEM hItem = TreeView_GetRoot(m_ResHwnd);
  757. HTREEITEM hNext = hItem;//= TreeView_GetNextItem(m_ResHwnd,hItem,TVGN_CHILD);
  758. DWORD firstCount = 0;
  759. if (hItem)
  760. {
  761. while (firstCount < Idx && hNext != NULL)
  762. {
  763. hNext = TreeView_GetNextItem(m_ResHwnd, hNext, TVGN_NEXT);
  764. ++firstCount;
  765. }
  766. if (hNext == NULL)
  767. {
  768. return false;
  769. }
  770. //found idx
  771. TreeView_Select(m_ResHwnd, hNext, TVGN_CARET);
  772. //TreeView_SetCheckState(m_ResHwnd, hFound,Checked);
  773. //TreeView_SetItemState(m_ResHwnd, hFound, INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1), TVIS_STATEIMAGEMASK);
  774. GetWindowThreadProcessId(m_ResHwnd, &dwProcessID);
  775. hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
  776. if (!hProcess) //得不到指定进程的句柄
  777. {
  778. return false;
  779. }
  780. TVITEM itemattr = { 0 };
  781. itemattr.mask = TVIF_HANDLE | TVIF_STATE;
  782. itemattr.hItem = (hNext);
  783. itemattr.stateMask = (TVIS_STATEIMAGEMASK);
  784. itemattr.state = (INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1));
  785. LPVOID lpHeadItemRemote = VirtualAllocEx(hProcess, NULL, sizeof(TVITEM), MEM_COMMIT, PAGE_READWRITE);
  786. if (lpHeadItemRemote == NULL)
  787. {
  788. CloseHandle(hProcess);
  789. return false;
  790. }
  791. bWriteOK = ::WriteProcessMemory(hProcess, lpHeadItemRemote, (LPVOID)&itemattr, sizeof(TVITEM), (SIZE_T*)&dwBytesWrite);
  792. if (!bWriteOK) //写内存错误
  793. {
  794. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  795. CloseHandle(hProcess);
  796. return false;
  797. }
  798. BOOL Sent = (BOOL)SendMessage(m_ResHwnd, TVM_SETITEMW, (WPARAM)0, (LPARAM)lpHeadItemRemote);
  799. //TVITEM _ms_TVi;
  800. //_ms_TVi.mask = TVIF_STATE;
  801. //_ms_TVi.hItem = (hFound);
  802. //_ms_TVi.stateMask = (TVIS_STATEIMAGEMASK);
  803. //_ms_TVi.state = (INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1));
  804. //SNDMSG((m_ResHwnd), TVM_SETITEM, 0, (LPARAM)(TV_ITEM *)&_ms_TVi);
  805. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  806. CloseHandle(hProcess);
  807. if (Sent == FALSE)
  808. {
  809. return false;
  810. }
  811. return true;
  812. }
  813. return false;
  814. }
  815. bool SysControl::TreeViewMakeChecked(TCHAR *pszName, BOOL Checked)
  816. {
  817. SIZE_T dwBytesWrite;
  818. DWORD dwProcessID;
  819. HANDLE hProcess;
  820. BOOL bWriteOK;
  821. AttachThreadInput(
  822. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  823. ::GetCurrentThreadId(), //自己的线程ID
  824. TRUE);
  825. HTREEITEM hItem = TreeView_GetRoot(m_ResHwnd);
  826. HTREEITEM hFound = (HTREEITEM)FindSpecificTreeNode(hItem, pszName);
  827. if (hFound)
  828. {
  829. TreeView_Select(m_ResHwnd, hFound, TVGN_CARET);
  830. //TreeView_SetCheckState(m_ResHwnd, hFound,Checked);
  831. //TreeView_SetItemState(m_ResHwnd, hFound, INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1), TVIS_STATEIMAGEMASK);
  832. GetWindowThreadProcessId(m_ResHwnd, &dwProcessID);
  833. hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
  834. if (!hProcess) //得不到指定进程的句柄
  835. {
  836. return false;
  837. }
  838. TVITEM itemattr = { 0 };
  839. itemattr.mask = TVIF_STATE;
  840. itemattr.hItem = (hFound);
  841. itemattr.stateMask = (TVIS_STATEIMAGEMASK);
  842. itemattr.state = (INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1));
  843. LPVOID lpHeadItemRemote = VirtualAllocEx(hProcess, NULL, sizeof(TVITEM), MEM_COMMIT, PAGE_READWRITE);
  844. if (lpHeadItemRemote == NULL)
  845. {
  846. CloseHandle(hProcess);
  847. return false;
  848. }
  849. bWriteOK = ::WriteProcessMemory(hProcess, lpHeadItemRemote, (LPVOID)&itemattr, sizeof(TVITEM), (SIZE_T*)&dwBytesWrite);
  850. if (!bWriteOK) //写内存错误
  851. {
  852. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  853. CloseHandle(hProcess);
  854. return false;
  855. }
  856. SendMessage(m_ResHwnd, TVM_SETITEM, (WPARAM)0, (LPARAM)lpHeadItemRemote);
  857. //TVITEM _ms_TVi;
  858. //_ms_TVi.mask = TVIF_STATE;
  859. //_ms_TVi.hItem = (hFound);
  860. //_ms_TVi.stateMask = (TVIS_STATEIMAGEMASK);
  861. //_ms_TVi.state = (INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1));
  862. //SNDMSG((m_ResHwnd), TVM_SETITEM, 0, (LPARAM)(TV_ITEM *)&_ms_TVi);
  863. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  864. CloseHandle(hProcess);
  865. return true;
  866. }
  867. return false;
  868. }
  869. bool SysControl::TreeViewGetSelectedItemTitle(TCHAR *pszTitle)
  870. {
  871. AttachThreadInput(
  872. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  873. ::GetCurrentThreadId(), //自己的线程ID
  874. TRUE);
  875. HTREEITEM hItem = TreeView_GetSelection(m_ResHwnd);
  876. if (hItem)
  877. {
  878. return GetTreeViewNodeName(hItem, pszTitle);
  879. }
  880. return false;
  881. }
  882. bool SysControl::TreeViewMakeCheckedItem(HTREEITEM hFound, BOOL Checked)
  883. {
  884. SIZE_T dwBytesWrite;
  885. DWORD dwProcessID;
  886. HANDLE hProcess;
  887. BOOL bWriteOK;
  888. if (hFound)
  889. {
  890. TreeView_Select(m_ResHwnd, hFound, TVGN_CARET);
  891. //TreeView_SetCheckState(m_ResHwnd, hFound,Checked);
  892. //TreeView_SetItemState(m_ResHwnd, hFound, INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1), TVIS_STATEIMAGEMASK);
  893. GetWindowThreadProcessId(m_ResHwnd, &dwProcessID);
  894. hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
  895. if (!hProcess) //得不到指定进程的句柄
  896. {
  897. return false;
  898. }
  899. TVITEM itemattr = { 0 };
  900. itemattr.mask = TVIF_STATE;
  901. itemattr.hItem = (hFound);
  902. itemattr.stateMask = (TVIS_STATEIMAGEMASK);
  903. itemattr.state = (INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1));
  904. LPVOID lpHeadItemRemote = VirtualAllocEx(hProcess, NULL, sizeof(TVITEM), MEM_COMMIT, PAGE_READWRITE);
  905. if (lpHeadItemRemote == NULL)
  906. {
  907. CloseHandle(hProcess);
  908. return false;
  909. }
  910. bWriteOK = ::WriteProcessMemory(hProcess, lpHeadItemRemote, (LPVOID)&itemattr, sizeof(TVITEM), (SIZE_T*)&dwBytesWrite);
  911. if (!bWriteOK) //写内存错误
  912. {
  913. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  914. CloseHandle(hProcess);
  915. return false;
  916. }
  917. SendMessage(m_ResHwnd, TVM_SETITEM, (WPARAM)0, (LPARAM)lpHeadItemRemote);
  918. //TVITEM _ms_TVi;
  919. //_ms_TVi.mask = TVIF_STATE;
  920. //_ms_TVi.hItem = (hFound);
  921. //_ms_TVi.stateMask = (TVIS_STATEIMAGEMASK);
  922. //_ms_TVi.state = (INDEXTOSTATEIMAGEMASK((Checked) ? 2 : 1));
  923. //SNDMSG((m_ResHwnd), TVM_SETITEM, 0, (LPARAM)(TV_ITEM *)&_ms_TVi);
  924. VirtualFreeEx(hProcess, lpHeadItemRemote, 0, MEM_RELEASE);
  925. CloseHandle(hProcess);
  926. return true;
  927. }
  928. return false;
  929. }
  930. BOOL SysControl::TreeViewSelectAB(DWORD firstLevel, DWORD secondLevel)
  931. {
  932. AttachThreadInput(
  933. ::GetWindowThreadProcessId((m_ResHwnd),NULL), //当前焦点窗口的线程ID
  934. ::GetCurrentThreadId(), //自己的线程ID
  935. TRUE);
  936. InputCmds::MakeFocus(m_ResHwnd, TRUE);
  937. //UINT count1 = TreeView_GetCount(m_ResHwnd);
  938. HTREEITEM hItem = TreeView_GetRoot(m_ResHwnd);
  939. HTREEITEM hChild ;//= TreeView_GetNextItem(m_ResHwnd,hItem,TVGN_CHILD);
  940. DWORD firstCount = 0;
  941. while(hItem)
  942. {
  943. if(firstCount == firstLevel)
  944. {
  945. hChild = TreeView_GetNextItem(m_ResHwnd,hItem,TVGN_CHILD);
  946. DWORD secondCount = 0;
  947. while(hChild)
  948. {
  949. if(secondCount == secondLevel)
  950. {
  951. TreeView_Select(m_ResHwnd,hChild,TVGN_CARET);
  952. return TRUE;
  953. }
  954. hChild = TreeView_GetNextItem(m_ResHwnd,hChild,TVGN_NEXT);
  955. ++secondCount;
  956. }
  957. break;
  958. }
  959. hItem = TreeView_GetNextItem(m_ResHwnd,hItem,TVGN_NEXT);
  960. ++firstCount;
  961. }
  962. return FALSE;
  963. }
  964. bool SysControl::TreeViewMakeCheckedAB(DWORD firstLevel, INT secondLevel)
  965. {
  966. AttachThreadInput(
  967. ::GetWindowThreadProcessId((m_ResHwnd), NULL), //当前焦点窗口的线程ID
  968. ::GetCurrentThreadId(), //自己的线程ID
  969. TRUE);
  970. InputCmds::MakeFocus(m_ResHwnd, FALSE);
  971. //UINT count1 = TreeView_GetCount(m_ResHwnd);
  972. HTREEITEM hItem = TreeView_GetRoot(m_ResHwnd);
  973. HTREEITEM hChild;//= TreeView_GetNextItem(m_ResHwnd,hItem,TVGN_CHILD);
  974. DWORD firstCount = 0;
  975. while (hItem)
  976. {
  977. if (secondLevel < 0)
  978. {
  979. if (firstCount == firstLevel)
  980. {
  981. //this is it
  982. TreeView_Select(m_ResHwnd, hItem, TVGN_CARET);
  983. return TreeViewMakeCheckedItem(hItem, TRUE);
  984. }
  985. hItem = TreeView_GetNextItem(m_ResHwnd, hItem, TVGN_NEXT);
  986. ++firstCount;
  987. }
  988. else
  989. {
  990. if (firstCount == firstLevel)
  991. {
  992. hChild = TreeView_GetNextItem(m_ResHwnd, hItem, TVGN_CHILD);
  993. DWORD secondCount = 0;
  994. while (hChild)
  995. {
  996. if (secondCount == secondLevel)
  997. {
  998. return TreeViewMakeCheckedItem(hChild, TRUE);
  999. }
  1000. hChild = TreeView_GetNextItem(m_ResHwnd, hChild, TVGN_NEXT);
  1001. ++secondCount;
  1002. }
  1003. break;
  1004. }
  1005. hItem = TreeView_GetNextItem(m_ResHwnd, hItem, TVGN_NEXT);
  1006. ++firstCount;
  1007. }
  1008. }
  1009. return false;
  1010. }
  1011. BOOL SysControl::TreeViewSelectAB(DWORD firstLevel, INT secondLevel,BOOL Focus)
  1012. {
  1013. AttachThreadInput(
  1014. ::GetWindowThreadProcessId((m_ResHwnd),NULL), //当前焦点窗口的线程ID
  1015. ::GetCurrentThreadId(), //自己的线程ID
  1016. TRUE);
  1017. if (Focus)
  1018. {
  1019. InputCmds::MakeFocus(m_ResHwnd, TRUE);
  1020. }
  1021. else
  1022. {
  1023. InputCmds::MakeFocus(m_ResHwnd, FALSE);
  1024. }
  1025. //UINT count1 = TreeView_GetCount(m_ResHwnd);
  1026. HTREEITEM hItem = TreeView_GetRoot(m_ResHwnd);
  1027. HTREEITEM hChild ;//= TreeView_GetNextItem(m_ResHwnd,hItem,TVGN_CHILD);
  1028. DWORD firstCount = 0;
  1029. while(hItem)
  1030. {
  1031. if (secondLevel < 0)
  1032. {
  1033. if (firstCount == firstLevel)
  1034. {
  1035. //this is it
  1036. TreeView_Select(m_ResHwnd, hItem, TVGN_CARET);
  1037. return TRUE;
  1038. }
  1039. hItem = TreeView_GetNextItem(m_ResHwnd, hItem, TVGN_NEXT);
  1040. ++firstCount;
  1041. }
  1042. else
  1043. {
  1044. if (firstCount == firstLevel)
  1045. {
  1046. hChild = TreeView_GetNextItem(m_ResHwnd, hItem, TVGN_CHILD);
  1047. DWORD secondCount = 0;
  1048. while (hChild)
  1049. {
  1050. if (secondCount == secondLevel)
  1051. {
  1052. TreeView_Select(m_ResHwnd, hChild, TVGN_CARET);
  1053. return TRUE;
  1054. }
  1055. hChild = TreeView_GetNextItem(m_ResHwnd, hChild, TVGN_NEXT);
  1056. ++secondCount;
  1057. }
  1058. break;
  1059. }
  1060. hItem = TreeView_GetNextItem(m_ResHwnd, hItem, TVGN_NEXT);
  1061. ++firstCount;
  1062. }
  1063. }
  1064. return FALSE;
  1065. }
  1066. //BOOL SysControl::HitTreeNode(BOOL dbClick,vector<wstring> &OneTreeRoute,BOOL real)
  1067. //{
  1068. // BOOL ret = FALSE;
  1069. // WCHAR szBuff[MAX_PATH];
  1070. // HTREEITEM hFound = NULL;
  1071. // HTREEITEM hItem = TreeView_GetRoot(m_ResHwnd);
  1072. //
  1073. // vector<wstring> curSelection;
  1074. // GetTreeSelection(curSelection);
  1075. //
  1076. // if(curSelection == OneTreeRoute)
  1077. // {
  1078. // return TRUE;
  1079. // }
  1080. //
  1081. // for(INT i = (INT)OneTreeRoute.size() - 1;i >= 0;i--)
  1082. // {
  1083. // hFound = FindSpecificTreeNode(hItem,(WCHAR*)OneTreeRoute[i].c_str(),szBuff);
  1084. //
  1085. // if(hFound)
  1086. // {
  1087. // ret = TreeView_EnsureVisible(m_ResHwnd,hFound);
  1088. // if(ret == FALSE)//send OK
  1089. // {
  1090. // //do hit
  1091. // ret = HitSpecificTreeNode(dbClick,hFound,real);
  1092. // if(i == OneTreeRoute.size() - 1)
  1093. // {
  1094. // //done
  1095. // //check selection,and return
  1096. // vector<wstring> realSel;
  1097. // int trytime = 50;
  1098. // while(trytime > 0)
  1099. // {
  1100. // GetTreeSelection(realSel);
  1101. // if(realSel.size() > 0)
  1102. // {
  1103. // if(realSel == OneTreeRoute)
  1104. // {
  1105. // return TRUE;
  1106. // }
  1107. // }
  1108. //
  1109. // Sleep(100);
  1110. // --trytime;
  1111. // }
  1112. //
  1113. // break;
  1114. // }
  1115. // else
  1116. // {
  1117. // i = (INT)OneTreeRoute.size() - 1;
  1118. //
  1119. // }
  1120. //
  1121. // }
  1122. // }
  1123. //
  1124. // }
  1125. //
  1126. //
  1127. // return ret;
  1128. //
  1129. //}