Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

transparentblt from VB6 form

237 views
Skip to first unread message

Johan Stäck

unread,
Aug 19, 2009, 10:27:25 AM8/19/09
to
I have a VB6 picturebox.
On this PB I want to perform a transparentblt of the visible contents of
a separate form.

I want this to work regardless of the visibility of the source form, but
I don't know how to make it work.

Any ideas?

Tia,

/Johan St�ck
Stockholm
Sweden

Nobody

unread,
Aug 19, 2009, 4:17:59 PM8/19/09
to
"Johan St�ck" <jo...@stack.se> wrote in message
news:7f2gedF...@mid.individual.net...

>I have a VB6 picturebox.
> On this PB I want to perform a transparentblt of the visible contents of a
> separate form.
>
> I want this to work regardless of the visibility of the source form, but I
> don't know how to make it work.

If the other form has AutoRedraw=True option or similar API, then perhaps.
See this article for how to capture another window:

How To Capture and Print the Screen, a Form, or Any Window
http://support.microsoft.com/default.aspx?scid=kb;en-us;161299

Johan Stäck

unread,
Aug 20, 2009, 4:18:42 AM8/20/09
to
Well, to get this to work it seems that the source form needs to be
visible and even on top.
(If not, I will get the visible part of the screen in the form's
position...)
I want it out of the way...

/Johan

Mike Williams

unread,
Oct 1, 2009, 4:51:34 AM10/1/09
to
"Johan St�ck" <jo...@stack.se> wrote in message
news:7f4f75F...@mid.individual.net...

> How To Capture and Print the Screen, a Form, or Any Window
> http://support.microsoft.com/default.aspx?scid=kb;en-us;161299
> Well, to get this to work it seems that the source form needs to
> be visible and even on top. (If not, I will get the visible part of the
> screen in the form's position...) I want it out of the way...

There are various ways of tackling this job, some of which are more complex
than others, and most of which have limitations of one kind or another, but
one very simple way which does have some limitations but which should be
okay for your needs is to print the Form you want to capture into an
invisible buffer picture box on the main Form and then transparently blit it
from there into the destination picturebox. The transparentblt function that
I've used did at one time have a memory leak, but that was fixed by MS ages
ago and it will be fine except on really very old systems. If that it a
problem for you then there are other ways of transparently blitting images.
The main restriction is that the Form you want to capture must not be
minimized and must not have its Visible property set to False, but it can be
positioned off the display by setting its Left property (or whatever) to
such a value that it cannot be seen (as in the example below). Here is an
example. For test purposes start a new project using two Forms and place a
Command Button and two Picture Boxes (one named picBuffer and the other
named picDest) on the main Form. You can place whatever controls you like on
the second Form (Form2) and you can also add code to draw stuff into it if
you wish, as long as in that case its Autoredraw property is set to True,
It's a long time since I wrote this code and I almost never actually use it,
and I can't remember what problems (if any) it might have had with certain
specific controls, but hopefully it should be fine for your own purposes.
You can use whatever you wish for Form2's BackColor as long as it matches
with the values in the call to the TransparentBlt function and as long as it
is not one of the colours you want to show. At the moment the code
transparently blits the result into the top left corner of the destination
picture box, since I don't know how large this is in your own project or
whether or not it is Autoredraw or where it is positioned or what else it
contains, but you can of course alter the code it so that it draws the
result at any other position you require in the destination. As I've said,
there are some limitations but hopefully it will suit your needs.

Mike

Option Explicit
Private Declare Function PrintWindow Lib "user32" _
(ByVal hwnd As Long, ByVal hdcBlt As Long, _
ByVal nFlags As Long) As Long
Private Declare Function TransparentBlt Lib "msimg32.dll" _
(ByVal hdc 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 nSrcWidth As Long, _
ByVal nSrcHeight As Long, ByVal crTransparent As Long) As Boolean
Private Const PW_CLIENTONLY As Long = 1

Private Sub Form_Load()
picBuffer.Visible = False
End Sub

Private Sub Command1_Click()
Dim pixWide As Long, pixHigh As Long
With picBuffer ' an otherwise unused picbox on main Form
.Visible = False
.BorderStyle = vbBSNone
.AutoRedraw = True ' a VB PictureBox on main Form
End With
With Form2 ' the Form to capture
.BackColor = RGB(254, 254, 254)
.Show
.Left = -.Width ' just to show it works when off screen
DoEvents
pixWide = .ScaleX(.ScaleWidth, .ScaleMode, vbPixels)
pixHigh = .ScaleY(.ScaleHeight, .ScaleMode, vbPixels)
picBuffer.Width = Me.ScaleX(pixWide, vbPixels, Me.ScaleMode)
picBuffer.Height = Me.ScaleX(pixHigh, vbPixels, Me.ScaleMode)
End With
PrintWindow Form2.hwnd, picBuffer.hdc, PW_CLIENTONLY
picBuffer.Refresh
picBuffer.PSet (0, 0), vbWhite
TransparentBlt picDest.hdc, 0, 0, pixWide, pixHigh, _
picBuffer.hdc, 0, 0, pixWide, pixHigh, RGB(254, 254, 254)
End Sub

0 new messages