Line on top of a user-defined control on a PictureBox 
Author Message
 Line on top of a user-defined control on a PictureBox

Dear all,

In my project, i will load user-defined  controls on different
locations on a picturebox during run-time.

Then, upon a PictureBox_MouseDown followed by a PictureBox_MouseUp
event, an existing line will be loaded with its index increased and so
on.

How can i draw the lines on top of both PictureBox and my user-defined
controls? What happen is that the lines are drawn on top of PictureBox
but they are always below the user-defined controls?

I tried to use ZOrder but couldnt solve this.

I tried using
Set Line1.Container = TheUserControl

It gives a Runtime error '425' Invalid object use

Please help.

Thanks.

Albert



Wed, 29 Jun 2011 12:14:12 GMT  
 Line on top of a user-defined control on a PictureBox

Quote:
> Dear all,

> In my project, i will load user-defined  controls on different
> locations on a picturebox during run-time.

> Then, upon a PictureBox_MouseDown followed by a PictureBox_MouseUp
> event, an existing line will be loaded with its index increased and so
> on.

> How can i draw the lines on top of both PictureBox and my user-defined
> controls? What happen is that the lines are drawn on top of PictureBox
> but they are always below the user-defined controls?

> I tried to use ZOrder but couldnt solve this.

> I tried using
> Set Line1.Container = TheUserControl

> It gives a Runtime error '425' Invalid object use

> Please help.

You can draw the line using the windows API and using the PictureBox's
WindowDC. Here is a sample. To try it, add a PictureBox, place a
CommandButton on the PictureBox, and a Timer to Form1, and use the following
code:

Option Explicit

Private Declare Function GetWindowDC Lib "user32" ( _
    ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, _
    ByVal hdc As Long) 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, ByVal lpPoint As Long) As Long

Private Sub DrawLine(ByVal hWnd As Long, ByVal x1 As Long, _
    ByVal y1 As Long, ByVal x2 As Long, ByVal y2 As Long)

    Dim hWindowDC As Long

    hWindowDC = GetWindowDC(hWnd)
    MoveToEx hWindowDC, x1, y1, ByVal 0&
    LineTo hWindowDC, x2, y2
    If hWindowDC <> 0 Then
        ReleaseDC hWnd, hWindowDC
    End If
End Sub

Private Sub Form_Load()
    Me.ScaleMode = vbPixels
    Picture1.ScaleMode = vbPixels
    Timer1.Enabled = False
    Timer1.Interval = 10
    Picture1.Move 10, 10, 200, 100
    Command1.Move 30, 30, 80, 40
End Sub

Private Sub Form_Paint()
    ' Delegate the drawing to another time
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()

    Timer1.Enabled = False

    DrawLine Picture1.hWnd, 0, 50, Picture1.ScaleWidth, 50

End Sub



Thu, 30 Jun 2011 00:07:50 GMT  
 Line on top of a user-defined control on a PictureBox

Quote:
> How can i draw the lines on top of both PictureBox and
> my user-defined controls? What happen is that the lines
> are drawn on top of PictureBox but they are always below
> the user-defined controls? I tried to use ZOrder but couldnt
> solve this. I tried using Set Line1.Container = TheUserControl

Line Controls are windowless "drawn" controls and they cannot be on top of
real controls, so they cannot be on top of a User Control in your
PictureBox. Even if you placed a Line Control in your User Control at design
time you would still not be able to place the Line control on top of real
controls in your User Control. There are various ways around this problem.
One way would be to draw your line to the PictureBox's WindowDC using the
API LineTo function. Alternatively, and perhaps more simply, you can set
your PictureBox's ClipControls property to False at design time and you will
then be able to use the standard native VB drawing methods to draw stuff
over the top of any controls it contains.

Mike



Thu, 30 Jun 2011 02:53:02 GMT  
 Line on top of a user-defined control on a PictureBox

