Drawing Transparent Objects

Posted on 2005-04-05
Last Modified: 2012-06-21
How can I draw a semi-transparent line (Like highlighting) across a form.
I'm currently using formn.Line (x1, y1-(x2, y2), color, but it only draws lines of a solid color.
How can I make that line semi-transparent, so you can still see some of the stuff behind it?

Thanks in advance for the help!

Question by:Technokid
    LVL 5

    Expert Comment

    I dont think yoiu can make it semi-transparent, but you could make a it dotted like this

    <line control>.BorderStyle = 3


    line1.BorderStyle = 3

    hope that helps

    LVL 13

    Accepted Solution

    you can use the BitBlt Api and use vbSrcPaint for the copy style (the last Parameter of the BitBlt)

    This is how you do it

    Create a project and add a picturebox (Picture1)

    paste the following code in form1:

    Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

    Private Sub Highlight()
    Dim tmpStr As String
    Dim txtWidth As Long
    Dim txtHight As Long

    tmpStr = "This is a tryout text"

    Picture1.Visible = False
    ' set the scalemode to pixel
    Form1.ScaleMode = 3
    Picture1.ScaleMode = 3
    ' let the control do the redrawing of itself
    Form1.AutoRedraw = True
    Picture1.AutoRedraw = True
    ' Picture1 is used for highlighting
    Picture1.Top = 0
    Picture1.Left = 0
    Picture1.Width = Form1.ScaleWidth
    Picture1.Height = Form1.ScaleHeight
    ' Set the color of the highlight
    Picture1.BackColor = vbYellow
    ' Get the text dimentions
    txtWidth = Form1.textWidth(tmpStr)
    txtheight = Form1.TextHeight(tmpStr)
    ' draw a text to the form
    Form1.Print tmpStr
    ' Do the highlight
    BitBlt Form1.hDC, 0, 0, txtWidth, txtheight, Picture1.hDC, 0, 0, vbSrcAnd ' try changing this setting and see what happens
    End Sub

    Author Comment


    That works like a charm!!!

    Now, 1 question -
    I've modified the code so that it will "highlight" the area under the cursor, as the mouse is moved and the mouse button is pressed.  If I move the cursor at a medium or lower speed, everything looks fine. But, if I zip it across the screen, the "highlighting" is broken up.  Can you think of anything off the top of your head that would help prevent those gaps between the drawn squares?


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now