| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 184 人关注过本帖
标题:求助 结构体二维数组
只看楼主 加入收藏
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:195
专家分:101
注 册:2021-4-9
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:8 
求助 结构体二维数组
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册




只能初始化 1个数组。。。。
如何注释掉 Array1Init(), 就可以初始化Array2。



图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册






另有一个问题。。。
拖动窗口,改变窗口大小后。
重新输出后,数字 9 变成 402653193.
不知道,什么原因。


程序代码:
#pragma once

struct POINT
{
    int _X;
    int _Y;
};
struct UNIT
{
    POINT Zero;
    int Wide;
    int Number;
};
struct MyStruct
{
    POINT inPoint;
    int width, height;
    int fontSize;
};
namespace NumberGame {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Form1 摘要
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: 在此处添加构造函数代码
            //
        }
    protected:
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>

        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::TableLayoutPanel^  tableLayoutPanel1;
    protected: 
    private: System::Windows::Forms::PictureBox^  pictureBox1;
    private: System::Windows::Forms::PictureBox^  pictureBox2;

    private:
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        void InitializeComponent(void)
        {
            this->tableLayoutPanel1 = (gcnew System::Windows::Forms::TableLayoutPanel());
            this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
            this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
            this->tableLayoutPanel1->SuspendLayout();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox2))->BeginInit();
            this->SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this->tableLayoutPanel1->ColumnCount = 2;
            this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent, 
                66.66666F)));
            this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent, 
                33.33333F)));
            this->tableLayoutPanel1->Controls->Add(this->pictureBox1, 0, 0);
            this->tableLayoutPanel1->Controls->Add(this->pictureBox2, 1, 0);
            this->tableLayoutPanel1->Dock = System::Windows::Forms::DockStyle::Fill;
            this->tableLayoutPanel1->Location = System::Drawing::Point(0, 0);
            this->tableLayoutPanel1->Name = L"tableLayoutPanel1";
            this->tableLayoutPanel1->RowCount = 1;
            this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 100)));
            this->tableLayoutPanel1->Size = System::Drawing::Size(784, 522);
            this->tableLayoutPanel1->TabIndex = 0;
            // 
            // pictureBox1
            // 
            this->pictureBox1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)), 
                static_cast<System::Int32>(static_cast<System::Byte>(192)));
            this->pictureBox1->Dock = System::Windows::Forms::DockStyle::Fill;
            this->pictureBox1->Location = System::Drawing::Point(3, 3);
            this->pictureBox1->Name = L"pictureBox1";
            this->pictureBox1->Size = System::Drawing::Size(516, 516);
            this->pictureBox1->TabIndex = 0;
            this->pictureBox1->TabStop = false;
            // 
            // pictureBox2
            // 
            this->pictureBox2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)), 
                static_cast<System::Int32>(static_cast<System::Byte>(192)));
            this->pictureBox2->Dock = System::Windows::Forms::DockStyle::Fill;
            this->pictureBox2->Location = System::Drawing::Point(525, 3);
            this->pictureBox2->Name = L"pictureBox2";
            this->pictureBox2->Size = System::Drawing::Size(256, 516);
            this->pictureBox2->TabIndex = 1;
            this->pictureBox2->TabStop = false;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(784, 522);
            this->Controls->Add(this->tableLayoutPanel1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->Resize += gcnew System::EventHandler(this, &Form1::Form1_Resize);
            this->tableLayoutPanel1->ResumeLayout(false);
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox2))->EndInit();
            this->ResumeLayout(false);

        }
