def prep_menu_icon(self, icon): # First load the icon. ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hicon = win32gui.LoadImage( 0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE ) hdcBitmap = win32gui.CreateCompatibleDC(0) hdcScreen = win32gui.GetDC(0) hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y) hbmOld = win32gui.SelectObject(hdcBitmap, hbm) # Fill the background. brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush) # unclear if brush needs to be feed. Best clue I can find is: # "GetSysColorBrush returns a cached brush instead of allocating a new # one." - implies no DeleteObject # draw the icon win32gui.DrawIconEx( hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL ) win32gui.SelectObject(hdcBitmap, hbmOld) win32gui.DeleteDC(hdcBitmap) return hbm
def OnMeasureItem(self, hwnd, msg, wparam, lparam): fmt = "5iP" buf = win32gui.PyMakeBuffer(struct.calcsize(fmt), lparam) data = struct.unpack(fmt, buf) ctlType, ctlID, itemID, itemWidth, itemHeight, itemData = data hicon, text = self.menu_item_map[itemData] if text is None: # Only drawing icon due to HBMMENU_CALLBACK cx = self.menu_icon_width cy = self.menu_icon_height else: # drawing the lot! dc = win32gui.GetDC(hwnd) oldFont = win32gui.SelectObject(dc, self.font_menu) cx, cy = win32gui.GetTextExtentPoint32(dc, text) win32gui.SelectObject(dc, oldFont) win32gui.ReleaseDC(hwnd, dc) cx += win32api.GetSystemMetrics(win32con.SM_CXMENUCHECK) cx += self.menu_icon_width + self.icon_x_pad cy = win32api.GetSystemMetrics(win32con.SM_CYMENU) new_data = struct.pack(fmt, ctlType, ctlID, itemID, cx, cy, itemData) win32gui.PySetMemory(lparam, new_data) return True
def prep_menu_icon(self, icon): ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE) hdcBitmap = win32gui.CreateCompatibleDC(0) hdcScreen = win32gui.GetDC(0) hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y) hbmOld = win32gui.SelectObject(hdcBitmap, hbm) brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush) win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL) win32gui.SelectObject(hdcBitmap, hbmOld) win32gui.DeleteDC(hdcBitmap) return hbm
def get_systemfont_style(): # Get DC hdc = win32gui.GetDC(0) # Get system DPI dpi = win32ui.GetDeviceCaps(hdc, win32con.LOGPIXELSY) # Get system font, it's name and size z = win32gui.SystemParametersInfo(win32con.SPI_GETNONCLIENTMETRICS) font = z['lfMessageFont'] font_name = font.lfFaceName font_size = int(round(abs(font.lfHeight) * 72 / dpi)) # Release DC win32gui.ReleaseDC(0, hdc) # Construct sytle for all widget font_style = ''' style "openerp-user-font" { font_name = "%s %s" } widget_class "*" style "openerp-user-font" ''' % (font_name, font_size) return font_style
def OnKeyDown(self, hwnd, msg, wparam, lparam): if self._first_keydown: # only update once, ignore keyboard autorepeat self._first_keydown = False self._display_string = "" if wparam == win32con.VK_UP: self._pixels[self._selected_pixel][0] -= self._increment elif wparam == win32con.VK_DOWN: self._pixels[self._selected_pixel][0] += self._increment elif wparam == win32con.VK_LEFT: self._pixels[self._selected_pixel][1] -= self._increment elif wparam == win32con.VK_RIGHT: self._pixels[self._selected_pixel][1] += self._increment elif wparam == VK_1: self._selected_pixel = 0 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_2: self._selected_pixel = 1 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_3: self._selected_pixel = 2 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_4: self._selected_pixel = 3 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_5: self._selected_pixel = 4 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_6: self._selected_pixel = 5 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_7: self._selected_pixel = 6 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_8: self._selected_pixel = 7 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_9: self._selected_pixel = 8 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == win32con.VK_CONTROL: self._display_string = "increment = %d" % self._increment elif wparam == win32con.VK_SHIFT: self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_F: if self._fullscreen: self.exitFullscreen() else: self.enterFullscreen() elif wparam == VK_N: if self._selected_pixel == len(self._pixels) - 1: self._selected_pixel = 0 else: self._selected_pixel += 1 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_O: # read parameters and pixels from a file self.openParameterFile(hwnd) # redraw the screen using the new pixels win32gui.RedrawWindow( hwnd, None, None, win32con.RDW_INVALIDATE | win32con.RDW_INTERNALPAINT) elif wparam == VK_P: if self._selected_pixel == 0: self._selected_pixel = len(self._pixels) - 1 else: self._selected_pixel -= 1 self._display_string = "Pixel " + str(self._selected_pixel + 1) elif wparam == VK_Q: win32gui.PostQuitMessage(0) elif wparam == VK_S: self.saveParameterFile(hwnd) elif wparam == VK_ESCAPE: self.exitFullscreen() elif wparam == VK_OEM_PLUS: self._increment = 10 self._display_string = "increment = %d" % self._increment elif wparam == VK_OEM_MINUS: self._increment = 1 self._display_string = "increment = %d" % self._increment else: self._show_help = True dc = win32gui.GetDC(hwnd) brush = win32gui.CreateSolidBrush(BLACK) win32gui.SelectObject(dc, brush) win32gui.SetTextColor(dc, WHITE) win32gui.SetBkColor(dc, BLACK) debug = win32gui.DrawText( dc, self._display_string, len(self._display_string), self._onscreen_display, win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER) win32gui.ReleaseDC(hwnd, dc)