Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to use Alpha blending on picture boxes 3

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
0
0
US
Is there a way to use Alpha blending on picture boxes within a form...

To see the form through a picture box...

I tried using the Picture box's hwnd in place of the form's, but had no luck...

***This will be for WinXP (and maybe Win2K)

Related Thread...
thread222-520827

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Not the picture box itself, but you can alphablit the image from a hidden picturebox to the form, which should produce much the same effect
 
BOOL AlphaBlend(
HDC hdcDest, // handle to destination DC
int nXOriginDest, // x-coord of upper-left corner
int nYOriginDest, // y-coord of upper-left corner
int nWidthDest, // destination width
int nHeightDest, // destination height
HDC hdcSrc, // handle to source DC
int nXOriginSrc, // x-coord of upper-left corner
int nYOriginSrc, // y-coord of upper-left corner
int nWidthSrc, // source width
int nHeightSrc, // source height
BLENDFUNCTION blendFunction // alpha-blending function
);


Are you saying to set the hdcDest to the visible pic box...
And hdcSrc to the invisible one?
Then the form is supposed to show through the pic box?
How do you control the transparency?

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
No, I'm not. What I'm really saying is that you need a memory DC containing the source image, and then alphablit it to the form, controlling the transparency level via the SourceConstantAlpha member of the BlendFunction UDT.

I don't have the time to knock together a specific example of this, but here is some (ropey) code that fades a form in and out on loading an unloading. You shoud be able to adapt to your needs. The example just needs a form, and the following code:
[tt]
Option Explicit

Const AC_SRC_OVER = &H0

Private Type BLENDFUNCTION
BlendOp As Byte
BlendFlags As Byte
SourceConstantAlpha As Byte
AlphaFormat As Byte
End Type

Private Declare Function AlphaBlend Lib "msimg32.dll" (ByVal hdc As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal hdc As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal BLENDFUNCT As Long) As Long
Private Declare Sub RtlMoveMemory Lib "kernel32.dll" (Destination As Any, Source As Any, ByVal Length As Long)

Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Const SRCCOPY = &HCC0020


Private Function Fader(Optional Direction As Long)
Dim ScreenCopyDC As Long
Dim myDC As Long
Dim myBMP As Long
Dim oldBMP As Long
Dim myrect As RECT
Dim BF As BLENDFUNCTION
Dim lBF As Long
Dim fade As Long


GetWindowRect Me.hwnd, myrect

If Direction = 0 Then
Me.Hide
Else
Me.Show
End If
DoEvents

' Snapshot the either the form or the desktop under the form, depending on whether
' fade in or fade out is required
myDC = GetWindowDC(GetDesktopWindow())
ScreenCopyDC = CreateCompatibleDC(myDC)
myBMP = CreateCompatibleBitmap(myDC, myrect.Right - myrect.Left, myrect.Bottom - myrect.Top) ' Me.ScaleWidth, Me.ScaleHeight)
oldBMP = SelectObject(ScreenCopyDC, myBMP)
BitBlt ScreenCopyDC, 0, 0, myrect.Right - myrect.Left + 100, myrect.Bottom - myrect.Top + 100, myDC, myrect.Left, myrect.Top, SRCCOPY

If Direction = 0 Then
Me.Show
Else
Me.Hide
End If
DoEvents

For fade = 0 To 127 ' Step 4 : add the step back in if you want to speed things up
With BF
.BlendOp = AC_SRC_OVER
.BlendFlags = 0
.SourceConstantAlpha = fade
.AlphaFormat = 0
End With

RtlMoveMemory lBF, BF, 4

AlphaBlend myDC, myrect.Left, myrect.Top, myrect.Right - myrect.Left, myrect.Bottom - myrect.Top, ScreenCopyDC, 0, 0, myrect.Right - myrect.Left, myrect.Bottom - myrect.Top, lBF

Next

'Clean up
SelectObject ScreenCopyDC, oldBMP
DeleteObject myBMP
ReleaseDC GetDesktopWindow(), myDC
DeleteDC myDC
DeleteDC ScreenCopyDC

If Direction = 0 Then
Me.Hide
Else
Me.Show
End If
End Function

Private Sub Form_Load()
Fader 1
End Sub

Private Sub Form_Unload(Cancel As Integer)
Fader 0
End Sub

 
try this...

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Const LWA_ALPHA = &H2&
Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000

Private Sub Form_Load()
Dim dwExStyle As Long
dwExStyle = GetWindowLong(hwnd, GWL_EXSTYLE)
dwExStyle = dwExStyle Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, dwExStyle
SetLayeredWindowAttributes hwnd, 0, 0, LWA_ALPHA
Show
Dim Opactiy As Byte '0 = transparent, 255 = opaque
For Opacity = 0 To 255 Step 3
SetLayeredWindowAttributes hwnd, 0, Opacity, LWA_ALPHA
Refresh
Next
Show
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim Opactiy As Byte '0 = transparent, 255 = opaque
For Opacity = 255 To 0 Step -4
SetLayeredWindowAttributes hwnd, 0, Opacity, LWA_ALPHA
Refresh
Next
End Sub



