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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

capture the background behind a form 8

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
How can you capture the background behind a form (such as desktop and other windows) and display it in the form, in a picture box to be exact...?

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

 
try using an API.

look at drJavaJoes 1st response in thread222-516967

i believe this code is also in MSDN and online at microsoft

good luck If somethings hard to do, its not worth doing - Homer Simpson
 
ADoozer, I think DrJavaJoe's comments were geared toward capturing the Actual form... I want to capture what is behind the form... however, it is a very good start, and thanks for the post. I will try your other solution and dig around in MSDN a bit and see what I can find.

To be a little More specific...
I want to capture the Background area (using the running program, via API's, with no use of KeyBoard) the size of the window, Capture the form using a similar method, then fade the bakground to the the Form... then using the same proceedures, Fade the form Out into the background on termination...
This will be done using a Picture box the size of the form... that will be "Visible" when the effects are being Preformed, and not "Visible" when executing the program.

Does anyone have any links to maybe some type of API for the Desktop Object?

or any other Ideas would also be appriciated...

Thank you for your post,
-Josh

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

 
the code captures the image of a window or desktop and a few other things(there are 7 options in the code) and turns it into a bitmap...

how exactly are you wanting to do this... is your form full screen or is it a small window and you want to fade the actual desktop?!?

what i was thinkin is, if your app is full screen just copy the desktop to the forms picture property and fade from there and mimic your application in a picture box on the form...

maybe you have a better idea but its just a thought!!

good luck If somethings hard to do, its not worth doing - Homer Simpson
 
No...

The form is about 1/4 of the screen...

I am using GDI32 and USER32 to create "Non-Standard" forms... (no title bar or borders...)

The fade is done with a Double Buffer Method...

I was planning on doing the following...

1) Capture the Background (this could be done with that code at the begining of the Form_Paint event)

2) Create a Pic-Box the size of the form and Fill W/ the Background.

While the Form_Paint event executes... the form is drawn...

3) Now use some method to grab A snapshot of the form Behind the Picture Box...
(The easiest way to do this is to Set the form up the way you want it and Manually get a Snap-Shot of it Via Alt-PrntScrn...
Then Create another PictureBox And Preload this image into it within the IDE BEFORE compilation) this will be invisible...

4) Use a method to Fade from the Backgrond in Pic-Box A to the Form Image in Pic-Box B, all within Pic-Box A (Pic-Box B is still not visible)

5) Now that the form has faded in... Hide Pic-Box A...
(now Pic-Box A & B are both hidden)

6) Execute Program...

On termination...

7) Grab Backgrond from behind Form...

8) insert into Pic-Box B

9) make Pic-Box A visible

10) repeat above method to fade from Form-Image (Pic A) to BG (now Pic B) still within Pic A

11) terminate the program...

Simple enough? ;-)

I could still use a little help with grabing the background f/ behind the form...

also I know how to fade graphics... But I'm not the best at working with DIB handles... any good tutorials or examples would be very helpful also... Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Reading this post I recalled that in old days of VB3 (actually then I started learning VB I choose VB3 for being simpler. Still think VB3 help was much better them MSDN now).
I used this to create transparent form (using mask image).
(BTW file date Dec 1997)
I looked at it, changed some declarations, and now it works under VB6.
So here's portion of code:
API declarations:
Code:
Private Declare Function GetDesktopWindow& Lib "user32" ()
Private Declare Function GetDC& Lib "user32" (ByVal hwnd 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)
 Const SRCCOPY = &HCC0020       'always 1
 Const SRCAND = &H8800C6        'AND
 Const SRCPAINT = &HEE0086      'OR

the code that draw thing I intended:
Code:
Private Sub DrawTheThing()
    ScaleMode = 3   'PIXELS: BitBlt needs 'em
    'get Descktop
    dskTop = GetDesktopWindow()
    dskTopDC = GetDC(dskTop)
    PicWidth = Picture1.Width
    PicHeight = Picture1.Height

    picX = Form1.Left / Screen.TwipsPerPixelX     'I need pixels once more
    picY = Form1.Top / Screen.TwipsPerPixelY      ': position on window

    Form1.Hide
    'pause to hide window?
    DoEvents
    Form1.Refresh
    '1)get background from desktop; 2) AND mask; 3) OR image
    t = BitBlt(Picture1.hDC, 0, 0, PicWidth, PicHeight, dskTopDC, picX, picY, SRCCOPY)
    t = BitBlt(Picture1.hDC, 0, 0, PicWidth, PicHeight, mask.hDC, 0, 0, SRCAND)
    t = BitBlt(Picture1.hDC, 0, 0, PicWidth, PicHeight, MainPicture.hDC, 0, 0, SRCPAINT)
    'and show if after
    Form1.Visible = 1
    Form1.Refresh
