codekingpro/portable-devtools
114k
1# Demo of Generic document windows, DC, and Font usage2# by Dave Brennan (brennan@hal.com)3 4# usage examples:5 6# >>> from fontdemo import *7# >>> d = FontDemo('Hello, Python')8# >>> f1 = { 'name':'Arial', 'height':36, 'weight':win32con.FW_BOLD}9# >>> d.SetFont(f1)10# >>> f2 = {'name':'Courier New', 'height':24, 'italic':1}11# >>> d.SetFont (f2)12 13import win32api14import win32con15import win32ui16from pywin.mfc import docview17 18# font is a dictionary in which the following elements matter:19# (the best matching font to supplied parameters is returned)20# name string name of the font as known by Windows21# size point size of font in logical units22# weight weight of font (win32con.FW_NORMAL, win32con.FW_BOLD)23# italic boolean; true if set to anything but None24# underline boolean; true if set to anything but None25 26 27class FontView(docview.ScrollView):28 def __init__(29 self, doc, text="Python Rules!", font_spec={"name": "Arial", "height": 42}30 ):31 docview.ScrollView.__init__(self, doc)32 self.font = win32ui.CreateFont(font_spec)33 self.text = text34 self.width = self.height = 035 # set up message handlers36 self.HookMessage(self.OnSize, win32con.WM_SIZE)37 38 def OnAttachedObjectDeath(self):39 docview.ScrollView.OnAttachedObjectDeath(self)40 del self.font41 42 def SetFont(self, new_font):43 # Change font on the fly44 self.font = win32ui.CreateFont(new_font)45 # redraw the entire client window46 selfInvalidateRect(None)47 48 def OnSize(self, params):49 lParam = params[3]50 self.width = win32api.LOWORD(lParam)51 self.height = win32api.HIWORD(lParam)52 53 def OnPrepareDC(self, dc, printinfo):54 # Set up the DC for forthcoming OnDraw call55 self.SetScrollSizes(win32con.MM_TEXT, (100, 100))56 dc.SetTextColor(win32api.RGB(0, 0, 255))57 dc.SetBkColor(win32api.GetSysColor(win32con.COLOR_WINDOW))58 dc.SelectObject(self.font)59 dc.SetTextAlign(win32con.TA_CENTER | win32con.TA_BASELINE)60 61 def OnDraw(self, dc):62 if self.width == 0 and self.height == 0:63 left, top, right, bottom = self.GetClientRect()64 self.width = right - left65 self.height = bottom - top66 x, y = self.width // 2, self.height // 267 dc.TextOut(x, y, self.text)68 69 70def FontDemo():71 # create doc/view72 template = docview.DocTemplate(win32ui.IDR_PYTHONTYPE, None, None, FontView)73 doc = template.OpenDocumentFile(None)74 doc.SetTitle("Font Demo")75 # print "template is ", template, "obj is", template._obj_76 template.close()77 78 79# print "closed"80# del template81 82if __name__ == "__main__":83 import demoutils84 85 if demoutils.NeedGoodGUI():86 FontDemo()87 