[RESOLVED] Drawing on UserControl with Transparent BackStyle.
I am trying to draw on a UserControl, but I want to have other regions apart from my drawing transparent, so the Background/Controls on the Form should be visible apart from my drawing when the UserControl is over them. As of now when I set the BackStyle Opaque the drawing is visible but when I change it to transparent it is not.
Code:
Private Sub UserControl_Initialize()
UserControl.BackStyle = 0 '0=Transparent 1=Opaque
Line (200, 200)-(800, 800), RGB(0, 0, 0), BF
End Sub
Last edited by software07; Apr 16th, 2018 at 10:38 PM.
Reason: Resolved
Re: Drawing on UserControl with Transparent BackStyle.
Easy enough, but there are limits to what you can do during a UserControl's Initialize event. For one thing the control isn't fully created yet, and even during InitProperties/ReadProperties it is not fully sited yet.
You can trap and do this during the first Show event and often in the Resize event. For example:
Code:
Option Explicit
Private Sub Draw()
AutoRedraw = True
Cls
FillStyle = vbFSTransparent
DrawWidth = 1
DrawStyle = vbDot
Line (0, 0)-(ScaleWidth - 15, ScaleHeight - 15), vbRed, B
DrawStyle = vbSolid
DrawWidth = 3
Line (180, 180)-(ScaleWidth - 180, ScaleHeight - 180), vbCyan, B
DrawWidth = 7
Line (420, 420)-(ScaleWidth - 420, ScaleHeight - 420), vbBlack
FillStyle = vbDiagonalCross
FillColor = vbBlue
DrawWidth = 8
Line (420, Int(ScaleHeight / 2))-(Int(ScaleWidth / 2), ScaleHeight - 420), vbYellow, B
AutoRedraw = False
Set MaskPicture = Image 'Render with mask transparency.
End Sub
Private Sub UserControl_Initialize()
MaskColor = &HFEFEFE 'Some unique color we don't otherwise use.
BackColor = MaskColor
End Sub
Private Sub UserControl_Resize()
Draw
End Sub
However even at best there are issues with this simple form of transparent rendering, such as interference with ClearType font rendering for many display drivers:
I'm pretty sure that a Windowless control can't be drawn over other controls (other than other windowless controls) so doesn't quite work as the OP specified.
Re: Drawing on UserControl with Transparent BackStyle.
Originally Posted by dilettante
Easy enough, but there are limits to what you can do during a UserControl's Initialize event. For one thing the control isn't fully created yet, and even during InitProperties/ReadProperties it is not fully sited yet.
You can trap and do this during the first Show event and often in the Resize event. For example:
Code:
Option Explicit
Private Sub Draw()
AutoRedraw = True
Cls
FillStyle = vbFSTransparent
DrawWidth = 1
DrawStyle = vbDot
Line (0, 0)-(ScaleWidth - 15, ScaleHeight - 15), vbRed, B
DrawStyle = vbSolid
DrawWidth = 3
Line (180, 180)-(ScaleWidth - 180, ScaleHeight - 180), vbCyan, B
DrawWidth = 7
Line (420, 420)-(ScaleWidth - 420, ScaleHeight - 420), vbBlack
FillStyle = vbDiagonalCross
FillColor = vbBlue
DrawWidth = 8
Line (420, Int(ScaleHeight / 2))-(Int(ScaleWidth / 2), ScaleHeight - 420), vbYellow, B
AutoRedraw = False
Set MaskPicture = Image 'Render with mask transparency.
End Sub
Private Sub UserControl_Initialize()
MaskColor = &HFEFEFE 'Some unique color we don't otherwise use.
BackColor = MaskColor
End Sub
Private Sub UserControl_Resize()
Draw
End Sub
However even at best there are issues with this simple form of transparent rendering, such as interference with ClearType font rendering for many display drivers: