2013-04-22 4 views
1

작동하지 않는 버튼을 사용하여 선을 만들려고합니다. 단추를 그릴 vb.net 줄이 작동하지 않습니까?

나는 다음과 같은 코드를 사용하는 경우

,

Imports System.Drawing 
Imports System.Drawing.Drawing2D 
Imports System.Windows.Forms 

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 
     Dim g As Graphics = e.Graphics 
     Dim pn As New Pen(Color.Blue) 
     Dim pt1 As New Point(30, 30) 
     Dim pt2 As New Point(110, 100) 
     g.DrawLine(pn, pt1, pt2) 
End sub 

그것은 완벽하게 작동,하지만 난 단지 버튼 등을 클릭 한 후 그리려는 경우,

Imports System.Drawing 
Imports System.Drawing.Drawing2D 
Imports System.Windows.Forms 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

     Dim g As Graphics = e.Graphics 
     Dim pn As New Pen(Color.Blue) 
     Dim pt1 As New Point(30, 30) 
     Dim pt2 As New Point(110, 100) 
     g.DrawLine(pn, pt1, pt2) 

    End Sub 

그것은 말한다 " '그래픽' 'System.EventArgs'의 멤버가 아닙니다. ???

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

에 :

은 또한 변화 시도

Private Sub Button1_Click(ByVal sender As graphics.Object, ByVal e As System.EventArgs) Handles Button1.Click 

그리고 몇 시뮬 변화 (많은 것에 목록으로)하지만 난 오류 응답의 alsorts를 얻을.

그렇다면 그래프를 사용하여 버튼 클릭으로 어떻게 선을 그립니까 ???

답변

0

양식의 그래픽 컨텍스트에 대한 참조가 필요합니다. 여기서는 WinForms라고 가정합니다.

버튼을 클릭 상단에이 줄을 추가하는 것입니다에 대한 가장 간단한 방법 : 당신이 정말 당신이 메모리와 다시로 그리는 것을 계속해야하기 때문에

Dim g As Graphics = Me.CreateGraphics 

그리기 그래픽이보다 복잡 당신이 그렸던 것이 PaintEventArgs이라는 e.ClipRectangle 속성에 포함되어 있다면 위의 코드 줄이 지금 당장 시작해야만한다면 OnPaint 이벤트가 발생했을 때 필요하면 그려 둡니다.

0

사용중인 두 이벤트에는 서로 다른 EventArg가 있습니다. Click EventArgs에는 클릭 할 정보와 PaintEventArgs에 대한 정보가 있습니다.

단추를 다시 칠하고 onPaint 메서드를 사용하여 호출 할 수 있습니다. 이것은 좋은 프로그래밍 스타일입니다. 이벤트를 해당 내용과 페어링하십시오. 페인트 칠하기. 클릭하여 클릭하십시오.

Private drawLine as boolean = false 

Private Sub Button1_Paint(sender As Object, e As PaintEventArgs) Handles Button1.Paint 
    If drawLine then 
    Dim g As Graphics = e.Graphics 
    Dim pn As New Pen(Color.Blue) 
    Dim pt1 As New Point(30, 30) 
    Dim pt2 As New Point(110, 100) 
    g.DrawLine(pn, pt1, pt2) 
    End if 
End sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    drawLine = true 
    Button1.refresh() 
End Sub 
관련 문제