共用方式為


CreateParams 類別

定義

封裝建立控制項時所需的資訊。

public ref class CreateParams
public class CreateParams
type CreateParams = class
Public Class CreateParams
繼承
CreateParams

範例

下列程式碼範例會 Button 建立名為 MyIconButton 的衍生類別,並提供按鈕顯示圖示而非 Image 所需的實作。 屬性 CreateParams 已擴充,且已將值新增至 Style 屬性,導致按鈕顯示 Icon ,而不是 Image

#include <windows.h>

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;
using namespace System::Diagnostics;
using namespace System::IO;

public ref class MyIconButton: public Button
{
private:
   Icon^ icon;

public:
   MyIconButton()
   {
      
      // Set the button's FlatStyle property.
      FlatStyle = ::FlatStyle::System;
   }

   MyIconButton( Icon^ ButtonIcon )
   {
      
      // Set the button's FlatStyle property.
      FlatStyle = ::FlatStyle::System;
      
      // Assign the icon to the private field.
      this->icon = ButtonIcon;
      
      // Size the button to 4 pixels larger than the icon.
      this->Height = icon->Height + 4;
      this->Width = icon->Width + 4;
   }


protected:

   property System::Windows::Forms::CreateParams^ CreateParams 
   {

      virtual System::Windows::Forms::CreateParams^ get() override
      {
         
         // Extend the CreateParams property of the Button class.
         System::Windows::Forms::CreateParams^ cp = __super::CreateParams;

         // Update the button Style.
         cp->Style |= 0x00000040; // BS_ICON value
         return cp;
      }
   }

public:
   property System::Drawing::Icon^ Icon
   {
      System::Drawing::Icon^ get()
      {
         return icon;
      }
      void set(System::Drawing::Icon^ value)
      {
         icon = value;
         UpdateIcon();
         this->Height = icon->Height + 4;
         this->Width = icon->Width + 4;
      }
   }

protected:
   virtual void OnHandleCreated( EventArgs^ e ) override
   {
      Button::OnHandleCreated( e );
      
      // Update the icon on the button if there is currently an icon assigned to the icon field.
      if ( icon != nullptr )
      {
         UpdateIcon();
      }
   }


private:
   void UpdateIcon()
   {
      IntPtr iconHandle = IntPtr::Zero;
      
      // Get the icon's handle.
      if ( icon != nullptr )
      {
         iconHandle = icon->Handle;
      }

      
      // Send Windows the message to update the button.
      SendMessage( (HWND)Handle.ToPointer(), 0x00F7, 1, (int)iconHandle );
      
      /*BM_SETIMAGE value*/
      /*IMAGE_ICON value*/
   }

   public:
    [DllImport("user32.dll")]
    static LRESULT SendMessage(HWND hWnd, int msg, int wParam, int lParam);

};
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;

public class MyIconButton : Button
{
    private Icon icon;

    public MyIconButton()
    {
        // Set the button's FlatStyle property.
        FlatStyle = FlatStyle.System;
    }

    public MyIconButton(Icon ButtonIcon)
        : this()
    {
        // Assign the icon to the private field.   
        this.icon = ButtonIcon;

        // Size the button to 4 pixels larger than the icon.
        this.Height = icon.Height + 4;
        this.Width = icon.Width + 4;
    }

    protected override CreateParams CreateParams
    {
        get
        {
            // Extend the CreateParams property of the Button class.
            CreateParams cp = base.CreateParams;
            // Update the button Style.
            cp.Style |= 0x00000040; // BS_ICON value

            return cp;
        }
    }

    public Icon Icon
    {
        get
        {
            return icon;
        }

        set
        {
            icon = value;
            UpdateIcon();
            // Size the button to 4 pixels larger than the icon.
            this.Height = icon.Height + 4;
            this.Width = icon.Width + 4;
        }
    }

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);

        // Update the icon on the button if there is currently an icon assigned to the icon field.
        if (icon != null)
        {
            UpdateIcon();
        }
    }

    private void UpdateIcon()
    {
        IntPtr iconHandle = IntPtr.Zero;

        // Get the icon's handle.
        if (icon != null)
        {
            iconHandle = icon.Handle;
        }

        // Send Windows the message to update the button. 
        SendMessage(Handle, 0x00F7 /*BM_SETIMAGE value*/, 1 /*IMAGE_ICON value*/, (int)iconHandle);
    }

    // Import the SendMessage method of the User32 DLL.   
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
}
Imports System.Windows.Forms
Imports System.Drawing
Imports System.IO
Imports System.Security.Permissions

