创建可通过上下文菜单栏选择的图形对象线 [英] Create a Graphics Object Line that is selectable by a context menu strip

查看:75
本文介绍了创建可通过上下文菜单栏选择的图形对象线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,用户可以选择他/她绘制的线条.他/她还可以右键单击它,然后会弹出一个上下文菜单栏.有任何想法吗?

问候:-D

解决方案

做到这一点的方法是将行的位置存储在代码中,并随着鼠标的移动检查其是否过线.如果要绘制多个对象,则代码中将包含一组对象,您的paint方法将绘制它们,并且您的鼠标移动代码将在它们上迭代,以查看您的鼠标是否在触摸.

在这里,我设法使用一个硬编码矩形来做到这一点.

<pre lang="cs">
        private bool selectGraph = false;
        private Rectangle myrec = new Rectangle(50, 50, 100, 100);
        private Graphics g;
        
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            g = panel1.CreateGraphics();
            SolidBrush sb = new SolidBrush(Color.Blue);
            Pen p = new Pen(Color.Blue, 5);
            g.DrawRectangle(p, myrec);
            g.FillRectangle(sb, myrec);
        }
  
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            Point mPT = new Point(e.X, e.Y);
            if (e.Button == MouseButtons.Left)
            {
                if (myrec.Contains(mPT))
                {
                    selectGraph = true;
                    button1.Enabled = true;
                }
                else
                {
                    selectGraph = false;
                    button1.Enabled = false;
                }
            }
            Invalidate();
        }




但是我想要的是运行时绘制线. :doh:


问题已解决.使用Microsoft VisualBasic PowerPack 3.0:笑:


I''m creating an application which the user is able to select the line he/she draw. He/She can also right click it and a context menu strip will pop out. Any Ideas?

Regards :-D

解决方案

The way to do this is to store the position of the line in your code, and as the mouse moves, check if it''s crossing the line. If you want to draw more than one object, you''d have a collection of objects in your code, your paint method would draw them and your mouse move code would iterate over them to see if your mouse was touching one.


Here I managed to do it using a hard coded rectangle.

<pre lang="cs">
        private bool selectGraph = false;
        private Rectangle myrec = new Rectangle(50, 50, 100, 100);
        private Graphics g;
        
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            g = panel1.CreateGraphics();
            SolidBrush sb = new SolidBrush(Color.Blue);
            Pen p = new Pen(Color.Blue, 5);
            g.DrawRectangle(p, myrec);
            g.FillRectangle(sb, myrec);
        }
  
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            Point mPT = new Point(e.X, e.Y);
            if (e.Button == MouseButtons.Left)
            {
                if (myrec.Contains(mPT))
                {
                    selectGraph = true;
                    button1.Enabled = true;
                }
                else
                {
                    selectGraph = false;
                    button1.Enabled = false;
                }
            }
            Invalidate();
        }




But what I want is a runtime draw line. :doh:


Problem Solved. Use Microsoft VisualBasic PowerPack 3.0 :laugh:


这篇关于创建可通过上下文菜单栏选择的图形对象线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