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

How do show parts of a picture as transparent?

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
AU
I am trying to show a large flashing arrow head over the top of everything else (like a very big mouse pointer).
Selectively repainting doesnt work because some controls are still on top.

Using the Drawing a masked pic from an Imagelist to a picturebox works but I cant Zorder the picture box over a frame

I had some success by putting the original pic on a small form using black to mask the shape of the form. New form sits over everything on the main form OK

Is there any way of making sure a picturebox is really over the top of everything else or is are there other controls I should be using?
 
In VB there are three 'layers' associated with forms (and containers). The back layer is the form's basic drawing area, where the results of graphics methods appear. The middle layer is where graphical controls appear. The front layer is where all non-graphical controls appear. Anything in one layer covers anything in the layer behind. The ZOrder method arranges controls only within the layer where the control appears

So a picturebox cannot be guaranteed to be above everything because it is in the middle of these three layers. And any other graphical control would also be in that layer.

Frankly I think I'd be tempted to go with your "original pic on a small form using black to mask the shape of the form. New form sits over everything on the main form OK" idea. You can make a form tranparent pretty cheaply, as demonstrated in my example code in thread222-1034117
 
>So a picturebox cannot be guaranteed to be above everything because it is in the middle of these three layers.

PictureBox is a windowed control and it lies in the topmost layer. It's the Image control which lies in the middle layer. You can make a PictureBox stay on top of other controls.

Make sure you don't draw other controls inside the PictureBox, as in this case it becomes their container and cannot sit on top of them.

You can give your PictureBox an arrow shape by creating a polygon region using CreatePolygonRgn function and assigning it to the PictureBox using SetWindowRgn function.
 
Hmmmm - Yes the picture box picture could be ZOrdered over a frame. What I was originally doing was in effect making a copy of the picture in picturebox via a listimage and painting it with transparent black parts on the form, naturally not over other controls.

Is there maybe a way of changing the edges of the picturebox corresponding to the black areas of the picture as is done with the transparent form example.

Strongm, I an intrigued by a line in your code of your transparent form example:-

BackColor = RGB(0, 0, 0)

What is this "backcolor"
It is not dimmed nor referred to in any declares yet the project is option explicit and it doesnt give an error.
I would be grateful for an explanation.

In that example it is interesting that the black areas of any control on the form make the form transparent too.
 
Ted,

The code assumes that you don't need to fully qualify the reference in the forms code (BackColor is the same as FormX.BackColor or Me.BackColor in this case).

It is mentioned further down in the referenced thread with regard to hWnd.

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
>Is there maybe a way of changing the edges of the picturebox corresponding to the black areas of the picture as is done with the transparent form example.

I'm afraid there is no function in GDI which can shape a control based on a color mask.

A control can only be shaped using window regions, as I told above.

See an example below. It requires a picturebox on the form.
___
[tt]
Private Declare Function BeginPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function Polyline Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
Private Declare Function EndPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function StrokeAndFillPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_Load()
Dim pt(6) As POINTAPI, hRgn As Long
pt(0).x = 0: pt(0).y = 0
pt(1).x = 71: pt(1).y = 71
pt(2).x = 37: pt(2).y = 71
pt(3).x = 57: pt(3).y = 111
pt(4).x = 44: pt(4).y = 116
pt(5).x = 24: pt(5).y = 76
pt(6).x = 0: pt(6).y = 100
Picture1.Width = 1095
Picture1.Height = 1815
Picture1.BorderStyle = 0
Picture1.BackColor = vbWhite
Picture1.AutoRedraw = True
Picture1.DrawWidth = 2
BeginPath Picture1.hdc
Polyline Picture1.hdc, pt(0), 7
EndPath Picture1.hdc
StrokeAndFillPath Picture1.hdc
hRgn = CreatePolygonRgn(pt(0), 7, 1)
SetWindowRgn Picture1.hWnd, hRgn, True
End Sub[/tt]
___

Besides polygons and other basic shapes, you can create more complicated regions using paths. You need to create a path on your picturebox, and convert that path using PathToRegion function. This allows you to create complex regions including text output.
See thread222-972212 for an example.
 
>it is interesting that the black areas of any control on the form make the form transparent too

Well ... yes.
 
>The code assumes that you don't need to fully qualify the reference in the forms code (BackColor is the same as FormX.BackColor or Me.BackColor in this case).

I have been using a form af Visual Basic ever since they introduced it (without even a version number I seem to remember) and never realised you could do that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top