-2

我在 Form1 顶层有一个标志:在构造函数中将 addFrame 设置为 false。然后在 picnt 事件中,我检查它是否为假让我画,如果它为真也让我画。这里的问题是我希望在我第一次运行程序时能够绘图!但是当我将 trackBar 向右移动时,我不希望它绘制任何东西。

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {


           Graphics g = e.Graphics;

           g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

               moveCounter++;
               label6.Text = moveCounter.ToString();



                   if (addFrame == false)
                   {
                       WireObjectGraphics.Draw(wireObject1, g);
                   }
                   else
                   {
                       addFrame = false;
                       WireObjectGraphics.Draw(wireObject1, g);
                   }

        }

这是我单击以将 addFrame 设置为 true 的按钮单击事件:

private void button16_Click(object sender, EventArgs e)
    {
        wireObjectAnimation1.AddFrame();
        addFrame = true;
        trackBar1.Select();
    }

在这种情况下,滚动条事件我想这样做,如果我将 trackBar 移动到右侧并且已经没有任何绘制,那么只需在图片框中显示图像就不要绘制任何东西!但是如果我把它移到右边并且已经有画了,那么一定要展示它们。

如果我把它移到左边总是显示以前的平局。

private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (addFrame == false)
            {

            }
            else
            {
                currentFrameIndex = trackBar1.Value - 1;
                textBox1.Text = "Frame Number : " + trackBar1.Value;

                wireObject1.woc.Set(wireObjectAnimation1.GetFrame(currentFrameIndex));
                trackBar1.Minimum = 0;
                trackBar1.Maximum = fi.Length - 1; 
                if (checkBox1.Checked)
                {
                    setpicture(trackBar1.Value);
                    Graphics g = Graphics.FromImage(pictureBox1.Image);
                    g.Clear(SystemColors.Control);
                    pictureBox1.Invalidate();
                }
                else
                {
                    setpicture(trackBar1.Value);
                }
                pictureBox1.Refresh();

                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
                button8.Enabled = false;
                SaveFormPicutreBoxToBitMapIncludingDrawings(currentFrameIndex);

                return;

            }

        }

这是 WireObjectGraphics 类中的绘制函数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace AnimationEditor
{
    class WireObjectGraphics
    {
        static Point connectionPointStart;
        static Point connectionPointEnd;
        static SolidBrush brush;
        static Pen p = null;

        public WireObjectGraphics()
        {

        }

        public static void Draw(WireObject wo, Graphics graphics)
        {
            brush = new SolidBrush(Color.Red);
            p = new Pen(brush);
            Graphics g = graphics;
            WireObject wireObject1 = wo;

            if (wireObject1 != null)
            {
                for (int idx = 0; idx < wireObject1.woc.Point_X.Count; ++idx)
                {
                    Point dPoint = new Point((int)wireObject1.woc.Point_X[idx], (int)wireObject1.woc.Point_Y[idx]);
                    dPoint.X = dPoint.X - 5; // was - 2
                    dPoint.Y = dPoint.Y - 5; // was - 2
                    Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
                    g.FillEllipse(brush, rect);
                    // bitmapGraphics.FillEllipse(brush, rect);

                    // g.FillEllipse(brush, rect);
                }

                for (int i = 0; i < wireObject1._connectionstart.Count; i++)
                {

                    int startIndex = wireObject1._connectionstart[i];
                    int endIndex = wireObject1._connectionend[i];



                    connectionPointStart = new Point((int)wireObject1.woc.Point_X[startIndex], (int)wireObject1.woc.Point_Y[startIndex]);
                    connectionPointEnd = new Point((int)wireObject1.woc.Point_X[endIndex], (int)wireObject1.woc.Point_Y[endIndex]);
                    p.Width = 2;
                    g.DrawLine(p, connectionPointStart, connectionPointEnd);
                    // bitmapGraphics.DrawLine(p, connectionPointStart, connectionPointEnd);
                }
            }
        }

    }


}

我需要的是第一次运行程序才能画画!然后,当将 trackBar 移动到右侧以检查绘图是否已经存在时,如果不存在则显示它们仅显示图像,并且只有当我单击按钮时,它才会在框架上添加绘图。

如果我向左移动,总是显示我在其他帧中所做的平局。

线对象类:

构造函数:

class WireObject
    {
        private string an;
        private bool fnExt;
        public string lockObject;
        private int idx;
        public WireObjectCoordinates woc;
        private List<int> connectionStart = new List<int>();
        private List<int> connectionEnd = new List<int>();

        private const string version = "01.00";

        string wo_name;


        public WireObject( string name )
        {
            wo_name = name;
            woc = new WireObjectCoordinates();
            fnExt = false;
        }

在wireobject类中,我有一些功能,比如连接点(像素),比如删除像素,比如保存和加载......

WireObjectCoordinates 类是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AnimationEditor
{
    class WireObjectCoordinates
    {
        public List<float> Point_X = new List<float>();
        public List<float> Point_Y = new List<float>();

        public WireObjectCoordinates()
        {
        }

        public WireObjectCoordinates(WireObjectCoordinates w)
        {
            Point_X.AddRange(w.Point_X);
            Point_Y.AddRange(w.Point_Y);
        }

        public void Set(WireObjectCoordinates w)
        {
            if (w == null)
            {
            }
            else
            {
                for (int i = 0; i < Point_X.Count; i++)
                {
                    Point_X[i] = w.Point_X[i];
                    Point_Y[i] = w.Point_Y[i];
                }
            }
        }
    }
}

问题仍然在 Form1 中,带有何时显示像素的标志,我的意思是何时以及如何调用诸如 pictureBox1.Refresh(); 之类的绘制事件;但是一旦它会使用内部的 Draw 函数,一旦它不会。当我运行程序时,让我使用绘图功能,一旦我将 trackBar 移动到右侧,不要使用绘图功能。

4

1 回答 1

0

对于 Windows 窗体控件 OnPaint 仅在以下情况下调用/1e430ef4.aspx

所以 OnPaint 应该被太频繁地调用。

于 2012-07-29T15:13:14.810 回答