在点击事件中在表单中绘制一个点

时间:2015-04-28 00:27:03

标签: vb.net winforms

任何帮助将不胜感激。如何在用户单击鼠标的面板上创建彩色圆点?我可以获取鼠标单击的坐标并通过消息框输出,但我无法在用户单击的面板上绘制点。我试过这些代码。

Private Sub createDot(x, y)
    MsgBox(x & " " & y)
    Dim myGraphics As Graphics = Me.CreateGraphics

    Dim myPen As Pen

    myPen = New Pen(Drawing.Color.Maroon, 20)

    myGraphics.DrawRectangle(myPen, x, y, 1, 1)

End Sub

Private Sub Panel1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseClick
    missed += 1
    lblMissed.Text = missed
    Dim x, y As Integer
    x = e.X.ToString
    y = e.Y.ToString
    createDot(x, y)
End Sub

谢谢!

2 个答案:

答案 0 :(得分:3)

三件事:

1.如果你在面板上画画,你应该使用Panel1.CreateGraphics而不是Me.CreateGraphics
2.笔的宽度对于点来说是大的。使用1代替
3.不要将x,y转换为字符串并将其传递给createDot

<强>注意:
面板无效后(例如,在其上移动另一个窗口),点将消失。绘图代码应该在Panel1_Paint事件中(Scott Chamberlain)

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <issue id="LongLogTag" severity="ignore" />
</lint>

答案 1 :(得分:0)

如果您想在面板上创建点,则应将Me.CreateGraphics更改为Panel1.CreateGraphics

从中删除ToString     x=e.X     y=e.Y