Sabrosa Posted April 1, 2014 Report Share Posted April 1, 2014 Boas tardes pessoal, Peço desculpa se já existe algum tópico sobre este tema mas como não encontrei decidi cria um. Eu estou a fazer um programa para a minha tese de mestrado, e sendo eu novato na programação, nomeadamente em visual basic, gostaria de expor aqui a minha duvida. Na ferramenta que estou a desenvolver necessito de fazer uma parte de desenho, que é a parte fundamental para esta, em que queria desenhar "livremente" em qualquer ponto de uma picturebox, como se faz no Autocad por exemplo, isto é, selecionar um botão de desenhar linhas e criar linhas retas, pois só estas me interessam, e em que posteriormente terá também uma funcionalidade que é as linhas ficarem com ângulos de 90 graus, ortogonais por assim dizer. Eu já pesquisei várias vezes por tutoriais sobre isso, mas a única coisa que encontro é tutoriais em que se carrega num botão e ele desenha automáticamente a linha, não sendo isso o que eu quero. Agradecia ajuda e peço desculpa por alguma coisa. Cumprimentos Link to comment Share on other sites More sharing options...
bioshock Posted April 1, 2014 Report Share Posted April 1, 2014 http://setha.info/ict-vb2008/82-ict-vb2008-0025.html http://www.java2s.com/Tutorial/VB/0300__2D-Graphics/Mousedraganddraw.htm http://stackoverflow.com/questions/13327311/draw-on-a-picturebox-image-using-mouse-vb-net http://social.msdn.microsoft.com/Forums/vstudio/en-US/39e307ed-383e-40c3-b980-5741fdf53e74/how-to-draw-line-on-picturebox-in-net-when-i-pass-picturebox-as-parameter-to-some-function?forum=vbgeneral Link to comment Share on other sites More sharing options...
ribeiro55 Posted April 1, 2014 Report Share Posted April 1, 2014 Dá uma espreitadela na edição 31 da revista PROGRAMAR: https://www.revista-programar.info/edicoes/edicao-31/ Traz um artigo dedicado ao recurso para desenhar nos controlos do form: a GDI+ Daí a desenhar linhas após clique, é uma questão de lógica. Cá estamos depois para dar uma ajuda nesse sentido. Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
Sabrosa Posted April 1, 2014 Author Report Share Posted April 1, 2014 Muito obrigado pessoal, vou ver os artigos que sugeriram e penso que ja vi o que queria num deles 😄 Um muito obrigado, e quando fizer as coisas posto aqui para ficar como agradecimento. Bem haja pelo tempo despendido 😉 Link to comment Share on other sites More sharing options...
killerbest Posted April 2, 2014 Report Share Posted April 2, 2014 Se ainda não conseguiste tens ai mais http://www.macoratti.net/d200503.htm https://groups.google.com/forum/#!topic/microsoft.public.pt.dotnet/tLQ53TVik58 Link to comment Share on other sites More sharing options...
Sabrosa Posted April 3, 2014 Author Report Share Posted April 3, 2014 Boas pessoal, Já encontrei parte do que queria neste site http://www.vb-helper.com/howto_net_snapto.html sendo que o que interessa não é o código que esta na pagina mas sim o exemplo que esta no fundo dela para sacar. Tenho uma duvida em relação ao exemplo que la está, que é quando eles fazem a "grid" (grelha de pontos) eles definem logo o tamanho dela -- está assim definido: Private m_Gridx as integer = 10 Mas eu queria era definir a distância entre os pontos da grelha através de textbox's, mas não estou a conseguir.. se não perceberam a minha duvida digam que eu tento esclarecer. Agradecido pelo apoio pessoal. Cumprimentos Link to comment Share on other sites More sharing options...
Sabrosa Posted April 6, 2014 Author Report Share Posted April 6, 2014 Boas pessoal, Para esclarecer o meu post anterior, a minha duvida consiste no seguinte: Eu tenho este código Public Class Form1 ' Drawing parameters. Private m_GridX As Integer = 20 Private m_GridY As Integer = 20 Private m_PageWid As Integer = 400 Private m_PageHgt As Integer = 300 Private m_ShadowThickness As Integer = 20 ' Pens. Private m_PenOldLine As Pen = Pens.Black Private m_PenNewLine As Pen = Pens.Red Private m_PenGrid As Pen = Pens.Black Private m_PenRulerNormal As Pen = Pens.Blue Private m_PenRulerDrawing As Pen = m_PenNewLine ' Options. Private m_ShowGrid As Boolean = False Private m_SnapToGrid As Boolean = False ' For drawing a new line. Private m_Drawing As Boolean = False Private m_X1 As Single Private m_Y1 As Single Private m_X2 As Single Private m_Y2 As Single ' For showing position on the rulers. Private m_MouseX As Integer = -10 Private m_MouseY As Integer = -10 ' Existing lines. Private m_Points1() As PointF = {} Private m_Points2() As PointF = {} Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load m_ShowGrid = mnuOptionsShowGrid.Checked m_SnapToGrid = mnuOptionsSnapToGrid.Checked ' Arrange the controls. Dim wid As Integer = Me.ClientSize.Width - picLeftRuler.Left - picLeftRuler.Width - 3 Dim hgt As Integer = Me.ClientSize.Height - picTopRuler.Top - picTopRuler.Height - 3 picLeftRuler.Size = New Size(picLeftRuler.Width, hgt) picTopRuler.Size = New Size(wid, picTopRuler.Height) picCanvas.Size = New Size(wid, hgt) picLeftRuler.Location = New Point(picLeftRuler.Left, picTopRuler.Top + picTopRuler.Height + 2) picTopRuler.Location = New Point(picLeftRuler.Left + picLeftRuler.Width + 2, picTopRuler.Top) picCanvas.Location = New Point(picTopRuler.Left, picLeftRuler.Top) End Sub Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click Me.Close() End Sub Private Sub mnuOptionsShowGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOptionsShowGrid.Click m_ShowGrid = Not m_ShowGrid mnuOptionsShowGrid.Checked = m_ShowGrid ' Redraw. picCanvas.Invalidate() End Sub Private Sub mnuOptionsSnapToGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOptionsSnapToGrid.Click m_SnapToGrid = Not m_SnapToGrid mnuOptionsSnapToGrid.Checked = m_SnapToGrid End Sub ' Start drawing a line. Private Sub picCanvas_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown ' Only on left mouse down. If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub m_Drawing = True ' Start drawing. m_X1 = e.X m_Y1 = e.Y SnapToGrid(m_X1, m_Y1) m_X2 = m_X1 m_Y2 = m_Y1 picCanvas.Invalidate() ShowMousePosition(m_X2, m_Y2) End Sub ' Continue drawing. Private Sub picCanvas_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseMove m_X2 = e.X m_Y2 = e.Y SnapToGrid(m_X2, m_Y2) ' Show the mouse position on the rulers. ShowMousePosition(m_X2, m_Y2) ' Redraw. If m_Drawing Then picCanvas.Invalidate() End Sub ' Finish drawing. Private Sub picCanvas_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseUp If Not m_Drawing Then Exit Sub m_Drawing = False ' Show the mouse position in the non-drawing color. ShowMousePosition(m_X2, m_Y2) ' Save the new line. Dim new_index As Integer = m_Points1.Length ReDim Preserve m_Points1(new_index) ReDim Preserve m_Points2(new_index) m_Points1(new_index) = New PointF(m_X1, m_Y1) m_Points2(new_index) = New PointF(m_X2, m_Y2) picCanvas.Invalidate() End Sub ' Snap the point to the nearest grid location. Private Sub SnapToGrid(ByRef X As Integer, ByRef Y As Integer) ' If grid snap is off, do nothing. If Not m_SnapToGrid Then Exit Sub Dim ix As Integer = CInt(X / m_GridX) Dim iy As Integer = CInt(Y / m_GridY) X = ix * m_GridX Y = iy * m_GridY End Sub ' Draw the lines. Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picCanvas.Paint e.Graphics.Clear(picCanvas.BackColor) ' Draw the grid. If m_ShowGrid Then For x As Integer = 0 To picCanvas.ClientSize.Width Step m_GridX For y As Integer = 0 To picCanvas.ClientSize.Height Step m_GridY e.Graphics.DrawLine(m_PenGrid, x, y, x + 0.5F, y + 0.5F) Next y Next x End If ' Draw existing lines. For i As Integer = 0 To m_Points1.Length - 1 e.Graphics.DrawLine(m_PenOldLine, m_Points1(i), m_Points2(i)) Next i ' Draw the new line. If m_Drawing Then e.Graphics.DrawLine(m_PenNewLine, m_X1, m_Y1, m_X2, m_Y2) End If End Sub ' Draw the top ruler. Private Sub picTopRuler_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picTopRuler.Paint e.Graphics.Clear(picTopRuler.BackColor) Dim y1 As Integer = picTopRuler.ClientSize.Height Dim y2 As Integer = (2 * picTopRuler.ClientSize.Height) \ 3 Dim y3 As Integer = picTopRuler.ClientSize.Height \ 3 Dim y4 As Integer = 0 Dim x As Integer = 0 For i As Integer = 0 To picTopRuler.ClientSize.Width \ m_GridX If i Mod 10 = 0 Then e.Graphics.DrawLine(m_PenGrid, x, y1, x, y4) ElseIf i Mod 5 = 0 Then e.Graphics.DrawLine(m_PenGrid, x, y1, x, y3) Else e.Graphics.DrawLine(m_PenGrid, x, y1, x, y2) End If x += m_GridX Next i ' Show the mouse position. If m_Drawing Then e.Graphics.DrawLine(m_PenRulerDrawing, m_MouseX, y1, m_MouseX, 0) Else e.Graphics.DrawLine(m_PenRulerNormal, m_MouseX, y1, m_MouseX, 0) End If End Sub ' Draw the left ruler. Private Sub picLeftRuler_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picLeftRuler.Paint e.Graphics.Clear(picLeftRuler.BackColor) Dim x1 As Integer = picLeftRuler.ClientSize.Width Dim x2 As Integer = (2 * picLeftRuler.ClientSize.Width) \ 3 Dim x3 As Integer = picLeftRuler.ClientSize.Width \ 3 Dim x4 As Integer = 0 Dim y As Integer = 0 For i As Integer = 0 To picLeftRuler.ClientSize.Height \ m_GridY If i Mod 10 = 0 Then e.Graphics.DrawLine(m_PenGrid, x1, y, x4, y) ElseIf i Mod 5 = 0 Then e.Graphics.DrawLine(m_PenGrid, x1, y, x3, y) Else e.Graphics.DrawLine(m_PenGrid, x1, y, x2, y) End If y += m_GridY Next i ' Show the mouse position. If m_Drawing Then e.Graphics.DrawLine(m_PenRulerDrawing, x1, m_MouseY, x4, m_MouseY) Else e.Graphics.DrawLine(m_PenRulerNormal, x1, m_MouseY, x4, m_MouseY) End If End Sub ' Show the mouse position on the rulers. Private Sub ShowMousePosition(ByVal X As Integer, ByVal Y As Integer) m_MouseX = X m_MouseY = Y picTopRuler.Invalidate() picLeftRuler.Invalidate() End Sub End Class Que faz isto https://www.dropbox.com/s/uch72osuuobn3f1/Captura%20de%20ecr%C3%A3%202014-04-6,%20%C3%A0s%2019.13.09.png A minha questão é como é que eu posso definir a distancia dos pontos da grelha (m_GridX e m_GridY) através das textbox's (que estão no canto superior direito da imagem), visto que neste exemplo que encontrei (código que está mencionado acima) já esta definido a distância entre os pontos da grelha Private m_GridX As Integer = 20 Private m_GridY As Integer = 20 Agradecido pelo tempo despendido por vocês, aguardando uma resposta 🙂 Cumprimentos e mais uma vez obrigado. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now