Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2048
  • Last Modified:

How to draw a semi-transparent line on a picture control in VB6

I have a picture control which has a gradient filled background.

This picture control is to be used as the background (and container) of a gant-chart style calendar which is drawn on using VB code.

We draw on top of the picture, several vertical and horizontal lines; vertical lines seperate hours, and the horizontal ones seperate different resources which can be scheduled.

Currently, the lines are drawn in a single colour (actually, 2 colours for a 3D effect) but I want them to be drawn semi-transparent, so that the gradient fill colour is shown through the line. This should allow for a more realistic 3D effect.

The whole application is already working, so please dont suggest re-writing in .Net etc as at this stage it is not possible.

Obviously, using the 2 colours and a single coloured / non-gradient background looks fine, but it looks much nicer with a gradient fill, and our clients have requested it.

Many thanks,

1 Solution
SamEdneyAuthor Commented:
To give you some idea of what I am trying to do.... this is the layout.

             1pm     2pm      3pm     4pm
Res 1 |          |           |           |           |
Res 2 |          |           |           |           |
Res 3 |          |           |           |           |
Res 4 |          |           |           |           |
why dont you make it in MSPaint and add that as your background image and then you can draw over that.
SamEdneyAuthor Commented:
No can do. The screen does not always show the same number of resources, or days. It is totally customisable by the user. It has to be dynamic. Also, the gradient colour is definable.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

You can use the flat GDI+ API to draw alpha blended lines in VB6.

Here are some references:

"Alpha Blending Lines and Fills"

Here is a comprehensive GDI+ sample project from psc.com

And here is a GDI+ Type Library (used in the example below)

And the example of drawing alpha lines on a picturebox:

'Add a reference to GDIPlus.tlb

Option Explicit

'Needed to close GDI+
Private token As Long

Private Sub Form_Load()
   ' Load the GDI+ Dll
   Dim GpInput As GdiplusStartupInput
   GpInput.GdiplusVersion = 1
   If GdiplusStartup(token, GpInput) <> Ok Then
      MsgBox "Error loading GDI+!", vbCritical
      Unload Me
   End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
   ' Unload the GDI+ Dll
   Call GdiplusShutdown(token)
End Sub

Private Sub Command1_Click()

    Dim graphics As Long, bitmap As Long
    Dim lngHeight As Long, lngWidth As Long
    Dim opaquePen As Long, semiTansPen As Long
    Dim stat As GpStatus
    ' Initialize the graphics class using Picture1.hDC
    stat = GdipCreateFromHDC(Picture1.hdc, graphics)
    ' Create 2 pens for line drawing, one solid, one with 50% alpha blending
    'ColorARGB takes: Alpha, Red, Green, Blue
    'The solid blue pen
    Call GdipCreatePen1(ColorARGB(255, 0, 0, 255), 15, UnitPixel, opaquePen)
    ' Has 50% alpha blending
    Call GdipCreatePen1(ColorARGB(128, 0, 0, 255), 15, UnitPixel, semiTansPen)
    ' Draw an opaque line over the image
    stat = GdipDrawLine(graphics, opaquePen, 0, 20, 300, 20)
    ' Draw the semi-transparent line over the image
    stat = GdipDrawLine(graphics, semiTansPen, 0, 40, 300, 40)
    ' Draw the same semi-transparent line, but with gamma correction
    stat = GdipSetCompositingQuality(graphics, CompositingQualityGammaCorrected)
    stat = GdipDrawLine(graphics, semiTansPen, 0, 60, 300, 60)
    ' Cleanup
    Call GdipDeletePen(opaquePen)
    Call GdipDeletePen(semiTansPen)
    Call GdipDeleteGraphics(graphics)
End Sub

Private Function ColorARGB(ByVal alpha As Byte, ByVal Red As Byte, ByVal Green As Byte, ByVal Blue As Byte) As Long
   Dim bytestruct As COLORBYTES
   Dim result As COLORLONG
   With bytestruct
      .AlphaByte = alpha
      .RedByte = Red
      .GreenByte = Green
      .BlueByte = Blue
   End With
   LSet result = bytestruct
   ColorARGB = result.longval
End Function

Ioannis ParaskevopoulosCommented:
Why don't you draw a line and set its border style property to dot or dash?
I suggest to draw smal pieces of vertical and horizontal 3D lines (say 3x10/10x3 pixels) in any graphics editor (MSPaint/PhotosShop etc). Then use AlphaBlend function from msimg32.dll to draw them blended on the BG one-by-one. See my sample at http://freevbcode.com/ShowCode.Asp?ID=1501 on using msimg32.dll
SamEdneyAuthor Commented:
Wow. Looks as though the time for completing questions has decreased a bit! I still havent even tried this, so have no idea if it works. Will post back any info though. Thanks for the help so far....!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now