AlphaTransparent Image as form background
My current project allows the user to skin the application. I'm using GDI+ for painting an image as the background for the program. The image is either an 32bit bitmap or an 24bit PNG. I want to paint the image directly on the screen (so alphatransparency is keeped, pixels which is semi-transparent in the image should be it on the screen). I'm currently painting the image on the form, which makes the image painted overlap the form, so it doesn't keep it's transparency. So you can say that i want to make the form-background invisible, while keeping the painted image and the controls on the form visible.
this is how it should look like (taken from another app):
http://vigge.net/temp/cmd/completed.gif
The image isn't painted on the form, it's painted directly on the screen or whatever you should call it.
this is the image which should be painted (24bit png with transparency):
http://vigge.net/temp/cmd/bg.png
This is how it currently looks like:
http://vigge.net/temp/cmd/cmd_problem.gif
The reason why i got pink/purple background is becuase i'm using SetLayeredWindowAttributes() to make the color pink/purple go invisible. But since the image is painted on the form, the image's parts where it is semi-transparent is chaning the pink behind it to another color. I would like to scrap the "pink is invisible"-function and instead use images with transparency to decide the invisibility of the pixels.
I have no idea on how to solve this. Are there anyone who is familiar with this? I've looked after a solution for one week, but i can't find anything regarding this.
Thanks in advance.
1 Attachment(s)
hmmm *Scratches his head* .?.?.
Module
VB Code:
Option Explicit
Declare Function CreateRectRgn Lib "GDI32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Declare Function CombineRgn Lib "GDI32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Declare Function GetPixel Lib "GDI32" (ByVal hDC As Long, ByVal x As Long, ByVal Y As Long) As Long
Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
Declare Function DeleteObject Lib "GDI32" (ByVal hObject As Long) As Long
Public Function createSkinnedForm(SkinnedForm As Form, skinSrc As PictureBox, Optional transparentColor As Long) As Long
Const RGN_OR = 2
Dim glSkinImage As Long
Dim glHeight As Long
Dim glwidth As Long
Dim lReturn As Long
Dim lRgnTmp As Long
Dim lSkinRgn As Long
Dim lStart As Long
Dim lRow As Long
Dim lCol As Long
skinSrc.AutoSize = True
With SkinnedForm
.Hide
.Picture = skinSrc.Picture
.Width = skinSrc.Width
.Height = skinSrc.Height
End With
lSkinRgn = CreateRectRgn(0, 0, 0, 0)
With skinSrc
.AutoRedraw = True
glHeight = .Height / Screen.TwipsPerPixelY
glwidth = .Width / Screen.TwipsPerPixelX
If transparentColor < 1 Then transparentColor = GetPixel(.hDC, 0, 0)
For lRow = 0 To glHeight - 1
lCol = 0
Do While lCol < glwidth
Do While lCol < glwidth And GetPixel(.hDC, lCol, lRow) = transparentColor
lCol = lCol + 1
Loop
If lCol < glwidth Then
lStart = lCol
Do While lCol < glwidth And GetPixel(.hDC, lCol, lRow) <> transparentColor
lCol = lCol + 1
Loop
If lCol > glwidth Then lCol = glwidth
lRgnTmp = CreateRectRgn(lStart, lRow, lCol, lRow + 1)
lReturn = CombineRgn(lSkinRgn, lSkinRgn, lRgnTmp, RGN_OR)
Call DeleteObject(lRgnTmp)
End If
Loop
Next
End With
Call SetWindowRgn(SkinnedForm.hWnd, lSkinRgn, True)
skinSrc.Picture = LoadPicture("")
SkinnedForm.Show
End Function
form should contain a picturebox with the image
FORM
VB Code:
Call createSkinnedForm(Me, BackPic)
result?