c# ピクチャーボックスをウインドウサイズに合わせて増やす

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
もも

c# ピクチャーボックスをウインドウサイズに合わせて増やす

#1

投稿記事 by もも » 13年前

ピクチャーボックスをウインドウサイズに合わせて増やそうとしてpicturebocを動的に配置したのですがフォームのサイズを変更してボタン1を押すとその前に配置したpictureboxがそのまま残ってしまいます。
ピクチャーボックスをクリックすると名前が出てきますのでやってみてください。
piturebox1,piturebox2,piturebox3,piturebox4,iturebox5,piturebox6 次の行に行ってピクチャーボックスをクリックすると
piturebox3,piturebox4,iturebox9,piturebox10,iturebox11,piturebox12
のようになってしまいます。

コード:

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;

namespace map
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //ボタンコントロール配列のフィールドを作成
        private PictureBox[] pictures = new PictureBox[0];
        
        private void Form1_Load(object sender, EventArgs e)
        {
            create_picturebox();
        }

        private void Form_ResizeEnd(object sender, EventArgs e)
        { 
        
        }

        private void create_picturebox() 
        {

            // フォームのクライアント領域のサイズを取得する
            System.Drawing.Size tSize = this.ClientSize;

            int Height = Size.Height / 255 + 1;
            int Width = Size.Width / 255 + 1;
            int Pic_count = 0;
            // 取得したフォームのクライアント領域のサイズを表示する
            //MessageBox.Show(tSize.ToString());

            for (int n1 = 1; n1 <= Height; n1++)
            {
                for (int n2 = 1; n2 <= Width; n2++)
                {
                    Pic_count++;
                    Array.Resize(ref this.pictures, Pic_count + 1);
                    this.pictures[Pic_count] = new System.Windows.Forms.PictureBox();
                    this.pictures[Pic_count].Location = new System.Drawing.Point((n2 - 1) * 255, (n1 - 1) * 255);
                    this.pictures[Pic_count].Name = "pictureBox" + Pic_count.ToString();
                    this.pictures[Pic_count].Size = new System.Drawing.Size(255, 255);
                    this.pictures[Pic_count].TabIndex = Pic_count;
                    this.pictures[Pic_count].TabStop = true;
                    this.pictures[Pic_count].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    //↓コレがないとだめ
                    this.pictures[Pic_count].Click += new System.EventHandler(this.pictureBox_Click);
                    this.Controls.Add(this.pictures[Pic_count]);
                    this.pictures[Pic_count].Show();


                }

            }
        
        
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            //senderはクリックされたピクチャボックスのどれか
            PictureBox pict = sender as PictureBox;
            MessageBox.Show(pict.Name);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            create_picturebox();
        }

    }
}


もも

Re: c# ピクチャーボックスをウインドウサイズに合わせて増やす

#2

投稿記事 by もも » 13年前

もう一つ有りました。
フォームのリサイズを検知してpictureboxを増やしたり減らしたりしたいです。

コード:

private void Form_ResizeEnd(object sender, EventArgs e)
        { 
        
        }

を使えばいいらしいのですがうまく動きません

もも

Re: c# ピクチャーボックスをウインドウサイズに合わせて増やす

#3

投稿記事 by もも » 13年前

下記のようにすることで前に配置したpictureboxがそのまま残ってしまうことは解決しました。
あとは、フォームのリサイズを検知してpictureboxを増やしたり減らしたりしたいです。

コード:

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;

namespace map
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //ボタンコントロール配列のフィールドを作成
        private PictureBox[] pictures = new PictureBox[0];
        int Pic_count = 0;
        
        private void Form1_Load(object sender, EventArgs e)
        {
            create_picturebox();
        }

        private void Form_ResizeEnd(object sender, EventArgs e)
        { 
        
        }

        private void create_picturebox() 
        {

            // フォームのクライアント領域のサイズを取得する
            System.Drawing.Size tSize = this.ClientSize;

            int Height = Size.Height / 255 + 1;
            int Width = Size.Width / 255 + 1;

            Pic_count = 0;
            // 取得したフォームのクライアント領域のサイズを表示する
            //MessageBox.Show(tSize.ToString());

            for (int n1 = 1; n1 <= Height; n1++)
            {
                for (int n2 = 1; n2 <= Width; n2++)
                {
                    Pic_count++;
                    Array.Resize(ref this.pictures, Pic_count + 1);
                    this.pictures[Pic_count] = new System.Windows.Forms.PictureBox();
                    this.pictures[Pic_count].Location = new System.Drawing.Point((n2 - 1) * 255, (n1 - 1) * 255);
                    this.pictures[Pic_count].Name = "pictureBox" + Pic_count.ToString();
                    this.pictures[Pic_count].Size = new System.Drawing.Size(255, 255);
                    this.pictures[Pic_count].TabIndex = Pic_count;
                    this.pictures[Pic_count].TabStop = true;
                    this.pictures[Pic_count].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    //↓コレがないとだめ
                    this.pictures[Pic_count].Click += new System.EventHandler(this.pictureBox_Click);
                    this.Controls.Add(this.pictures[Pic_count]);
                    this.pictures[Pic_count].Show();


                }

            }
        
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            //senderはクリックされたピクチャボックスのどれか
            PictureBox pict = sender as PictureBox;
            MessageBox.Show(pict.Name);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            for (; Pic_count >= 0; Pic_count--)
            {
                this.Controls.Remove(this.pictures[Pic_count]);
            }
                create_picturebox();
        }

    }
}