#pragma endregion
    private: static int barrier = 3;
    private: static int P2row = 0;
    private: static int P2col = 0;
    private: static int P2left = 0;
    private: static int P2top = 0;
    private: static int P2wide = 0;

    private: UNIT** Array1;
    private: UNIT** Array2;

    private: void Array1Init()
             {
              //------Array1初始化-------------
                 Array1 = new UNIT*[barrier];
                 for(int i=0; i<barrier; i++)
                 {
                      Array1[i] = new UNIT;
                     // Array1[i]->fontSize = i;  
                 }
                 int wide = (this->pictureBox1->Width-12)/barrier;
                 for(int i=0; i<barrier; i++)
                     for(int j=0; j<barrier; j++)
                     {
                         Array1[i][j].Wide = wide;
                         //Array1[i][j].Number = 0;
                         Array1[i][j].Number = i*barrier+j+1;
                         Array1[i][j].Zero._X = j*wide + 6;
                         Array1[i][j].Zero._Y = i*wide + 6;
                     }
             }
    //private: void Array2Init()
    //         {
    //             //------Array2初始化-------------
    //             Array2 = new UNIT*[P2row];
    //             for(int i=0; i<P2col; i++)
    //             {
    //                  Array2[i] = new UNIT;
    //                 // Array2[i]->fontSize = i;  
    //             }
    //             int wide = (this->pictureBox2->Width-2*P2left)/P2col;
    //         }
    private: MyStruct get_format(Point^ inpoint, int wide, int num)
    {

        int imgWide = (wide * 3) / 2;
        int fontsize = 0;// wide / len;

        System::Drawing::Font^ drawFont;

        SizeF layoutSize = SizeF(wide, wide);

        Graphics^ g = CreateGraphics();

        StringFormat^ sformat = gcnew StringFormat();
        sformat->Alignment = StringAlignment::Center;
        sformat->LineAlignment = StringAlignment::Center;
        sformat->FormatFlags = StringFormatFlags(0);

        SizeF sizeF(0, 0);
        while (sizeF.Width < wide)
        {
            fontsize++;
            drawFont = gcnew System::Drawing::Font("宋体", fontsize);
            sizeF = g->MeasureString(num.ToString(), drawFont, *inpoint, sformat);

            delete drawFont;
        }

        MyStruct _format;
        _format.fontSize = fontsize;
        _format.inPoint._X = inpoint->X + (wide - sizeF.Width) / 2;
        _format.inPoint._Y = inpoint->Y + (wide - sizeF.Height) / 2 + 5;
        _format.height = sizeF.Height;
        _format.width = sizeF.Width;

        delete sformat;
        delete g;

        return _format;

    }
    private: void draw_p1_num(Point^ inpoint, int wide, int num)
    {
        Bitmap^ drawimg = gcnew Bitmap(this->pictureBox1->Image);
        Graphics^ g = Graphics::FromImage(drawimg);
        SolidBrush^ MyBrush = gcnew SolidBrush(this->pictureBox1->BackColor);
        g->FillRectangle(MyBrush, Rectangle(inpoint->X + wide / 20, inpoint->Y + wide / 20, (wide * 9) / 10, (wide * 9) / 10));
        MyBrush->Color = Color::Black;

        inpoint->X += wide / 10;
        inpoint->Y += wide / 10;
        int d = (wide * 4) / 5;
        Rectangle rectEllip = Rectangle(inpoint->X, inpoint->Y, d, d);
        g->FillEllipse(MyBrush, rectEllip);

        MyStruct drawformat = get_format(inpoint, rectEllip.Width, num);
        System::Drawing::Font^ drawFont = gcnew System::Drawing::Font("宋体", drawformat.fontSize);

        Rectangle rectFont = Rectangle(drawformat.inPoint._X, drawformat.inPoint._Y, drawformat.width, drawformat.height);

        MyBrush->Color = this->pictureBox1->BackColor;
        g->DrawString(num.ToString(), drawFont, MyBrush, Point(drawformat.inPoint._X, drawformat.inPoint._Y));

        this->pictureBox1->Image = (Image^)drawimg->Clone();
        delete drawFont;
        delete g;
        delete drawimg;
    }
    private: void p1_all_out()
             {
                 for(int i=0; i<barrier; i++)
                    for(int j=0; j<barrier; j++)
                    {
                        draw_p1_num(Point(Array1[i][j].Zero._X, Array1[i][j].Zero._Y), Array1[i][j].Wide, Array1[i][j].Number);
                    }
             }
    private: void SetMaxSize()
             {
                int maxwidth = Screen::PrimaryScreen->WorkingArea.Width;
                int maxheight = Screen::PrimaryScreen->WorkingArea.Height;
                if(maxwidth > ((maxheight - 39)*3)/2+16)
                    maxwidth = ((maxheight - 39)*3)/2+16;
                else if(maxheight> ((maxwidth-16)*2)/3+39)
                    maxheight = ((maxwidth-16)*2)/3+39;
                //this->MaximumSize.Width = maxwidth;
                //this->MaximumSize.Height = maxheight;
                this->MaximumSize = System::Drawing::Size( maxwidth, maxheight);
             }
    private: void DrawChart2(int num)
    {
        int Swidth = (this->pictureBox2->Width - 12) / num;
        int totality = ((this->pictureBox2->Width - 12) / Swidth) * ((this->pictureBox2->Height - 12) / Swidth);
        while (totality >= num * num)
        {
            Swidth++;
            totality = ((this->pictureBox2->Width - 12) / Swidth) * ((this->pictureBox2->Height - 12) / Swidth);
        }
        Swidth--;
        
        int col = (this->pictureBox2->Width - 12) / Swidth;
        int row = (barrier * barrier) % col == 0 ? (barrier * barrier) / col : (barrier * barrier) / col + 1;
        int left = (this->pictureBox2->Width - col * Swidth) / 2;
        int    top = (this->pictureBox2->Height - row * Swidth) / 2;
        P2col = col;
        P2row = row;
        P2left = left;
        P2top = top;
        P2wide = Swidth;

        Bitmap^ drawimg = gcnew Bitmap(this->pictureBox2->Width, this->pictureBox2->Height);
        Graphics^ g = Graphics::FromImage(drawimg);
        Pen^ mypen = gcnew Pen(Color::Green, 1);

        for (int i = 0; i <= row; i++)
            g->DrawLine(mypen, Point(left, i * Swidth + top), Point(col * Swidth + left, i * Swidth + top));
        for (int i = 0; i <= col; i++)
            g->DrawLine(mypen, Point(i * Swidth + left, top), Point(i * Swidth + left, row * Swidth + top));

        this->pictureBox2->Image = (Image^)drawimg->Clone();
        delete g;
        delete drawimg;
    }
    private: void DrawChart(int num)
    {

        Bitmap^ drawimg = gcnew Bitmap(pictureBox1->Width, pictureBox1->Height);
        Graphics^ g = Graphics::FromImage(drawimg);

        Pen^ mypen = gcnew Pen(Color::Red, 1);
        int Swidth = (pictureBox1->Width - 12) / num;
        for (int i = 0; i <= num; i++)
            g->DrawLine(mypen, Point(6, i * Swidth + 6), Point(num * Swidth + 6, i * Swidth + 6));
        for (int i = 0; i <= num; i++)
            g->DrawLine(mypen, Point(i * Swidth + 6, 6), Point(i * Swidth + 6, num * Swidth + 6));

        this->pictureBox1->Image = (Image^)drawimg->Clone();

        delete g;
        delete drawimg;
    }
    private: void calc_point()
             {
                 int wide = (this->pictureBox1->Width-12)/barrier;
                 for(int i=0; i<barrier; i++)
                     for(int j=0; j<barrier; j++)
                     {
                         Array1[i][j].Wide = wide;
                         //Array1[i][j].Number = 0;
                         //Array1[i][j].Number = i*barrier+j+1;
                         Array1[i][j].Zero._X = j*wide + 6;
                         Array1[i][j].Zero._Y = i*wide + 6;
                     }
             }
    private: System::Void Form1_Resize(System::Object^  sender, System::EventArgs^  e) 
             {
                 Control^ control = dynamic_cast<Control^>(sender);

                 //拖动窗口比例缩放,确保pictureBox1 Size 为正方形.
                 int swidth = control->Size.Width;
                 int sheight = control->Size.Height;


                 if(sheight > 28) //sheight > 28 窗口不是最小化。
                 {
                     int maxwidth = Screen::PrimaryScreen->WorkingArea.Width;
                     int maxheight = Screen::PrimaryScreen->WorkingArea.Height;

                     if(swidth > maxwidth)
                        swidth = maxwidth;

                     sheight = ((swidth-16)*2)/3+39;

                     if(sheight>maxheight)
                     {
                         sheight = maxheight;
                         swidth = ((sheight - 39)*3)/2+16;
                     }
                     
                     control->Size = System::Drawing::Size(swidth, sheight);

                     //this->pictureBox1->Image=nullptr;
                     //this->pictureBox2->Image=nullptr;
                     DrawChart(barrier);
                     DrawChart2(barrier);
                     calc_point();
                     p1_all_out();
                 }
             }
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
             {
                 SetMaxSize();            //设置窗口最大尺寸
                 DrawChart(barrier);    //绘制图片框1,的表格
                 DrawChart2(barrier);    //绘制图片框2,的表格
                 Array1Init();            //Array1数组初始化
                 //Array2Init();            //Array2数组初始化

                 p1_all_out();            //输出图片框1全部单元格。
             }
};
}