Quote:
> How can i draw the lines on top of both PictureBox and
> my user-defined controls? What happen is that the lines
> are drawn on top of PictureBox but they are always below
> the user-defined controls? I tried to use ZOrder but couldnt
> solve this. I tried using Set Line1.Container = TheUserControl

Line Controls are windowless "drawn" controls and they cannot be on top of
real controls, so they cannot be on top of a User Control in your
PictureBox. Even if you placed a Line Control in your User Control at design
time you would still not be able to place the Line control on top of real
controls in your User Control.

There are various ways around this problem. One way would be to draw your
line to the PictureBox's WindowDC using the API LineTo function as has
already been suggested by Expvb. Alternatively, and perhaps more simply, you
can set your PictureBox's ClipControls property to False at design time and
you will then be able to use the standard native VB drawing methods to draw
lines, circles, images or anything you wish over the top of any controls
your PictureBox contains. Another method would be to create a transparent
UserControl containing only a Line control and to expose settings such that
your code can set the coordinates of the UC's Line in code at run time. You
could then effectively place that "Line UC" (or an array of them) on top of
any other standard controls. That might get a bit messy though and it would
certainly be very "top heavy" for the use to which you are putting it.

Mike



Thu, 30 Jun 2011 03:11:08 GMT  
 Line on top of a user-defined control on a PictureBox
Thank you so much for all your helps.

There are 3 suggestions by Mike.
I tried the 1st one suggested by Expvb. It works. However, how can i
do things like changing the color and thickness of the line? Also, how
can i remove those lines (make them invisible) when i dont need them.
What my program does is that whenever i click on a user-defined
control (which has a few inputs and outputs), a connector which makes
up of 5 lines will be drawn to the input(s) or output(s) of the other
user-defined control which has the same variable. Once mouse-up, those
lines will disappear.

For the 2nd suggestion by setting PictureBox's Clipscontrol to false
during design time and draw using VB native drawing method. I tried it
and load another line during runtime from line(0) i created in design
time.  The line's still under the user control. is that what you mean
by VB native drawing method? Please correct me if i'm wrong.

I havent tried the 3rd as i think i'll use either 1st or 2nd
suggestion if possible.

Also, how can i learn all the Windows API functions? Please recommend
me some resources as i have seen so many useful applications of it but
have no idea how to make use of them. I bet i really have to dig into
them.

Thanks.


Quote:


> > How can i draw the lines on top of both PictureBox and
> > my user-defined controls? What happen is that the lines
> > are drawn on top of PictureBox but they are always below
> > the user-defined controls? I tried to use ZOrder but couldnt
> > solve this. I tried using Set Line1.Container = TheUserControl

> Line Controls are windowless "drawn" controls and they cannot be on top of
> real controls, so they cannot be on top of a User Control in your
> PictureBox. Even if you placed a Line Control in your User Control at design
> time you would still not be able to place the Line control on top of real
> controls in your User Control.

> There are various ways around this problem. One way would be to draw your
> line to the PictureBox's WindowDC using the API LineTo function as has
> already been suggested by Expvb. Alternatively, and perhaps more simply, you
> can set your PictureBox's ClipControls property to False at design time and
> you will then be able to use the standard native VB drawing methods to draw
> lines, circles, images or anything you wish over the top of any controls
> your PictureBox contains. Another method would be to create a transparent
> UserControl containing only a Line control and to expose settings such that
> your code can set the coordinates of the UC's Line in code at run time. You
> could then effectively place that "Line UC" (or an array of them) on top of
> any other standard controls. That might get a bit messy though and it would
> certainly be very "top heavy" for the use to which you are putting it.

> Mike



Sun, 03 Jul 2011 15:50:51 GMT  
 Line on top of a user-defined control on a PictureBox
After i have posted the new questions, i have found the way to remove
those lines by
Picture1.cls or Picture1.refresh

Probably, most of you are still in your sweet dream in your country
while i'm writing this. :)


Quote:
> Thank you so much for all your helps.

