?
Solved

How can I draw a line on top of other controls

Posted on 2002-03-22
12
Medium Priority
?
287 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 1200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: 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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

800 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