我如何使用标志绘制或不绘制pictureBox绘制事件?

时间:2012-07-29 12:36:00

标签: c# winforms

我在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向右移动并且没有任何绘图,那么只需在pictureBox中显示图像就不要画任何东西!但是,如果我向右移动并且已经有抽奖,那么就显示它们。

如果我向左移动它总是显示之前的抽奖。

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类中的draw函数:

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移动到righ以检查绘图是否已经存在时显示它们是否存在只显示图像,并且只有当我单击按钮时它才会在帧上添加绘制。

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

WireObject类:

构造

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中带有标志何时显示像素我的意思是何时以及如何调用paint事件,如pictureBox1.Refresh();但是一旦它将使用内部的Draw功能,一旦它不会。当我运行程序时,让我使用绘图功能,一旦我将trackBar向右移动,就不要使用绘图功能。

1 个答案:

答案 0 :(得分:0)

对于Windows窗体控件OnPaint仅在以下情况下调用。)与当前控件重叠的窗口被移开或b。)您手动使控件无效:http://msdn.microsoft.com/en-us/library/1e430ef4.aspx

所以OnPaint应该经常被调用。