> There are 3 suggestions by Mike.
> I tried the 1st one suggested by Expvb. It works. However, how can i
> do things like changing the color and thickness of the line? Also, how
> can i remove those lines (make them invisible) when i dont need them.
> What my program does is that whenever i click on a user-defined
> control (which has a few inputs and outputs), a connector which makes
> up of 5 lines will be drawn to the input(s) or output(s) of the other
> user-defined control which has the same variable. Once mouse-up, those
> lines will disappear.

> For the 2nd suggestion by setting PictureBox's Clipscontrol to false
> during design time and draw using VB native drawing method. I tried it
> and load another line during runtime from line(0) i created in design
> time. ?The line's still under the user control. is that what you mean
> by VB native drawing method? Please correct me if i'm wrong.

> I havent tried the 3rd as i think i'll use either 1st or 2nd
> suggestion if possible.

> Also, how can i learn all the Windows API functions? Please recommend
> me some resources as i have seen so many useful applications of it but
> have no idea how to make use of them. I bet i really have to dig into
> them.

> Thanks.




> > > How can i draw the lines on top of both PictureBox and
> > > my user-defined controls? What happen is that the lines
> > > are drawn on top of PictureBox but they are always below
> > > the user-defined controls? I tried to use ZOrder but couldnt
> > > solve this. I tried using Set Line1.Container = TheUserControl

> > Line Controls are windowless "drawn" controls and they cannot be on top of
> > real controls, so they cannot be on top of a User Control in your
> > PictureBox. Even if you placed a Line Control in your User Control at design
> > time you would still not be able to place the Line control on top of real
> > controls in your User Control.

> > There are various ways around this problem. One way would be to draw your
> > line to the PictureBox's WindowDC using the API LineTo function as has
> > already been suggested by Expvb. Alternatively, and perhaps more simply, you
> > can set your PictureBox's ClipControls property to False at design time and
> > you will then be able to use the standard native VB drawing methods to draw
> > lines, circles, images or anything you wish over the top of any controls
> > your PictureBox contains. Another method would be to create a transparent
> > UserControl containing only a Line control and to expose settings such that
> > your code can set the coordinates of the UC's Line in code at run time. You
> > could then effectively place that "Line UC" (or an array of them) on top of
> > any other standard controls. That might get a bit messy though and it would
> > certainly be very "top heavy" for the use to which you are putting it.

> > Mike



Sun, 03 Jul 2011 17:06:47 GMT  
 Line on top of a user-defined control on a PictureBox

Quote:
> After i have posted the new questions, i have found
> the way to remove those lines by Picture1.cls or
> Picture1.refresh

Don't forget to also include a flag in the Paint event so that the Timer
only gets enabled if the flag is True. You should set the flag to True just
before you draw your lines and to False as soon as you have used Cls to
erase them, otherwise the lines will just get redrawn if the user moves the
Form on and off the display or if some other window is dragged over it.
Something like the following (on a Form with one TextBox named txtDummy and
lots of other Controls and with the Form's ClipControls property set to
False). In this example I'm using a Form rather than a PictureBox and I'm
using the native VB line drawing methods rather than the API methods and I'm
also using an invisible TextBox Change event rather than a one shot Timer,
but otherwise it works in much the same way as the code you've already got:

Mike

Option Explicit
Private flag As Boolean

Private Sub Command2_Click()
Me.Cls
End Sub

Private Sub Form_Load()
Me.ScaleMode = vbPixels
txtDummy.Visible = False
txtDummy.Text = 1
End Sub

Private Sub Form_MouseDown(Button As Integer, _
  Shift As Integer, X As Single, Y As Single)
flag = True
txtDummy.Text = -Val(txtDummy.Text)
End Sub

Private Sub Form_MouseUp(Button As Integer, _
  Shift As Integer, X As Single, Y As Single)
flag = False
Me.Cls
End Sub

Private Sub Form_Paint()
If flag = True Then
  txtDummy.Text = -Val(txtDummy.Text)
End If
End Sub

Private Sub txtDummy_Change()
DoEvents
Line (20, 20)-(500, 500), vbBlue
Line (30, 400)-(600, 10), vbRed
Line (100, 120)-(600, 200), vbGreen
End Sub



