[python-win32] Flicker and OnEraseBkgnd

cyril giraudon cyril.giraudon at free.fr
Fri Oct 6 14:55:43 CEST 2006


Hi,

I currently try to add a matplotlib backend based on the Gecko engine 
(XPCOM, plugin), PIL and pywin.

I simply build a new window and place it in the plugin area as a child.

All works fine, bur when I change the size of the window, there is a 
flicker .
I 've read some pages on the web, but I don't see how to adapt the 
solutions with python.
Precisely, i can't overwrite the OnResizeBkgnd often spotted in articles.

Is there any solutions ?

Thanks a lot,

Cyril.


Below some lines of my code.

window_id is the HWND of the plugin window.




from PIL import Image
from PIL import ImageWin

import matplotlib
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.backends.backend_agg import RendererAgg 

from matplotlib.axes import Subplot
from matplotlib.figure import Figure
from matplotlib.numerix import arange, sin, pi

import win32ui
import win32api
import win32con
import win32gui
import winxpgui

wc = win32gui.WNDCLASS()
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
wc.lpfnWndProc = wndProc
wc.cbWndExtra = 0;
hinst = wc.hInstance = win32api.GetModuleHandle(None)
wc.hIcon = 0
wc.hbrBackground = win32con.NULL
wc.lpszMenuName = ""
wc.lpszClassName = "cmgui_embed_wndclass"
classAtom = win32gui.RegisterClass(wc)

self.hwnd = win32gui.CreateWindow(classAtom, "",\
 win32con.WS_CHILD | win32con.WS_VISIBLE, 0, 0, \
 width, height, window_id, 0, hinst, None)

fig = Figure(figsize=(5,4), dpi=100)
a = fig.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)
a.plot(t,s)
dpival = fig.dpi.get()
winch = width/dpival
hinch = height/dpival
fig.set_figsize_inches(winch, hinch)

self.canvas = FigureCanvasAgg(fig)
self.canvas.renderer = RendererAgg(width, height, fig.dpi)
self.canvas.draw()
image = Image.frombuffer('RGBA', self.canvas.get_width_height(), self.canvas.buffer_rgba(0,0))
self.dib = ImageWin.Dib(image)
z = ImageWin.HWND(hwnd)
self.dib.expose(z)
 

  def wndProc(self, hwnd, message, wParam, lParam):
    if message == win32con.WM_PAINT:
      hdc, paintStruct = win32gui.BeginPaint(hwnd)
      self.canvas.draw()
      image = Image.frombuffer('RGBA', self.canvas.get_width_height(), self.canvas.buffer_rgba(0,0))
      image = image.transpose(Image.FLIP_TOP_BOTTOM)
      dib = ImageWin.Dib(image)
      z = ImageWin.HWND(self.hwnd)
      dib.expose(z)

      win32gui.EndPaint(hwnd, paintStruct)
      return 0

    elif message == win32con.WM_NCPAINT:
      return 1

    elif message == win32con.WM_ERASEBKGND:
      return 1

    elif message == win32con.WM_SIZE:
      pass



  def resize_canvas( self, width, height):
    return
    win32gui.SetWindowPos(self.hwnd, win32con.HWND_TOP, 0, 0, width, height,\
      win32con.SWP_SHOWWINDOW)

    fig = self.canvas.figure
    dpival = fig.dpi.get()
    winch = width/dpival
    hinch = height/dpival
    fig.set_figsize_inches(winch, hinch)
    self.canvas.draw()
    image = Image.frombuffer('RGBA', self.canvas.get_width_height(), self.canvas.buffer_rgba(0,0))
    image = image.transpose(Image.FLIP_TOP_BOTTOM)
    dib = ImageWin.Dib(image)
    z = ImageWin.HWND(self.hwnd)
    dib.expose(z)








More information about the Python-win32 mailing list