def _create_bitmap(self, width, height): self._black = gdi32.GetStockObject(BLACK_BRUSH) self._white = gdi32.GetStockObject(WHITE_BRUSH) if self._dc: gdi32.ReleaseDC(self._dc) if self._bitmap: gdi32.DeleteObject(self._bitmap) pitch = width * 4 data = POINTER(c_byte * (height * pitch))() info = BITMAPINFO() info.bmiHeader.biSize = sizeof(info.bmiHeader) info.bmiHeader.biWidth = width info.bmiHeader.biHeight = height info.bmiHeader.biPlanes = 1 info.bmiHeader.biBitCount = 32 info.bmiHeader.biCompression = BI_RGB self._dc = gdi32.CreateCompatibleDC(None) self._bitmap = gdi32.CreateDIBSection(None, byref(info), DIB_RGB_COLORS, byref(data), None, 0) # Spookiness: the above line causes a "not enough storage" error, # even though that error cannot be generated according to docs, # and everything works fine anyway. Call SetLastError to clear it. kernel32.SetLastError(0) self._bitmap_data = data.contents self._bitmap_rect = RECT() self._bitmap_rect.left = 0 self._bitmap_rect.right = width self._bitmap_rect.top = 0 self._bitmap_rect.bottom = height self._bitmap_height = height if _debug_font: _debug('%r._create_dc(%d, %d)' % (self, width, height)) _debug('_dc = %r' % self._dc) _debug('_bitmap = %r' % self._bitmap) _debug('pitch = %r' % pitch) _debug('info.bmiHeader.biSize = %r' % info.bmiHeader.biSize)
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')