End Sub

Picture1, MainPicture and mask are pictureboxes (MainPicture and mask - with image to be masked and mask). Picture1 is the only thing visible. Form BorderStyle set to None.

Hope this helps.
 
Hey, thanks...

That's Exactly what I was looking for...

so... I guess there is no way to capture the BG w/o hiding the form... right?

...and if you Don't hide the form you end up w/ a snap shot of the form, correct?

Is there a way to use BitBlt to create a Fading effect...

Similar to menus in MS Office 2000 & XP? Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Fading effect? You may want to consider the AlphaBlend API call...(effectively a drop in replacement for BitBlt). It is not available on W95...
 
AlphaBlend?

How do you use it (what DLL?... user32, gdi32?)

Not available for W95...

So It is available for 98 through XP right? ... NT 4?

the range of machines this will be effective on is...
WinNT 4.0 , Win 2000, Win XP
All "Should" be on XP within the next 6 months to a year...

when you say "effectively a drop" ... does it also have the same declaration (or similar) to BitBlt?

Thnx for the Heads Up Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
The following code fades-in and -out the main form as you start and end the program. It uses SetLayeredWindowAttribute API which enables alpha blending. This function is only supported on Windows 2000 and XP.
---
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 5
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 -5
SetLayeredWindowAttributes hwnd, 0, Opacity, LWA_ALPHA
Refresh
Next
End Sub
---
See also thread222-493597.
 
Hypetia...
YOU ROCK!

That Kix-azZ

X-Actly What I wanted to do... But hella-easier...

Thnx ALOT

Since I can Only give you one Star... here are Ten More...

star.gif
star.gif
star.gif
star.gif
star.gif
star.gif
star.gif
star.gif
star.gif
star.gif
Hypetia

Thnx Again... ;-) Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
BTW, Hypetia...

>>This function is only supported on Windows 2000 and XP.

MSDN says (for all 3)...
Minimum operating systems: Included in Windows 95, Windows NT 3.1

I'll try it on my NT machine at work 2morrow, and let ya'll know the outcome (for those interested)...
(It works great on my XP here @ home) Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
star.gif
star.gif
star.gif
star.gif
star.gif
star.gif
star.gif
star.gif
star.gif
star.gif
Hypetia
My head completely blown up when I saw this at the first glance.
[bomb]
Thanks for the complements.[rainbow]

As far as the support of this function is concerned, I think that alpha blending and 32-bit bitmaps are only supported on Windows 2000 and XP. Do tell me if you find it working on Windows 98 or NT.
I am unable to comment on the statement:
Included in Windows 95, Windows NT 3.1

Anyway, thanks again.[smile]
 
I got excited but unfortunately it doesn't run on '98 and gives an overflow on '2000 Pro. (watch for the 'Opactiy'. Hypetia was probably getting too excited keying this in).
 
bigalbigal,
What do you mean by: watch for the 'Opactiy'.

Opasity is the Opaque value of the form...

A value between 0 and 255...

127 is half transparent...

You can use a 'hard' value of 127 and and you will be able to see the desktop through your form as you work...

I don't think I understand your comment...

It 'should' work on 2000 though... This is how Office 2000 fades in and out the Menus...

Are you saying it did not... ?

Try this:

1) Open VB6

2) New Project: Standard EXE

3) double click the form to got to the code...

4) delete the Form_Load sub that is generated...

5) copy the code above and paste it in the form code...

6) DO NOT modify it...

7) try to run it...

If it does not work, Please let us know

I only have XP and NT ... It works great on XP(NT did not have the same luck :-()

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

 
What do you mean by: watch for the 'Opactiy'

I think he meant, 'Opactiy' is spelled wrong and if you paste this directly into you project, you have to correct it. It is declared as 'Opactiy' but used in the code as 'Opacity'.

The overflow error is because Opacity is declared as a byte, change it to an Integer and it will work.

And Hypetia, that's pretty cool...I think I might use that, a star for you.
Rob
 
rdavis,
yep, that's excactly what I meant. Opasitityas was spleledd rwong in a couple of places. Thanks for the clarification
and thanks for the tip on declaring it as an integer. I will try it soon.

CubeE101,
That's excactly how I pasted the code, including deleting the 'Form Load'. It didn't work on 2000 probably because of the declaration as a byte.
I will try it again an report back. I am also about to try it on 'ME' shudder shudder...
BigAl
 
Nope, it didn't work on ME (does anything). It gives the same "Can't find DLL entry point SetLayeredWindowAttributes in user32.dll" as '98.
Now let me see, somewhere i've got a 386 running Win 3.1!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top