Form1.rar (3.15 KB)
搜索更多相关主题的帖子: int private Forms System Windows 
2022-08-02 23:19
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:133
帖 子:1204
专家分:7155
注 册:2010-3-16
收藏
得分:7 
程序代码:
typedef struct _UNIT
{
    POINT Zero;
    int Wide;
    int Number;
}UNIT, *pUNIT;

private: void Array1Init()
         {
          //------Array1初始化-------------
             Array1 = new pUNIT[barrier];//////
             for(int i=0; i<barrier; i++)
             {
                  Array1[i] = new UNIT[barrier];//////
                 // Array1[i]->fontSize = i;  
             }
             int wide = (this->pictureBox1->Width-12)/barrier;
             for(int i=0; i<barrier; i++)
                 for(int j=0; j<barrier; j++)
                 {
                     Array1[i][j].Wide = wide;
                     //Array1[i][j].Number = 0;
                     Array1[i][j].Number = i*barrier+j+1;
                     Array1[i][j].Zero._X = j*wide + 6;
                     Array1[i][j].Zero._Y = i*wide + 6;
                 }
         }
2022-08-03 00:24
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:195
专家分:101
注 册:2021-4-9
收藏
得分:0 
回复 2楼 apull
图片附件: 游客没有浏览图片的权限,请 登录注册



这里需要修改吗???

1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(143): error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(143): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(143): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(144): error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(144): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(144): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(149): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(149): error C2061: 语法错误: 标识符“pUNIT”
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(152): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(152): error C2061: 语法错误: 标识符“UNIT”
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(159): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(159): error C2228: “.Wide”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(161): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(161): error C2228: “.Number”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(162): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(162): error C2228: “.Zero”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(162): error C2228: “._X”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(163): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(163): error C2228: “.Zero”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(163): error C2228: “._Y”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(169): error C2065: “Array2”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(169): error C2061: 语法错误: 标识符“pUNIT”
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(172): error C2065: “Array2”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(172): error C2061: 语法错误: 标识符“UNIT”
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(249): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(249): error C2228: “.Zero”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(249): error C2228: “._X”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(249): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(249): error C2228: “.Zero”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(249): error C2228: “._Y”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(249): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(249): error C2228: “.Wide”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(249): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(249): error C2228: “.Number”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(322): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(322): error C2228: “.Wide”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(325): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(325): error C2228: “.Zero”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(325): error C2228: “._X”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(326): error C2065: “Array1”: 未声明的标识符
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(326): error C2228: “.Zero”的左边必须有类/结构/联合
1>c:\users\administrator\documents\visual studio 2010\projects\numbergame\numbergame\Form1.h(326): error C2228: “._Y”的左边必须有类/结构/联合
1>
1>生成失败。
2022-08-03 04:11
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:195
专家分:101
注 册:2021-4-9
收藏
得分:0 
以下是引用apull在2022-8-3 00:24:08的发言:


typedef struct _UNIT
{
    POINT Zero;
    int Wide;
    int Number;
}UNIT, *pUNIT;

private: void Array1Init()
         {
          //------Array1初始化-------------
             Array1 = new pUNIT;//////
             for(int i=0; i<barrier; i++)
             {
                  Array1 = new UNIT;//////
                 // Array1->fontSize = i;  
             }
             int wide = (this->pictureBox1->Width-12)/barrier;
             for(int i=0; i<barrier; i++)
                 for(int j=0; j<barrier; j++)
                 {
                     Array1[j].Wide = wide;
                     //Array1[j].Number = 0;
                     Array1[j].Number = i*barrier+j+1;
                     Array1[j].Zero._X = j*wide + 6;
                     Array1[j].Zero._Y = i*wide + 6;
                 }
         }



没办法了,折腾了半天, 二级指针对二维数组,按道理来说。没有问题。
折腾了半天, 语法就是蒙不对。。。。
用二级指针对二维数组,只能初始化 一个二维数组。
并且,最后一个数字‘9’在反复使用时,数据还出错

如果初始化 俩个二维数组,就出如下错误。。。。
“System.Runtime.InteropServices.SEHException”类型的未经处理的异常出现在 NumberGame.exe 中。
其他信息: 外部组件发生异常。

实在没有办法,降级使用,二级指针变一级指针,  二维数组变一维数组。
麻烦了一些,功能可以实现了。

程序代码:
#pragma once

struct POINT
{
    int _X;
    int _Y;
};
struct UNIT
{
    POINT Zero;
    int Wide;
    int Number;
};


    private: static int barrier = 3;//表格行、列数。
    private: static int P2row;        //图片框2 表格行数    DrawChart2()函数中初始化
    private: static int P2col;        //图片框2 表格列数    DrawChart2()函数中初始化
    private: static int P2left;        //图片框2 表格左边距  DrawChart2()函数中初始化
    private: static int P2top;        //图片框2 表格右边距  DrawChart2()函数中初始化
    private: static int P2wide;        //图片框2 单元格宽度  DrawChart2()函数中初始化

    private: UNIT* Array1;
    private: UNIT* Array2;



    private: void Array1Init()
             {
              //------Array1初始化-------------
                 Array1 = new UNIT[barrier*barrier];

                 int wide = (this->pictureBox1->Width-12)/barrier;
                 for(int i=0; i<barrier; i++)
                     for(int j=0; j<barrier; j++)
                     {
                         Array1[i*barrier+j].Wide = wide;
                         //Array1[i*barrier+j].Number = 0;
                         Array1[i*barrier+j].Number = i*barrier+j+1;
                         Array1[i*barrier+j].Zero._X = j*wide + 6;
                         Array1[i*barrier+j].Zero._Y = i*wide + 6;
                     }
             }
    private: void Array2Init()
             {
                 //------Array2初始化-------------
                 Array2 = new UNIT[P2row*P2col];

                 for(int i=0; i<P2row; i++) 
                     for(int j=0; j<P2col; j++)
                     {
                         if(i*P2col+j < barrier*barrier)
                         {
                             Array2[i*P2col+j].Wide = P2wide;
                             Array2[i*P2col+j].Number = i*P2col+j+1;
                             Array2[i*P2col+j].Zero._X = j*P2wide + P2left;
                             Array2[i*P2col+j].Zero._Y = i*P2wide + P2top;
                         }
                     }
             }


