CreateParams 类

定义

封装创建控件时所需的信息。

C#
public class CreateParams
继承
CreateParams

示例

下面的代码示例创建一MyIconButton个名为Button的派生类,并提供按钮显示图标而不是图像所需的实现。 该CreateParams属性已扩展,并且已添加到Style属性的值,该属性导致按钮显示一个而不是一个IconImage值。

C#
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);
}

下面的代码示例创建标准 Button 控件的实例和派生控件的实例, MyIconButton并在上面的示例中创建。 此示例要求在应用程序所在的同一位置有一个名为 Icon Default.ico 的文件。 应用程序启动时,图标 Default 会显示在 MyIconButton 按钮上。 Default如果图标不存在,按钮面为空。 单击标准 Button 后,将显示一个 OpenFileDialog 框,以便你可以选择要显示在该图标上的 MyIconButton新图标。

C#
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);
        }
    }
}

注解

CreateParams 的信息可用于传递有关控件的初始状态和外观的信息。 大多数 Control 派生控件将重写属性 CreateParams 以传入相应的值或包含其他信息 CreateParams

有关创建控件参数的详细信息,请参阅 CreateWindow 宏CreateWindowEx 函数CREATESTRUCT 结构

备注

用于设置StyleExStyleClassStyle属性的常量在 Winuser.h 头文件中定义。 此文件由平台 SDK 或Visual Studio安装。

构造函数

CreateParams()

初始化 CreateParams 类的新实例。

属性

Caption

获取或设置控件的初始文本。

ClassName

获取或设置从中派生该控件的 Windows 类的名称。

ClassStyle

获取或设置类样式值的按位组合。

ExStyle

获取或设置扩展窗口样式值的按位组合。

Height

获取或设置控件的初始高度。

Param

获取或设置创建控件所需的附加参数信息。

Parent

获取或设置控件的父级。

Style

获取或设置窗口样式值的按位组合。

Width

获取或设置控件的初始宽度。

X

获取或设置控件的初始左边位置。

Y

获取或设置控件的初始顶部位置。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