Sun, 03 Jul 2011 18:34:26 GMT  
 Line on top of a user-defined control on a PictureBox

Quote:
> For the 2nd suggestion by setting PictureBox's Clipscontrol
> to false during design time and draw using VB native drawing
> method. I tried it and load another line during runtime from
> line(0) i created in design time.  The line's still under the user
> control. is that what you mean by VB native drawing method?

Yes. You should draw the lines using the standard native VB Line method or
the alternative API LineTo method (rather than using a VB Line Control). The
native VB Line method in particular makes it very easy for you to specify
the colour of the line and the VB DrawWidth method allows you to easily set
the desired pixel thickness, for example:

  Me.DrawWidth = 2
  Me.Line (20, 20)-(400, 250), vbBlue
  Me.DrawWidth = 1
  Me.Line (30, 400)-(600, 10), vbRed

I've just posted some code which uses the native VB Line drawing method on a
Form with its ClipControls property set to False for you to have a play
with. If your lines are always horizontal or vertical (as I think I remember
seeing in a recent post of yours in a different thread) then they will all
look okay, but if you are drawing lines that are not entirely horizontal or
vertical (some diagonal lines) then they might look a little "blocky". If
this is a problem for you then there are various ways of drawing anti
aliased (smoother) diagonal lines, but personally I've always found such
things a bit "fuzzy" and less pleasing on the eye than a standard line (the
same goes for smoothed screen fonts unless they are very large). That's just
a personal thing though and different people will see things differently.

Mike



Sun, 03 Jul 2011 19:19:07 GMT  
 Line on top of a user-defined control on a PictureBox

Quote:
> For the 2nd suggestion by setting PictureBox's Clipscontrol
> to false during design time and draw using VB native drawing
> method. I tried it and load another line during runtime from
> line(0) i created in design time.  The line's still under the user
> control. is that what you mean by VB native drawing method?

I think I misread part of your question when I posted my earlier response
and when I said, "Yes" [by which I meant Yes, you should use native VB
drawing methods]". Looking at your question again I should of course have
said, "No, you should not use Line Controls". The rest of my previous
response is exactly as it already stands. Anyway, you will by now have seen
my other response in which I posted some sample code showing how to perform
your task by drawing lines using the native VB method Line (x1,y1)-(x2,y2)
into a Form with its ClipControls property set to False, and you've also got
Expvb's response in which he shows you how to perform essentially the same
task using the API drawing methods, so you should be home and dry by now :-)

Mike



Mon, 04 Jul 2011 00:13:00 GMT  
 Line on top of a user-defined control on a PictureBox

Hi Albert. Further to my previous responses in this thread I think there
actually is a way of having the Line Controls on top of your User Control in
a PictureBox. Try setting your PictureBox's ClipControls property to False
at design time and also placing a DoEvents in the PictureBox's Paint event.

Mike



Thu, 07 Jul 2011 05:04:33 GMT  
 Line on top of a user-defined control on a PictureBox

Quote:


> Hi Albert. Further to my previous responses in this thread I think there
> actually is a way of having the Line Controls on top of your User Control in
> a PictureBox. Try setting your PictureBox's ClipControls property to False
> at design time and also placing a DoEvents in the PictureBox's Paint event.

> Mike

Thank you so much, Mike.
It's been a great help from all of you in my work and my learning. :)


Sat, 09 Jul 2011 02:05:41 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. User Control - User-defined type not defined

2. User-defined error when the user doesn't define any errors

3. User Control and User Defined Type Passing

4. Controls on top of User Controls?

5. Drawing a line on top af graph control.

6. How To Display a Line Control over top Windows Media Player

7. Line Control over Picturebox

8. User Defined Type Not Defined

9. User-defined not defined

10. Compile Error: User-defined type not defined

11. User-defined type not defined (Access 97)

12. Excel : User-defined type not define

 

 
Powered by phpBB® Forum Software