图片附件: 游客没有浏览图片的权限,请 登录注册


图片附件: 游客没有浏览图片的权限,请 登录注册



[此贴子已经被作者于2022-8-3 12:29编辑过]

2022-08-03 12:25
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:133
帖 子:1204
专家分:7155
注 册:2010-3-16
收藏
得分:7 
程序代码:
#pragma once

namespace NumberGame {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    // 修改了声明位置
    typedef struct _POINT
    {
        int _X;
        int _Y;
    }POINT;

    typedef struct _UNIT
    {
        POINT Zero;
        int Wide;
        int Number;
    }UNIT, * pUNIT;    //修改了结构体声明

    struct MyStruct
    {
        POINT inPoint;
        int width, height;
        int fontSize;
    };

    /// <summary>
    /// Form1 摘要
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: 在此处添加构造函数代码
            //
        }
    protected:
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>

        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel1;
    protected:
    private: System::Windows::Forms::PictureBox^ pictureBox1;
    private: System::Windows::Forms::PictureBox^ pictureBox2;

    private:
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        System::ComponentModel::Container^ components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        void InitializeComponent(void)
        {
            this->tableLayoutPanel1 = (gcnew System::Windows::Forms::TableLayoutPanel());
            this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
            this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
            this->tableLayoutPanel1->SuspendLayout();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->BeginInit();
            this->SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this->tableLayoutPanel1->ColumnCount = 2;
            this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
                66.66666F)));
            this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
                33.33333F)));
            this->tableLayoutPanel1->Controls->Add(this->pictureBox1, 0, 0);
            this->tableLayoutPanel1->Controls->Add(this->pictureBox2, 1, 0);
            this->tableLayoutPanel1->Dock = System::Windows::Forms::DockStyle::Fill;
            this->tableLayoutPanel1->Location = System::Drawing::Point(0, 0);
            this->tableLayoutPanel1->Name = L"tableLayoutPanel1";
            this->tableLayoutPanel1->RowCount = 1;
            this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 100)));
            this->tableLayoutPanel1->Size = System::Drawing::Size(784, 522);
            this->tableLayoutPanel1->TabIndex = 0;
            // 
            // pictureBox1
            // 
            this->pictureBox1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)),
                static_cast<System::Int32>(static_cast<System::Byte>(192)));
            this->pictureBox1->Dock = System::Windows::Forms::DockStyle::Fill;
            this->pictureBox1->Location = System::Drawing::Point(3, 3);
            this->pictureBox1->Name = L"pictureBox1";
            this->pictureBox1->Size = System::Drawing::Size(516, 516);
            this->pictureBox1->TabIndex = 0;
            this->pictureBox1->TabStop = false;
            // 
            // pictureBox2
            // 
            this->pictureBox2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
                static_cast<System::Int32>(static_cast<System::Byte>(192)));
            this->pictureBox2->Dock = System::Windows::Forms::DockStyle::Fill;
            this->pictureBox2->Location = System::Drawing::Point(525, 3);
            this->pictureBox2->Name = L"pictureBox2";
            this->pictureBox2->Size = System::Drawing::Size(256, 516);
            this->pictureBox2->TabIndex = 1;
            this->pictureBox2->TabStop = false;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(784, 522);
            this->Controls->Add(this->tableLayoutPanel1);
            this->Name = L"Form1";
            this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->Resize += gcnew System::EventHandler(this, &Form1::Form1_Resize);
            this->tableLayoutPanel1->ResumeLayout(false);
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->EndInit();
            this->ResumeLayout(false);

        }