Public Class MyIconButton
    Inherits Button

    Private ButtonIcon As Icon

    Public Sub New()
        MyBase.New()

        ' Set the button's FlatStyle property.
        Me.FlatStyle = System.Windows.Forms.FlatStyle.System
    End Sub

    Public Sub New(ByVal Icon As Icon)
        MyBase.New()

        ' Assign the icon to the private field.   
        Me.ButtonIcon = Icon

        ' Size the button to 4 pixels larger than the icon.
        Me.Height = ButtonIcon.Height + 4
        Me.Width = ButtonIcon.Width + 4
    End Sub


    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim SecPerm As New SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
            SecPerm.Demand()

            ' Extend the CreateParams property of the Button class.
            Dim cp As System.Windows.Forms.CreateParams = MyBase.CreateParams
            ' Update the button Style.
            cp.Style = cp.Style Or &H40 ' BS_ICON value

            Return cp
        End Get
    End Property

    Public Property Icon() As Icon
        Get
            Return ButtonIcon
        End Get

        Set(ByVal Value As Icon)
            ButtonIcon = Value
            UpdateIcon()
            ' Size the button to 4 pixels larger than the icon.
            Me.Height = ButtonIcon.Height + 4
            Me.Width = ButtonIcon.Width + 4
        End Set
    End Property

    <SecurityPermission(SecurityAction.Demand, UnmanagedCode := True)> _
    Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)
        MyBase.OnHandleCreated(e)

        ' Update the icon on the button if there is currently an icon assigned to the icon field.
        If Me.ButtonIcon IsNot Nothing Then
            UpdateIcon()
        End If
    End Sub

    Private Sub UpdateIcon()
        Dim IconHandle As IntPtr = IntPtr.Zero

        ' Get the icon's handle.
        If Me.Icon IsNot Nothing Then
            IconHandle = Icon.Handle
        End If

        ' Send Windows the message to update the button. 
        ' BM_SETIMAGE (second parameter) and IMAGE_ICON (third parameter).
        SendMessage(Handle, &HF7, &H1, IconHandle.ToInt32())
    End Sub

    ' Declare the SendMessage function.
    Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As IntPtr, _
            ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Class

下列程式碼範例會建立標準 Button 控制項的實例,以及上述範例中建立之衍生控制項 MyIconButton 的實例。 此範例要求在與應用程式相同的位置有一個名為 Icon Default.ico 的檔案。 應用程式啟動時, Default 圖示會顯示在 MyIconButton 按鈕上。 Default如果圖示不存在,按鈕臉部會是空白的。 按一下標準 Button 時,會出現方 OpenFileDialog 塊,讓您可以選取要顯示在 上的 MyIconButton 新圖示。

public ref class MyApplication: public Form
{
private:
   MyIconButton^ myIconButton;
   Button^ stdButton;
   OpenFileDialog^ openDlg;

public:
   MyApplication()
   {
      try
      {
         
         // Create the button with the default icon.
         myIconButton = gcnew MyIconButton( gcnew System::Drawing::Icon( String::Concat( Application::StartupPath, "\\Default.ico" ) ) );
      }
      catch ( Exception^ ex ) 
      {
         
         // If the default icon does not exist, create the button without an icon.
         myIconButton = gcnew MyIconButton;
         #if defined(DEBUG)
         Debug::WriteLine( ex );
         #endif
      }
      finally
      {
         stdButton = gcnew Button;
         
         // Add the Click event handlers.
         myIconButton->Click += gcnew EventHandler( this, &MyApplication::myIconButton_Click );
         stdButton->Click += gcnew EventHandler( this, &MyApplication::stdButton_Click );
         
         // Set the location, text and width of the standard button.
         stdButton->Location = Point(myIconButton->Location.X,myIconButton->Location.Y + myIconButton->Height + 20);
         stdButton->Text = "Change Icon";
         stdButton->Width = 100;
         
         // Add the buttons to the Form.
         this->Controls->Add( stdButton );
         this->Controls->Add( myIconButton );
      }

   }


private:
   void myIconButton_Click( Object^ /*Sender*/, EventArgs^ /*e*/ )
   {
      
#undef MessageBox 
      
      // Make sure MyIconButton works.
      MessageBox::Show( "MyIconButton was clicked!" );
   }

   void stdButton_Click( Object^ /*Sender*/, EventArgs^ /*e*/ )
   {
      
      // Use an OpenFileDialog to allow the user to assign a new image to the derived button.
      openDlg = gcnew OpenFileDialog;
      openDlg->InitialDirectory = Application::StartupPath;
      openDlg->Filter = "Icon files (*.ico)|*.ico";
      openDlg->Multiselect = false;
      openDlg->ShowDialog();
      if (  !openDlg->FileName->Equals( "" ) )
      {
         myIconButton->Icon = gcnew System::Drawing::Icon( openDlg->FileName );
      }
   }

};

int main()
{
   Application::Run( gcnew MyApplication );
}
public class MyApplication : Form
{
    private MyIconButton myIconButton;
    private Button stdButton;
    private OpenFileDialog openDlg;

    static void Main()
    {
        Application.Run(new MyApplication());
    }

