Solved

How can I draw a line on top of other controls

Posted on 2002-03-22
12
284 Views
Last Modified: 2010-05-03
How can I draw a line on top of other controls?
0
Comment
Question by:marti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Macro: print each sheet to pdf using ExportAsFixedFormat 11 64
Copy a row 12 69
VBA: copy range dynamically based on config sheet v2 3 55
Collapse and expand table in Word 2010 2 100
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

749 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