1

右クリックボタンを押しながらマウスを動かすと、パネルに線を引きたいと思います。これが私がこれまでに試したことです:

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    Point point = panel1.PointToClient(Cursor.Position);
    Point cordinatedClikedPoint = new Point(xPanelPosition, yPanelPositon);
    DrawPoint((point.X), (point.Y), Color.Navy);                
}

public void DrawPoint(int x, int y, Color color)
{
    Graphics g = this.panel1.CreateGraphics();
    Pen pen = new Pen(color);
    g.DrawRectangle(pen, x, y, 3, 3);
}
4

5 に答える 5

3

MouseDownとイベントを使用して bool 変数を設定し、 EventHandlerMouseUpで描画を行います。MouseMoveグラフィックオブジェクトを直接呼び出しているため、パネルでの無効化または再描画操作によりクリアされるため、他の2つの回答がそれに対処します。

public partial class Form1 : Form
{
    bool mouseIsDown;
    public Form1()
    {
        InitializeComponent();
    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button== System.Windows.Forms.MouseButtons.Right)
            mouseIsDown = true;
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mouseIsDown)
        {
            Point point = panel1.PointToClient(Cursor.Position);
            DrawPoint((point.X), (point.Y), Color.Navy);
        }
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        mouseIsDown = false;
    }

    public void DrawPoint(int x, int y, Color color)
    {
        Graphics g = this.panel1.CreateGraphics();
        Pen pen = new Pen(color);
        g.DrawRectangle(pen, x, y, 3, 3);
    }
}
于 2013-08-25T01:53:42.247 に答える
3

もう 1 つ...今回は ControlPaint.DrawReversibleLine() を使用します。

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private Point StartPoint, EndPoint;

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            Point pt = Cursor.Position;
            StartPoint = pt;
            EndPoint = pt;
            ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black);
        }
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // erase previous line
            EndPoint = Cursor.Position;
            ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // draw new line
        }
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // erase previous line

            // ... do something with StartPont and EndPoint in here ...

            // possibly add them to a class level structure to use in the Paint() event to make it persistent?

        }
    }

}
于 2013-08-25T02:43:58.833 に答える
0

これはあなたがやろうとしていることの完全な解決策だと思います:

public partial class Form1 : Form
{
    private Graphics g;
    private readonly Pen pen = new Pen(Color.Navy, 7);
    private Point oldCoords;
    private readonly GraphicsPath graphicsPaths = new GraphicsPath();

    public Form1()
    {
        InitializeComponent();
        g = panel1.CreateGraphics();
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            if (oldCoords.IsEmpty)
                graphicsPaths.StartFigure();
            else
            {
                graphicsPaths.AddLine(oldCoords, new Point(e.X, e.Y));
                g.DrawPath(pen, graphicsPaths);
            }
            oldCoords= new Point(e.X,e.Y);
        }
        else
            oldCoords = Point.Empty;
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        g.DrawPath(pen, graphicsPaths);
    }
}

乾杯 :)

于 2013-08-25T02:22:15.457 に答える