def _create_bitmap(self, width, height): self._data = (ctypes.c_byte * (4 * width * height))() self._bitmap = ctypes.c_void_p() self._format = PixelFormat32bppARGB gdiplus.GdipCreateBitmapFromScan0(width, height, width * 4, self._format, self._data, ctypes.byref(self._bitmap)) self._graphics = ctypes.c_void_p() gdiplus.GdipGetImageGraphicsContext(self._bitmap, ctypes.byref(self._graphics)) gdiplus.GdipSetPageUnit(self._graphics, UnitPixel) self._dc = user32.GetDC(0) gdi32.SelectObject(self._dc, self.font.hfont) gdiplus.GdipSetTextRenderingHint(self._graphics, TextRenderingHintAntiAliasGridFit) self._brush = ctypes.c_void_p() gdiplus.GdipCreateSolidFill(0xffffffff, ctypes.byref(self._brush)) self._matrix = ctypes.c_void_p() gdiplus.GdipCreateMatrix(ctypes.byref(self._matrix)) self._flags = (DriverStringOptionsCmapLookup | DriverStringOptionsRealizedAdvance) self._rect = Rect(0, 0, width, height) self._bitmap_height = height
def get_matching_configs(self, template): canvas = Win32Canvas(self.display, 0, _user32.GetDC(0)) configs = template.match(canvas) # XXX deprecate config's being screen-specific for config in configs: config.screen = self return configs
def __init__(self, name, size, bold=False, italic=False, dpi=None): super(Win32Font, self).__init__() self.logfont = self.get_logfont(name, size, bold, italic, dpi) self.hfont = gdi32.CreateFontIndirectA(byref(self.logfont)) # Create a dummy DC for coordinate mapping dc = user32.GetDC(0) metrics = TEXTMETRIC() gdi32.SelectObject(dc, self.hfont) gdi32.GetTextMetricsA(dc, byref(metrics)) self.ascent = metrics.tmAscent self.descent = -metrics.tmDescent self.max_glyph_width = metrics.tmMaxCharWidth
def get_logfont(name, size, bold, italic, dpi): # Create a dummy DC for coordinate mapping dc = user32.GetDC(0) if dpi is None: dpi = 96 logpixelsy = dpi logfont = LOGFONT() # Conversion of point size to device pixels logfont.lfHeight = int(-size * logpixelsy // 72) if bold: logfont.lfWeight = FW_BOLD else: logfont.lfWeight = FW_NORMAL logfont.lfItalic = italic logfont.lfFaceName = asbytes(name) logfont.lfQuality = ANTIALIASED_QUALITY return logfont
def get_icon(image): # Alpha-blended icon: see http://support.microsoft.com/kb/318876 format = 'BGRA' pitch = len(format) * image.width header = BITMAPV5HEADER() header.bV5Size = sizeof(header) header.bV5Width = image.width header.bV5Height = image.height header.bV5Planes = 1 header.bV5BitCount = 32 header.bV5Compression = BI_BITFIELDS header.bV5RedMask = 0x00ff0000 header.bV5GreenMask = 0x0000ff00 header.bV5BlueMask = 0x000000ff header.bV5AlphaMask = 0xff000000 hdc = _user32.GetDC(None) dataptr = c_void_p() bitmap = _gdi32.CreateDIBSection(hdc, byref(header), DIB_RGB_COLORS, byref(dataptr), None, 0) _user32.ReleaseDC(None, hdc) image = image.get_image_data() data = image.get_data(format, pitch) memmove(dataptr, data, len(data)) mask = _gdi32.CreateBitmap(image.width, image.height, 1, 1, None) iconinfo = ICONINFO() iconinfo.fIcon = True iconinfo.hbmMask = mask iconinfo.hbmColor = bitmap icon = _user32.CreateIconIndirect(byref(iconinfo)) _gdi32.DeleteObject(mask) _gdi32.DeleteObject(bitmap) return icon
def _create_cursor_from_image(self, cursor): """Creates platform cursor from an ImageCursor instance.""" fmt = 'BGRA' image = cursor.texture pitch = len(fmt) * image.width header = BITMAPINFOHEADER() header.biSize = sizeof(header) header.biWidth = image.width header.biHeight = image.height header.biPlanes = 1 header.biBitCount = 32 hdc = _user32.GetDC(None) dataptr = c_void_p() bitmap = _gdi32.CreateDIBSection(hdc, byref(header), DIB_RGB_COLORS, byref(dataptr), None, 0) _user32.ReleaseDC(None, hdc) image = image.get_image_data() data = image.get_data(fmt, pitch) memmove(dataptr, data, len(data)) mask = _gdi32.CreateBitmap(image.width, image.height, 1, 1, None) iconinfo = ICONINFO() iconinfo.fIcon = False iconinfo.hbmMask = mask iconinfo.hbmColor = bitmap iconinfo.xHotspot = int(cursor.hot_x) iconinfo.yHotspot = int(image.height - cursor.hot_y) icon = _user32.CreateIconIndirect(byref(iconinfo)) _gdi32.DeleteObject(mask) _gdi32.DeleteObject(bitmap) return icon
def _create(self): # Ensure style is set before determining width/height. if self._fullscreen: self._ws_style = WS_POPUP self._ex_ws_style = 0 # WS_EX_TOPMOST else: styles = { self.WINDOW_STYLE_DEFAULT: (WS_OVERLAPPEDWINDOW, 0), self.WINDOW_STYLE_DIALOG: (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, WS_EX_DLGMODALFRAME), self.WINDOW_STYLE_TOOL: (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, WS_EX_TOOLWINDOW), self.WINDOW_STYLE_BORDERLESS: (WS_POPUP, 0), } self._ws_style, self._ex_ws_style = styles[self._style] if self._resizable and not self._fullscreen: self._ws_style |= WS_THICKFRAME else: self._ws_style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX) if self._fullscreen: width = self.screen.width height = self.screen.height else: width, height = \ self._client_to_window_size(self._width, self._height) if not self._window_class: module = _kernel32.GetModuleHandleW(None) white = _gdi32.GetStockObject(WHITE_BRUSH) black = _gdi32.GetStockObject(BLACK_BRUSH) self._window_class = WNDCLASS() self._window_class.lpszClassName = u'GenericAppClass%d' % id(self) self._window_class.lpfnWndProc = WNDPROC( self._get_window_proc(self._event_handlers)) self._window_class.style = CS_VREDRAW | CS_HREDRAW self._window_class.hInstance = 0 self._window_class.hIcon = _user32.LoadIconW( module, MAKEINTRESOURCE(1)) self._window_class.hbrBackground = black self._window_class.lpszMenuName = None self._window_class.cbClsExtra = 0 self._window_class.cbWndExtra = 0 _user32.RegisterClassW(byref(self._window_class)) self._view_window_class = WNDCLASS() self._view_window_class.lpszClassName = \ u'GenericViewClass%d' % id(self) self._view_window_class.lpfnWndProc = WNDPROC( self._get_window_proc(self._view_event_handlers)) self._view_window_class.style = 0 self._view_window_class.hInstance = 0 self._view_window_class.hIcon = 0 self._view_window_class.hbrBackground = white self._view_window_class.lpszMenuName = None self._view_window_class.cbClsExtra = 0 self._view_window_class.cbWndExtra = 0 _user32.RegisterClassW(byref(self._view_window_class)) if not self._hwnd: self._hwnd = _user32.CreateWindowExW( self._ex_ws_style, self._window_class.lpszClassName, u'', self._ws_style, CW_USEDEFAULT, CW_USEDEFAULT, width, height, 0, 0, self._window_class.hInstance, 0) self._view_hwnd = _user32.CreateWindowExW( 0, self._view_window_class.lpszClassName, u'', WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, self._hwnd, 0, self._view_window_class.hInstance, 0) self._dc = _user32.GetDC(self._view_hwnd) else: # Window already exists, update it with new style # We need to hide window here, otherwise Windows forgets # to redraw the whole screen after leaving fullscreen. _user32.ShowWindow(self._hwnd, SW_HIDE) _user32.SetWindowLongW(self._hwnd, GWL_STYLE, self._ws_style) _user32.SetWindowLongW(self._hwnd, GWL_EXSTYLE, self._ex_ws_style) if self._fullscreen: hwnd_after = HWND_TOPMOST else: hwnd_after = HWND_NOTOPMOST # Position and size window if self._fullscreen: _user32.SetWindowPos(self._hwnd, hwnd_after, self._screen.x, self._screen.y, width, height, SWP_FRAMECHANGED) elif False: # TODO location not in pyglet API x, y = self._client_to_window_pos(*factory.get_location()) _user32.SetWindowPos(self._hwnd, hwnd_after, x, y, width, height, SWP_FRAMECHANGED) else: _user32.SetWindowPos(self._hwnd, hwnd_after, 0, 0, width, height, SWP_NOMOVE | SWP_FRAMECHANGED) self._update_view_location(self._width, self._height) # Context must be created after window is created. if not self._wgl_context: self.canvas = Win32Canvas(self.display, self._view_hwnd, self._dc) self.context.attach(self.canvas) self._wgl_context = self.context._context self.set_caption(self._caption) self.switch_to() self.set_vsync(self._vsync) if self._visible: self.set_visible() # Might need resize event if going from fullscreen to fullscreen self.dispatch_event('on_resize', self._width, self._height) self.dispatch_event('on_expose')
def _create(self): # Ensure style is set before determining width/height. if self._fullscreen: self._ws_style = WS_POPUP self._ex_ws_style = 0 # WS_EX_TOPMOST else: styles = { self.WINDOW_STYLE_DEFAULT: (WS_OVERLAPPEDWINDOW, 0), self.WINDOW_STYLE_DIALOG: (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, WS_EX_DLGMODALFRAME), self.WINDOW_STYLE_TOOL: (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, WS_EX_TOOLWINDOW), self.WINDOW_STYLE_BORDERLESS: (WS_POPUP, 0), } self._ws_style, self._ex_ws_style = styles[self._style] if self._resizable and not self._fullscreen: self._ws_style |= WS_THICKFRAME else: self._ws_style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX) if self._fullscreen: width = self.screen.width height = self.screen.height else: width, height = \ self._client_to_window_size(self._width, self._height) if not self._window_class: module = _kernel32.GetModuleHandleW(None) white = _gdi32.GetStockObject(WHITE_BRUSH) black = _gdi32.GetStockObject(BLACK_BRUSH) self._window_class = WNDCLASS() self._window_class.lpszClassName = u'GenericAppClass%d' % id(self) self._window_class.lpfnWndProc = WNDPROC( self._get_window_proc(self._event_handlers)) self._window_class.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC self._window_class.hInstance = 0 self._window_class.hIcon = _user32.LoadIconW(module, MAKEINTRESOURCE(1)) self._window_class.hbrBackground = black self._window_class.lpszMenuName = None self._window_class.cbClsExtra = 0 self._window_class.cbWndExtra = 0 _user32.RegisterClassW(byref(self._window_class)) self._view_window_class = WNDCLASS() self._view_window_class.lpszClassName = \ u'GenericViewClass%d' % id(self) self._view_window_class.lpfnWndProc = WNDPROC( self._get_window_proc(self._view_event_handlers)) self._view_window_class.style = 0 self._view_window_class.hInstance = 0 self._view_window_class.hIcon = 0 self._view_window_class.hbrBackground = white self._view_window_class.lpszMenuName = None self._view_window_class.cbClsExtra = 0 self._view_window_class.cbWndExtra = 0 _user32.RegisterClassW(byref(self._view_window_class)) if not self._hwnd: self._hwnd = _user32.CreateWindowExW( self._ex_ws_style, self._window_class.lpszClassName, u'', self._ws_style, CW_USEDEFAULT, CW_USEDEFAULT, width, height, 0, 0, self._window_class.hInstance, 0) self._view_hwnd = _user32.CreateWindowExW( 0, self._view_window_class.lpszClassName, u'', WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, self._hwnd, 0, self._view_window_class.hInstance, 0) self._dc = _user32.GetDC(self._view_hwnd) # Only allow files being dropped if specified. if self._file_drops: # Allows UAC to not block the drop files request if low permissions. All 3 must be set. if WINDOWS_7_OR_GREATER: _user32.ChangeWindowMessageFilterEx(self._hwnd, WM_DROPFILES, MSGFLT_ALLOW, None) _user32.ChangeWindowMessageFilterEx(self._hwnd, WM_COPYDATA, MSGFLT_ALLOW, None) _user32.ChangeWindowMessageFilterEx(self._hwnd, WM_COPYGLOBALDATA, MSGFLT_ALLOW, None) _shell32.DragAcceptFiles(self._hwnd, True) # Register raw input keyboard to allow the window to receive input events. raw_keyboard = RAWINPUTDEVICE(0x01, 0x06, 0, self._view_hwnd) if not _user32.RegisterRawInputDevices( byref(raw_keyboard), 1, sizeof(RAWINPUTDEVICE)): print("Warning: Failed to register raw input keyboard. on_key events for shift keys will not be called.") else: # Window already exists, update it with new style # We need to hide window here, otherwise Windows forgets # to redraw the whole screen after leaving fullscreen. _user32.ShowWindow(self._hwnd, SW_HIDE) _user32.SetWindowLongW(self._hwnd, GWL_STYLE, self._ws_style) _user32.SetWindowLongW(self._hwnd, GWL_EXSTYLE, self._ex_ws_style) if self._fullscreen: hwnd_after = HWND_TOPMOST else: hwnd_after = HWND_NOTOPMOST # Position and size window if self._fullscreen: _user32.SetWindowPos(self._hwnd, hwnd_after, self._screen.x, self._screen.y, width, height, SWP_FRAMECHANGED) elif False: # TODO location not in pyglet API x, y = self._client_to_window_pos(*factory.get_location()) _user32.SetWindowPos(self._hwnd, hwnd_after, x, y, width, height, SWP_FRAMECHANGED) else: _user32.SetWindowPos(self._hwnd, hwnd_after, 0, 0, width, height, SWP_NOMOVE | SWP_FRAMECHANGED) self._update_view_location(self._width, self._height) # Context must be created after window is created. if not self._wgl_context: self.canvas = Win32Canvas(self.display, self._view_hwnd, self._dc) self.context.attach(self.canvas) self._wgl_context = self.context._context self.set_caption(self._caption) self.switch_to() self.set_vsync(self._vsync) if self._visible: self.set_visible() # Might need resize event if going from fullscreen to fullscreen self.dispatch_event('on_resize', self._width, self._height) self.dispatch_event('on_expose')