マウスホイールでズームボタン・ラベル・テキストボックスの共通ルーチンをアダプタ
http://d.hatena.ne.jp/kabacsharp/20111119
で書いた、拡大縮小コントロ−ルの続きです。
初期化ルーチンや比率セットメソッドで、ロジックをフォーム側に書いていましたが、これを修正しました。
ボタンやラベルなどのコントロールはButton, Labelクラスで共通の親クラスはControl。
C#は多重継承できないので、継承によってアダプタクラスを作れませんでした。
そこで委譲によるアダプタクラスを作りました。
拡大縮小責務インターフェース:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace メールのみ.UserClass { public interface IControlResizable { int pWidth { get; set; } int pHeight { get; set; } float pFontSize { get; set; } int pTop { get; set; } int pLeft { get; set; } int OrgHeight { get; set; } int OrgWidth { get; set; } float OrgFontsize { get; set; } int OrgTop { get; set; } int OrgLeft { get; set; } float TuneRatio { get; set; } void InitSize(); void ChangeViewRatio(int ratio); } }
アダプタクラス:
ControlResizableAdapter.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using メールのみ.UserClass; namespace 波佐見弁利v2.UserClass { public class ControlResizableAdapter { private IControlResizable ctl_ = null; public float tuneratio = 1.0F; public ControlResizableAdapter(IControlResizable ctl) { this.ctl_ = ctl; } public void InitSize() { this.ctl_.OrgWidth = this.ctl_.pWidth; this.ctl_.OrgHeight = this.ctl_.pHeight; this.ctl_.OrgFontsize = this.ctl_.pFontSize; this.ctl_.OrgTop = this.ctl_.pTop; this.ctl_.OrgLeft = this.ctl_.pLeft; } public void ChangeViewRatio(int ratio) //ratio: 50-200 { this.ctl_.pHeight = (int)((this.ctl_.OrgHeight * ratio / 100) * this.tuneratio); this.ctl_.pWidth = (int)((this.ctl_.OrgWidth * ratio / 100) * this.tuneratio); this.ctl_.pFontSize = (float)((this.ctl_.OrgFontsize * ratio / 100) * this.tuneratio); this.ctl_.pTop = (int)((this.ctl_.OrgTop * ratio / 100) * this.tuneratio); this.ctl_.pLeft = (int)((this.ctl_.OrgLeft * ratio / 100) * this.tuneratio); } } }
アダプタクラスに委譲したボタンクラス:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using 波佐見弁利v2.UserClass; namespace メールのみ.UserClass { public class MyButtonKnowsOrgSize : Button, IControlResizable { private ControlResizableAdapter adapt_ = null; public MyButtonKnowsOrgSize() : base() { //フォーカスをとらない this.SetStyle(ControlStyles.Selectable, false); this.adapt_ = new ControlResizableAdapter(this); } public void ChangeViewRatio(int ratio) { this.adapt_.ChangeViewRatio(ratio); } public void InitSize() { this.adapt_.InitSize(); } public float TuneRatio { get { return this.adapt_.tuneratio; } set { this.adapt_.tuneratio = value; } } public int OrgHeight { get; set; } public int OrgWidth { get; set; } public float OrgFontsize { get; set; } public int OrgTop { get; set; } public int OrgLeft { get; set; } public int pHeight { get { return this.Height; } set { this.Height = value; } } public int pWidth { get { return this.Width; } set { this.Width = value; } } public float pFontSize { get { return this.Font.Size; } set { this.Font = new System.Drawing.Font("MS UI Gothic", value, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128))); } } public int pTop { get { return this.Top; } set { this.Top = value; } } public int pLeft { get { return this.Left; } set { this.Left = value; } } } }
同じくアダプタクラスに委譲したラベルクラス:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using 波佐見弁利v2.UserClass; namespace メールのみ.UserClass { public class MyLabelKnowsOrgSize : Label, IControlResizable { private ControlResizableAdapter adapt_ = null; public MyLabelKnowsOrgSize() : base() { //フォーカスをとらない this.SetStyle(ControlStyles.Selectable, false); this.adapt_ = new ControlResizableAdapter(this); } public void ChangeViewRatio(int ratio) { this.adapt_.ChangeViewRatio(ratio); } public void InitSize() { this.adapt_.InitSize(); } public float TuneRatio { get { return this.adapt_.tuneratio; } set { this.adapt_.tuneratio = value; } } public int OrgHeight { get; set; } public int OrgWidth { get; set; } public float OrgFontsize { get; set; } public int OrgTop { get; set; } public int OrgLeft { get; set; } public int pHeight { get { return this.Height; } set { this.Height = value; } } public int pWidth { get { return this.Width; } set { this.Width = value; } } public float pFontSize { get { return this.Font.Size; } set { this.Font = new System.Drawing.Font("MS UI Gothic", value, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128))); } } public int pTop { get { return this.Top; } set { this.Top = value; } } public int pLeft { get { return this.Left; } set { this.Left = value; } } } }
フォーム側(メイン側)のルーチン:
private void ChangeViewRatio(int ratio_org) { foreach (IControlResizable ctl in this.panel1.Controls.OfType()) { ctl.ChangeViewRatio(ratio); } } private void InitOrgSize() { //ボタンオリジナルサイズ foreach (IControlResizable ctl in this.panel1.Controls.OfType ()) { ctl.InitSize(); } } private void Form1_Load(object sender, EventArgs e) { this.InitOrgSize(); }
まだメイン側に改良の余地が。
IControlResizableのコレクションを持って、イベントで一気にInitSizeやChangeViewRatioをまとめて送信したいのですが、無駄ですかね。