codekingpro/portable-devtools
114k
1# A demo which creates a view and a frame which displays a PPM format bitmap2#3# This hasnnt been run in a while, as I dont have many of that format around!4 5import win32api6import win32con7import win32ui8 9 10class DIBView:11 def __init__(self, doc, dib):12 self.dib = dib13 self.view = win32ui.CreateView(doc)14 self.width = self.height = 015 # set up message handlers16 # self.view.OnPrepareDC = self.OnPrepareDC17 self.view.HookMessage(self.OnSize, win32con.WM_SIZE)18 19 def OnSize(self, params):20 lParam = params[3]21 self.width = win32api.LOWORD(lParam)22 self.height = win32api.HIWORD(lParam)23 24 def OnDraw(self, ob, dc):25 # set sizes used for "non strecth" mode.26 self.view.SetScrollSizes(win32con.MM_TEXT, self.dib.GetSize())27 dibSize = self.dib.GetSize()28 dibRect = (0, 0, dibSize[0], dibSize[1])29 # stretch BMP.30 # self.dib.Paint(dc, (0,0,self.width, self.height),dibRect)31 # non stretch.32 self.dib.Paint(dc)33 34 35class DIBDemo:36 def __init__(self, filename, *bPBM):37 # init data members38 f = open(filename, "rb")39 dib = win32ui.CreateDIBitmap()40 if len(bPBM) > 0:41 magic = f.readline()42 if magic != "P6\n":43 print("The file is not a PBM format file")44 raise ValueError("Failed - The file is not a PBM format file")45 # check magic?46 rowcollist = f.readline().split()47 cols = int(rowcollist[0])48 rows = int(rowcollist[1])49 f.readline() # whats this one?50 dib.LoadPBMData(f, (cols, rows))51 else:52 dib.LoadWindowsFormatFile(f)53 f.close()54 # create doc/view55 self.doc = win32ui.CreateDoc()56 self.dibView = DIBView(self.doc, dib)57 self.frame = win32ui.CreateMDIFrame()58 self.frame.LoadFrame() # this will force OnCreateClient59 self.doc.SetTitle("DIB Demo")60 self.frame.ShowWindow()61 62 # display the sucka63 self.frame.ActivateFrame()64 65 def OnCreateClient(self, createparams, context):66 self.dibView.view.CreateWindow(self.frame)67 return 168 69 70if __name__ == "__main__":71 import demoutils72 73 demoutils.NotAScript()74 