Python - Per pixel alpha - UpdateLayeredWindow

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alexandru Ionescu
    New Member
    • Aug 2010
    • 1

    Python - Per pixel alpha - UpdateLayeredWindow

    Hello,

    In the last two weeks I've been trying to properly use UpdateLayeredWi ndow in Python but I'm only getting errors. I want to make a quality window with per pixel alpha graphics. I tried with ctypes and I also tried with win32gui & win32api. I got all kinds of errors. In the end I got the error code 5 (ERROR_ACCESS_D ENIED) and nothing appears on the screen.
    Here is my latest code (made with ctypes):
    Code:
    import wx
    import win32api    #for RGB
    import ctypes
    
    cUser32 = ctypes.windll.user32
    cGdi32 = ctypes.windll.gdi32
    cKernel32 = ctypes.windll.kernel32
    
    class POINT(ctypes.Structure):
        _fields_ = [("x", ctypes.c_int), ("y", ctypes.c_int)]
    
    class SIZE(ctypes.Structure):
        _fields_ = [("cx", ctypes.c_int), ("cy", ctypes.c_int)]
    
    class BLENDFUNCTION(ctypes.Structure):
        _fields_ = [("AlphaFormat", ctypes.c_int), ("BlendFlags", ctypes.c_int),("BlendOp", ctypes.c_int),("SourceConstantAlpha", ctypes.c_int)]
    
    ULW = cUser32.UpdateLayeredWindow
    ULW.argtypes = [ctypes.c_ulong,
                    ctypes.c_ulong,
                    ctypes.POINTER(POINT),
                    ctypes.POINTER(SIZE),
                    ctypes.c_ulong,
                    ctypes.POINTER(POINT),
                    ctypes.c_ulong,
                    ctypes.POINTER(BLENDFUNCTION),
                    ctypes.c_ulong]
    
    ULW.restype = ctypes.c_long
    
    GWL_EXSTYLE = 0xFFFFFFEC
    WS_EX_LAYERED = 0x00080000
    WS_EX_TRANSPARENT = 0x00000020
    LWA_ALPHA = 0x00000002
    LWA_COLORKEY = 0x00000001
    AC_SRC_ALPHA = 0x01
    AC_SRC_OVER = 0x00
    
    class MyFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, title = "Test")
            
            hwnd = self.GetHandle()        
            print hwnd        
            gwl = cUser32.GetWindowLongA(hwnd, GWL_EXSTYLE)
            print gwl
            print cUser32.SetWindowLongA(hwnd, GWL_EXSTYLE, (gwl | WS_EX_LAYERED))
            print cKernel32.GetLastError()
            
            self.bmp = wx.EmptyBitmap(800,600)
            
            jpg1 = wx.Image('Multiple alpha.png', wx.BITMAP_TYPE_PNG)
            self.bmp = jpg1.ConvertToBitmap()
            w1, h1 = self.bmp.GetWidth(), self.bmp.GetHeight()
            self.SetClientSize( (w1, h1) )
            
            screenDC = cUser32.GetDC(None)
            cScreenDC = cGdi32.CreateCompatibleDC(screenDC)
            cGdi32.SelectObject(cScreenDC, self.bmp.GetHandle())
            p = POINT(50,50)
            p2 = POINT(0,0)
            s = SIZE(816,612)
            blend = BLENDFUNCTION(AC_SRC_OVER,0,AC_SRC_ALPHA,200)
            
            crkey = win32api.RGB(255,255,255)
            flags = (LWA_ALPHA | LWA_COLORKEY)
            
            print cUser32.UpdateLayeredWindow(hwnd,
                                              screenDC,
                                              ctypes.byref(p),
                                              ctypes.byref(s),
                                              cScreenDC,
                                              ctypes.byref(p2),
                                              crkey,
                                              ctypes.byref(blend),
                                              flags)
            
            print cKernel32.GetLastError()
    
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.SetTopWindow(frame)
    app.MainLoop()
    You can find a multi layer PNG at the following link
    http://en.wikipedia.or g/wiki/File:PNG_transp arency_demonstr ation_1.png

    I also attached this image and the code (in a .pyw file).

    My email address is q_crack@yahoo.c om

    Thank you,
    Alex
    Attached Files
Working...