Custom Controls Transparency

akrocks
03-04-2006, 11:06 PM
For my own future refrence and your learning pleasure...

Ok, whatever... But I do think this is a safe place to hold my new found know-how, and I'm sure someone will find this useful. I finally figured out how to make a custom control transparent. Well, I guess not transparent, but it works good enough for me.

The experts who hang around here probably already know all I did was set the regions, but the point is: that I figured out how to do I on my own with the exception of lots of researching on finding the right methods.

So anyways, here is how I did it. Let's make a shape using PointF in the Paint Sub!


'Create your canvas, pens, brushes and whatever you will need for your basic drawings.
Dim Canvas As Graphics = Me.CreateGraphics
Dim myBrush As New SolidBrush(System.Drawing.SystemColors.Info)
Dim newFillMode As FillMode = FillMode.Winding

'Now, lets make our shape.
Dim myShape() as PointF = { _
New PointF(8, 8), _
New PointF(12, 4), _
New pointF(16, 8), _
new pointf(16, 12), _
New PointF(12, 16), _
New PointF(8, 12), _
New PointF(8, 8) _
}
'By the way, I just made that shape as I was typing this up... So not really sure what it looks like o_O

'Create a new graphics path (Not really sure if this is nessisary but it keeps our original shape safe)
Dim msPath As New Drawing2D.GraphicsPath
msPath.AddLines(myShape)

'Now we make a region as the path.
Dim Rgn As New Region(msPath)
'And set the control to the new region.
Me.Region = Rgn


'Draw the shape
Canvas.FillPolygon(myBrush, myShape, newFillMode)

'Clean up
Canvas.Dispose()


And there you have it! Mock up Transparent Controls from shapes! Hope this helps you, because it really helped me! Happy control building!

Iceplug
03-05-2006, 06:59 AM
Yes, very good. There's only one problem... I see that you do have the GraphicsPath and the Region part, but the thing is that you don't have to create a full region to simply empty and then put in a GraphicsPath... rather, a constructor for the region will create a region based on the graphicspath you supply it.

(I personally think GraphicsPath and Region should be collapsed into one object, but sadly I'm not behind the driver's wheel)

akrocks
03-05-2006, 09:19 AM
Ahh, Ok! Thanks iceplug! I didn't realize that. That makes things alot simpiler, really.
And I totally agree about putting the two in the same object.. I think.... I can't really think of any good reasons why not.

*updates code*

Gaal
03-07-2006, 02:51 PM
This looks like it may hold the key to my lack of understanding in the area of regions and shapes and such. There are still a lot of confused faces showing up, but it looks like what you're doing for your controls could also work for whole forms. I'd be interested in seeing an example of this in action.

akrocks
03-07-2006, 03:10 PM
Yup! I defently does work on forms.. Check it out! Just create a new form and set the borderstyle to none and paste this in

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
'Create your canvas, pens, brushes and whatever you will need for your basic drawings.
Dim Canvas As Graphics = Me.CreateGraphics
Dim myBrush As New SolidBrush(System.Drawing.Color.Tomato)
Dim newFillMode As FillMode = FillMode.Winding

'Now, lets make our shape.
Dim myShape() As PointF = { _
New PointF(7, 1), _
New PointF(238, 1), _
New PointF(238, 2), _
New PointF(240, 2), _
New PointF(240, 3), _
New PointF(241, 3), _
New PointF(241, 4), _
New PointF(242, 4), _
New PointF(242, 6), _
New PointF(243, 6), _
New PointF(243, 40), _
New PointF(242, 40), _
New PointF(242, 42), _
New PointF(241, 42), _
New PointF(241, 43), _
New PointF(240, 43), _
New PointF(240, 44), _
New PointF(238, 44), _
New PointF(238, 45), _
New PointF(228, 45), _
New PointF(228, 65), _
New PointF(209, 45), _
New PointF(6, 45), _
New PointF(6, 44), _
New PointF(4, 44), _
New PointF(4, 43), _
New PointF(3, 43), _
New PointF(3, 42), _
New PointF(2, 42), _
New PointF(2, 40), _
New PointF(1, 40), _
New PointF(1, 6), _
New PointF(2, 6), _
New PointF(2, 4), _
New PointF(3, 4), _
New PointF(3, 3), _
New PointF(4, 3), _
New PointF(4, 2), _
New PointF(6, 2) _
}


'Create a new graphics path (Not really sure if this is nessisary but it keeps our original shape safe)
Dim msPath As New Drawing2D.GraphicsPath
msPath.AddLines(myShape)

'Now we make a region as the path.
Dim Rgn As New Region(msPath)
'And set the control to the new region.
Me.Region = Rgn


'Draw the shape
Canvas.FillPolygon(myBrush, myShape, newFillMode)

'Clean up
Canvas.Dispose()
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum