def OnPaint_1(hwnd, msg, wp, lp): dc, ps = win32gui.BeginPaint(hwnd) win32gui.SetGraphicsMode(dc, win32con.GM_ADVANCED) br = win32gui.CreateSolidBrush(win32api.RGB(255, 0, 0)) win32gui.SelectObject(dc, br) angle = win32gui.GetWindowLong(hwnd, win32con.GWL_USERDATA) win32gui.SetWindowLong(hwnd, win32con.GWL_USERDATA, angle + 2) r_angle = angle * (math.pi / 180) win32gui.SetWorldTransform( dc, { 'M11': math.cos(r_angle), 'M12': math.sin(r_angle), 'M21': math.sin(r_angle) * -1, 'M22': math.cos(r_angle), 'Dx': 250, 'Dy': 250 }) win32gui.MoveToEx(dc, 250, 250) win32gui.BeginPath(dc) win32gui.Pie(dc, 10, 70, 200, 200, 350, 350, 75, 10) win32gui.Chord(dc, 200, 200, 850, 0, 350, 350, 75, 10) win32gui.LineTo(dc, 300, 300) win32gui.LineTo(dc, 100, 20) win32gui.LineTo(dc, 20, 100) win32gui.LineTo(dc, 400, 0) win32gui.LineTo(dc, 0, 400) win32gui.EndPath(dc) win32gui.StrokeAndFillPath(dc) win32gui.EndPaint(hwnd, ps) return 0
def wnd_proc(h_wnd, message, w_param, l_param): """Displays a transparent window with some graphic elements Displays a transparent window with some graphic elements :param h_wnd: an input argument :returns: nothing """ if message == win32con.WM_QUIT: # print("Closing the window overlay") win32gui.PostQuitMessage(0) return 0 if message == win32con.WM_PAINT: hdc, paint_struct = win32gui.BeginPaint(h_wnd) win32gui.SetGraphicsMode(hdc, win32con.GM_ADVANCED) win32gui.BringWindowToTop(h_wnd) for r in self.graphical_elements: if 'geometry' in r: geometry = r['geometry'] else: geometry = None if 'x' in r: x = r['x'] - self.x_min else: x = 0 if 'y' in r: y = r['y'] - self.y_min else: y = 0 if 'width' in r: width = r['width'] else: width = 100 if 'height' in r: height = r['height'] else: height = 100 if 'xyrgb_array' in r: xyrgb_array = r['xyrgb_array'] else: xyrgb_array = ((15, 15, 255, 0, 0), (15, 45, 0, 255, 0), (45, 30, 0, 0, 255)) if 'color' in r: color_r = r['color'][0] color_g = r['color'][1] color_b = r['color'][2] else: color_r = 255 color_g = 0 color_b = 0 if 'thickness' in r: thickness = r['thickness'] else: thickness = 0 if 'angle' in r: angle = r['angle'] else: angle = 0 if 'geometry' in r and r['geometry'] is Shape.triangle: vertices = () for xyrgb in xyrgb_array: vertices = vertices + ( { 'x': int(round(xyrgb[0])) - self.x_min, 'y': int(round(xyrgb[1])) - self.y_min, 'Red': xyrgb[2] * 256, 'Green': xyrgb[3] * 256, 'Blue': xyrgb[4] * 256, 'Alpha': 0 }, ) mesh = ((0, 1, 2), ) win32gui.GradientFill(hdc, vertices, mesh, win32con.GRADIENT_FILL_TRIANGLE) if 'brush_color' in r: brush_color_r = r['brush_color'][0] brush_color_g = r['brush_color'][1] brush_color_b = r['brush_color'][2] else: brush_color_r = 255 brush_color_g = 255 brush_color_b = 255 if thickness == 0: color_r = brush_color_r color_g = brush_color_g color_b = brush_color_b if 'font_size' in r: font_size = r['font_size'] else: font_size = 18 if 'font_name' in r: font_name = r['font_name'] else: font_name = "Arial" my_brush = None if 'brush' in r and width > 1 and height > 1: brush = r['brush'] brush_color = win32api.RGB(brush_color_r, brush_color_g, brush_color_b) if brush is Brush.solid: my_brush = win32gui.CreateSolidBrush(brush_color) elif brush is Brush.b_diagonal: my_brush = win32gui.CreateHatchBrush( win32con.HS_BDIAGONAL, brush_color) elif brush is Brush.cross: my_brush = win32gui.CreateHatchBrush( win32con.HS_CROSS, brush_color) elif brush is Brush.diag_cross: my_brush = win32gui.CreateHatchBrush( win32con.HS_DIAGCROSS, brush_color) elif brush is Brush.f_diagonal: my_brush = win32gui.CreateHatchBrush( win32con.HS_FDIAGONAL, brush_color) elif brush is Brush.horizontal: my_brush = win32gui.CreateHatchBrush( win32con.HS_HORIZONTAL, brush_color) elif brush is Brush.vertical: my_brush = win32gui.CreateHatchBrush( win32con.HS_VERTICAL, brush_color) old_brush = win32gui.SelectObject(hdc, my_brush) pen = win32gui.CreatePen( win32con.PS_GEOMETRIC, thickness, win32api.RGB(color_r, color_g, color_b)) old_pen = win32gui.SelectObject(hdc, pen) if 'center_of_rotation' in r: center_of_rotation_x = r['center_of_rotation'][0] center_of_rotation_y = r['center_of_rotation'][1] else: center_of_rotation_x = 0 center_of_rotation_y = 0 if angle != 0: r_angle = angle * (math.pi / 180) Py_XFORM = win32gui.GetWorldTransform(hdc) win32gui.SetWorldTransform( hdc, { 'M11': math.cos(r_angle), 'M12': math.sin(r_angle), 'M21': math.sin(r_angle) * -1, 'M22': math.cos(r_angle), 'Dx': x, 'Dy': y }) x, y = -center_of_rotation_x, -center_of_rotation_y if 'text_format' in r: text_format = eval(r['text_format']) else: text_format = win32con.DT_CENTER | win32con.DT_SINGLELINE | win32con.DT_VCENTER if 'geometry' in r: if r['geometry'] is Shape.rectangle: win32gui.Rectangle(hdc, int(round(x)), int(round(y)), int(round(x + width)), int(round(y + height))) elif r['geometry'] is Shape.ellipse: win32gui.Ellipse(hdc, int(round(x)), int(round(y)), int(round(x + width)), int(round(y + height))) elif r['geometry'] is Shape.arrow: a = thickness t = ((x - int(a * 1.4), y), (x - a * 4, y + a * 3), (x, y), (x - a * 4, y - a * 3), (x - int(a * 1.4), y), (x - a * 9, y)) win32gui.Polyline(hdc, t) elif r['geometry'] is Shape.image: hicon = r['hicon'] win32gui.DrawIconEx(hdc, x, y, hicon, 0, 0, 0, None, 0x0003) elif r['geometry'] is Shape.triangle and thickness > 0: t = () for xyrgb in xyrgb_array: t = t + ((int(round( xyrgb[0])), +int(round(xyrgb[1]))), ) t = t + ((int(round(xyrgb_array[0][0])), int(round(xyrgb_array[0][1]))), ) win32gui.Polyline(hdc, t) if angle != 0: win32gui.SetWorldTransform(hdc, Py_XFORM) win32gui.SelectObject(hdc, old_pen) if 'brush' in r and width > 1 and height > 1: win32gui.SelectObject(hdc, old_brush) if 'text' in r: text = r['text'] lf = win32gui.LOGFONT() lf.lfFaceName = font_name lf.lfHeight = font_size lf.lfWeight = win32con.FW_NORMAL lf.lfQuality = win32con.ANTIALIASED_QUALITY hf = win32gui.CreateFontIndirect(lf) old_font = win32gui.SelectObject(hdc, hf) if 'text_color' in r: text_color_r = r['text_color'][0] text_color_g = r['text_color'][1] text_color_b = r['text_color'][2] else: text_color_r = 0 text_color_g = 0 text_color_b = 0 win32gui.SetTextColor( hdc, win32api.RGB(text_color_r, text_color_g, text_color_b)) if 'text_bg_color' in r: text_bg_color_r = r['text_bg_color'][0] text_bg_color_g = r['text_bg_color'][1] text_bg_color_b = r['text_bg_color'][2] else: text_bg_color_r = brush_color_r text_bg_color_g = brush_color_g text_bg_color_b = brush_color_b win32gui.SetBkMode(hdc, win32con.TRANSPARENT) win32gui.SetBkColor( hdc, win32api.RGB(text_bg_color_r, text_bg_color_g, text_bg_color_b)) tuple_r = tuple([ int(round(x)), int(round(y)), int(round(x + width)), int(round(y + height)) ]) win32gui.DrawTextW(hdc, text, -1, tuple_r, text_format) win32gui.SelectObject(hdc, old_font) win32gui.EndPaint(h_wnd, paint_struct) return 0 else: return win32gui.DefWindowProc(h_wnd, message, w_param, l_param)