    public MyApplication()
    {
        try
        {
            // Create the button with the default icon.
            myIconButton = new MyIconButton(new Icon(Application.StartupPath + "\\Default.ico"));
        }
        catch (Exception ex)
        {
            // If the default icon does not exist, create the button without an icon.
            myIconButton = new MyIconButton();
            Debug.WriteLine(ex.ToString());
        }
        finally
        {
            stdButton = new Button();

            // Add the Click event handlers.
            myIconButton.Click += new EventHandler(this.myIconButton_Click);
            stdButton.Click += new EventHandler(this.stdButton_Click);

            // Set the location, text and width of the standard button.
            stdButton.Location = new Point(myIconButton.Location.X, myIconButton.Location.Y + myIconButton.Height + 20);
            stdButton.Text = "Change Icon";
            stdButton.Width = 100;

            // Add the buttons to the Form.
            this.Controls.Add(stdButton);
            this.Controls.Add(myIconButton);
        }
    }

    private void myIconButton_Click(object Sender, EventArgs e)
    {
        // Make sure MyIconButton works.
        MessageBox.Show("MyIconButton was clicked!");
    }

    private void stdButton_Click(object Sender, EventArgs e)
    {
        // Use an OpenFileDialog to allow the user to assign a new image to the derived button.
        openDlg = new OpenFileDialog();
        openDlg.InitialDirectory = Application.StartupPath;
        openDlg.Filter = "Icon files (*.ico)|*.ico";
        openDlg.Multiselect = false;
        openDlg.ShowDialog();

        if (openDlg.FileName != "")
        {
            myIconButton.Icon = new Icon(openDlg.FileName);
        }
    }
}
Public Class Form1
    Inherits System.Windows.Forms.Form

    Friend WithEvents MyStdButton As System.Windows.Forms.Button
    Friend WithEvents MyIconButton As MyIconButton
    Friend WithEvents OpenDlg As OpenFileDialog

    Public Sub New()
        MyBase.New()
        Try
            ' Create the button with the default icon.
            MyIconButton = New MyIconButton(New Icon(Application.StartupPath + "\Default.ico"))

        Catch ex As Exception
            ' If the default icon does not exist, create the button without an icon.
            MyIconButton = New MyIconButton()
            System.Diagnostics.Debug.WriteLine(ex.ToString())
        Finally
            MyStdButton = New Button()

            'Set the location, text and width of the standard button.
            MyStdButton.Location = New Point(MyIconButton.Location.X, _
                        MyIconButton.Location.Y + MyIconButton.Height + 20)
            MyStdButton.Text = "Change Icon"
            MyStdButton.Width = 100

            ' Add the buttons to the Form.
            Me.Controls.Add(MyStdButton)
            Me.Controls.Add(MyIconButton)
        End Try

    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub

    Private Sub MyStdButton_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles MyStdButton.Click
        ' Use an OpenFileDialog to allow the user to assign a new image to the derived button.
        OpenDlg = New OpenFileDialog()
        OpenDlg.InitialDirectory = Application.StartupPath
        OpenDlg.Filter = "Icon files (*.ico)|*.ico"
        OpenDlg.Multiselect = False
        OpenDlg.ShowDialog()

        If OpenDlg.FileName <> "" Then
            MyIconButton.Icon = New Icon(OpenDlg.FileName)
        End If
    End Sub

    Private Sub MyIconButton_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles MyIconButton.Click
        ' Make sure MyIconButton works.
        MessageBox.Show("MyIconButton was clicked!")
    End Sub
End Class

備註

CreateParams 的資訊可用來傳遞控制項初始狀態和外觀的相關資訊。 大部分 Control 衍生控制項都會 CreateParams 覆寫 屬性,以傳入適當的值,或在 中包含 CreateParams 其他資訊。

如需建立控制項參數的詳細資訊,請參閱 CreateWindow 宏CreateWindowEx 函式CREATESTRUCT 結構

注意

用來設定 、 ExStyleClassStyle 屬性的 Style 常數定義于 Winuser.h 標頭檔中。 此檔案是由 Platform SDK 或 Visual Studio 所安裝。

建構函式

CreateParams()

初始化 CreateParams 類別的新執行個體。

屬性

Caption

取得或設定控制項的初始文字。

ClassName

取得或設定要衍生控制項類別的名稱。

ClassStyle

取得或設定類別樣式值的位元組合。

ExStyle

取得或設定延伸視窗樣式值的位元組合。

Height

取得或設定控制項的初始高度。

Param

取得或設定建立控制項所需的其他參數資訊。

Parent

取得或設定控制項的父代 (Parent)。

Style

取得或設定視窗樣式值的位元組合。

Width

取得或設定控制項的初始寬度。

X

取得或設定控制項的初始左邊位置。

Y

取得或設定控制項初始位置的上方位置。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

適用於

另請參閱