#pragma endregion


    private: static int barrier = 3;
    private: static int P2row = 0;
    private: static int P2col = 0;
    private: static int P2left = 0;
    private: static int P2top = 0;
    private: static int P2wide = 0;

    private: UNIT** Array1;
    private: UNIT** Array2;

    private: void Array1Init()
    {
        //------Array1初始化-------------
        Array1 = new pUNIT[barrier]; //修改,new指针数组
        for (int i = 0; i < barrier; i++)
        {
            Array1[i] = new UNIT[barrier];//修改 new数组
            // Array1[i]->fontSize = i;  
        }
        int wide = (this->pictureBox1->Width - 12) / barrier;
        for (int i = 0; i < barrier; i++)
            for (int j = 0; j < barrier; j++)
            {
                Array1[i][j].Wide = wide;
                //Array1[i][j].Number = 0;
                Array1[i][j].Number = i * barrier + j + 1;
                Array1[i][j].Zero._X = j * wide + 6;
                Array1[i][j].Zero._Y = i * wide + 6;
            }
    }
           //private: void Array2Init()
           //         {
           //             //------Array2初始化-------------
           //             Array2 = new UNIT*[P2row];
           //             for(int i=0; i<P2col; i++)
           //             {
           //                  Array2[i] = new UNIT;
           //                 // Array2[i]->fontSize = i;  
           //             }
           //             int wide = (this->pictureBox2->Width-2*P2left)/P2col;
           //         }
    private: MyStruct get_format(Point^ inpoint, int wide, int num)
    {

        int imgWide = (wide * 3) / 2;
        int fontsize = 0;// wide / len;

        System::Drawing::Font^ drawFont;

        SizeF layoutSize = SizeF(wide, wide);

        Graphics^ g = CreateGraphics();

        StringFormat^ sformat = gcnew StringFormat();
        sformat->Alignment = StringAlignment::Center;
        sformat->LineAlignment = StringAlignment::Center;
        sformat->FormatFlags = StringFormatFlags(0);

        SizeF sizeF(0, 0);
        while (sizeF.Width < wide)
        {
            fontsize++;
            drawFont = gcnew System::Drawing::Font("宋体", fontsize);
            sizeF = g->MeasureString(num.ToString(), drawFont, *inpoint, sformat);

            delete drawFont;
        }

        MyStruct _format;
        _format.fontSize = fontsize;
        _format.inPoint._X = inpoint->X + (wide - sizeF.Width) / 2;
        _format.inPoint._Y = inpoint->Y + (wide - sizeF.Height) / 2 + 5;
        _format.height = sizeF.Height;
        _format.width = sizeF.Width;

        delete sformat;
        delete g;

        return _format;

    }
    private: void draw_p1_num(Point^ inpoint, int wide, int num)
    {
        Bitmap^ drawimg = gcnew Bitmap(this->pictureBox1->Image);
        Graphics^ g = Graphics::FromImage(drawimg);
        SolidBrush^ MyBrush = gcnew SolidBrush(this->pictureBox1->BackColor);
        g->FillRectangle(MyBrush, Rectangle(inpoint->X + wide / 20, inpoint->Y + wide / 20, (wide * 9) / 10, (wide * 9) / 10));
        MyBrush->Color = Color::Black;

        inpoint->X += wide / 10;
        inpoint->Y += wide / 10;
        int d = (wide * 4) / 5;
        Rectangle rectEllip = Rectangle(inpoint->X, inpoint->Y, d, d);
        g->FillEllipse(MyBrush, rectEllip);

        MyStruct drawformat = get_format(inpoint, rectEllip.Width, num);
        System::Drawing::Font^ drawFont = gcnew System::Drawing::Font("宋体", drawformat.fontSize);

        Rectangle rectFont = Rectangle(drawformat.inPoint._X, drawformat.inPoint._Y, drawformat.width, drawformat.height);

        MyBrush->Color = this->pictureBox1->BackColor;
        g->DrawString(num.ToString(), drawFont, MyBrush, Point(drawformat.inPoint._X, drawformat.inPoint._Y));

        this->pictureBox1->Image = (Image^)drawimg->Clone();
        delete drawFont;
        delete g;
        delete drawimg;
    }
    private: void p1_all_out()
    {
        for (int i = 0; i < barrier; i++)
            for (int j = 0; j < barrier; j++)
            {
                draw_p1_num(Point(Array1[i][j].Zero._X, Array1[i][j].Zero._Y), Array1[i][j].Wide, Array1[i][j].Number);
            }
    }
    private: void SetMaxSize()
    {
        int maxwidth = Screen::PrimaryScreen->WorkingArea.Width;
        int maxheight = Screen::PrimaryScreen->WorkingArea.Height;
        if (maxwidth > ((maxheight - 39) * 3) / 2 + 16)
            maxwidth = ((maxheight - 39) * 3) / 2 + 16;
        else if (maxheight > ((maxwidth - 16) * 2) / 3 + 39)
            maxheight = ((maxwidth - 16) * 2) / 3 + 39;
        //this->MaximumSize.Width = maxwidth;
        //this->MaximumSize.Height = maxheight;
        this->MaximumSize = System::Drawing::Size(maxwidth, maxheight);
    }
    private: void DrawChart2(int num)
    {
        int Swidth = (this->pictureBox2->Width - 12) / num;
        int totality = ((this->pictureBox2->Width - 12) / Swidth) * ((this->pictureBox2->Height - 12) / Swidth);
        while (totality >= num * num)
        {
            Swidth++;
            totality = ((this->pictureBox2->Width - 12) / Swidth) * ((this->pictureBox2->Height - 12) / Swidth);
        }
        Swidth--;

        int col = (this->pictureBox2->Width - 12) / Swidth;
        int row = (barrier * barrier) % col == 0 ? (barrier * barrier) / col : (barrier * barrier) / col + 1;
        int left = (this->pictureBox2->Width - col * Swidth) / 2;
        int    top = (this->pictureBox2->Height - row * Swidth) / 2;
        P2col = col;
        P2row = row;
        P2left = left;
        P2top = top;
        P2wide = Swidth;

        Bitmap^ drawimg = gcnew Bitmap(this->pictureBox2->Width, this->pictureBox2->Height);
        Graphics^ g = Graphics::FromImage(drawimg);
        Pen^ mypen = gcnew Pen(Color::Green, 1);

        for (int i = 0; i <= row; i++)
            g->DrawLine(mypen, Point(left, i * Swidth + top), Point(col * Swidth + left, i * Swidth + top));
        for (int i = 0; i <= col; i++)
            g->DrawLine(mypen, Point(i * Swidth + left, top), Point(i * Swidth + left, row * Swidth + top));

        this->pictureBox2->Image = (Image^)drawimg->Clone();
        delete g;
        delete drawimg;
    }
    private: void DrawChart(int num)
    {

        Bitmap^ drawimg = gcnew Bitmap(pictureBox1->Width, pictureBox1->Height);
        Graphics^ g = Graphics::FromImage(drawimg);

        Pen^ mypen = gcnew Pen(Color::Red, 1);
        int Swidth = (pictureBox1->Width - 12) / num;
        for (int i = 0; i <= num; i++)
            g->DrawLine(mypen, Point(6, i * Swidth + 6), Point(num * Swidth + 6, i * Swidth + 6));
        for (int i = 0; i <= num; i++)
            g->DrawLine(mypen, Point(i * Swidth + 6, 6), Point(i * Swidth + 6, num * Swidth + 6));

        this->pictureBox1->Image = (Image^)drawimg->Clone();

        delete g;
        delete drawimg;
    }
    private: void calc_point()
    {
        int wide = (this->pictureBox1->Width - 12) / barrier;
        for (int i = 0; i < barrier; i++)
            for (int j = 0; j < barrier; j++)
            {
                Array1[i][j].Wide = wide;
                //Array1[i][j].Number = 0;
                //Array1[i][j].Number = i*barrier+j+1;
                Array1[i][j].Zero._X = j * wide + 6;
                Array1[i][j].Zero._Y = i * wide + 6;
            }
    }
    private: System::Void Form1_Resize(System::Object^ sender, System::EventArgs^ e)
    {
        //Control^ control = dynamic_cast<Control^>(sender);
        
        //拖动窗口比例缩放,确保pictureBox1 Size 为正方形.
        int swidth = this->Size.Width; // Form事件中的sender就是Form本身
        int sheight = this->Size.Height;


        if (sheight > 28) //sheight > 28 窗口不是最小化。
        {
            int maxwidth = this->MaximumSize.Width ;//Screen::PrimaryScreen->WorkingArea.Width;    //
            int maxheight =  this->MaximumSize.Height;//Screen::PrimaryScreen->WorkingArea.Height;    //

            if (swidth > maxwidth)
                swidth = maxwidth;

            sheight = ((swidth - 16) * 2) / 3 + 39;

            if (sheight > maxheight)
            {
                sheight = maxheight;
                swidth = ((sheight - 39) * 3) / 2 + 16;
            }

            this->Size = System::Drawing::Size(swidth, sheight);

            //this->pictureBox1->Image=nullptr;
            //this->pictureBox2->Image=nullptr;
            DrawChart(barrier);
            DrawChart2(barrier);
            calc_point();
            p1_all_out();
        }
    }
    private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
    {
        SetMaxSize();            //设置窗口最大尺寸
        DrawChart(barrier);    //绘制图片框1,的表格
        DrawChart2(barrier);    //绘制图片框2,的表格
        Array1Init();            //Array1数组初始化
        //Array2Init();            //Array2数组初始化

        p1_all_out();            //输出图片框1全部单元格。
    }
    };
}




