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 OnDrawItem(self, hwnd, msg, wparam, lparam): fmt = "5i2P4iP" data = struct.unpack( fmt, win32gui.PyGetString(lparam, struct.calcsize(fmt))) ctlType, ctlID, itemID, itemAction, itemState, hwndItem, \ hDC, left, top, right, bot, itemData = data rect = left, top, right, bot hicon, text = self.menu_item_map[itemData] if text is None: # This means the menu-item had HBMMENU_CALLBACK - so all we # draw is the icon. rect is the entire area we should use. win32gui.DrawIconEx(hDC, left, top, hicon, right - left, bot - top, 0, 0, win32con.DI_NORMAL) else: # If the user has selected the item, use the selected # text and background colors to display the item. selected = itemState & win32con.ODS_SELECTED if selected: crText = win32gui.SetTextColor( hDC, win32gui.GetSysColor(win32con.COLOR_HIGHLIGHTTEXT)) crBkgnd = win32gui.SetBkColor( hDC, win32gui.GetSysColor(win32con.COLOR_HIGHLIGHT)) each_pad = self.icon_x_pad // 2 x_icon = left + each_pad x_text = x_icon + self.menu_icon_width + each_pad # Draw text first, specifying a complete rect to fill - this sets # up the background (but overwrites anything else already there!) # Select the font, draw it, and restore the previous font. hfontOld = win32gui.SelectObject(hDC, self.font_menu) win32gui.ExtTextOut(hDC, x_text, top + 2, win32con.ETO_OPAQUE, rect, text) win32gui.SelectObject(hDC, hfontOld) # Icon image next. Icons are transparent - no need to handle # selection specially. win32gui.DrawIconEx(hDC, x_icon, top + 2, hicon, self.menu_icon_width, self.menu_icon_height, 0, 0, win32con.DI_NORMAL) # Return the text and background colors to their # normal state (not selected). if selected: win32gui.SetTextColor(hDC, crText) win32gui.SetBkColor(hDC, crBkgnd)
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 OnPaint(self, hwnd, msg, wparam, lparam): dc, ps = win32gui.BeginPaint(hwnd) brush = win32gui.CreateSolidBrush(BLACK) win32gui.SelectObject(dc, brush) win32gui.Rectangle(dc, 0, 0, self._width - 1, self._height - 1) # draw a square of four pixels for each center pixel for i in range(len(self._pixels)): pixel = self._pixels[i] left = int(pixel[1] * self._width / PROJECTOR_PIXEL_WIDTH) right = left + 1 top = int(pixel[0] * self._height / PROJECTOR_PIXEL_HEIGHT) bottom = top + 1 for row in [top, bottom]: for col in [left, right]: win32gui.SetPixel(dc, col, row, GREEN) win32gui.SetBkColor(dc, BLACK) win32gui.SetTextColor(dc, WHITE) win32gui.EndPaint(hwnd, ps) return 0
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)