次の方法で共有


方法 : カスタム イメージ ボタン コントロールを作成します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

イメージで、ボタンを作成するにはカスタム コントロールを Windows フォーム Control クラスから派生を作成する必要があります。

イメージをボタンを作成するには

  1. System.Windows.Forms.Control クラスから派生するクラスを作成します。

  2. ボタン イメージの押された状態で、いない押された状態の状態を示すクラス内でプロパティを定義します。

  3. フォームにボタンがクリックされたときに再描画させるには、Invalidate メソッドを使用します。

  4. 適切なイメージを持つカスタム コントロールを描画するには、OnPaint メソッド オーバーライドします。

使用例

この例には、PictureButton カスタム コントロールを定義するクラスが含まれています。 フォーム上のコントロールのインスタンスを作成し、コントロールによって使用されるビットマップを定義します。

                        Imports System
Imports System.Drawing
Imports System.Windows.Forms

PublicClass PictureButtonDemo
    Inherits System.Windows.Forms.Form

    FriendWithEvents PictureButton1 AsNew PictureButton

    PublicSubNew()

        ' Display the OK close button.Me.MinimizeBox = FalseMe.Text = "Picture Button Demo"
        ' Create an instance of the PictureButton custom control.With PictureButton1
            .Parent = Me
            .Bounds = New Rectangle(10, 30, 150, 30)
            .ForeColor = Color.White
            .BackgroundImageValue = MakeBitmap(Color.Blue, _
                PictureButton1.Width, PictureButton1.Height)
            .PressedImageValue = MakeBitmap(Color.LightBlue, _
                PictureButton1.Width, PictureButton1.Height)
            .Text = "Click Me"EndWithEndSub
    ' Clean up any resources being used.ProtectedOverloadsOverridesSub Dispose(ByVal disposing AsBoolean)
        MyBase.Dispose(disposing)
    EndSub
    ' Create a bitmap object and fill it with the specified color.       ' To make it look like a custom image, draw an ellipse in it.Function MakeBitmap(ByVal ButtonColor As Color, ByVal width AsInteger, _
        ByVal height AsInteger) As Bitmap

        Dim bmp AsNew Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.FillRectangle(New SolidBrush(ButtonColor), 0, 0, bmp.Width, bmp.Height)
        g.DrawEllipse(New Pen(Color.LightGray), 3, 3, width - 6, height - 6)
        g.Dispose()

        Return bmp
    EndFunctionSharedSub Main()
        Application.Run(New PictureButtonDemo)
    EndSub
    ' Because PictureButton inherits from Control,     ' you can use the default Click event.PrivateSub PictureButton1_Click(ByVal sender AsObject, ByVal e As EventArgs) _
        Handles PictureButton1.Click

        'Add code here to respond to button click. EndSubEndClass
'Button with an image custom control.PublicClass PictureButton
    Inherits Control

    Private backgroundImg As Image
    Private pressedImg As Image
    Private pressed AsBoolean = False
    ' Property for the background image to be drawn behind the button text.PublicProperty BackgroundImageValue() As Image
        GetReturnMe.backgroundImg
        EndGetSet(ByVal Value As Image)
            Me.backgroundImg = Value
        EndSetEndProperty
    ' Property for the background image to be drawn behind the button text when    ' the button is pressed.PublicProperty PressedImageValue() As Image
        GetReturnMe.pressedImg
        EndGetSet(ByVal Value As Image)
            Me.pressedImg = Value
        EndSetEndProperty
    ' When the mouse button is pressed, set the "pressed" flag to true    ' and ivalidate the form to cause a repaint.  The .NET Compact Framework    ' sets the mouse capture automatically.ProtectedOverridesSub OnMouseDown(ByVal e As MouseEventArgs)
        Me.pressed = TrueMe.Invalidate()
        MyBase.OnMouseDown(e)
    EndSub
    ' When the mouse is released, reset the "pressed" flag    ' and invalidate to redraw the button in the unpressed state.ProtectedOverridesSub OnMouseUp(ByVal e As MouseEventArgs)
        Me.pressed = FalseMe.Invalidate()
        MyBase.OnMouseUp(e)
    EndSub
    ' Override the OnPaint method to draw the background image and the text.ProtectedOverridesSub OnPaint(ByVal e As PaintEventArgs)
        IfMe.pressed AndAlso (Me.pressedImg IsNotNothing) Then
            e.Graphics.DrawImage(Me.pressedImg, 0, 0)
        Else
            e.Graphics.DrawImage(Me.backgroundImg, 0, 0)
        EndIf
        ' Draw the text if there is any.IfMe.Text.Length > 0 ThenDim size As SizeF = e.Graphics.MeasureString(Me.Text, Me.Font)

            ' Center the text inside the client area of the PictureButton.
            e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), _
                (Me.ClientSize.Width - size.Width) / 2, _
                (Me.ClientSize.Height - size.Height) / 2)
        EndIf
        ' Draw a border around the outside of the           ' control to look like Pocket PC buttons.
        e.Graphics.DrawRectangle(New Pen(Color.Black), 0, 0, _
            Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)

        MyBase.OnPaint(e)
    EndSubEndClass
                        using System;
