Results 1 to 24 of 24

Thread: AlphaTransparent Image as form background

  1. #1

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172

    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):

    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):


    This is how it currently looks like:

    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.
    Last edited by vigge89; May 31st, 2004 at 02:04 PM.


  2. #2
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    I think you could probably get the desktop DC and blend the two images together and then paint it to the form.
    You should also hide the form before getting the desktop DC...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  3. #3

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    yeah, i guess, but that's not the real way, it must be some way else
    what if there's a window under the app?


  4. #4
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    If I'm not wrong, the desktop DC holds an image of the entire screen. You should be able to do it this way:

    1) Hide the program
    2) Copy the desktop DC to a local DC
    3) Mix it with the skin
    4) Paint it on the form
    5) Show the program

    Also everytime you move the form you'll have to remix the skin with the other DC. And everytime your program loses/gets focus, you should update the desktop DC, because other windows might have been moved.
    (You don't have to get the whole DC, just the part that's behind the program)

    Just as vigge89 says, there should be another way, but I don't know it. I know there is a way to make the program fully transparent in different regions, but you can not make it partially transparent using that method.
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  5. #5

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    that way would be kinda slow, laggy, and it would probably flicker alot, so it wouldn't look to good


  6. #6
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Originally posted by vigge89
    that way would be kinda slow, laggy, and it would probably flicker alot, so it wouldn't look to good
    That's probably true.

    I've heard that the new windows "longhorn" will keep separate buffers for each windows which allows them to be blended with the other windows. Maybe you should wait for longhorn
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  7. #7

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    it is possible right now, i've seen lots of system-monitoring apps and stuff do this


  8. #8
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Originally posted by vigge89
    it is possible right now, i've seen lots of system-monitoring apps and stuff do this
    I know that in 3D Mark when you load it, the splash screen fades in and blends with the background. But I think it does it using my method... Is it those kind of windows you're talking about? Or are you talking about those that you can move around?
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  9. #9

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    yeah, the form/window should be moveable


  10. #10

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    The image on how it should look like is taken from RainLendar, which is written in C++.
    I just saw that he had released the source code for it, which can be found at:
    http://www.ipi.fi/~rainy/index.php?pn=projects&project=rainlendar

    I downloaded the code and looked trhough it, but i don't know C++ at all, so i can't "read" the code. If anyone could find the code for using the trans, could you tell me how it's done?
    thanks...


  11. #11
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    From looking at rainwindow.cpp, it looks like it takes a pic of the desktop (inc. windows), draws itself on it, then displays it.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  12. #12

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    hmm, can you see how it paints itself?


  13. #13
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    In that file the updatetransparency function seems to be the one. It uses a C++ function called 'UpdateLayeredWindow' It has the following parameters:

    ( HWND hwnd,
    HDC hdcDst,
    POINT *pptDst,
    SIZE *psize,
    HDC hdcSrc,
    POINT *pptSrc,
    COLORREF crKey,
    BLENDFUNCTION *pblend,
    DWORD dwFlags
    );

    Have checked and it's not an API function.

    Here's it being used:
    Code:
    LONG style = GetWindowLong(m_Window, GWL_EXSTYLE);
    SetWindowLong(m_Window, GWL_EXSTYLE, style | WS_EX_LAYERED);
    
    RECT r;
    GetWindowRect(m_Window, &r);
    typedef BOOL (WINAPI * FPUPDATELAYEREDWINDOW)(HWND hWnd, HDC hdcDst,
     POINT *pptDst, SIZE *psize, HDC hdcSrc, POINT *pptSrc, COLORREF crKey,
     BLENDFUNCTION *pblend, DWORD dwFlags);
    HINSTANCE h = LoadLibrary("user32.dll");
    FPUPDATELAYEREDWINDOW UpdateLayeredWindow = (FPUPDATELAYEREDWINDOW)GetProcAddress(h, "UpdateLayeredWindow");
    
    BLENDFUNCTION blendPixelFunction= {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA};
    POINT ptWindowScreenPosition = {r.left, r.top};
    POINT ptSrc = {0, 0};
    SIZE szWindow = {m_Width, m_Height};
    			
    HDC dcScreen = GetDC(GetDesktopWindow());
    HDC dcMemory = CreateCompatibleDC(dcScreen);
    HBITMAP oldBitmap = (HBITMAP)SelectObject(dcMemory, m_DoubleBuffer.GetBitmap());
    UpdateLayeredWindow(m_Window, dcScreen, &ptWindowScreenPosition, &szWindow, dcMemory,
     &ptSrc, 0, &blendPixelFunction, ULW_ALPHA);
    			ReleaseDC(GetDesktopWindow(), dcScreen);
    SelectObject(dcMemory, oldBitmap);
    DeleteDC(dcMemory);
    FreeLibrary(h);
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  14. #14

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    Hmm, i'm using SetLayeredWindowAttributes() which is an API in my project for setting alphatransparency, maybe they're related?
    How i call the function:
    VB Code:
    1. SetLayeredWindowAttributes hWnd, RGB(255, 0, 255), bAlpha, LWA_COLORKEY Or LWA_ALPHA

    Does anyone know what the UpdateLayeredWindow() does, and what it "is"?

    It's called with:
    Code:
    UpdateLayeredWindow(m_Window, dcScreen, &ptWindowScreenPosition, &szWindow, dcMemory, &ptSrc, 0, &blendPixelFunction, ULW_ALPHA);
    what is "&blendPixelFunction" and "ULW_ALPHA"?
    Last edited by vigge89; May 21st, 2004 at 11:20 AM.


  15. #15
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    &PixelBlendFunction looks like it wants a pointer to the address of a blend function.
    The blend function has this structure:
    Code:
    {
      BYTE     BlendOp;
      BYTE     BlendFlags;
      BYTE     SourceConstantAlpha;
      BYTE     AlphaFormat;
    }
    Judging by C++ naming conventions i'd guess that ULW_ALPHA was a constant (unsigned long) value.

    Sorry i can't help more.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  16. #16

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    Originally posted by SLH
    &PixelBlendFunction looks like it wants a pointer to the address of a blend function.
    The blend function has this structure:
    Code:
    {
      BYTE     BlendOp;
      BYTE     BlendFlags;
      BYTE     SourceConstantAlpha;
      BYTE     AlphaFormat;
    }
    Judging by C++ naming conventions i'd guess that ULW_ALPHA was a constant (unsigned long) value.

    Sorry i can't help more.
    yea, i knew it was a constant, but what is it for?

    thanks for answering btw


  17. #17
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Well according to the declaration of 'updatelayeredwindow' it's its flags.
    From the MSDN help:

    dwFlags
    This parameter can be one of the following values.
    ULW_ALPHA
    Use pblend as the blend function. If the display mode is 256 colors or less, the effect of this value is the same as the effect of ULW_OPAQUE.
    ULW_COLORKEY
    Use crKey as the transparency color.
    ULW_OPAQUE
    Draw an opaque layered window.
    If hdcSrc is NULL, dwFlags should be zero.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  18. #18

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    I looked at the function on MSDN, seems to act exactly like SetLayeredWindowAttributes(), so that's not what i'm looking for. I'll keep searching...


  19. #19
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    hmmm *Scratches his head* .?.?.

    Module
    VB Code:
    1. Option Explicit
    2. Declare Function CreateRectRgn Lib "GDI32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    3. Declare Function CombineRgn Lib "GDI32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
    4. Declare Function GetPixel Lib "GDI32" (ByVal hDC As Long, ByVal x As Long, ByVal Y As Long) As Long
    5. Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
    6. Declare Function DeleteObject Lib "GDI32" (ByVal hObject As Long) As Long
    7.  
    8. Public Function createSkinnedForm(SkinnedForm As Form, skinSrc As PictureBox, Optional transparentColor As Long) As Long
    9. Const RGN_OR = 2
    10. Dim glSkinImage As Long
    11. Dim glHeight    As Long
    12. Dim glwidth     As Long
    13. Dim lReturn     As Long
    14. Dim lRgnTmp     As Long
    15. Dim lSkinRgn    As Long
    16. Dim lStart      As Long
    17. Dim lRow        As Long
    18. Dim lCol        As Long
    19. skinSrc.AutoSize = True
    20. With SkinnedForm
    21.     .Hide
    22.     .Picture = skinSrc.Picture
    23.     .Width = skinSrc.Width
    24.     .Height = skinSrc.Height
    25. End With
    26. lSkinRgn = CreateRectRgn(0, 0, 0, 0)
    27. With skinSrc
    28.     .AutoRedraw = True
    29.     glHeight = .Height / Screen.TwipsPerPixelY
    30.     glwidth = .Width / Screen.TwipsPerPixelX
    31.     If transparentColor < 1 Then transparentColor = GetPixel(.hDC, 0, 0)
    32.     For lRow = 0 To glHeight - 1
    33.         lCol = 0
    34.         Do While lCol < glwidth
    35.             Do While lCol < glwidth And GetPixel(.hDC, lCol, lRow) = transparentColor
    36.                 lCol = lCol + 1
    37.             Loop
    38.             If lCol < glwidth Then
    39.                 lStart = lCol
    40.                 Do While lCol < glwidth And GetPixel(.hDC, lCol, lRow) <> transparentColor
    41.                     lCol = lCol + 1
    42.                 Loop
    43.                 If lCol > glwidth Then lCol = glwidth
    44.                 lRgnTmp = CreateRectRgn(lStart, lRow, lCol, lRow + 1)
    45.                 lReturn = CombineRgn(lSkinRgn, lSkinRgn, lRgnTmp, RGN_OR)
    46.                 Call DeleteObject(lRgnTmp)
    47.             End If
    48.         Loop
    49.     Next
    50. End With
    51. Call SetWindowRgn(SkinnedForm.hWnd, lSkinRgn, True)
    52. skinSrc.Picture = LoadPicture("")
    53. SkinnedForm.Show
    54. End Function

    form should contain a picturebox with the image

    FORM
    VB Code:
    1. Call createSkinnedForm(Me, BackPic)


    result?
    Attached Images Attached Images  
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  20. #20

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    TokersBall: yeah, that's for a normal skinned form, i looking for code which allows semi-transparent images


  21. #21

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    i've got another question now (don't forget the old one tho ):

    I use this code to draw an image on an picturebox:
    VB Code:
    1. gfx.CreateFromHDC cmdExe.hDC
    2.  
    3.     gfx.DrawImageI img_btn_normal.Handle, 0, 0
    4.     cmdExe.Refresh
    gfx is the GDI+ graphics handle, and cmdExe is the picturebox (wrong name, i know). Now i'm wondering how to clear the drawn picture, so i can paint another image (which could be transprarent) on the picturebox. I've tried cmdExe.Cls, but that stops the picturebox from being painted on again. Does anyone know how to do it properly?

    Edit: nevermind, got it to work by using:
    VB Code:
    1. cmdExe.Cls
    2.     gfx.CreateFromHDC cmdExe.hDC
    3.     gfx.DrawImageI img_btn_normal.Handle, 0, 0
    4.     cmdExe.Refresh

    ANy new ideas on the main problem?
    Last edited by vigge89; May 22nd, 2004 at 03:32 AM.


  22. #22

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    pulling this thread up again after i contacted the author of Rainlendar. He gave me an example source for doing this in C++.

    Check the attachment for the Vc++ source. Now, i've studied some C++, but i'm not that good on it, so if anyone could take a look, if you could translate the code into english, so I can port the code into VB.

    Thanks in advance...
    Attached Files Attached Files


  23. #23

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    i finally found a working example, but it seems to be kinda complicated. I'll attach the code for the example and the project im trying to create.

    Here's the example i found. Drag an image with alphatransparency onto the top slider, and it will create a window for it.
    Attached Files Attached Files


  24. #24

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    Here's the program i'm trying to add the feuture to (just an simple example program which uses the current way of painting images which i got in my main project)

    I can't get the UpdateLayeredWindow() function to work

    Thanks in advance....
    Attached Files Attached Files


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width