猿问

如何单击绘制的点并打开外部窗口?

我想单击我绘制的点。

如果能弹出一个窗口并且我可以用它做点什么,那就太酷了。但我想做的一般事情是单击绘制的点。我想让它发挥作用,我可以点击地图上我绘制的点。

示例图片:

public partial class Form1 : Form

    {

        Graphics g;

        Pen p;

        Point cursor;



        int k = 0;

        Point[] points = new Point[50];

        public Form1()

        {

            InitializeComponent();


            g = pbxkarte.CreateGraphics();

            p = new Pen(Color.DeepSkyBlue, 3);



        }


        private void Form1_Load(object sender, EventArgs e)

        {


        }



        private void Pbxkarte_Click(object sender, EventArgs e)

        {


           if (drawmodecbx.Checked == true)

            {


                g.DrawEllipse(p, cursor.X - 10, cursor.Y - 10, 20, 20);

                points[k++] = new Point(cursor.X, cursor.Y);

                lbxDrawnPoints.Items.Add("X:" + cursor.X + "Y:" + cursor.Y);



            }

        }


        private void Pbxkarte_MouseMove(object sender, MouseEventArgs e)

        {

            cursor = this.PointToClient(Cursor.Position);

            xydisplay.Text = "X:" + cursor.X + "Y:" + cursor.Y;

        }

    }

}


MYYA
浏览 118回答 1
1回答

慕标5832272

示例代码:两个类级变量和一个辅助函数:List<Point> dots = new List<Point>();int dotSize = 12;Rectangle fromPoint(Point pt, int size){&nbsp; &nbsp; return new Rectangle(pt.X - size/ 2, pt.Y - size / 2, size, size);}mouseclick(与 click 事件相反)包含位置:private void Pbxkarte_MouseClick(object sender, MouseEventArgs e){&nbsp; &nbsp; if (!dots.Contains(e.Location))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; dots.Add(e.Location);&nbsp; &nbsp; &nbsp; &nbsp; Pbxkarte.Invalidate();&nbsp; // show the dots&nbsp; &nbsp; }}您可以添加代码来删除点或更改属性,尤其是。如果您创建一个点类。- 如果您想避免重叠点,您可以使用类似于 mousemove 中的代码来检测这一点。但。不要重复代码!相反,分解出一个boolOrPoint IsDotAt(Point)可以两次使用的函数!在鼠标移动中我只显示点击状态。你做你的事..private void Pbxkarte_MouseMove(object sender, MouseEventArgs e){&nbsp; &nbsp; bool hit = false;&nbsp; &nbsp; foreach (var dot in dots)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; using (GraphicsPath gp = new GraphicsPath())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp.AddEllipse(fromPoint(dot, dotSize));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (gp.IsVisible(e.Location))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hit = true; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Cursor = hit ? Cursors.Hand : Cursors.Default;}每当列表中或系统中发生任何更改时,列表中的所有点都必须显示。:private void Pbxkarte_Paint(object sender, PaintEventArgs e){&nbsp; &nbsp; foreach (var dot in dots)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; using (GraphicsPath gp = new GraphicsPath())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp.AddEllipse(fromPoint(dot, dotSize));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.Graphics.FillPath(Brushes.Red, gp);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}如果您想要更多属性,例如文本或颜色,请创建class dot并使用List<dot>!
随时随地看视频慕课网APP
我要回答