I'm Keep Studying.... please show the way...
Not Good in English
 
Sure, but 1) SetLayeredWindowAttributes only works for 2000 and XP (and it was that limitation that originally provoked me to write the code I gave above); 2) even on 2000 and XP it doesn't work on child windows - which is what the original question refers to.

Also, under 2000 and XP I tend to use AnimateWindow to get the form fade effect
 
AnimateWindow? (...User32.lib)

I never heard of that one...

Where do you learn all this stuff at???

Do you have an example of how YOU use it...?

Here are the Specs...

Code:
BOOL AnimateWindow(HWND hwnd,
                   DWORD dwTime,
                   DWORD dwFlags);

hwnd
[in] Handle to the window to animate. The calling thread must own this window.
dwTime
[in] Specifies how long it takes to play the animation, in milliseconds. Typically, an animation takes 200 milliseconds to play.
dwFlags
[in] Specifies the type of animation. This parameter can be one or more of the following values. Note that, by default, these flags take effect when showing a window. To take effect when hiding a window, use AW_HIDE and a logical OR operator with the appropriate flags.

FLAGS...
AW_SLIDE
Uses slide animation. By default, roll animation is used. This flag is ignored when used with AW_CENTER.
AW_ACTIVATE
Activates the window. Do not use this value with AW_HIDE.
AW_BLEND
Uses a fade effect. This flag can be used only if hwnd is a top-level window.
AW_HIDE
Hides the window. By default, the window is shown.
AW_CENTER
Makes the window appear to collapse inward if AW_HIDE is used or expand outward if the AW_HIDE is not used. The various direction flags have no effect.
AW_HOR_POSITIVE
Animates the window from left to right. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.
AW_HOR_NEGATIVE
Animates the window from right to left. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.
AW_VER_POSITIVE
Animates the window from top to bottom. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.
AW_VER_NEGATIVE
Animates the window from bottom to top. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.

Ref:
how do you declare it in VB? With Longs?
Private Declare Function AnimateWindow Lib "user32" (ByVal hwnd As Long, _
ByVal dwTime As Long, ByVal dwFlags As Long) As Boolean


also... what are the const values for the flags?

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 

Cube101,

If you have the studio you can find a lot of undefined constants (among other things) in the *.h files located at C:\Program Files\Microsoft Visual Studio\VC98\Include\ (may be different if you installed to a different path)
[tt]
From the link that you gave...

Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 98, Windows 2000

'from the file it points to

/*
* AnimateWindow() Commands
*/
#define AW_HOR_POSITIVE 0x00000001
#define AW_HOR_NEGATIVE 0x00000002
#define AW_VER_POSITIVE 0x00000004
#define AW_VER_NEGATIVE 0x00000008
#define AW_CENTER 0x00000010
#define AW_HIDE 0x00010000
#define AW_ACTIVATE 0x00020000
#define AW_SLIDE 0x00040000
#define AW_BLEND 0x00080000

'and if I have my conversion right (Please check, this was off the top of my head) these would be your constant declarations...

Public Const AW_HOR_POSITIVE = 1 'or &H1
Public Const AW_HOR_NEGATIVE = 2 'or &H2
Public Const AW_VER_POSITIVE = 4 'or &H4
Public Const AW_VER_NEGATIVE = 8 'or &H8
Public Const AW_CENTER = 16 'or &H10
Public Const AW_HIDE = 65536 'or &H10000
Public Const AW_ACTIVATE = 131072 'or &H20000
Public Const AW_SLIDE = 262144 'or &H40000
Public Const AW_BLEND = 524288 'or &H80000
[/tt]

and your declaration looks right...

Good Luck

 
I have VC++ at home...

