Link to home
Start Free TrialLog in
Avatar of marti
marti

asked on

How can I draw a line on top of other controls

How can I draw a line on top of other controls?
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

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


GOOD LUCK
ASKER CERTIFIED SOLUTION
Avatar of ameba
ameba
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marti
marti

ASKER

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?
Avatar of marti

ASKER

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
    PosLine
End Sub

Private Sub PosLine()
    On Error Resume Next
    Me.ctlLine1.Move 0, 0, ScaleWidth, ScaleHeight
    Me.ctlLine1.ZOrder 0
End Sub
Avatar of marti

ASKER

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...
Avatar of marti

ASKER

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.
Avatar of marti

ASKER

It gave me a good direction...
Thanks.

>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
    Cls
    UserControl.PaintPicture New_Picture, 0, 180
    UserControl.Print "Hello"
    UserControl.AutoRedraw = False
    Set UserControl.MaskPicture = UserControl.Image