def enum_fonts(logfont, *largs): try: _gui_font = ui.CreateFont({ 'name': logfont.lfFaceName, 'height': -17, 'weight': 400 }) _gui_font2 = GUI.Font(family=logfont.lfFaceName, size=17) _font = gui.CreateFontIndirect(logfont) hdc = win_none.GetDC().GetSafeHdc() gui.SelectObject(hdc, _font) print '1.------' print ''.join(['[', logfont.lfFaceName, ']']), gui.GetTextExtentPoint32(hdc, '12345'), logfont gui.SelectObject(hdc, _gui_font.GetSafeHandle()) logfont.lfHeight = 17 _gdip_font2 = gdip.Font.from_logfont(hdc, logfont) _gui_font2._win_gdip_font = _gdip_font2 print _gui_font, (logfont.lfHeight, logfont.lfWidth, logfont.lfEscapement, logfont.lfOrientation, logfont.lfWeight, logfont.lfItalic, logfont.lfUnderline, logfont.lfStrikeOut, logfont.lfCharSet, logfont.lfOutPrecision, logfont.lfClipPrecision, logfont.lfQuality, logfont.lfPitchAndFamily), gui.GetTextExtentPoint32( hdc, '12345'), _gui_font2.width('12345') _gdip_font2 = gdip.Font.from_logfont(hdc, logfont) print '2.------', _gdip_font2.ptr except: import traceback traceback.print_exc() return True
def _gdi_width(self, s): dc = win_none.GetDC() dc.SelectObject(self._win_font) #w, h = dc.GetTextExtent(s) w, h = gui.GetTextExtentPoint32(dc.GetSafeHdc(), s) win_none.ReleaseDC(dc) return w
def get_text_extents(self, text): if sys.platform == "win32": cx, cy = win32gui.GetTextExtentPoint32(self.dc, text) return ((cx * self.downscale + self.hspace * (len(text) - 1)) * self.xscale, cy * self.downscale * self.yscale) elif sys.platform == "linux": if not text: return 0.0, 0.0 def get_rect(new_text): self.layout.set_markup( f'<span ' f'strikethrough="{str(self.strikeout).lower()}" ' f'underline="{"single" if self.underline else "none"}"' f'>' f'{html.escape(new_text)}' f'</span>', -1) return self.layout.get_pixel_extents()[1] width = 0 for char in text: width += get_rect(char).width return ( (width * self.downscale * self.fonthack_scale + self.hspace * (len(text) - 1)) * self.xscale, get_rect(text).height * self.downscale * self.yscale * self.fonthack_scale) else: raise NotImplementedError
def getExtents(self, text=None, style=None): if text == None: text = str(self.text) if style == None: style = self.style txtext = {} dc = win32gui.CreateCompatibleDC(0) win32gui.SetMapMode(dc, win32con.MM_TEXT) font = self.selectFont(dc, style, 64) if style.spacing: for char in text: cx, cy = win32gui.GetTextExtentPoint32(dc, char) txtext['width'] += cx + (style.spacing * 64) txtext['height'] = cy else: cx, cy = win32gui.GetTextExtentPoint32(dc, text) txtext['width'] = cx txtext['height'] = cy tm = win32gui.GetTextMetrics(dc) txtext['ascent'] = tm['Ascent'] txtext['descent'] = tm['Descent'] txtext['internal_lead'] = tm['InternalLeading'] txtext['external_lead'] = tm['ExternalLeading'] txtext['width'] = txtext['width'] * (style.scale_x / float(100)) / float(64) txtext['height'] = txtext['height'] * (style.scale_y / float(100)) / float(64) txtext['ascent'] = txtext['ascent'] * (style.scale_y / float(100)) / float(64) txtext['descent'] = txtext['descent'] * (style.scale_y / float(100)) / float(64) txtext['internal_lead'] = txtext['internal_lead'] * ( style.scale_y / float(100)) / float(64) txtext['external_lead'] = txtext['external_lead'] * ( style.scale_y / float(100)) / float(64) return txtext
_gdip_font2 = gdip.Font.from_logfont(hdc, logfont) print '2.------', _gdip_font2.ptr except: import traceback traceback.print_exc() return True try: gui.EnumFontFamilies(win_none.GetDC().GetSafeHdc(), None, enum_fonts, None) _gui_font2 = GUI.Font(family=u'文泉驿等宽微米黑', size=17) _gui_font3 = GUI.Font(family='Noto Sans Mono CJK SC Regular', size=17) _gui_font4 = ui.CreateFont({ 'name': 'Noto Sans Mono CJK SC Regular', 'height': -17, 'weight': 400 }) print _gui_font2.width(u'12345文'), _gui_font3.width(u'12345文') hdc = win_none.GetDC().GetSafeHdc() gui.SelectObject(hdc, _gui_font4.GetSafeHandle()) print gui.GetTextExtentPoint32(hdc, u'12345文文') gui.SelectObject(hdc, _gui_font3._win_font.GetSafeHandle()) print gui.GetTextExtentPoint32(hdc, u'12345文') print _gui_font3._win_gdip_font.ptr, _gui_font2._win_gdip_font.ptr print _gui_font2.width(u'文泉驿等宽微米黑'), _gui_font2.width( u'文'), _gui_font3.width(u'12345文') except: import traceback traceback.print_exc()
def wndProc(hWnd, message, wParam, lParam): if message == self.MESSAGE: if lParam == win32con.WM_RBUTTONUP: self.show_menu() return 0 elif message == win32con.WM_COMMAND: self.execute_menu_item(win32gui.LOWORD(wParam)) return 0 elif message == self.__MESSAGE_TC: # TaskbarCreated message indicates all tray icons have been removed, # so we should add our tray icon again, not updating it. self.__NOTIFY_ID = None self.update_tray_icon() return 0 elif message == win32con.WM_PAINT: hdc, paintStruct = win32gui.BeginPaint(hWnd) if self.font is None: self.init_font(hdc, paintStruct) # Set the font win32gui.SelectObject(hdc, self.font) text = str(self.key_count) # Clear window content win32gui.DefWindowProc(hWnd, message, wParam, lParam) # Dynamically change window size & position if necessary text_extent = win32gui.GetTextExtentPoint32(hdc, text) window_rect = win32gui.GetClientRect(hWnd) window_width = window_rect[2] - window_rect[0] window_height = window_rect[3] - window_rect[1] if window_width != text_extent[0]\ or window_height != text_extent[1]: pass _, _, screen_width, screen_height = get_workarea_rect() win32gui.SetWindowPos( self.HWND, None, screen_width - text_extent[0], # x screen_height - text_extent[1], # y text_extent[0], # width text_extent[1], # height 0) # http://msdn.microsoft.com/en-us/library/windows/desktop/dd162498(v=vs.85).aspx win32gui.DrawText( hdc, text, len(text), # somehow -1 does not work tuple(win32gui.GetClientRect(hWnd)), (win32con.DT_BOTTOM | win32con.DT_NOCLIP | win32con.DT_SINGLELINE | win32con.DT_RIGHT)) self.__last_text_extent = text_extent win32gui.EndPaint(hWnd, paintStruct) return 0 elif message == win32con.WM_CLOSE: self.log('Window is closing, saving data now') super(KeyCounter, self).stop() return win32gui.DefWindowProc(hWnd, message, wParam, lParam) # The operating system wants to end the session elif message == win32con.WM_QUERYENDSESSION: self.log('Session might end soon, saving data now') super(KeyCounter, self).stop() return win32gui.DefWindowProc(hWnd, message, wParam, lParam) # The operating system is ending the session elif message == win32con.WM_ENDSESSION: self.log('Session %s ending', 'is' if wParam == win32con.TRUE else 'is not') super(KeyCounter, self).stop() return win32gui.DefWindowProc(hWnd, message, wParam, lParam) else: return win32gui.DefWindowProc(hWnd, message, wParam, lParam)