質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

1612閲覧

c#_動作変更について

seep0619

総合スコア25

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2016/10/03 07:52

編集2016/10/07 01:39

*.°前提・実現したいこと

複数の四角形が連なっており
その四角形にマウスが乗ると色を変えたい

*.°質問したいこと

四角形が複数表示することろまで出来てます。
それ以降のマウスを四角形に乗ると色を変更するところでつまづいています。

クラスを使用し代入するところまで考えつき、
実践したのですが2日間悩んでもわからないため投稿させて頂きました。

※現段階ではエラー表示なし
主なソースとしては


(Form1)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

private void Form1_Load(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { Class1 person = new Class1(); //using System.Drawing; //描画先とするImageオブジェクトを作成する Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height); //ImageオブジェクトのGraphicsオブジェクトを作成する Graphics g = Graphics.FromImage(canvas); //Penオブジェクトの作成(幅1の黒色) //(この場合はPenを作成せずに、Pens.Blackを使っても良い) Pen p = new Pen(Color.Blue, 1); //3つの長方形の位置と大きさを配列に入れる for (int i = 20; i < 400; i++) { Rectangle[] recst = {new Rectangle(1, 10, 100, 100), new Rectangle(3, 12, 100, 100), new Rectangle(5, 14, 100, 100), new Rectangle(7, 16, 100, 100), new Rectangle(i,i,100,100), }; //3つの長方形を描く g.DrawRectangles(p, recst); Console.WriteLine("recst" + i); } //リソースを解放する p.Dispose(); g.Dispose(); //PictureBox1に表示する pictureBox1.Image = canvas; Console.WriteLine(); } } }

(class1)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C_Level_1
{
class Class1
{
private string strpictureBox1;
private string strForeColor;

public virtual string pictureBox1 { get { return strpictureBox1; } set { strpictureBox1 = value; } } public virtual string ForeColor { get { return strForeColor; } set { strForeColor = value; } } public System.Drawing.Color BackColor { get; internal set; } public void pictureBox1_Mousemove(object sender, MouseEventArgs e) { this.BackColor = System.Drawing.Color.Red; } public class Application { public static void Run(Class1 class1) { Class1 person = new Class1(); System.Console.WriteLine(); Application.Run(new Class1()); } } public class MouseEventArgs { } }

}

お読み頂きまして有難うございます。
宜しくお願い致します!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ここを参考にどうぞ
指定した座標がRectangle内か判定する

後はmousemoveイベント時に全Rectの配列に対してcontains関数でチェックして、該当したものの色を変えればいけるはずです。(該当してないRectの色を元に戻すのも忘れずに)

投稿2016/10/03 08:00

編集2016/10/03 08:06
ishi9

総合スコア1294

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

seep0619

2016/10/03 08:31

ishi9さん*.° 初めまして。早速お返事ありがとうございます! 早速実践してみます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問