But I only have VB at work
(I don't have a license for VC at work... only at home... a personal copy)
I don't use it enough to as for the company to purchase it for me...

I use eVC for icon creation and any other tools I need from VC++... (eVC is a free DL @ microsoft.com... ~200MB)

That is what I tried to do at first (search for winuser.h)

but the only one that showed up on this PC (work) was one in eVC (embeded VC++ for PocketPC)
(C:\Windows CE Tools\wce300\Pocket PC 2002\include)

AND... it did not include those values...

Thanks for the suppling the values...

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Ok... It keeps failing on me...

[frustrated]WHERE/HOW DO YOU USE IT ?????[/frustrated]

This is the code I have so far...


Const AW_HOR_POSITIVE = 1 'or &H1
Const AW_HOR_NEGATIVE = 2 'or &H2
Const AW_VER_POSITIVE = 4 'or &H4
Const AW_VER_NEGATIVE = 8 'or &H8
Const AW_CENTER = 16 'or &H10
Const AW_HIDE = 65536 'or &H10000
Const AW_ACTIVATE = 131072 'or &H20000
Const AW_SLIDE = 262144 'or &H40000
Const AW_BLEND = 524288 'or &H80000

Private Declare Function AnimateWindow Lib "user32" (ByVal hwnd As Long, _
ByVal dwTime As Long, ByVal dwFlags As Long) As Boolean

...........

Private Sub Form_Load()
Dim flags As Long
Dim ret As Boolean
'Me.Visible = False '<-- USE???
flags = AW_ACTIVATE Or AW_CENTER
ret = AnimateWindow(Me.hwnd, 200, flags)
'Me.Visible = True '???
......

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Be warned that AnimateWindow does not work great in anything W98. Right, in a module:
[tt]
Option Explicit

Public Declare Function AnimateWindow Lib &quot;user32&quot; (ByVal hwnd As Long, ByVal dwTime As Long, ByVal dwFlags As Long) As Boolean
Public Const AW_HOR_POSITIVE = &H1 'Animates the window from left to right. This flag can be used with roll or slide animation.
Public Const AW_HOR_NEGATIVE = &H2 'Animates the window from right to left. This flag can be used with roll or slide animation.
Public Const AW_VER_POSITIVE = &H4 'Animates the window from top to bottom. This flag can be used with roll or slide animation.
Public Const AW_VER_NEGATIVE = &H8 'Animates the window from bottom to top. This flag can be used with roll or slide animation.
Public Const AW_CENTER = &H10 'Makes the window appear to collapse inward if AW_HIDE is used or expand outward if the AW_HIDE is not used.
Public Const AW_HIDE = &H10000 'Hides the window. By default, the window is shown.
Public Const AW_ACTIVATE = &H20000 'Activates the window.
Public Const AW_SLIDE = &H40000 'Uses slide animation. By default, roll animation is used.
Public Const AW_BLEND = &H80000 'Uses a fade effect. This flag can be used only if hwnd is a top-level window and only on 2000 and XP
[/tt]
In a form (this particular example slides the form):
[COLOR=blue[][tt]
Option Explicit

Private Sub Form_Load()
AnimateWindow Me.hwnd, 1000, AW_SLIDE Or AW_VER_NEGATIVE Or AW_ACTIVATE
End Sub

Private Sub Form_Unload(Cancel As Integer)
AnimateWindow Me.hwnd, 1000, AW_SLIDE Or AW_VER_POSITIVE Or AW_HIDE
End Sub

 
Ok... 2 things (WinXP Pro)

1) It DOES NOT work on windows with BorderStyle = 0
(This is a Splash Screen... I DO NOT WANT A @#*& BORDER!?!)

With Borders...
2) It starts and animates the form in the top left corner...
Even though I set the startup position to center screen.
It centers on the screen when animation completes...!?!

Is this normal?

Am I missing anything?

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Is this normal? No. Are you missing something? Dunno, it works fine here (which isn't much comfort to you, I know)
 
lol... (yeah thanks for your KIND words of comfort) ;-)

hmmmmm......

Are you using Windows XP Professional? (should it even make a difference)

And you are using VB5/6 right??? (Not .net)

...other than that... I have NO CLUE!?!?!?

thanks though...

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
VB6 Enterprise SP5. The particular code given above was tested on W98 (which has acknowledged issues with AnimateWindows, in that the image gets garbled in the animation area) and 2000. No XP at home (and I won't be near an XP box for 3 or 4 days), so I haven't been able to test it on that, so I guess that might be the issue.

Anyone awake out ther with XP who is prepared to test and report back results?
 
Ok... I figured it out...

1) It DOES NOT work on windows with BorderStyle = 0
It Does let you use regions

2) It starts and animates the form in the top left corner...
I had the start position tool bar turned off...
Turn it on and position the windows in the center manually...
Works fine now


here's your starz ;-)

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
CubeE101>>also... what are the const values for the flags?

CubeE101, here is a method which you can use to find the value of virtually any constant or find a type or function declaration if they are missing from API Viewer's.

Search the name of that constant preceding the keyword Const. For example do a google search on Const AW_SLIDE. You will see what you will see.
 
hmmmm... You Know... I never thought to do that...

Take a star... That'll same me a load of time in the future...

thanks

BTW... What is the Formula to clip the border off of a framed window... (I think it's in Pixels)

Private Sub Form_Load()
Dim X1 As Integer, Y1 As Integer, X2 As Integer, Y2 As Integer
X1 = ?
Y1 = ?
X2 = ?
Y2 = ?

SetWindowRgn Me.hwnd, CreateRectRgn(X1, Y1, X2, Y2), True
AnimateWindow Me.hwnd, 1000, AW_BLEND Or AW_ACTIVATE
End Sub


Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
You guys amaze me with the depth of your knowledge. Honest.

--Bill

Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy.
--Albert Einstein



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top