勝手に回答
イメージの表示と非表示
VisualBasicのメソッドを利用したプログラムで「消去」ボタンをクリックするとイメージが消え、「表示」ボタンをクリックするとイメージが表示されるようにするにはどんなプログラムを打てばよろしいのですか?
よくよくOOPで例えられるテレビの電源ってことで実モデルを参考にしてみる
使用する規程クラス
| クラス名 | 名前空間 |
| Form | System.Windows.Form |
| PictureBox | System.Windows.Forms |
| BitMap | System.Drawing |
| Graphics | System.Drawing |
作成クラス
| クラス名 | 名前空間 |
| Form1 | WindowsFormsApplication1 |
| RemoteController | WindowsFormsApplication1 |
| IRemoteController | WindowsFormsApplication1 |
| TV | WindowsFormsApplication1 |
| ITV | WindowsFormsApplication1 |
| ChannelGuide | WindowsFormsApplication1 |
とりあえず必要になりそうなクラスを6つ作成
ON OFFの簡単なプログラムだからリモコンの蓋とテレビのカバーも実モデルに
してみる
フォームクラス Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Click += new System.EventHandler(this.button2_Click);
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
}
イベントの実装のみに留めておく
TVクラス TV.cs
public class TV : ITV
{
private System.Windows.Forms.PictureBox Display;
private System.Drawing.Image TempImage;
public TV(System.Windows.Forms.PictureBox pict)
{
Display = pict;
}
public void ON()
{
if (TempImage != null)
{
Display.Image = TempImage;
}
else
{
Display.Image = null;
}
TvState = true;
}
public void OFF()
{
if (Display.Image != null)
{
TempImage = Display.Image;
}
Display.Image = null;
TvState = false;
}
public void OutPutToDisplay(System.Drawing.Image images)
{
Display.Image = images;
}
private bool _TvState = false ;
public bool TvState
{
get { return _TvState; }
set { _TvState = value; }
}
}
コンストラクタにPictureBoxを使用した、この際の考えとしてはForm上全てをテレビと見立てた場合
PictureBoxはディスプレイとして存在する事とします!
ITVインターフェイス ITV.cs
public interface ITV
{
void ON();
void OFF();
void OutPutToDisplay(System.Drawing.Image value);
bool TvState { get; set; }
}
次にTVクラスのインターフェイスを作成します。実モデルに考えてみるとカバー。
若干実モデルと比べるとおかしいがあくまで参考なのでこれでいく。
RemoteControllerクラス RemoteController.cs
public class RemoteController :IRemoteController
{
private ITV _tv;
public RemoteController(ITV tv)
{
_tv = tv;
}
public void RemoteON()
{
_tv.ON();
}
public void RemoteOFF()
{
_tv.OFF();
}
public void ChannelPush(int pushkey)
{
ChannelGuide cg = new ChannelGuide(pushkey);
_tv.OutPutToDisplay(cg.SelectedChannel);
}
}
コンストラクタにTVクラス、基本的にはテレビとリモコンで1セット。
IRemoteControllerインターフェイス
public interface IRemoteController
{
void RemoteON();
void RemoteOFF();
void ChannelPush(int value);
}
実モデルを考えた場合、12個のボタンが必要になってくるがPC上でテンキーをリモコンの番号として
見立て場合、楽だった為。
ChannelGuideクラス ChannelGuide.cs
public class ChannelGuide
{
private int _SelectKey;
private System.Drawing.Image _SeletedChannel;
public ChannelGuide(int SelectKey)
{
_SelectKey = SelectKey;
if (_SelectKey == 97)
{
SelectedChannel = oneChannel;
}
else if (_SelectKey == 98)
{
SelectedChannel = twoChannel;
}
}
public System.Drawing.Image SelectedChannel
{
get{return _SeletedChannel;}
set {_SeletedChannel = value ;}
}
private System.Drawing.Image _oneChannel =
System.Drawing.Image.FromFile
(@"C:\Documents and Settings\***\My Documents\My Pictures\sample1.jpg");
private System.Drawing.Image _twoChannel =
System.Drawing.Image.FromFile
(@"C:\Documents and Settings\***\My Documents\My Pictures\sample2.jpg");
private System.Drawing.Image oneChannel
{
get{ return _oneChannel;}
set{_oneChannel = value;}
}
private System.Drawing.Image twoChannel
{
get{return _twoChannel;}
set{_twoChannel = value;}
}
このクラスは番組表クラスとしてあるフィールド毎に値を設置したいがめんどかったので2つにしてみた。
xmlやらなんやらで外部的に受け取りやすくしたり色々改善の余地もあったけどやめた。
フォームにイベントの中身を実装
private void button1_Click(object sender, EventArgs e)
{
if (tv != null)
{
tv.ON();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (tv != null)
{
tv.OFF();
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (tv.TvState != false)
{
RemoteController remocon = new RemoteController(tv);
remocon.ChannelPush(e.KeyValue);
}
}
0 件のコメント:
コメントを投稿