Transparent Rectangle In User Control

Tech-Archive recommends: Speed Up your PC by fixing your registry



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

.



Relevant Pages

  • Project Error
    ... Private Declare Sub Sleep Lib "Kernel32" ... Dim strDataSrc As String ...
    (microsoft.public.vb.bugs)
  • Re: How Much Longer Can SRians Ignore Their Fundamental Error.
    ... ecc As Variant ... Dim lightspeed, position, circlerun, Pref, prefmod As Integer, ... Private Sub Combo12_Change ... Erase Vangle ...
    (sci.astro)
  • Re: How Much Longer Can SRians Ignore Their Fundamental Error.
    ... ecc As Variant ... Dim lightspeed, position, circlerun, Pref, prefmod As Integer, ... Private Sub Combo12_Change ... Erase Vangle ...
    (sci.physics.relativity)
  • Re: Rearrange desktop
    ... Dim m_htTitlebar ' used in geom calcs ... Const GWL_STYLE = ... Sub RepositionDesktopIcons() ... Public Property Let Mask ...
    (microsoft.public.scripting.vbscript)
  • Font and DC - strange problem
    ... I am trying to set a Font to a DC, but something strange happens: The font is set correctly, but all fonts on other controls change to some strange system font... ... Public Sub DrawTextWEx ... Dim tLF As LOGFONT ...
    (microsoft.public.vb.general.discussion)