もも

Re: c# ピクチャーボックスをウインドウサイズに合わせて増やす

#4

投稿記事 by もも » 13年前

一応ウインドウサイスに合わせてピクチャーボックスを作ろことができました。
下記のプログラムはマウスでドラッグしてピクチャーボックス全体を移動する機能もついています。
ただちゃんと動きません。
これに関してはhttp://dixq.net/forum/viewtopic.php?f=3&t=11457で質問しています。

コード:

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;

namespace map
{
    public partial class Form1 : Form
    {
        //ボタンコントロール配列のフィールドを作成
        private PictureBox[] pictures = new PictureBox[0];
        int Pic_count = 0;
        private bool resize_flag = false;

        public Form1()
        {
            InitializeComponent();

        }

        
        
        private void Form1_Load(object sender, EventArgs e)
        {
            create_picturebox(0,0);
        }

        private void create_picturebox(int custom_x,int custom_y) 
        {

            // フォームのクライアント領域のサイズを取得する
            System.Drawing.Size tSize = this.ClientSize;

            int Height = Size.Height / 255 + 1;
            int Width = Size.Width / 255 + 1;
            
            Pic_count = 0;
            // 取得したフォームのクライアント領域のサイズを表示する
            //MessageBox.Show(tSize.ToString());

            for (int n1 = 1; n1 <= Height; n1++)
            {
                for (int n2 = 1; n2 <= Width; n2++)
                {
                    Pic_count++;
                    Array.Resize(ref this.pictures, Pic_count + 1);
                    this.pictures[Pic_count] = new System.Windows.Forms.PictureBox();
                    this.pictures[Pic_count].Location = new System.Drawing.Point((n2 - 1) * 255 + custom_x, (n1 - 1) * 255 + custom_x);
                    this.pictures[Pic_count].Name = "pictureBox" + Pic_count.ToString();
                    this.pictures[Pic_count].Size = new System.Drawing.Size(255, 255);
                    this.pictures[Pic_count].TabIndex = Pic_count;
                    this.pictures[Pic_count].TabStop = true;
                    this.pictures[Pic_count].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    //↓コレがないとだめ
                    this.pictures[Pic_count].Click += new System.EventHandler(this.pictureBox_Click);
                    this.Controls.Add(this.pictures[Pic_count]);
                    this.pictures[Pic_count].Show();
                    this.pictures[Pic_count].MouseDown += new MouseEventHandler(PictureBox_MouseDown);
                    this.pictures[Pic_count].MouseMove += new MouseEventHandler(PictureBox_MouseMove);

                }

            }
        
        }

        private void remove_picturebox() {
            for (; Pic_count >= 0; Pic_count--)
            {
                this.Controls.Remove(this.pictures[Pic_count]);
            }
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            //senderはクリックされたピクチャボックスのどれか
            //PictureBox pict = sender as PictureBox;
            //MessageBox.Show(pict.Name);
        }

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            const int WM_EXITSIZEMOVE = 0x0232;
            const int WM_SYSCOMMAND = 0x112;
            const int SC_RESTORE = 0xf120;
            const int SC_MINIMIZE = 0xf020;
            const int SC_MAXIMIZE = 0xf030;

            if (m.Msg == WM_SYSCOMMAND)
            {
                int wparam = m.WParam.ToInt32() & 0xfff0;
                switch (wparam)
                {
                    case SC_MINIMIZE:
                        // 最小化する前
                        base.WndProc(ref m); // ここをコメント化すると最小化されない
                        // 最小化した後
                        return;

                    case SC_MAXIMIZE:
                        // 最大化する前
                        base.WndProc(ref m); // ここをコメント化すると最大化されない
                        // 最大化した後
                        remove_picturebox();
                        create_picturebox(0,0);
                        return;

                    case SC_RESTORE:
                        // 元のサイズに戻す前
                        base.WndProc(ref m); // ここをコメント化すると元のサイズに戻されない
                        // 元のサイズに戻した後
                        remove_picturebox();
                        create_picturebox(0,0);
                        return;

                }
            }

            if (m.Msg == WM_EXITSIZEMOVE && resize_flag)
            {
                //textBox1.Text += "リサイズ完了\r\n";

                remove_picturebox();
                create_picturebox(0,0);

                resize_flag = false;
            }

            base.WndProc(ref m);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (Visible == true) resize_flag = true;
        }

        // マウスポインタの位置を保存する
        private Point mousePoint;

        //マウスのボタンが押されたとき
        private void PictureBox_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                //位置を記憶する
                mousePoint = new Point(e.X, e.Y);
            }
        }

        //マウスが動いたとき
        private void PictureBox_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                    remove_picturebox();
                    create_picturebox(e.X - mousePoint.X, e.Y - mousePoint.Y);
            }
        }
          
    }
}


閉鎖

“C言語何でも質問掲示板” へ戻る