当前位置:   article > 正文

python窗口绘制,文字绘制,绘制矩形,yolo绘制方框,自瞄,绘制源码_python left, top, right, bottom,

python left, top, right, bottom,
  1. from win32gui import *
  2. import ctypes, win32process, win32api, pygame
  3. version = "1.0.0"
  4. STANDARD_RIGHTS_REQUIRED = 0x000F0000
  5. SYNCHRONIZE = 0x00100000
  6. TH32CS_SNAPMODULE = 0x00000008
  7. class PROCESS_BASIC_INFORMATION(ctypes.Structure):
  8. _fields_ = [
  9. ('ExitStatus', ctypes.c_ulonglong),
  10. ('PebBaseAddress', ctypes.c_ulonglong),
  11. ('AffinityMask', ctypes.c_ulonglong),
  12. ('BasePriority', ctypes.c_ulonglong),
  13. ('UniqueProcessId', ctypes.c_ulonglong),
  14. ('InheritedFromUniqueProcessId', ctypes.c_ulonglong)]
  15. class MODULEENTRY32(ctypes.Structure):
  16. _fields_ = [
  17. ('dwSize', ctypes.c_long),
  18. ('th32ModuleID', ctypes.c_long),
  19. ('th32ProcessID', ctypes.c_long),
  20. ('GlblcntUsage', ctypes.c_long),
  21. ('ProccntUsage', ctypes.c_long),
  22. ('modBaseAddr', ctypes.c_long),
  23. ('modBaseSize', ctypes.c_long),
  24. ('hModule', ctypes.c_void_p),
  25. ('szModule', ctypes.c_char * 256),
  26. ('szExePath', ctypes.c_char * 260)]
  27. kernel32 = ctypes.windll.LoadLibrary("kernel32.dll")
  28. user32 = ctypes.windll.LoadLibrary("user32.dll")
  29. GetLastError = kernel32.GetLastError
  30. GetLastError.rettype = ctypes.c_long
  31. GetLastError = ctypes.windll.kernel32.GetLastError
  32. Module32First = ctypes.windll.kernel32.Module32First
  33. Module32First.argtypes = [ctypes.c_void_p, ctypes.POINTER(MODULEENTRY32)]
  34. Module32First.rettype = ctypes.c_int
  35. Module32Next = ctypes.windll.kernel32.Module32Next
  36. Module32Next.argtypes = [ctypes.c_void_p, ctypes.POINTER(MODULEENTRY32)]
  37. Module32Next.rettype = ctypes.c_int
  38. CreateToolhelp32Snapshot = ctypes.windll.kernel32.CreateToolhelp32Snapshot
  39. PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF)
  40. def FindWindowPid(ClassName, WindowName):
  41. hWindow = FindWindow(ClassName, WindowName)
  42. return win32process.GetWindowThreadProcessId(hWindow)[1]
  43. def MessageBoxTimeoutA(hwnd,title,cont,dwtimeout,state):
  44. return user32.MessageBoxTimeoutA(hwnd,cont,title,state,0,dwtimeout)
  45. def GetMousePos(hwnd):
  46. return user32.SetCapture(hwnd)
  47. def MonitorHotkeys(hotkey):
  48. return user32.GetAsyncKeyState(hotkey)
  49. def GetWindRect(hwnd):
  50. left, top, right, bottom = GetWindowRect(hwnd)
  51. Width = right - left
  52. Height = bottom - top
  53. return (Width, Height)
  54. class SetupProcess():
  55. def __init__(self, pid):
  56. self.ntdll = ctypes.WinDLL("ntdll.dll")
  57. self.hProcess = win32api.OpenProcess(PROCESS_ALL_ACCESS, 0, pid)
  58. # ReadMemory62 Method -------------------------------------------------------------------------------
  59. def ReadMemory64(self, addr, n=4):
  60. addr = ctypes.c_ulonglong(addr)
  61. retn = ctypes.c_ulonglong()
  62. BufferLength = ctypes.c_ulonglong(n)
  63. self.ntdll.NtWow64ReadVirtualMemory64(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  64. return retn.value
  65. def ReadMemory64_float(self,addr, n=4):
  66. addr = ctypes.c_ulonglong(addr)
  67. retn = ctypes.c_float()
  68. BufferLength = ctypes.c_ulonglong(n)
  69. self.ntdll.NtWow64ReadVirtualMemory64(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  70. return retn.value
  71. def ReadMemory64_double(self, addr, n=8):
  72. addr = ctypes.c_ulonglong(addr)
  73. retn = ctypes.c_double()
  74. BufferLength = ctypes.c_ulonglong(n)
  75. self.ntdll.NtWow64ReadVirtualMemory64(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  76. return retn.value
  77. def ReadMemory64_byte(self,addr, n=8):
  78. addr = ctypes.c_int(addr)
  79. retn = ctypes.c_byte()
  80. BufferLength = ctypes.c_int(n)
  81. self.ntdll.NtWow64ReadVirtualMemory64(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  82. return retn.value
  83. def ReadMemory64_Wchar(self, addr, n, length):
  84. addr = ctypes.c_ulonglong(addr)
  85. retn = ctypes.c_wchar_p("0" * length)
  86. BufferLength = ctypes.c_ulonglong(n)
  87. self.ntdll.NtWow64ReadVirtualMemory64(int(self.hProcess), addr, retn, BufferLength, 0)
  88. return retn.value
  89. # WriteMemory62 Method -------------------------------------------------------------------------------
  90. def WriteMemory64(self, addr, s, n=4):
  91. addr = ctypes.c_ulonglong(addr)
  92. retn = ctypes.c_ulonglong(s)
  93. BufferLength = ctypes.c_ulonglong(n)
  94. self.ntdll.NtWow64WriteVirtualMemory64(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  95. def WriteMemory64_float(self, addr, s, n=4):
  96. addr = ctypes.c_ulonglong(addr)
  97. retn = ctypes.c_float(s)
  98. BufferLength = ctypes.c_ulonglong(n)
  99. self.ntdll.NtWow64WriteVirtualMemory64(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  100. def WriteMemory64_double(self, addr,s, n=8):
  101. addr = ctypes.c_ulonglong(addr)
  102. retn = ctypes.c_double(s)
  103. BufferLength = ctypes.c_ulonglong(n)
  104. self.ntdll.NtWow64WriteVirtualMemory64(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  105. def WriteMemory64_byte(self,addr,s, n=8):
  106. addr = ctypes.c_ulonglong(addr)
  107. retn = ctypes.c_byte(s)
  108. BufferLength = ctypes.c_ulonglong(n)
  109. self.ntdll.NtWow64WriteVirtualMemory64(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  110. def GetBaseAddr64(self, ModuleName):
  111. NumberOfBytesRead = ctypes.c_ulong()
  112. Buffer = PROCESS_BASIC_INFORMATION()
  113. Size = ctypes.c_ulong(48)
  114. name_len = len(ModuleName)
  115. self.ntdll.NtWow64QueryInformationProcess64(int(self.hProcess), 0, ctypes.byref(Buffer), Size,ctypes.byref(NumberOfBytesRead))
  116. ret = self.ReadMemory64(Buffer.PebBaseAddress + 24, 8)
  117. ret = self.ReadMemory64(ret + 24, 8)
  118. for i in range(100000):
  119. modulehandle = self.ReadMemory64(ret + 48, 8)
  120. if modulehandle == 0:
  121. break
  122. nameaddr = self.ReadMemory64(ret + 96, 8)
  123. name = self.ReadMemory64_Wchar(nameaddr, name_len * 2 + 1, name_len)
  124. if name == ModuleName:
  125. return modulehandle
  126. ret = self.ReadMemory64(ret + 8, 8)
  127. # ReadMemory32 Method -------------------------------------------------------------------------------
  128. def ReadMemory32(self, addr, n=4):
  129. addr = ctypes.c_int32(addr)
  130. retn = ctypes.c_int()
  131. BufferLength = ctypes.c_int32(n)
  132. kernel32.ReadProcessMemory(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  133. return retn.value
  134. def ReadMemory32_float(self, addr, n=4):
  135. addr = ctypes.c_int(addr)
  136. retn = ctypes.c_float()
  137. BufferLength = ctypes.c_int(n)
  138. kernel32.ReadProcessMemory(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  139. return retn.value
  140. def ReadMemory32_double(self, addr, n=8):
  141. addr = ctypes.c_int(addr)
  142. retn = ctypes.c_double()
  143. BufferLength = ctypes.c_int(n)
  144. kernel32.ReadProcessMemory(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  145. return retn.value
  146. def ReadMemory32_byte(self, addr, n=8):
  147. addr = ctypes.c_int(addr)
  148. retn = ctypes.c_byte()
  149. BufferLength = ctypes.c_int(n)
  150. kernel32.ReadProcessMemory(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  151. return retn.value
  152. # WriteMemory32 Method -------------------------------------------------------------------------------
  153. def WriteMemory32(self, addr, s, n=4):
  154. addr = ctypes.c_int(addr)
  155. retn = ctypes.c_int(s)
  156. BufferLength = ctypes.c_int(n)
  157. kernel32.WriteProcessMemory(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  158. def WriteMemory32_float(self, addr, s, n=4):
  159. addr = ctypes.c_int(addr)
  160. retn = ctypes.c_float(s)
  161. BufferLength = ctypes.c_int(n)
  162. kernel32.WriteProcessMemory(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  163. def WriteMemory32_double(self, addr,s, n=8):
  164. addr = ctypes.c_int(addr)
  165. retn = ctypes.c_double(s)
  166. BufferLength = ctypes.c_int(n)
  167. kernel32.WriteProcessMemory(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  168. def WriteMemory32_byte(self,addr,s, n=8):
  169. addr = ctypes.c_int(addr)
  170. retn = ctypes.c_byte(s)
  171. BufferLength = ctypes.c_int(n)
  172. kernel32.WriteProcessMemory(int(self.hProcess), addr, ctypes.byref(retn), BufferLength, 0)
  173. def GetModlueAddr32(self,ProcessId, moduleName):
  174. me32 = MODULEENTRY32()
  175. me32.dwSize = ctypes.sizeof(MODULEENTRY32)
  176. hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessId)
  177. if GetLastError() != 0:
  178. print("hModuleSnap: %d" % hModuleSnap)
  179. print('Handle Error %s' % GetLastError())
  180. win32api.CloseHandle(hModuleSnap)
  181. return 'Not Find Modlue.'
  182. else:
  183. if Module32First(hModuleSnap, ctypes.pointer(me32)):
  184. if me32.szModule.decode() == moduleName:
  185. win32api.CloseHandle(hModuleSnap)
  186. return me32.modBaseAddr
  187. else:
  188. Module32Next(hModuleSnap, ctypes.pointer(me32))
  189. while int(GetLastError()) != 18:
  190. if me32.szModule.decode() == moduleName:
  191. win32api.CloseHandle(hModuleSnap)
  192. return me32.modBaseAddr
  193. else:
  194. Module32Next(hModuleSnap, ctypes.pointer(me32))
  195. win32api.CloseHandle(hModuleSnap)
  196. print('Couldn\'t find Process with name %s' % moduleName)
  197. else:
  198. print('Module32 First is False %s' % GetLastError())
  199. win32api.CloseHandle(hModuleSnap)
  200. class FindWinDraw():
  201. def __init__(self, ClassName, WindowName):
  202. self.hwndsr = FindWindow(ClassName, WindowName)
  203. # hwnd = FindWindow(None, 'C:/Windows/system32/cmd.exe')
  204. self.LONGARG = (-20, 524288)
  205. def SetupExGui(self):
  206. pygame.init()
  207. left, top, right, bottom = GetWindowRect(self.hwndsr)
  208. Width = right - left
  209. Height = bottom - top
  210. self.screen = pygame.display.set_mode([Width, Height], pygame.NOFRAME )
  211. self.hwnd = FindWindow("pygame", None)
  212. SetWindowPos(self.hwnd, -1, left, top, Width, Height, 1)
  213. SetWindowLong(self.hwnd, self.LONGARG[0], self.LONGARG[1])
  214. SetLayeredWindowAttributes(self.hwnd, 0, 0, 1)
  215. def SetupExGui_1(self,left, top, Width, Height):
  216. pygame.init()
  217. # left, top, right, bottom = GetWindowRect(self.hwndsr)
  218. # Width = right - left
  219. # Height = bottom - top
  220. self.screen = pygame.display.set_mode([Width, Height], pygame.NOFRAME )
  221. self.hwnd = FindWindow("pygame", None)
  222. SetWindowPos(self.hwnd, -1, left, top, Width, Height, 1)
  223. SetWindowLong(self.hwnd, self.LONGARG[0], self.LONGARG[1])
  224. SetLayeredWindowAttributes(self.hwnd, 0, 0, 1)
  225. def DrawText(self, text, size, x, y, color):
  226. textr = pygame.font.SysFont("simhei",size)
  227. text_fmt = textr.render(text,1 ,color)
  228. self.screen.blit(text_fmt,(x,y))
  229. def DrawRect(self, x, y, width, height, c, color):
  230. pygame.draw.rect(self.screen, color, (x, y, width, height), c)
  231. def DrawLine(self,startX,startY, endX, endY, width, color):
  232. pygame.draw.line(self.screen, color, (startX,startY),(endX,endY), width)
  233. def DrawCircle(self,x, y, c, color):
  234. pygame.draw.circle(self.screen, color, (x, y), c)
  235. def StartLoop(self):
  236. self.screen.fill((0, 0, 0))
  237. def EndLoop(self):
  238. for _ in pygame.event.get():
  239. pass
  240. left, top, right, bottom = GetWindowRect(self.hwndsr)
  241. Width = right - left
  242. Height = bottom - top
  243. SetWindowPos(self.hwnd, -1, left, top, Width, Height, 1)
  244. pygame.display.flip()
  245. # The End -------------------------------------------------------------------------------

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/742902
推荐阅读
相关标签
  

闽ICP备14008679号