How can I draw a line on top of other controls

How can I draw a line on top of other controls?
Who is Participating?
amebaConnect With a Mentor Commented:
You should draw lines on the screen:

' Form1 code
Option Explicit
Private Type POINTAPI
        x As Long
        y As Long
End Type
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As _
    Long, lpPoint As POINTAPI) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x _
    As Long, ByVal y As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal _
    x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long

Private Sub Form_Load()
    Controls.Add("vb.textbox", "cmd1").Visible = True
End Sub

Private Sub Form_Click()
    Dim screendc As Long, pt As POINTAPI, ptdummy As POINTAPI
    screendc = GetDC(0)         ' get screen dc
    ClientToScreen Me.hwnd, pt  ' convert point 0,0 to screen coordinate
    MoveToEx screendc, pt.x, pt.y + 4, ptdummy
    LineTo screendc, pt.x - 4 + ScaleWidth / 15, pt.y - 4 + ScaleHeight / 15
    LineTo screendc, pt.x - 4 + ScaleWidth / 15, pt.y + 4
    LineTo screendc, pt.x, pt.y + 4
End Sub
Roshan DavisCommented:
U can use the APIs for the above operation:

EnumChildWindows : For enumerating all the child windows of one main window

GetDC : getting the device context of the specified child window

martiAuthor Commented:
Ameba, thank you for the code. I have two problems though. Sometimes the lines are on top of other forms and sometimes the lines are not drawn on top of the text control. Can you help?
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

martiAuthor Commented:
I think that this question worths more, so I will make ammendment later on...
Maybe you can create usercontrol, try this:

' UserControl name=ctlLine, BackStyle=Transparent
'   add Line control, name=Line1
'   add this code
Option Explicit

Private Sub UserControl_Resize()
    On Error Resume Next
    With Line1
        .BorderColor = vbHighlight
        .BorderWidth = 2
        .X1 = 0
        .Y1 = 0
        .X2 = UserControl.ScaleWidth - 15
        .Y2 = UserControl.Height - 15
    End With
End Sub

' Form1, add usercontrol (ctlLine1) and some other controls
Option Explicit

Private Sub Form_Resize()
    On Error Resume Next
End Sub

Private Sub PosLine()
    On Error Resume Next
    Me.ctlLine1.Move 0, 0, ScaleWidth, ScaleHeight
    Me.ctlLine1.ZOrder 0
End Sub
martiAuthor Commented:
User controls are heavy. Any ideas?
Windowless UserControls are not that heavy, just set the Windowless property to True.
Also, if you have more lines, all can use the same UserControl.

Without UserControl, redrawing line via APIs would be too complex - each control would require study when line must be redrawn, e.g. when text is changed in textbox, when button receives mousedown...
APIs are simple if there are no other controls to be refreshed or other forms covering parts of the form...
martiAuthor Commented:
If I set the Windowless property to True I can not draw on top of other controls.
So I would like to paint after everything else has been repainted and on top of it. The first code you sent me will probably work, but how can I make sure which areas of the form are currently visible. I will reward you the points on your next answer.
Please, do not award me the points, since it doesn't work as you want it.
You can delete the question, if you want.

The code to deal with areas (regions) is too complicated...
The only other idea I have, is to use PictureBox and make it transparent (except where there is a line).  But that also takes hwnd and code is more complex than with usercontrol, which supports Backstyle = 0 'transparent.
martiAuthor Commented:
It gave me a good direction...

>Actually I would like to draw frames on top of other controls

I use usercontrol (ctlMarker) with 4 Line controls to draw frame around controls.

But, you don't have to use Line controls - it is possible to draw on usercontrol, or draw pictures or - draw text  (to create 'label' with transparent background, which is 'on top' of other controls), small snippet:

    UserControl.AutoRedraw = True
    UserControl.PaintPicture New_Picture, 0, 180
    UserControl.Print "Hello"
    UserControl.AutoRedraw = False
    Set UserControl.MaskPicture = UserControl.Image
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.