2022-08-03 22:05
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:195
专家分:101
注 册:2021-4-9
收藏
得分:0 
回复 5楼 apull
初始化一个二维数组。正常。。。。
最后一个数字‘9’不会再出错。。。。

初始化俩个二维数组, 就出错。。。。

图片附件: 游客没有浏览图片的权限,请 登录注册


程序代码:
#pragma once

//struct POINT
//{
//    int _X;
//    int _Y;
//};
//struct UNIT
//{
//    POINT Zero;
//    int Wide;
//    int Number;
//};
//struct MyStruct
//{
//    POINT inPoint;
//    int width, height;
//    int fontSize;
//};
namespace test {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    // 修改了声明位置
    typedef struct _POINT
    {
        int _X;
        int _Y;
    }POINT;

    typedef struct _UNIT
    {
        POINT Zero;
        int Wide;
        int Number;
    }UNIT, * pUNIT;    //修改了结构体声明

    struct MyStruct
    {
        POINT inPoint;
        int width, height;
        int fontSize;
    };
    /// <summary>
    /// Form1 摘要
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: 在此处添加构造函数代码
            //
        }

    protected:
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::TableLayoutPanel^  tableLayoutPanel1;
    protected: 
    private: System::Windows::Forms::PictureBox^  pictureBox1;
    private: System::Windows::Forms::PictureBox^  pictureBox2;
    private:
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        void InitializeComponent(void)
        {
            this->tableLayoutPanel1 = (gcnew System::Windows::Forms::TableLayoutPanel());
            this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
            this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
            this->tableLayoutPanel1->SuspendLayout();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox2))->BeginInit();
            this->SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this->tableLayoutPanel1->ColumnCount = 2;
            this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent, 
                66.66666F)));
            this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent, 
                33.33333F)));
            this->tableLayoutPanel1->Controls->Add(this->pictureBox1, 0, 0);
            this->tableLayoutPanel1->Controls->Add(this->pictureBox2, 1, 0);
            this->tableLayoutPanel1->Dock = System::Windows::Forms::DockStyle::Fill;
            this->tableLayoutPanel1->Location = System::Drawing::Point(0, 0);
            this->tableLayoutPanel1->Name = L"tableLayoutPanel1";
            this->tableLayoutPanel1->RowCount = 1;
            this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 100)));
            this->tableLayoutPanel1->Size = System::Drawing::Size(784, 522);
            this->tableLayoutPanel1->TabIndex = 0;
            // 
            // pictureBox1
            // 
            this->pictureBox1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)), 
                static_cast<System::Int32>(static_cast<System::Byte>(192)));
            this->pictureBox1->Dock = System::Windows::Forms::DockStyle::Fill;
            this->pictureBox1->Location = System::Drawing::Point(3, 3);
            this->pictureBox1->Name = L"pictureBox1";
            this->pictureBox1->Size = System::Drawing::Size(516, 516);
            this->pictureBox1->TabIndex = 0;
            this->pictureBox1->TabStop = false;
            // 
            // pictureBox2
            // 
            this->pictureBox2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)), 
                static_cast<System::Int32>(static_cast<System::Byte>(192)));
            this->pictureBox2->Dock = System::Windows::Forms::DockStyle::Fill;
            this->pictureBox2->Location = System::Drawing::Point(525, 3);
            this->pictureBox2->Name = L"pictureBox2";
            this->pictureBox2->Size = System::Drawing::Size(256, 516);
            this->pictureBox2->TabIndex = 1;
            this->pictureBox2->TabStop = false;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(784, 522);
            this->Controls->Add(this->tableLayoutPanel1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->Resize += gcnew System::EventHandler(this, &Form1::Form1_Resize);
            this->tableLayoutPanel1->ResumeLayout(false);
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox2))->EndInit();
            this->ResumeLayout(false);
        }
