473,698 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Draw and Click a Line on PictureBox

1 New Member
I did one program that draws a line with two squares at the ends and I can just click and extend each ends. It works ok but I need to move the whole line. Would anyone knows how?

Private Const gs As Integer = 34
Private Const hw As Integer = 6
Private Const hhw As Integer = hw \ 2
Private bf As Bitmap
Private drag As Integer = -1
Private pts() As Point
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
ReDim pts(1)
pts(0).X = 50 : pts(0).Y = 50
pts(1).X = 250 : pts(1).Y = 50
For i As Integer = 0 To UBound(pts)
SnapToGrid(pts( i).X, pts(i).Y)
Next
End Sub
Private Sub Draw(ByVal Gr As Graphics)
Gr.DrawImage(bf , 0, 0)
Gr.DrawPolygon( Pens.Black, pts)

Dim rect() As Rectangle
ReDim rect(UBound(pts ))
For i As Integer = 0 To UBound(pts)
rect(i).X = pts(i).X - hhw
rect(i).Y = pts(i).Y - hhw
rect(i).Width = hw
rect(i).Height = hw
Next
Gr.FillRectangl es(Brushes.Beig e, rect)
Gr.DrawRectangl es(Pens.Black, rect)
End Sub
Private Sub pic_MouseDown(B yVal sender As Object, ByVal e As System.Windows. Forms.MouseEven tArgs) Handles pic.MouseDown
For i As Integer = 0 To UBound(pts)
If Abs(pts(i).X - e.X) < hhw And Abs(pts(i).Y - e.Y) < hhw Then
drag = i
pic.Cursor = Cursors.Hand
Exit For
End If
Next

End Sub
Private Sub pic_MouseMove(B yVal sender As Object, ByVal e As System.Windows. Forms.MouseEven tArgs) Handles pic.MouseMove
If drag = -1 Then Exit Sub
pts(drag).X = e.X
pts(drag).Y = e.Y
SnapToGrid(pts( drag).X, pts(drag).Y)
Draw(pic.Create Graphics)
End Sub
Private Sub pic_MouseUp(ByV al sender As Object, ByVal e As System.Windows. Forms.MouseEven tArgs) Handles pic.MouseUp
drag = -1
pic.Cursor = Cursors.Default
End Sub
Private Sub pic_Paint(ByVal sender As Object, ByVal e As System.Windows. Forms.PaintEven tArgs) Handles pic.Paint
Draw(e.Graphics )
End Sub
Private Sub SnapToGrid(ByRe f x As Integer, ByRef y As Integer)
x = gs * CInt(x / gs)
y = gs * CInt(y / gs)
End Sub
Private Sub DrawGrid(ByVal bm As Bitmap)
For x As Integer = 0 To pic.Width - 1 Step gs
For y As Integer = 0 To pic.Height - 1 Step gs
bm.SetPixel(x, y, Color.Black)
Next
Next
End Sub
Private Sub pic_Resize(ByVa l sender As Object, ByVal e As System.EventArg s) Handles pic.Resize
bf = New Bitmap(pic.Widt h, pic.Height)

Dim Gr As Graphics = Graphics.FromIm age(bf)
Gr.Clear(pic.Ba ckColor)
For x As Integer = 0 To pic.Width - 1 Step gs
For y As Integer = 0 To pic.Height - 1 Step gs
bf.SetPixel(x, y, Color.Black)
Next
Next
End Sub
Jan 31 '08 #1
0 1379

Sign in to post your reply or Sign up for a free account.

Similar topics

1
9702
by: Dennis | last post by:
Hello, Ive to draw a line on a picture i used this code: Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) x1 = z.X y1 = z.Y lbl3 = "x1: " & x1 lbl4 = "y1: " & y1
2
3051
by: John | last post by:
I created a number of pictureboxes in a panel, and want to draw lines in those pictureboxes but I cannot. Please see the following code and make corrections. Thanks. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Panel1.AutoScroll = True Panel1.Width = 200 Panel1.Height = 200
18
8565
by: Ed Bitzer | last post by:
Can draw a line on my form with a button click event but cannot upon the Load event when I wish. No more line object so used the following: Dim bit As Bitmap = New Bitmap(Me.Width, Me.Height) Dim g As Graphics = Graphics.FromImage(bit) Dim myPen As Pen = New Pen(Color.Blue, 1) Me.CreateGraphics.DrawLine(myPen, 0, 5, Me.Width, 5) Appreciate some help. Ed
2
6152
by: Mattbooty | last post by:
Hello, Not sure if anyone else has seen this bug, but I have a form where the entire form is covered with a picturebox. The picturebox has a mouseup event. I also have an open file dialog for loading images into the picturebox. If you double click the file you want to open in the open file dialog, it somehow interperets one of the clicks as a mouseup on the picturebox and fires the mouseup event for the picturebox. How can I get...
5
2116
by: wxnut | last post by:
In VB 2005 I have six seperately named picture boxes (arranged in a table layout control) in which I plot different X/Y data lines. I would like to be able to right-click on a specific picture box (say, plot #3) and select a different line color for that one plot. How can I figure out which of the six plots is the one in question when right-clicking on it? I can easily change the line color using the context menu control, but I don't...
1
2565
by: Alejandro | last post by:
Hi, I Have a form with Collection of 52 picturebox. Public cl As New Collection Private m_Bitmap As Bitmap Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
0
1215
by: Ben3eeE | last post by:
Im having trouble making a Paint.exe straight line draw. You know the one where you point out where to start then hold down the mouse button and release it to finish the drawing. My problem is that if i move the mouse back the line should move back with it and if i spin the mouse the line should spin and dont draw anything before i release the button. im using Microsoft visual stuido.net 2003
2
4223
by: Overseer | last post by:
What is the best way to draw graphics like Task Manager's Performance Graphic??
0
1530
by: Niu Kun | last post by:
Dear all, I'm planning to draw some overlapped pictures on a tabpage. I use PictureBox control. And the picture seems well at first. But when I try to draw a PictureBox on the other. The transparent part of the PictureBox seems to have the same colour with the tabpage background. And the PictureBox under it is covered by that. The covered part is never seen again. Can I simply solve the problem with existing simple control?
0
8672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9155
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9018
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8858
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7711
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.