using System.Drawing;
using System.Windows.Forms;

namespace PictureButton
{
    publicclass PictureButtonDemo : System.Windows.Forms.Form
    {
        int clickCount = 0;

        // Create a bitmap object and fill it with the specified color.   // To make it look like a custom image, draw an ellipse in it.
        Bitmap MakeBitmap(Color color, int width, int height)
        {
            Bitmap bmp = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(new SolidBrush(color), 0, 0, bmp.Width, bmp.Height);
            g.DrawEllipse(new Pen(Color.DarkGray), 3, 3, width - 6, height - 6);
            g.Dispose();

            return bmp;
        }

        // Create a new PictureButton control and hook up its properties.public PictureButtonDemo()
        {
            InitializeComponent();

            // Display the OK close button.this.MinimizeBox = false;

            PictureButton button = new PictureButton();
            button.Parent = this;
            button.Bounds = new Rectangle(10, 30, 150, 30);
            button.ForeColor = Color.White;
            button.BackgroundImage = MakeBitmap(Color.Blue, button.Width, button.Height);
            button.PressedImage = MakeBitmap(Color.LightBlue, button.Width, button.Height);
            button.Text = "click me";
            button.Click +=new EventHandler(button_Click);
        }

        protectedoverridevoid Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }

        privatevoid InitializeComponent()
        {
            this.Text = "Picture Button Demo";
        }

        staticvoid Main() 
        {
            Application.Run(new PictureButtonDemo());
        }

        // Since PictureButton inherits from Control, we can just use the default// Click event that Control supports.privatevoid button_Click(object sender, EventArgs e)
        {
            this.Text = "Click Count = " + ++this.clickCount;
        }
    }

    // This code shows a simple way to have a // button-like control that has a background image.publicclass PictureButton : Control
    {
        Image backgroundImage, pressedImage;
        bool pressed = false;

        // Property for the background image to be drawn behind the button text.public Image BackgroundImage
        {
            get
            {
                returnthis.backgroundImage;
            }
            set
            {
                this.backgroundImage = value;
            }
        }

        // Property for the background image to be drawn behind the button text when// the button is pressed.public Image PressedImage
        {
            get
            {
                returnthis.pressedImage;
            }
            set
            {
                this.pressedImage = value;
            }
        }

        // When the mouse button is pressed, set the "pressed" flag to true// and invalidate the form to cause a repaint.  The .NET Compact Framework // sets the mouse capture automatically.protectedoverridevoid OnMouseDown(MouseEventArgs e)
        {
            this.pressed = true;
            this.Invalidate();
            base.OnMouseDown (e);
        }

        // When the mouse is released, reset the "pressed" flag 
        // and invalidate to redraw the button in the unpressed state.protectedoverridevoid OnMouseUp(MouseEventArgs e)
        {
            this.pressed = false;
            this.Invalidate();
            base.OnMouseUp (e);
        }

        // Override the OnPaint method to draw the background image and the text.protectedoverridevoid OnPaint(PaintEventArgs e)
        {
            if(this.pressed && this.pressedImage != null)
                e.Graphics.DrawImage(this.pressedImage, 0, 0);
            else
                e.Graphics.DrawImage(this.backgroundImage, 0, 0);

            // Draw the text if there is any.if(this.Text.Length > 0)
            {
                SizeF size = e.Graphics.MeasureString(this.Text, this.Font);

                // Center the text inside the client area of the PictureButton.
                e.Graphics.DrawString(this.Text,
                    this.Font,
                    new SolidBrush(this.ForeColor),
                    (this.ClientSize.Width - size.Width) / 2,
                    (this.ClientSize.Height - size.Height) / 2);
            }

            // Draw a border around the outside of the // control to look like Pocket PC buttons.
            e.Graphics.DrawRectangle(new Pen(Color.Black), 0, 0, 
                this.ClientSize.Width - 1, this.ClientSize.Height - 1);

            base.OnPaint(e);
        }
    }
}

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

参照

概念

カスタム コントロールの開発

その他の技術情報

Windows フォーム コントロール、.NET Framework を最適化します。