473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Python - Per pixel alpha - UpdateLayeredWindow

Hello,

In the last two weeks I've been trying to properly use UpdateLayeredWindow 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_DENIED) and nothing appears on the screen.
Here is my latest code (made with ctypes):
Expand|Select|Wrap|Line Numbers
  1. import wx
  2. import win32api    #for RGB
  3. import ctypes
  4.  
  5. cUser32 = ctypes.windll.user32
  6. cGdi32 = ctypes.windll.gdi32
  7. cKernel32 = ctypes.windll.kernel32
  8.  
  9. class POINT(ctypes.Structure):
  10.     _fields_ = [("x", ctypes.c_int), ("y", ctypes.c_int)]
  11.  
  12. class SIZE(ctypes.Structure):
  13.     _fields_ = [("cx", ctypes.c_int), ("cy", ctypes.c_int)]
  14.  
  15. class BLENDFUNCTION(ctypes.Structure):
  16.     _fields_ = [("AlphaFormat", ctypes.c_int), ("BlendFlags", ctypes.c_int),("BlendOp", ctypes.c_int),("SourceConstantAlpha", ctypes.c_int)]
  17.  
  18. ULW = cUser32.UpdateLayeredWindow
  19. ULW.argtypes = [ctypes.c_ulong,
  20.                 ctypes.c_ulong,
  21.                 ctypes.POINTER(POINT),
  22.                 ctypes.POINTER(SIZE),
  23.                 ctypes.c_ulong,
  24.                 ctypes.POINTER(POINT),
  25.                 ctypes.c_ulong,
  26.                 ctypes.POINTER(BLENDFUNCTION),
  27.                 ctypes.c_ulong]
  28.  
  29. ULW.restype = ctypes.c_long
  30.  
  31. GWL_EXSTYLE = 0xFFFFFFEC
  32. WS_EX_LAYERED = 0x00080000
  33. WS_EX_TRANSPARENT = 0x00000020
  34. LWA_ALPHA = 0x00000002
  35. LWA_COLORKEY = 0x00000001
  36. AC_SRC_ALPHA = 0x01
  37. AC_SRC_OVER = 0x00
  38.  
  39. class MyFrame(wx.Frame):
  40.     def __init__(self):
  41.         wx.Frame.__init__(self, None, wx.ID_ANY, title = "Test")
  42.  
  43.         hwnd = self.GetHandle()        
  44.         print hwnd        
  45.         gwl = cUser32.GetWindowLongA(hwnd, GWL_EXSTYLE)
  46.         print gwl
  47.         print cUser32.SetWindowLongA(hwnd, GWL_EXSTYLE, (gwl | WS_EX_LAYERED))
  48.         print cKernel32.GetLastError()
  49.  
  50.         self.bmp = wx.EmptyBitmap(800,600)
  51.  
  52.         jpg1 = wx.Image('Multiple alpha.png', wx.BITMAP_TYPE_PNG)
  53.         self.bmp = jpg1.ConvertToBitmap()
  54.         w1, h1 = self.bmp.GetWidth(), self.bmp.GetHeight()
  55.         self.SetClientSize( (w1, h1) )
  56.  
  57.         screenDC = cUser32.GetDC(None)
  58.         cScreenDC = cGdi32.CreateCompatibleDC(screenDC)
  59.         cGdi32.SelectObject(cScreenDC, self.bmp.GetHandle())
  60.         p = POINT(50,50)
  61.         p2 = POINT(0,0)
  62.         s = SIZE(816,612)
  63.         blend = BLENDFUNCTION(AC_SRC_OVER,0,AC_SRC_ALPHA,200)
  64.  
  65.         crkey = win32api.RGB(255,255,255)
  66.         flags = (LWA_ALPHA | LWA_COLORKEY)
  67.  
  68.         print cUser32.UpdateLayeredWindow(hwnd,
  69.                                           screenDC,
  70.                                           ctypes.byref(p),
  71.                                           ctypes.byref(s),
  72.                                           cScreenDC,
  73.                                           ctypes.byref(p2),
  74.                                           crkey,
  75.                                           ctypes.byref(blend),
  76.                                           flags)
  77.  
  78.         print cKernel32.GetLastError()
  79.  
  80. app = wx.App()
  81. frame = MyFrame()
  82. frame.Show()
  83. app.SetTopWindow(frame)
  84. app.MainLoop()
  85.  
  86.  
You can find a multi layer PNG at the following link
http://en.wikipedia.org/wiki/File:PNG_transparency_demonstration_1.png

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

My email address is q_crack@yahoo.com

Thank you,
Alex
Attached Files
File Type: zip Multiple alpha test.zip (215.7 KB, 67 views)
Aug 16 '10 #1
0 1360

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Edmond Rusjan | last post by:
Hi All, I'd like to use Python-2.3.4 on OSF1 V4.0, but have trouble installing. With a plain "./configure; make" build, I cannot import socket. If I uncomment the socketmodule in Modules/Setup,...
2
by: tjv | last post by:
Hi all, I am working with images in python using PIL. I come from a MATLAB background so I am finding it to be sometimes frustrating to do things correctly and quickly. All I need to do is load an...
12
by: eduardo.padoan | last post by:
I'm reading about "high order messages" in Ruby by Nat Pryce, and thinking if it could be util and if so, if it could be done in Python. Someone already tried? References:...
2
by: Allan | last post by:
I've been trying to get this to work for days. I have a form which needs to be transparent (only the form background). On the form there are two images (32 bit PNGs) with alpha channel...
18
by: Andrew Christiansen | last post by:
Hey all. The images I create in photoshop with semi-transparent pixels (for instance in Photoshop text with a dropshadow with a transparent canvas) I've been saving in PNG format and then using...
3
by: illegal.prime | last post by:
Hi all, I'm looking for the right way to create a checkered background in a PictureBox. I will be applying various images to this PictureBox and I want to to see the checkered background based on...
4
by: Carsten Schmitt | last post by:
Hello, I want to draw a simple pixel (i.e. a red pixel in the center of the screen), which is always in the foreground - even when running a fullscreen application like a DirectX game. I need...
3
by: Gernot Frisch | last post by:
Hi, I have this code that blends 2 pixels, but it's not really fast. Can someone help me speeding it up? #define GETR(a) (unsigned char)(((((a)>>11)&31)*255)/31) #define GETG(a) (unsigned...
1
by: livibetter | last post by:
Hi! I am trying to making an On-Screen Display, which is implemented by wx.Frame. Basically I created a wx.Frame with style like super(OSDBase, self).__init__(parent, id, title, style =...
9
by: raylopez99 | last post by:
After refering to the below thread, I take it for C# Forms 2.0, there a way to draw a 1 pixel by 1 pixel rectangle, which I was able to do just now successfully. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.