Solved

How can I draw a line on top of other controls

Posted on 2002-03-22
12
271 Views
Last Modified: 2010-05-03
How can I draw a line on top of other controls?
0
Comment
Question by:marti
  • 6
  • 5
12 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 6890514
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
0
 
LVL 15

Accepted Solution

by:
ameba earned 300 total points
ID: 6890600
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
0
 
LVL 2

Author Comment

by:marti
ID: 6890958
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?
0
 
LVL 2

Author Comment

by:marti
ID: 6890971
I think that this question worths more, so I will make ammendment later on...
0
 
LVL 15

Expert Comment

by:ameba
ID: 6891422
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
0
 
LVL 2

Author Comment

by:marti
ID: 6891742
User controls are heavy. Any ideas?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:ameba
ID: 6891969
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...
0
 
LVL 2

Author Comment

by:marti
ID: 6892281
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.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6892337
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...
0
 
LVL 15

Expert Comment

by:ameba
ID: 6892352
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.
0
 
LVL 2

Author Comment

by:marti
ID: 6892566
It gave me a good direction...
0
 
LVL 15

Expert Comment

by:ameba
ID: 6893058
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
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

706 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

22 Experts available now in Live!

Get 1:1 Help Now