def ScreenCapture(x, y, width, height): from UnmanagedCode import User32, GDI32 from System.Drawing import Bitmap, Image hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()) hdcDest = GDI32.CreateCompatibleDC(hdcSrc) hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height) GDI32.SelectObject(hdcDest, hBitmap) # 0x00CC0020 is the magic number for a copy raster operation GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, x, y, 0x00CC0020) result = Bitmap(Image.FromHbitmap(hBitmap)) User32.ReleaseDC(User32.GetDesktopWindow(), hdcSrc) GDI32.DeleteDC(hdcDest) GDI32.DeleteObject(hBitmap) return result
def getPixelColor(*args): # the result will be such as Color [A=255, R=107, G=107, B=107]. if len(args) == 2: x = args[0] y = args[1] if isinstance(args[0], Point): x = args[0].X y = args[0].Y hdc = User32.GetDC(IntPtr.Zero) pixel = GDI32.GetPixel(hdc, x, y) User32.ReleaseDC(IntPtr.Zero, hdc) result = Color.FromArgb( pixel & 0x000000FF, # R # First And operation (pixel & 0x0000FF00) >> 8, # G (pixel & 0x00FF0000) >> 16) # B return result
def getRectangle(self): ''' This will return WRECT struct. public struct WRECT { public int Left; public int Top; public int Right; public int Bottom; } ''' hwnd = AniImageBoard.getProcessHanddle(self.name) # rec has 0. But rec_result has the result. Why??? rec = User32.WRECT() rec_result = User32.GetWindowRect(hwnd, rec) if rec_result[0] is False: return return rec_result[1]
def killClick(self, x1, y1, x2, y2): Cursor.Position = Point(x1, y1) User32.mouse_event(0x202, 0, 0, 0, 0) Cursor.Position = Point(x2, y2) User32.mouse_event(0x204, 0, 0, 0, 0)
def click(self, x, y): info = 0 Cursor.Position = Point(x, y) User32.mouse_event(0x202, 0, 0, 0, info) User32.mouse_event(0x204, 0, 0, 0, info)
def foregroundWindow(self): hwnd = AniImageBoard.getProcessHanddle(self.name) if hwnd == 0: raise RuntimeError('There is no Window.') User32.SwitchToThisWindow(hwnd, True)
using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace UnmanagedCode { public class User32 { [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options); } } """ # Bytecode, no disk forl to LoadLibrary from assembly = Generate(unmanaged_code, 'UnmanagedCode', inMemory=True) print("Type : %s" % (type(assembly))) print("CodeBase: %s" % (assembly.CodeBase)) print("FullName: %s" % (assembly.FullName)) print("Dynamic? %s" % (assembly.IsDynamic)) print("Location: %s" % (assembly.Location)) clr.AddReference(assembly) from UnmanagedCode import User32 from System import IntPtr User32.MessageBox(IntPtr.Zero, "Hello World", "Platform Invoke Sample", 0)
return compile.CompiledAssembly return compile.PathToAssembly unmanaged_code = """ using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace UnmanagedCode { public class User32 { [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options); } } """ # Bytecode, no disk forl to LoadLibrary from assembly = Generate(unmanaged_code, 'UnmanagedCode', inMemory=True) print(assembly) clr.AddReference(assembly) from UnmanagedCode import User32 User32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0)