获取具有由鼠标位置定义的坐标的线 [英] Getting a line that has the coordinates defined by the mouse location

查看:145
本文介绍了获取具有由鼠标位置定义的坐标的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图制作一个直径为100的圆形小图形程序,并且从它的中心开始,一条直线出现,它总是附着在鼠标指针上,直到用户点击一下,然后该线被永久绘制。它和MSPaint的界限完全一样,除了出发点始终是圆圈的中心。



我尝试了一些不起作用的东西。


  1. 我只能在鼠标点击后才显示该行。这不是我想要的。我希望线条始终存在,并从圆心旋转到点击鼠标,然后永久性地在屏幕上显示。

  2. 我可以得到一条线总是被绘制的污迹。它是一种星形,但这不是我想要的。


    基本上,我想要相同的功能当你绘制一条线时你在MSPaint中有。我应该做些什么?画一条线,然后在一秒之后擦除它,然后在鼠标处于新位置时再次绘制它?我尝试过类似的东西,但是它会消除背景,然后只在鼠标移动时绘制线条,而不是在鼠标静止时绘制。



    如果任何人都可以提供代码片段,那会很棒。或者只是一些伪代码。

    这是正确的伪代码吗?
    开始:
    左键单击,从圆心到鼠标尖端出现一条线
    线保持在那里,直到生成新的鼠标坐标(我如何保持跟踪)?
    从圆形中心到原始位置的线条被擦除
    新线条被放置到鼠标坐标的新位置。



    我认为这是一个状态机使用我在数字课上学到的东西。如何在C#中实现状态?



    任何帮助将不胜感激,并感谢所有能够理解我的问题的人,即使我可能没有使用正确的术语。 / p>

    解决方案

    如此简短的答案是您需要一些自定义绘画。更长的答案涉及到自定义绘图和事件处理。



    您需要的另一段代码是用于保存所有行的某种列表。下面的代码创建一个用户控件,并在不依赖状态机的情况下执行自定义绘画。为了测试它,创建一个新项目添加一个名为UserControl1的用户控件,并将其添加到表单中。确保你与列出的活动联系在一起。



    我试图评论相关部分,这显示了一种快速和肮脏的方式来做你似乎正在尝试做的事情。

     使用System.Collections.Generic; 
    使用System.Drawing;
    使用System.Windows.Forms;

    命名空间CustomDrawingAndEvents
    {
    public partial class UserControl1:UserControl
    {
    private struct MyLine
    {
    public Point mStart;
    public Point mEnd;
    public MyLine(Point xStart,Point xEnd)
    {
    mStart = xStart;
    mEnd = xEnd;
    }
    }

    私人列表< MyLine> m-行;
    私人点mCircleCenter;
    私人点mMousePosition;

    public UserControl1()
    {
    InitializeComponent();
    mLines = new List< MyLine>();

    //双缓冲区,防止闪烁
    DoubleBuffered = true;
    //为我们的圈子创建中心。这只是把它放在
    //控件的中心。
    mCircleCenter = new Point(this.Width / 2,this.Height / 2);
    }

    private void UserControl1_MouseClick(object sender,MouseEventArgs e)
    {
    //用户单击创建一个新行以添加到列表中。
    mLines.Add(新MyLine(mCircleCenter,e.Location));
    }

    private void UserControl1_MouseMove(object sender,MouseEventArgs e)
    {
    //更新鼠标位置
    mMousePosition = e.Location;
    //使控件重绘本身
    Invalidate();
    }

    private void UserControl1_Paint(object sender,PaintEventArgs e)
    {
    //创建100宽度/高度的矩形(减去直径的一半以居中直方图)
    Rectangle lCenterRect =新的矩形(mCircleCenter.X - 50,mCircleCenter.Y - 50,100,100);

    //在直径为100
    的控件中心绘制我们的圆圈e.Graphics.DrawEllipse(new Pen(Brushes.Black),lCenterRect);

    //画出我们所有保存的行
    foreach(myLine line in mLines)
    e.Graphics.DrawLine(new Pen(Brushes.Red),lLine.mStart,lLine 。修补);

    //将我们的活动线从圆的中心绘制到
    //我们的鼠标位置
    e.Graphics.DrawLine(新笔(Brushes.Blue),mCircleCenter, mMousePosition);
    }
    }

    }


    I'm trying to make a little graphics program that has a circle of diameter 100 on the screen and from the center of it, a line is coming out of it that is always attached to the mouse pointer until such time that the user does a click, and then the line is permanently drawn. It's exactly like MSPaint's line, except that starting point is always the center of the circle.

    I tried a few things that DON'T work.

    1. I can get the line to appear only after a mouse-click. That's not what I want. I want the line to always be present and pivoting from the circle-center until the mouse is clicked and then it's then permanently on the screen.

    2. I can get a smeary thing where the line is always being drawn. It makes a sort of star shape, but that's not what I want either.

    Basically, I want the same functionality that you have in MSPaint when you draw a line. What am I supposed to do? Draw the line and then erase it a second later, and then draw it again when the mouse is in a new position? I tried something like that, but it does a thing where it erases the background a little bit, and then the line is only drawn when the mouse is in motion, but not when the mouse is stationary.

    If anyone can provide a code snippet, that'd be great. Or just some pseudo-code.

    Is this the right pseudo code? Start: Left click and a line appears from center of circle to mouse tip Line stays there until a new mouse coordinate is made (how do I keep track)? Line from center of circle to original location gets erased New line is made to new location of mouse coordinates.

    I think this something of a state-machine to use what I learned in digital class. How are states implemented in C#?

    Any help would be appreciated, and thanks to everyone that can understand my question even though I'm probably not using the proper terminology.

    解决方案

    So short answer is you will need some custom painting. The longer answer involves custom drawing, and event handling.

    The other piece of code you need is a list of some sort to hold all of the lines. The code below creates a user control and does the custom painting without relying on a state machine. To test it, create a new project add a user control called UserControl1, and add it to a form. Make sure you tie into the listed events.

    I tried to comment the relevant sections and this shows a quick and dirty way to do what you appear to be trying to do.

    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace CustomDrawingAndEvents
    {
    public partial class UserControl1 : UserControl
    {
        private struct MyLine
        {
            public Point mStart;
            public Point mEnd;
            public MyLine(Point xStart, Point xEnd)
            {
                mStart = xStart;
                mEnd = xEnd;
            }
        }
    
        private List<MyLine> mLines;
        private Point mCircleCenter;
        private Point mMousePosition;
    
        public UserControl1()
        {
            InitializeComponent();
            mLines = new List<MyLine>();
    
            //Double Buffer to prevent flicker
            DoubleBuffered = true;
            //Create the center for our circle. For this just put it in the center of 
            //the control.
            mCircleCenter = new Point(this.Width / 2, this.Height / 2);
        }
    
        private void UserControl1_MouseClick(object sender, MouseEventArgs e)
        {
            //User clicked create a new line to add to the list.
            mLines.Add(new MyLine(mCircleCenter, e.Location));
        }
    
        private void UserControl1_MouseMove(object sender, MouseEventArgs e)
        {
            //Update mouse position
            mMousePosition = e.Location;
            //Make the control redraw itself
            Invalidate();
        }
    
        private void UserControl1_Paint(object sender, PaintEventArgs e)
        {
            //Create the rect with 100 width/height (subtract half the diameter to center the rect over the circle)
            Rectangle lCenterRect = new Rectangle(mCircleCenter.X - 50, mCircleCenter.Y - 50, 100, 100);
    
            //Draw our circle in the center of the control with a diameter of 100 
            e.Graphics.DrawEllipse(new Pen(Brushes.Black), lCenterRect);
    
            //Draw all of our saved lines
            foreach (MyLine lLine in mLines) 
                e.Graphics.DrawLine(new Pen(Brushes.Red), lLine.mStart, lLine.mEnd);            
    
            //Draw our active line from the center of the circle to
            //our mouse location
            e.Graphics.DrawLine(new Pen(Brushes.Blue), mCircleCenter, mMousePosition);
        }
    }
    

    }

    这篇关于获取具有由鼠标位置定义的坐标的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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