Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1514

[VB6] Bi-Color Dot/Dash using drawing methods

$
0
0
Most of us are already aware of the limitations of DrawStyle with the VB6 drawing methods (Line and Circle). The main one being that even a "dot" is more of a "dash" and more importantly that these are really just for "hairline" drawing (DrawWidth = 1).

But I have seen few examples showing how you can draw such dotted/dashed hairlines using two colors instead of a single interrupted ForeColor. Well there is a fairly simple trick that involves using one GDI call along with assigning FontTransparent = False.

Here is some code that draws a bunch of randomly placed and sized bi-colored circles against a contrasting checkerboard background. Getting it all working right is a little tricky because it requires some guessing about the GDI calls that VB6 does under the covers statement by statement:

Code:

Option Explicit

Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long

Private Declare Function SetBkColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long

Private Unit As Single

Private Function ColorRGB(ByVal Color As OLE_COLOR) As Long
    ColorRGB = Color
    If Color < 0 Then ColorRGB = GetSysColor(Color And &HFFFF&)
End Function

Private Sub Form_Load()
    Unit = ScaleWidth / 100
    DrawWidth = 1
    DrawStyle = vbDot
    ForeColor = vbBlue
    FillColor = vbYellow
    tmrCircle.Enabled = True
End Sub

Private Sub tmrCircle_Timer()
    Dim CenterX As Single
    Dim CenterY As Single
    Dim Radius As Single
   
    AutoRedraw = True
    'Use our FillColor as a "second ForeColor" for drawing:
    FontTransparent = False
    SetBkColor hDC, ColorRGB(FillColor)
    CenterX = (Int(Rnd() * 50) + 25) * Unit
    CenterY = (Int(Rnd() * 30) + 15) * Unit
    Radius = (Int(Rnd() * 25) + 15) * Unit
    Circle (CenterX, CenterY), Radius
    AutoRedraw = False
End Sub

Name:  sshot.png
Views: 5
Size:  12.5 KB

After letting it run a while

Why might you want to do this? Well the possibilities abound, but I was making use of the new capability starting in Windows 8 of layered child windows to draw and display a "reticle" over preview windows from USB imaging instruments such as microscopes. Here's a snippet of a screen capture from a microscope aimed at a metal ruler:

Name:  sshot snippet.png
Views: 8
Size:  37.2 KB


Requirements

As far as I can tell this is just plain old GDI and works all the way back to Windows 95's original release.

Of course to use it for something like a transparent reticle over a webcam preview you'd need Windows 8 or later.
Attached Images
  
Attached Files

Viewing all articles
Browse latest Browse all 1514

Trending Articles