2

我当前的代码允许我从用户定义的位置绘制矩形,但不是以我想要的方式。我需要它就像你在油漆中那样做,这是我当前的代码:

namespace SimpleDraw2 { /// /// MainForm 的描述。/// public partial class MainForm : Form { bool IsMouseDown = false; 点鼠标位置;int DrawShape = 0; 位图存储图像;

    public MainForm()
    {
        //
        // The InitializeComponent() call is required for Windows Forms designer support.
        //
        InitializeComponent();

        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //
        pictureBox1.Image = new Bitmap    (pictureBox1.Width,pictureBox1.Height);                      
        StoredImage =  new Bitmap(pictureBox1.Width,pictureBox1.Height);
    }

    void PictureBox1MouseDown(object sender, MouseEventArgs e)
    {
        IsMouseDown = true;
        MousePosition = e.Location;
        Graphics gStored = Graphics.FromImage(StoredImage);
        gStored.Clear(Color.Transparent);
        gStored.DrawImage(pictureBox1.Image, 0, 0);
    }

    void PictureBox1MouseUp(object sender, MouseEventArgs e)
    {
        IsMouseDown = false;
    }

    void PictureBox1MouseMove(object sender, MouseEventArgs e)
    {
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        if (DrawShape == 0) 
        {
            Pen p = new Pen(Color.Red, 10);
            if (IsMouseDown) 
            {
                g.DrawLine(p,MousePosition,e.Location);
                MousePosition = e.Location;
            }
        }
        if (DrawShape == 1) 
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage,0,0);
            g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y);

        }
        if (DrawShape == 2)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage, 0, 0);
            g.DrawEllipse(Pens.HotPink, MousePosition.X, MousePosition.Y, e.X, e.Y);
        }
        if (DrawShape == 3)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(StoredImage, 0, 0);
            g.DrawArc(Pens.Indigo,pictureBox1.Bounds, e.Y, e.X);
        }
        //if (DrawShape == 4)
        //{
        //    g.Clear(Color.Transparent);
        //    g.DrawImage(StoredImage, 0, 0);
        //    g.DrawPolygon(Pens.Indigo, Point[] e.X);
        //}

        this.Refresh();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            axWindowsMediaPlayer1.URL = ofd.FileName;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.Ctlcontrols.pause();
        Bitmap bmp = new Bitmap(axWindowsMediaPlayer1.Width, axWindowsMediaPlayer1.Height);
        Graphics gfx = Graphics.FromImage(bmp);
        gfx.CopyFromScreen(PointToScreen(axWindowsMediaPlayer1.Location), new Point(0, 0), axWindowsMediaPlayer1.Bounds.Size, CopyPixelOperation.SourceCopy);
        pictureBox1.BackgroundImage = bmp;
        //axWindowsMediaPlayer1.Visible = false;
        //pictureBox1.Visible = true;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Graphics gg = Graphics.FromImage(pictureBox1.BackgroundImage);
        gg.Clear(Color.Transparent);
        Graphics gStored = Graphics.FromImage(StoredImage);
        gStored.Clear(Color.Transparent);
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        g.Clear(Color.Transparent);


    }

    private void button4_Click(object sender, EventArgs e)
    {
        DrawShape = 1;

    }

    private void button6_Click(object sender, EventArgs e)
    {
        DrawShape = 2;
    }

    private void button8_Click(object sender, EventArgs e)
    {
       DrawShape = 3;
    }

    private void button7_Click(object sender, EventArgs e)
    {
        DrawShape = 0;
    }
}

}

如果有人可以帮助我编辑我的代码以解决这个问题,使其易于拖动和绘制系统,我将不胜感激。

提前致谢

克里斯

4

2 回答 2

2

来自msdn

绘制由坐标对、宽度和高度指定的矩形。

所以你的代码不起作用:

g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y);

应该是这样的

g.DrawRectangle(Pens.Green, MousePosition.X, MousePosition.Y, Math.Abs(e.X - MousePosition.X), Math.Abs(e.Y - MousePosition.Y));
于 2010-10-05T09:18:44.710 回答
1

我看到的最大问题是您正在尝试绘制鼠标事件。这意味着您的绘图将在您获得刷新事件的那一刻被擦除。

只在 Paint 事件中绘制,从不在鼠标事件中。如果您希望您的应用程序根据鼠标事件进行绘制,请在鼠标事件中设置一个点、矩形或任何内容(就像您开始使用 IsMouseDown 一样),使您想要在 MouseMoved 事件中更改的区域无效,然后绘制您的矩形或您的 Paint 事件中的任何内容。

于 2010-10-05T19:36:46.960 回答