Top > .NET > コントロール > 画像をボタンにする

単にButtonのBackgroundImageを設定すると

  • 枠と画像の間にすきまが空いてしまう
  • クリックしたときの表示が気に入らない
    などがあるので、PictureBoxを拡張してしまう。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace CustomButton
{
    class ImageButton : System.Windows.Forms.PictureBox
    {
        public int MoveDeltaX = 3;
        public int MoveDeltaY = 3;
        protected Point OriginalLocation;

        protected override void InitLayout()
        {
            base.InitLayout();
            OriginalLocation = this.Location;
        }


        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            if (OriginalLocation != null)
            {
                Point p = this.Location;
                p.X += MoveDeltaX;
                p.Y += MoveDeltaY;
                this.Location = p;
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            if (OriginalLocation != null)
            {
                this.Location = OriginalLocation;
            }
            base.OnMouseUp(e);
        }
    }
}


URL B I U SIZE Black Maroon Green Olive Navy Purple Teal Gray Silver Red Lime Yellow Blue Fuchsia Aqua White

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup Referer   Help   RSS of recent changes