#pragma endregion
    private: static int barrier = 3;//表格行、列数。
    private: static int P2row;        //图片框2 表格行数    DrawChart2()函数中初始化
    private: static int P2col;        //图片框2 表格列数    DrawChart2()函数中初始化
    private: static int P2left;        //图片框2 表格左边距  DrawChart2()函数中初始化
    private: static int P2top;        //图片框2 表格右边距  DrawChart2()函数中初始化
    private: static int P2wide;        //图片框2 单元格宽度  DrawChart2()函数中初始化

    private: UNIT** Array1;
    private: UNIT** Array2;

    private: void Array1Init()
             {
                 //------Array1初始化-------------
                 Array1 = new pUNIT[barrier];
                 for (int i = 0; i < barrier; i++)
                 {
                     Array1[i] = new UNIT[barrier];//修改 new数组
                 }
                 int wide = (this->pictureBox1->Width-12)/barrier;
                 for(int i=0; i<barrier; i++)
                     for(int j=0; j<barrier; j++)
                     {
                         Array1[i][j].Wide = wide;
                         //Array1[i][j].Number = 0;
                         Array1[i][j].Number = i*barrier+j+1;
                         Array1[i][j].Zero._X = j*wide + 6;
                         Array1[i][j].Zero._Y = i*wide + 6;
                     }
             }
    private: void Array2Init()
             {
                 //------Array2初始化-------------
                 Array2 = new pUNIT[P2row]; //修改,new指针数组
                 for (int i = 0; i < P2row; i++)
                 {
                     Array1[i] = new UNIT[P2col];//修改 new数组
                     // Array1[i]->fontSize = i;  
                 }
                 for (int i = 0; i < P2row; i++)
                     for (int j = 0; j < P2col; j++)
                     {
                         if( i * P2col + j < barrier*barrier)
                         {
                             Array2[i][j].Wide = P2wide;
                             //Array2[i][j].Number = 0;
                             Array2[i][j].Number = i * P2col + j + 1;
                             Array2[i][j].Zero._X = j * P2wide + P2left;
                             Array2[i][j].Zero._Y = i * P2wide + P2top;
                         }
                     }
             }
    private: MyStruct get_format(Point^ inpoint, int wide, int num)
    {

        int imgWide = (wide * 3) / 2;
        int fontsize = 0;// wide / len;

        System::Drawing::Font^ drawFont;

        SizeF layoutSize = SizeF(wide, wide);

        Graphics^ g = CreateGraphics();

        StringFormat^ sformat = gcnew StringFormat();
        sformat->Alignment = StringAlignment::Center;
        sformat->LineAlignment = StringAlignment::Center;
        sformat->FormatFlags = StringFormatFlags(0);

        SizeF sizeF(0, 0);
        while (sizeF.Width < wide)
        {
            fontsize++;
            drawFont = gcnew System::Drawing::Font("宋体", fontsize);
            sizeF = g->MeasureString(num.ToString(), drawFont, *inpoint, sformat);

            delete drawFont;
        }

        MyStruct _format;
        _format.fontSize = fontsize;
        _format.inPoint._X = inpoint->X + (wide - sizeF.Width) / 2;
        _format.inPoint._Y = inpoint->Y + (wide - sizeF.Height) / 2 + 5;
        _format.height = sizeF.Height;
        _format.width = sizeF.Width;

        delete sformat;
        delete g;

        return _format;

    }
    private: void draw_p1_num(Point^ inpoint, int wide, int num)
    {
        Bitmap^ drawimg = gcnew Bitmap(this->pictureBox1->Image);
        Graphics^ g = Graphics::FromImage(drawimg);
        SolidBrush^ MyBrush = gcnew SolidBrush(this->pictureBox1->BackColor);
        g->FillRectangle(MyBrush, Rectangle(inpoint->X + wide / 20, inpoint->Y + wide / 20, (wide * 9) / 10, (wide * 9) / 10));
        MyBrush->Color = Color::Black;

        inpoint->X += wide / 10;
        inpoint->Y += wide / 10;
        int d = (wide * 4) / 5;
        Rectangle rectEllip = Rectangle(inpoint->X, inpoint->Y, d, d);
        g->FillEllipse(MyBrush, rectEllip);


        MyStruct drawformat = get_format(inpoint, rectEllip.Width, num);
        System::Drawing::Font^ drawFont = gcnew System::Drawing::Font("宋体", drawformat.fontSize);
        Rectangle rectFont = Rectangle(drawformat.inPoint._X, drawformat.inPoint._Y, drawformat.width, drawformat.height);

        MyBrush->Color = this->pictureBox1->BackColor;
        g->DrawString(num.ToString(), drawFont, MyBrush, Point(drawformat.inPoint._X, drawformat.inPoint._Y));

        this->pictureBox1->Image = (Image^)drawimg->Clone();
        delete drawFont;
        delete g;
        delete drawimg;
    }
    //private: void draw_p2_num(Point^ inpoint, int wide, int num)
    //         {
    //             Bitmap^ drawimg = gcnew Bitmap(this->pictureBox2->Image);
    //             Graphics^ g = Graphics::FromImage(drawimg);
    //             SolidBrush^ MyBrush = gcnew SolidBrush(this->pictureBox2->BackColor);
    //             g->FillRectangle(MyBrush, Rectangle(inpoint->X + wide / 20, inpoint->Y + wide / 20, (wide * 9) / 10, (wide * 9) / 10));
    //             MyBrush->Color = Color::Black;

    //             inpoint->X += wide / 10;
    //             inpoint->Y += wide / 10;
    //             int d = (wide * 4) / 5;
    //             Rectangle rectEllip = Rectangle(inpoint->X, inpoint->Y, d, d);
    //             g->FillEllipse(MyBrush, rectEllip);


    //             MyStruct drawformat = get_format(inpoint, rectEllip.Width, num);
    //             System::Drawing::Font^ drawFont = gcnew System::Drawing::Font("宋体", drawformat.fontSize);
    //             Rectangle rectFont = Rectangle(drawformat.inPoint._X, drawformat.inPoint._Y, drawformat.width, drawformat.height);

    //             MyBrush->Color = this->pictureBox2->BackColor;
    //             g->DrawString(num.ToString(), drawFont, MyBrush, Point(drawformat.inPoint._X, drawformat.inPoint._Y));

    //             this->pictureBox2->Image = (Image^)drawimg->Clone();
    //             delete drawFont;
    //             delete g;
    //             delete drawimg;
    //         }
    private: void p1_all_out()
             {
                 for(int i=0; i<barrier; i++)
                    for(int j=0; j<barrier; j++)
                    {
                        draw_p1_num(Point(Array1[i][j].Zero._X, Array1[i][j].Zero._Y), Array1[i][j].Wide, Array1[i][j].Number);
                    }
             }
    //private: void p2_all_out()
    //         {
    //             for(int i=0; i<P2row; i++)
    //                for(int j=0; j<P2col; j++)
    //                {
    //                    if(i*P2col+j < barrier*barrier)
    //                        draw_p2_num(Point( Array2[i][j].Zero._X,  Array2[i][j].Zero._Y),  Array2[i][j].Wide,  Array2[i][j].Number);
    //                }
    //         }
    private: void SetMaxSize()
             {
                int maxwidth = Screen::PrimaryScreen->WorkingArea.Width;
                int maxheight = Screen::PrimaryScreen->WorkingArea.Height;
                if(maxwidth > ((maxheight - 39)*3)/2+16)
                    maxwidth = ((maxheight - 39)*3)/2+16;
                else if(maxheight> ((maxwidth-16)*2)/3+39)
                    maxheight = ((maxwidth-16)*2)/3+39;
                this->MaximumSize = System::Drawing::Size( maxwidth, maxheight);
             }
    private: void DrawChart2(int num)
    {
        int Swidth = (this->pictureBox2->Width - 12) / num;
        int totality = ((this->pictureBox2->Width - 12) / Swidth) * ((this->pictureBox2->Height - 12) / Swidth);
        while (totality >= num * num)
        {
            Swidth++;
            totality = ((this->pictureBox2->Width - 12) / Swidth) * ((this->pictureBox2->Height - 12) / Swidth);
        }
        Swidth--;
        
        int col = (this->pictureBox2->Width - 12) / Swidth;
        int row = (barrier * barrier) % col == 0 ? (barrier * barrier) / col : (barrier * barrier) / col + 1;
        int left = (this->pictureBox2->Width - col * Swidth) / 2;
        int    top = (this->pictureBox2->Height - row * Swidth) / 2;
        P2col = col;
        P2row = row;
        P2left = left;
        P2top = top;
        P2wide = Swidth;

        Bitmap^ drawimg = gcnew Bitmap(this->pictureBox2->Width, this->pictureBox2->Height);
        Graphics^ g = Graphics::FromImage(drawimg);
        Pen^ mypen = gcnew Pen(Color::Green, 1);

        for (int i = 0; i <= row; i++)
            g->DrawLine(mypen, Point(left, i * Swidth + top), Point(col * Swidth + left, i * Swidth + top));
        for (int i = 0; i <= col; i++)
            g->DrawLine(mypen, Point(i * Swidth + left, top), Point(i * Swidth + left, row * Swidth + top));

        this->pictureBox2->Image = (Image^)drawimg->Clone();
        delete g;
        delete drawimg;
    }
    private: void DrawChart(int num)
    {

        Bitmap^ drawimg = gcnew Bitmap(pictureBox1->Width, pictureBox1->Height);
        Graphics^ g = Graphics::FromImage(drawimg);

        Pen^ mypen = gcnew Pen(Color::Red, 1);
        int Swidth = (pictureBox1->Width - 12) / num;
        for (int i = 0; i <= num; i++)
            g->DrawLine(mypen, Point(6, i * Swidth + 6), Point(num * Swidth + 6, i * Swidth + 6));
        for (int i = 0; i <= num; i++)
            g->DrawLine(mypen, Point(i * Swidth + 6, 6), Point(i * Swidth + 6, num * Swidth + 6));

        this->pictureBox1->Image = (Image^)drawimg->Clone();

        delete g;
        delete drawimg;
    }
    private: void calc_point()
             {
                 int wide = (this->pictureBox1->Width-12)/barrier;
                 for(int i=0; i<barrier; i++)
                     for(int j=0; j<barrier; j++)
                     {
                         Array1[i][j].Wide = wide;
                         Array1[i][j].Zero._X = j*wide + 6;
                         Array1[i][j].Zero._Y = i*wide + 6;
                     }
                //for(int i=0; i<P2row; i++) 
                     //for(int j=0; j<P2col; j++)
                     //{
                        // if(i*P2col+j < barrier*barrier)
                        // {
                        //      Array2[i][j].Wide = P2wide;
                        //      Array2[i][j].Zero._X = j*P2wide + P2left;
                        //      Array2[i][j].Zero._Y = i*P2wide + P2top;
                        // }
                     //}
             }
    private: System::Void Form1_Resize(System::Object^  sender, System::EventArgs^  e) 
             {
                 Control^ control = dynamic_cast<Control^>(sender);

                 //拖动窗口比例缩放,确保pictureBox1 Size 为正方形.
                 int swidth = control->Size.Width;
                 int sheight = control->Size.Height;


                 if(sheight > 28) //sheight > 28 窗口不是最小化。
                 {
                     int maxwidth = Screen::PrimaryScreen->WorkingArea.Width;
                     int maxheight = Screen::PrimaryScreen->WorkingArea.Height;

                     if(swidth > maxwidth)
                        swidth = maxwidth;

                     sheight = ((swidth-16)*2)/3+39;

                     if(sheight>maxheight)
                     {
                         sheight = maxheight;
                         swidth = ((sheight - 39)*3)/2+16;
                     }
                     
                     control->Size = System::Drawing::Size(swidth, sheight);

                     //this->pictureBox1->Image=nullptr;
                     //this->pictureBox2->Image=nullptr;
                     DrawChart(barrier);
                     DrawChart2(barrier);
                     calc_point();
                     p1_all_out();
                     //p2_all_out();
                 }
             }
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
             {
                 SetMaxSize();            //设置窗口最大尺寸
                 DrawChart(barrier);    //绘制图片框1,的表格
                 DrawChart2(barrier);    //绘制图片框2,的表格
                 Array1Init();            //Array1数组初始化
                 Array2Init();            //Array2数组初始化

                p1_all_out();            //输出图片框1全部单元格。
                //p2_all_out();            //输出图片框2全部单元格。
             }
    };
}

2022-08-03 23:12
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:133
帖 子:1204
专家分:7155
注 册:2010-3-16
收藏
得分:6 
图片附件: 游客没有浏览图片的权限,请 登录注册
2022-08-03 23:42
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:195
专家分:101
注 册:2021-4-9
收藏
得分:0 
回复 7楼 apull
终于找到原因了。
以后复制,粘贴要小心仔细了。。。。
2022-08-04 00:00
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:195
专家分:101
注 册:2021-4-9
收藏
得分:0 
回复 2楼 apull
求助
自定义 MessageBox 消息框按钮。。。。
https://bbs.bccn.net/viewthread.php?tid=509725&page=1&extra=page%3D1#pid2764627
2022-08-05 21:04
快速回复:求助 结构体二维数组
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.110546 second(s), 9 queries.
Copyright©2004-2022, BCCN.NET, All Rights Reserved