亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何單擊繪制的點并打開外部窗口?

如何單擊繪制的點并打開外部窗口?

C#
MYYA 2023-08-13 16:06:02
我想單擊我繪制的點。如果能彈出一個窗口并且我可以用它做點什么,那就太酷了。但我想做的一般事情是單擊繪制的點。我想讓它發揮作用,我可以點擊地圖上我繪制的點。示例圖片: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;        }    }}
查看完整描述

1 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

示例代碼:


兩個類級變量和一個輔助函數:


List<Point> dots = new List<Point>();

int dotSize = 12;


Rectangle fromPoint(Point pt, int size)

{

    return new Rectangle(pt.X - size/ 2, pt.Y - size / 2, size, size);

}

mouseclick(與 click 事件相反)包含位置:


private void Pbxkarte_MouseClick(object sender, MouseEventArgs e)

{

    if (!dots.Contains(e.Location))

    {

        dots.Add(e.Location);

        Pbxkarte.Invalidate();  // show the dots

    }

}

您可以添加代碼來刪除點或更改屬性,尤其是。如果您創建一個點類。- 如果您想避免重疊點,您可以使用類似于 mousemove 中的代碼來檢測這一點。但。不要重復代碼!相反,分解出一個boolOrPoint IsDotAt(Point)可以兩次使用的函數!


在鼠標移動中我只顯示點擊狀態。你做你的事..


private void Pbxkarte_MouseMove(object sender, MouseEventArgs e)

{

    bool hit = false;

    foreach (var dot in dots)

    {

        using (GraphicsPath gp = new GraphicsPath())

        {

            gp.AddEllipse(fromPoint(dot, dotSize));

            if (gp.IsVisible(e.Location))

            {

                hit = true; break;

            }

        }

    }

    Cursor = hit ? Cursors.Hand : Cursors.Default;


}

每當列表中或系統中發生任何更改時,列表中的所有點都必須顯示。:


private void Pbxkarte_Paint(object sender, PaintEventArgs e)

{

    foreach (var dot in dots)

    {

        using (GraphicsPath gp = new GraphicsPath())

        {

            gp.AddEllipse(fromPoint(dot, dotSize));

            e.Graphics.FillPath(Brushes.Red, gp);

        }

    }

}

如果您想要更多屬性,例如文本或顏色,請創建class dot并使用List<dot>!


查看完整回答
反對 回復 2023-08-13
  • 1 回答
  • 0 關注
  • 141 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號