Visual C# 2008を使用しています。
ボタンを押すと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 algorithm
{
public partial class Form1 : Form
{
int space = 10;
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void kihonsyori_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image == null)
{
Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\基本処理箱.png");
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height);
space += 50;
img.Dispose();
g.Dispose();
}
else
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void hanpuku_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image == null)
{
Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\反復箱.png");
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height);
space += 80;
img.Dispose();
g.Dispose();
}
else
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
}
private void sentaku_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image == null)
{
Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\選択箱.png");
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height);
space += 80;
img.Dispose();
g.Dispose();
}
else
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
}
private void sakujyo_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
space = 0;
}
}
}
No.1ベストアンサー
- 回答日時:
こんにちは。
別口で実験台のC#のプロジェクトを作成しても良いのならば、以下で試せませんか。
pictureBox1, button1(読み込み), button2(削除)が必要です。参考程度で。
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//スタートアップ
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.Image = new Bitmap("test.bmp");
this.pictureBox1.Invalidate();
this.button2.Enabled = false;
}
//読み込みボタン
private void button1_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image != null)
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = new Bitmap("test.bmp");
this.pictureBox1.Invalidate();
}
//削除ボタン
private void button2_Click(object sender, EventArgs e)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
this.pictureBox1.Invalidate();
this.button2.Enabled = false;
}
//ピクチャーボックスをクリックする
private void pictureBox1_Click(object sender, EventArgs e)
{
if (this.pictureBox1.Image == null)
return;
this.button2.Enabled ^= true;
}
}
}
回答ありがとうございます。
書いていただいたプログラムを参考にして、プログラムを組みなおしてみました。
しかし、picturebox自体をクリックしてもボタンが押せるようになってしまいます。picturebox内で画像のない場所をクリックしてもボタンが押せてしまうという状況です。
pictureboxに複数の画像を表示させ、一つ一つの画像をクリックできるようにしたいと思っています。
うまく参考にできず申し訳ないのですが、教えていただけないでしょうか?
No.2
- 回答日時:
こんばんは。
御礼頂きました。サムネイルの様な処理をしたいのでしょうか。
不可能ではないのですが、プログラム内でビットマップの配列と、それぞれの位置を保持しておき、ピクチャーボックスのクリックイベントで、マウスの辺り判定を計測するなど、モグラ叩きの様なプログラムをする必要があります。
どちらにしろ、ピクチャーボックスは複数の絵を出すのに向いていないのは確かです。
リストビューでは駄目なのでしょうか。
http://oshiete1.goo.ne.jp/qa5400139.html
上記URLの様にビットマップを並べて表示出来ますし、一貫した操作方法で追加、削除が行えます。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
int main()、void main()、void...
-
多重定義が起きている?--lnk20...
-
静的でないメンバ関数の呼び出...
-
gcc: incompatible pointer type
-
既定のコンストラクタがありま...
-
コールバックって・・・
-
C# Controls.Addで動的に配置し...
-
C/C++でのScene管理について
-
C言語 ① 5秒間 1秒間隔で点滅を...
-
C言語 extern について
-
voidポインタをハンドルへ
-
【gcc・cygwin】multiple defin...
-
戻り値を返す関数の前に(void)...
-
C++にてtemplateで受け取った任...
-
ArduinoでMouse関数を使用して...
-
コンパイルエラー: LNK2001
-
マルチメディアタイマー
-
関数の戻り値にその関数のポイ...
-
動的に作ったリッチエディット...
-
アルディーノ初心者です。 下記...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
【gcc・cygwin】multiple defin...
-
C++にてtemplateで受け取った任...
-
戻り値を返す関数の前に(void)...
-
静的でないメンバ関数の呼び出...
-
多重定義が起きている?--lnk20...
-
既定のコンストラクタがありま...
-
C# Controls.Addで動的に配置し...
-
int main()、void main()、void...
-
void*型の配列について
-
(void)0 はどんな意味ですか
-
gcc: incompatible pointer type
-
C# KeyDownイベントでショート...
-
C言語 ① 5秒間 1秒間隔で点滅を...
-
Notepad++の関数リスト表示の変...
-
コールバックって・・・
-
マルチメディアタイマーの使用方法
-
_beginthreadにて発生するコン...
-
C#でラジオボタンを設定に記録...
-
VC++でGetKeyboardStateがうま...
-
typedef unsigned int UINT;が...
おすすめ情報