dibdemo.py 1.9 KB

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