Transparent Rectangle In User Control
- From: "Rico" <richard.copeman@xxxxxxxxxxxxxxxx>
- Date: Fri, 13 Feb 2009 16:20:30 -0000
Hi,
I have declared my own object as per the code below. What I am having difficulty with is trying to make the centre portion of the rectangle transparent. I want tobe able to see other controls that are placed on the form but in overlapping locations. The idea is for the LBBox object to behave like a border with a title in it.
Does anyone have any suggestions?
I have tried gfx.fillRectanlge() with a transparent brush but that only works if I don't draw the two outline rectangles, which defeats my purpose.
Anyway, here is the code:
Imports System.Windows.Forms
Imports System.Drawing
Public Class LBBox : Inherits UserControl
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Me.SuspendLayout()
Dim gfx As Graphics = e.Graphics
Dim penWidth As Integer = 1
Dim pen As Pen = New Pen(Color.Gray, penWidth)
Dim fontHeight As Integer = 10
Dim font As Font = New Font("Arial", fontHeight)
Dim brush As SolidBrush = New SolidBrush(BackGroundColor)
Dim textBrush As SolidBrush = New SolidBrush(Color.Black)
Dim fBrush As SolidBrush = New SolidBrush(Color.HotPink)
Dim shadowPen As Pen = New Pen(Color.White, penWidth)
Dim fontoffset As Integer = CInt(2 * fontHeight / 3) + 1
Dim yline As Integer = myY + fontoffset
Dim strsize As New SizeF
strsize = gfx.MeasureString(Text, font)
'Box to draw
Dim myRect As Rectangle = New Rectangle(0, fontoffset, _
Me.Size.Width - 2, _
Me.Size.Height - (fontoffset + 2))
'Clear background for text
Dim myfontRect As Rectangle = New Rectangle(5, 0, strsize.Width + 2, _
CInt(fontHeight * 3 / 2))
'Shadow box to look pretty :-)
Dim sxRect As Rectangle = New Rectangle(1, fontoffset + 1, _
Me.Size.Width - 2, _
Me.Size.Height - (fontoffset + 2))
gfx.DrawRectangle(shadowPen, sxRect)
gfx.DrawRectangle(pen, myRect)
gfx.FillRectangle(fBrush, myRect)
gfx.FillRectangle(brush, myfontRect)
gfx.DrawString(Text, font, textBrush, 6, 0)
Me.ResumeLayout()
End Sub
Private Sub LBBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'LBBox
'
Me.BackColor = System.Drawing.Color.Transparent
Me.Name = "LBBox"
Me.ResumeLayout(False)
End Sub
End Class
Thanks,
Rico
.
- Follow-Ups:
- Re: Transparent Rectangle In User Control
- From: Jeff Johnson
- Re: Transparent Rectangle In User Control
- Prev by Date: Re: TreeView.DropHighlight
- Next by Date: Re: Transparent Rectangle In User Control
- Previous by thread: TreeView.DropHighlight
- Next by thread: Re: Transparent Rectangle In User Control
- Index(es):
Relevant Pages
|