下記のようなプログラムを作ったのですが、なめ方向しか動かなクて困っています。
また移動するときに新たにピクチャーボックスを作っているので効率が悪いような気がしています。
これも解決したいです。
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);
}
}
}
}