百变方块V1.0

    技术2024-10-07  58

    百变方块V1.0

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

     

    using System.Threading;            //线程

    using System.Drawing.Drawing2D;    //GraphicsPath

    using System.Reflection;

     

     

    namespace VarietyBox              //百变方块

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

            //在类class Form1中声明私有的数据成员变量

            private const int CHIP_COUNT=8;

            private const int MAX_POINTS = 8;

            private const int CHIP_WIDTH = 40;

            private const int DELTA = 240;

            CChip []m_chipList=new CChip[CHIP_COUNT];

            private int m_nCurrIndex;       //用户选中的拼块

            private int oldx, oldy;         //记录鼠标原位置

            private bool Drag_PictBox = false;

            private int n = 0;             //图案序号

            private int max = 3;           //图案的数量

            private void Form1_Paint(object sender, PaintEventArgs e)

            {

                Graphics gp = e.Graphics;

                Pen p = new Pen(Color.Brown, 1);

                for (int i = 1; i < 7; i++)

                    for (int j = 1; j < 7; j++)

                    {

                        gp.DrawLine(p, 0, j * CHIP_WIDTH, CHIP_WIDTH*6, j * CHIP_WIDTH);

                        gp.DrawLine(p, i * CHIP_WIDTH, 0, i * CHIP_WIDTH, CHIP_WIDTH * 6);

                    }

            }

     

            private void Form1_MouseMove(object sender, MouseEventArgs e)

            {

                Point p = new Point(e.X, e.Y);

                label1.Text = p.ToString();

                //if (Drag_PictBox == true)

                //{

                //    for (int i = 0; i < CHIP_COUNT; i++)

                //        if (m_chipList[i].PtInChip(p) == true)

                //        {

                //            m_chipList[i].Move2(e.X - oldx, e.Y - oldy); //移动

                //            Draw_AllChip();//画出所有拼块                   

                //        }

                //}

                if (Drag_PictBox == true)

                {

                    m_chipList[m_nCurrIndex].Move2(e.X - oldx, e.Y - oldy); //移动

                    Draw_AllChip();                //画出所有拼块

                    //Thread.Sleep(10);              //线程sleep0.01秒,明显减少屏幕闪烁           

                }       

                oldx = e.X;

                oldy = e.Y;

            }

            private void Form1_MouseDown(object sender, MouseEventArgs e)

            {

                Point p = new Point(e.X, e.Y);

                for (int i = 0; i < CHIP_COUNT; i++)

                    if (m_chipList[i].PtInChip(p) == true)

                    {

                        m_nCurrIndex = i;           //记录用户选中的拼块

                        break; 

                    }           

                if (e.Button == MouseButtons.Right) //右键旋转

                {

                    Cursor.Current = Cursors.Hand;

                    m_chipList[m_nCurrIndex].Rotation2(); //旋转顺时针45

                    m_chipList[m_nCurrIndex].Rotation2(); //旋转顺时针45

                    Draw_AllChip();//画出所有拼块

     

                }           

                oldx = e.X;

                oldy = e.Y;

                Drag_PictBox = true;

            }

     

            private void Form1_MouseUp(object sender, MouseEventArgs e)

            {

                Drag_PictBox = false;

                //对用户选中的m_nCurrIndex拼块坐标进行修正,放置到正确位置

                m_chipList[m_nCurrIndex].Verity(CHIP_WIDTH);           

                Draw_AllChip();                //画出所有拼块

            }

            private void Reset()

            {

                for (int i = 0; i < CHIP_COUNT; i++)

                    m_chipList[i] = new CChip();

                Point[] pointList = new Point[MAX_POINTS];

                pointList[0].X = DELTA;

                pointList[0].Y = DELTA;

                pointList[1].X = DELTA + CHIP_WIDTH * 2;

                pointList[1].Y = DELTA;

                pointList[2].X = DELTA + CHIP_WIDTH * 2;

                pointList[2].Y = DELTA + CHIP_WIDTH * 2;

                pointList[3].X = DELTA;

                pointList[3].Y = DELTA + CHIP_WIDTH * 2;

                //m_chipList[0]为一个2W*2W的正方形

                m_chipList[0].SetChip(1, pointList, 4);

                pointList[0].X = DELTA + CHIP_WIDTH * 2;

                pointList[0].Y = DELTA;

                pointList[1].X = DELTA + CHIP_WIDTH * 6;

                pointList[1].Y = DELTA;

                pointList[2].X = DELTA + CHIP_WIDTH * 6;

                pointList[2].Y = DELTA + CHIP_WIDTH;

                pointList[3].X = DELTA + CHIP_WIDTH * 2;

                pointList[3].Y = DELTA + CHIP_WIDTH;

                //m_chipList[1]为一个4W*1W的长方形

                m_chipList[1].SetChip(2, pointList, 4);

     

                pointList[0].X = DELTA;

                pointList[0].Y = DELTA + CHIP_WIDTH * 2; ;

                pointList[1].X = DELTA + CHIP_WIDTH;

                pointList[1].Y = DELTA + CHIP_WIDTH * 2;

                pointList[2].X = DELTA + CHIP_WIDTH;

                pointList[2].Y = DELTA + CHIP_WIDTH * 3;

                pointList[3].X = DELTA + CHIP_WIDTH * 2;

                pointList[3].Y = DELTA + CHIP_WIDTH * 3;

                pointList[4].X = DELTA + CHIP_WIDTH * 2;

                pointList[4].Y = DELTA + CHIP_WIDTH * 4;

                pointList[5].X = DELTA + CHIP_WIDTH;

                pointList[5].Y = DELTA + CHIP_WIDTH * 4;

                pointList[6].X = DELTA + CHIP_WIDTH;

                pointList[6].Y = DELTA + CHIP_WIDTH * 5;

                pointList[7].X = DELTA;

                pointList[7].Y = DELTA + CHIP_WIDTH * 5;

                //m_chipList[2]为一个头朝右的┣形

                m_chipList[2].SetChip(3, pointList, 8);

     

                pointList[0].X = DELTA + CHIP_WIDTH;

                pointList[0].Y = DELTA + CHIP_WIDTH * 4;

                pointList[1].X = DELTA + CHIP_WIDTH * 2;

                pointList[1].Y = DELTA + CHIP_WIDTH * 4;

                pointList[2].X = DELTA + CHIP_WIDTH * 2;

                pointList[2].Y = DELTA + CHIP_WIDTH * 6;

                pointList[3].X = DELTA;

                pointList[3].Y = DELTA + CHIP_WIDTH * 6;

                pointList[4].X = DELTA;

                pointList[4].Y = DELTA + CHIP_WIDTH * 5;

                pointList[5].X = DELTA + CHIP_WIDTH;

                pointList[5].Y = DELTA + CHIP_WIDTH * 5;

                //m_chipList[3]为一个┛的折形

                m_chipList[3].SetChip(4, pointList, 6);

     

                Point[] p ={new Point(DELTA +2*CHIP_WIDTH,DELTA +2*CHIP_WIDTH),

                           new Point(DELTA +3*CHIP_WIDTH,DELTA +2*CHIP_WIDTH),

                           new Point(DELTA +3*CHIP_WIDTH,DELTA +5*CHIP_WIDTH),

                           new Point(DELTA +2*CHIP_WIDTH,DELTA +5*CHIP_WIDTH)};

                pointList[0] = new Point(DELTA + 2 * CHIP_WIDTH, DELTA + 2 * CHIP_WIDTH);

                pointList[1] = new Point(DELTA + 3 * CHIP_WIDTH, DELTA + 2 * CHIP_WIDTH);

                pointList[2] = new Point(DELTA + 3 * CHIP_WIDTH, DELTA + 5 * CHIP_WIDTH);

                pointList[3] = new Point(DELTA + 2 * CHIP_WIDTH, DELTA + 5 * CHIP_WIDTH);

                m_chipList[4].SetChip(5, pointList, 4);//m_chipList[4]为一个1W*3W的长方形

     

     

                pointList[0] = new Point(DELTA + 2 * CHIP_WIDTH, DELTA + 5 * CHIP_WIDTH);

                pointList[1] = new Point(DELTA + 4 * CHIP_WIDTH, DELTA + 5 * CHIP_WIDTH);

                pointList[2] = new Point(DELTA + 4 * CHIP_WIDTH, DELTA + 6 * CHIP_WIDTH);

                pointList[3] = new Point(DELTA + 2 * CHIP_WIDTH, DELTA + 6 * CHIP_WIDTH);

                m_chipList[5].SetChip(6, pointList, 4);//m_chipList[5]为一个1W*2W的长方形

     

                pointList[0] = new Point(DELTA + 5 * CHIP_WIDTH, DELTA + CHIP_WIDTH);

                pointList[1] = new Point(DELTA + 6 * CHIP_WIDTH, DELTA + CHIP_WIDTH);

                pointList[2] = new Point(DELTA + 6 * CHIP_WIDTH, DELTA + 3 * CHIP_WIDTH);

                pointList[3] = new Point(DELTA + 5 * CHIP_WIDTH, DELTA + 3 * CHIP_WIDTH);

                pointList[4] = new Point(DELTA + 5 * CHIP_WIDTH, DELTA + 4 * CHIP_WIDTH);

                pointList[5] = new Point(DELTA + 4 * CHIP_WIDTH, DELTA + 4 * CHIP_WIDTH);

                pointList[6] = new Point(DELTA + 4 * CHIP_WIDTH, DELTA + 2 * CHIP_WIDTH);

                pointList[7] = new Point(DELTA + 5 * CHIP_WIDTH, DELTA + 2 * CHIP_WIDTH);

                m_chipList[6].SetChip(7, pointList, 8);//m_chipList[6]为一个Z

     

                pointList[0] = new Point(DELTA + 5 * CHIP_WIDTH, DELTA + 3 * CHIP_WIDTH);

                pointList[1] = new Point(DELTA + 6 * CHIP_WIDTH, DELTA + 3 * CHIP_WIDTH);

                pointList[2] = new Point(DELTA + 6 * CHIP_WIDTH, DELTA + 6 * CHIP_WIDTH);

                pointList[3] = new Point(DELTA + 4 * CHIP_WIDTH, DELTA + 6 * CHIP_WIDTH);

                pointList[4] = new Point(DELTA + 4 * CHIP_WIDTH, DELTA + 5 * CHIP_WIDTH);

                pointList[5] = new Point(DELTA + 5 * CHIP_WIDTH, DELTA + 5 * CHIP_WIDTH);

                m_chipList[7].SetChip(8, pointList, 6);//m_chipList[6]为一个Z

     

            }

     

            private void button1_Click(object sender, EventArgs e)//“新图案”按钮

            {

                n++;

                if (n > max)

                {

                    MessageBox.Show("没有新图案了");

                    n--;

                    return;

                }

                Reset();// 初始化拼图块

                Draw_AllChip();//画出所有拼块

            }

     

            //画出所有拼块

            private void Draw_AllChip()

            {

                Bitmap bmp = new Bitmap(this.Width, this.Height);

                this.BackgroundImage = bmp;

                //Graphics g = this.CreateGraphics();

                Graphics g = Graphics.FromImage(bmp);           

                g.Clear(this.BackColor);

                //Bitmap s = (Bitmap)Image.FromFile("11.jpg");

                string r="s"+n.ToString()+".jpg";

                //Bitmap s = (Bitmap)Properties.Resources.s1; //目标图案;

                Bitmap s = (Bitmap)Image.FromFile(r);

                g.DrawImage(s,new  Point(300, 40));  

                for (int i = 0; i < CHIP_COUNT; i++)

                    m_chipList[i].DrawChip(g);

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

                Reset();// 初始化拼图块

                //以下两句是为了设置控件样式为双缓冲,这可以有效减少图片闪烁的问题,

                this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);

                this.UpdateStyles();

            }

     

     

       

        }

        class CChip

        {

            Point[] m_pointList;     //顶点坐标

            int m_nPointCount;     //顶点个数

            int m_nType;          //类型代号

            private GraphicsPath myPath;

            // 设置拼图块参数

            public void SetChip(int type,  Point []ppointlist, int count)

            {

                m_nType = type;

                m_nPointCount = count;

                m_pointList = new Point[m_nPointCount];

                for(int i=0; i<count; i++)

                m_pointList[i] = ppointlist[i];

                myPath = new GraphicsPath();

                myPath.AddLines(m_pointList);

                myPath.CloseFigure();//CloseFigure方法闭合当前图形并开始新的图形。

            }

             // 检测一点是否在拼图块中

            public bool PtInChip(Point p)

            {

                return(myPath.IsVisible(p));

            }

            public void Verity(int CHIP_WIDTH)

            {

                myPath.Reset();                         //清空路径

                int x_offset=0, y_offset=0;

                if (m_pointList[0].X % CHIP_WIDTH != 0)

                    x_offset -= m_pointList[0].X % CHIP_WIDTH;

                if (m_pointList[0].Y % CHIP_WIDTH != 0)

                    y_offset -= m_pointList[0].Y % CHIP_WIDTH;

                for (int i = 0; i < m_nPointCount; i++) // 平移各点

                {

                    m_pointList[i].X += x_offset;

                    m_pointList[i].Y += y_offset;

                }

                myPath.AddLines(m_pointList);

                myPath.CloseFigure();

            }

          

            public  void Move(int x_offset, int y_offset)

            {

                Matrix matrix = new Matrix();

                matrix.Translate(x_offset, y_offset); //追加平移

                myPath.Transform(matrix);            //应用变形

            }

            public  void Rotation()

            {

                Matrix matrix = new Matrix();

                RectangleF rect = new RectangleF();

                rect = myPath.GetBounds();

                // 计算旋转中心坐标(x,y)

                double x = rect.Left + rect.Width / 2;

                double y = rect.Top + rect.Height / 2;

                //matrix.Rotate(45.0f); //旋转顺时针45

                matrix.RotateAt(45.0f, new Point((int)x, (int)y)); //旋转顺时针45

                myPath.Transform(matrix);//应用变形

            }

            public void Move2(int x_offset, int y_offset)

            {

                myPath.Reset();                         //清空路径

                for (int i = 0; i < m_nPointCount; i++) // 平移各点

                {

                    m_pointList[i].X += x_offset;

                    m_pointList[i].Y += y_offset;

                }

                myPath.AddLines(m_pointList);

                myPath.CloseFigure();

            }

            public void Rotation2()

            {

                RectangleF rect=new RectangleF() ;

                rect=myPath.GetBounds();

                myPath.Reset();

                double x = rect.Left + rect.Width/2; // 计算旋转中心

                double y = rect.Top + rect.Height/ 2;

                double dx, dy;

                for (int i = 0; i < m_nPointCount; i++) // 旋转各点

                {

                    dx = m_pointList[i].X  - x;

                    dy = m_pointList[i].Y - y;

                    m_pointList[i].X = (int)(x + dx * 0.7071 - dy * 0.7071);

                    m_pointList[i].Y = (int)(y + dx * 0.7071 + dy * 0.7071);

                }            

                myPath.AddLines(m_pointList);

                myPath.CloseFigure();

            }

            public void DrawChip(Graphics g)//画拼块

            { 

                Pen myPen = new Pen(Color.Black, 1);

                g.DrawPath(myPen, myPath);

                int alpha = 140;   //透明度

                Color c= Color.FromArgb(alpha, 228, 128, 128);

                if(m_nType<=8)

                    c=Color.FromArgb(alpha, 0, 0, 255);            

                else

                    c = Color.FromArgb(alpha, 0, 255, 0);            

     

                SolidBrush brushNew = new SolidBrush(c);

                g.FillPath(brushNew, myPath);

            }

        }

    }

     

    最新回复(0)