Results 1 to 5 of 5

Thread: [RESOLVED] Drawing on UserControl with Transparent BackStyle.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2016
    Posts
    28

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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:

    Name:  sshot.png
Views: 1663
Size:  6.0 KB
    Attached Files Attached Files

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,092

    Re: Drawing on UserControl with Transparent BackStyle.

    This works:
    thinBasic Code:
    1. Option Explicit
    2.  
    3. '--- design-time settings
    4. '    UserControl.Windowless = True
    5. '    UserControl.ClipBehaviour = 0-None
    6.  
    7. Private Sub UserControl_Initialize()
    8.     UserControl.BackStyle = 0 '0=Transparent 1=Opaque
    9. End Sub
    10.  
    11. Private Sub UserControl_Paint()
    12.     Line (200, 200)-(800, 800), RGB(0, 0, 0), BF
    13. End Sub
    14.  
    15. Private Sub UserControl_HitTest(X As Single, Y As Single, HitResult As Integer)
    16.     HitResult = vbHitResultHit
    17. End Sub
    cheers,
    </wqw>

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Drawing on UserControl with Transparent BackStyle.

    Quote Originally Posted by software07 View Post
    ...Controls on the Form should be visible apart from my drawing when the UserControl is over them. ...
    Quote Originally Posted by wqweto View Post
    This works:
    ...
    '--- design-time settings
    ' UserControl.Windowless = True
    ...
    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2016
    Posts
    28

    Re: Drawing on UserControl with Transparent BackStyle.

    Quote Originally Posted by dilettante View Post
    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:

    Name:  sshot.png
Views: 1663
Size:  6.0 KB
    Thanks it worked perfectly.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width