テキストボックス、ボタン、ラベルなどをホイールでズームしてみた。

メールのみ!というメーラーを作っています。
http://lsl.way-nifty.com/mailonly/

画面や文字を拡大したいな、と思って作ってみました。

*拡大前(多少縮小:60%)

*拡大後(150%)

ICtlResizableというインターフェースを作った。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace メールのみ.UserClass
{
    interface ICtlResizable
    {
        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; }
    }
}

テキストボックスなどは、継承して作成。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace メールのみ.UserClass
{
    class MyTextBoxOrgKnowsSize : TextBox, ICtlResizable
    {
        public MyTextBoxOrgKnowsSize() : base() { }

        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;
            }
        }
    }
}

フォームロード時に、以下の拡大縮小時使う初期サイズを取得する関数を呼ぶ。
ただし、拡張メソッドを使っているので、LINQをインポートする。

using System.Linq;

        private void InitOrgSize()
        {
            foreach (ICtlResizable ctl in this.panel1.Controls.OfType())
            {
                ctl.OrgWidth = ctl.pWidth;
                ctl.OrgHeight = ctl.pHeight;
                ctl.OrgFontsize = ctl.pFontSize;
                ctl.OrgTop = ctl.pTop;
                ctl.OrgLeft = ctl.pLeft;
            }
        }


そして、ホイールボタンを設定。

//Form_Load時
            //ホイール
            this.btnWheel.TextBoxForWheel = this.tbxWheel;
            this.btnWheel.OnMouseWheelHook += this.ChangeViewRatio;


こうすると、ホイールボタン上で、ホイールすると以下のメソッドが呼び出される。

        private void ChangeViewRatio(int ratio)
        {            
            if (ratio < 50) ratio = 50;
            if (ratio > 200) ratio = 200;

            foreach (ICtlResizable ctl in this.panel1.Controls.OfType())
            {
                ctl.pHeight = ctl.OrgHeight * ratio / 100;
                ctl.pWidth = ctl.OrgWidth * ratio / 100;
                float sz = ((float)ctl.OrgFontsize * ratio) / 100;
                ctl.pFontSize = sz;

                ctl.pTop = ctl.OrgTop * ratio / 100;
                ctl.pLeft = ctl.OrgLeft * ratio / 100;
            }

            float lsz = this.listView1OrgFontSize * ratio / 100;
            this.listView1.Font = new System.Drawing.Font("MS UI Gothic", lsz, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));


            this.panel1.Height = this.pnl1OrgHeight * ratio / 100;
            this.Height = this.frmOrgHeight * ratio / 100;
            this.Width = this.frmOrgWidth * ratio / 100;

            this.Resize_Form();
        }

*ホイールボタンクラスは以下のよう。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ABCS.UserClass
{
    class WheelButton : Button
    {
        public WheelButton()
            : base()
        {
            this.Font = new Font(this.Font.FontFamily, 6, FontStyle.Bold);
            this.Text = "↑\n↓";
        }

        public delegate void OnMouseWheelHookDelegate(int zoomsize);

        public event OnMouseWheelHookDelegate OnMouseWheelHook = null;

        public TextBox TextBoxForWheel
        {
            get;
            set;
        }

        protected override void OnMouseHover(EventArgs e)
        {
            base.OnMouseHover(e);
            this.Focus();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (this.TextBoxForWheel != null)
            {
                this.TextBoxForWheel.Focus();
            }
        }

        public int wheelValue = 100;

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            this.wheelValue += e.Delta / 24;
            if (this.wheelValue < 50)
            {
                this.wheelValue = 50;
            }
            if (this.wheelValue >= 200)
            {
                this.wheelValue = 200;
            }
            this.TextBoxForWheel.Text = this.wheelValue.ToString();
            if (this.OnMouseWheelHook != null)
            {
                this.OnMouseWheelHook(this.wheelValue);
            }
        }

    }
}

まだ汚いですが。