電子書籍の厳選無料作品が豊富!

ほぼ独学でVisual Studio .NET 2003 を使ってC#を勉強しています。


フォーム内でマウスクリックするごとにGraphics.DrawImageで描画した画像を表示していきたいのですが、

'System.Windows.Form.MouseEventArgs'に'Graphics'の定義がありません。

というエラーが出てくるので2枚目以降を写す事ができません。
クリックして座標を得たり、DrawImageを使うだけなら問題はないのですが、二つを組み合わせようとするとうまくいかないきません。
クリックしてGraphicsが使える方法知っていましたら教えてください。

勉強してきた日数が少ないので初歩的な質問だと思いますが、ご存知の方いらっしゃいましたら、よろしくおねがいします。

A 回答 (1件)

//こっちで使っているのC# 2.0とかC# 3.0なので


//動かなかったら申し訳ない。自分ならこう書くということで。

//画像作るの面倒だからDrawPolygonを使ってますが,Graphics使っているところまであればわかるよね。
//無理にMouseClickで使おうとしなくてもいいんじゃない?ってこと。

//ひょっとしたらSystem.Windows.Forms.Form.CreateGraphicsとか出来るかもしれないけど
//使い方をよく知らない


namespace Q4325074A
{
class Q4325074A:System.Windows.Forms.Form
{

System.Collections.Generic.List<System.Drawing.PointF> plist = new System.Collections.Generic.List<System.Drawing.PointF>();

Q4325074A(){
plist.Add(new System.Drawing.PointF((float)100,(float)100));
plist.Add(new System.Drawing.PointF((float)200,(float)200));
plist.Add(new System.Drawing.PointF((float)200,(float)300));
this.MouseClick += new System.Windows.Forms.MouseEventHandler(Q4325074A_MouseClick);
this.Paint += new System.Windows.Forms.PaintEventHandler(Q4325074A_Paint);

}

public void Q4325074A_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e){
plist.Add(new System.Drawing.PointF((float)e.X,(float)e.Y));
this.Refresh();
}
public void Q4325074A_Paint(object sender, System.Windows.Forms.PaintEventArgs e){
e.Graphics.Clear(System.Drawing.SystemColors.Control);
if (plist.Count >= 3){
e.Graphics.DrawPolygon(System.Drawing.Pens.Black,plist.ToArray());
}
}

public static void Main(string[] args)
{
Q4325074A Form1 = new Q4325074A();
Form1.ShowDialog();
}
}
}

この回答への補足

//himajin100000さんに申し訳ないのですが、私では理解できませんでした。
//私が作成したソースはこのような感じなのですが、//1 だとaiai1.jpgを表示してabcdeとコメントを書いたものが表示されます。
//これ//2を書き加えて、クリック(やキー)操作で次のaiai2.jpgを表示させてfghijをコメントとして表示させたいのです。次にaiai3..aiai4..aiti5..と続けていきたいのですが
//Protected override void OnMouseDown(MouseEventArgs e)の中でGraphicsが使えないのです。
//もし、お時間に余裕ありましたら再度教えて頂けないでしょうか

//1
using System.Drawing;
using System.Windows.Forms;

public class Test : Form
{
private Image image;

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if(image == null) image = Image.FromFile("aiai1.jpg");
e.Graphics.DrawImage(image, 0, 0);

Font font1 = new Font("Arial", 80, FontStyle.Bold);
SolidBrush brush1 = new SolidBrush(Color.Black);
string text1 = "abcde";
RectangleF rect1 = new RectangleF(120, 100, 784, 120);
e.Graphics.DrawString(text1, font1, brush1, rect1);
}

//ココに下記のMouseDownを入れようとしてます。

static void Main()
{
Application.Run(new Test());
}
}




//2
Protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);

image = Image.FromFile("aiai2.jpg");
e.Graphics.DrawImage(image, 0, 0);

string text2 = "fghij";
RectangleF rect1 = new RectangleF(120, 100, 784, 120);
e.Graphics.DrawString(text2, font1, brush1, rect1);
}

補足日時:2008/09/14 00:44
    • good
    • 0
この回答へのお礼

自分の記事を検索しても見つからないと思ったら、カテゴリ間違えてました。。
違うカテゴリなのに教えてくれてどうもありがとうございます。
力不足のため、パッと見で理解するのは難しいですが、himajin100000さんのソースを元に頑張ってみます。
有難う御座いました。

お礼日時:2008/09/13 18:25

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!