Export (0) Print
Expand All

CreateParams Class

Encapsulates the information needed when creating a control.

System.Object
  System.Windows.Forms.CreateParams

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Class CreateParams

The CreateParams type exposes the following members.

  NameDescription
Public methodCreateParamsInitializes a new instance of the CreateParams class.
Top

  NameDescription
Public propertyCaptionGets or sets the control's initial text.
Public propertyClassNameGets or sets the name of the Windows class to derive the control from.
Public propertyClassStyleGets or sets a bitwise combination of class style values.
Public propertyExStyleGets or sets a bitwise combination of extended window style values.
Public propertyHeightGets or sets the initial height of the control.
Public propertyParamGets or sets additional parameter information needed to create the control.
Public propertyParentGets or sets the control's parent.
Public propertyStyleGets or sets a bitwise combination of window style values.
Public propertyWidthGets or sets the initial width of the control.
Public propertyXGets or sets the initial left position of the control.
Public propertyYGets or sets the top position of the initial location of the control.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Overrides Object.ToString.)
Top

The information in a CreateParams can be used to pass information about the initial state and appearance of a control. Most Control derived controls override the CreateParams property to pass in the appropriate values or include additional information in the CreateParams.

For more information about creating control parameters, see the CreateWindow and CreateWindowEx functions and the CREATESTRUCT structure documentation in the Windows Platform SDK reference located in the MSDN Library.

NoteNote

The constants used to set the Style, ExStyle, and ClassStyle properties are defined in the Winuser.h header file. This file is installed by the Platform SDK or Visual Studio.

The following code example creates a Button derived class named MyIconButton and provides the implementation needed for the button to display an Icon rather than an Image. The CreateParams property is extended and a value added to the Style property that causes the button to display an Icon rather than an Image.

Imports System
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

The following code example creates an instance of a standard Button control and an instance of the derived control, MyIconButton, created in the example above. This example requires that there is an Icon file named Default.ico in the same location as the application. When the application starts, the Default icon is displayed on the MyIconButton button. If the Default icon is not present, the button face is blank. When the standard Button is clicked, an OpenFileDialog box will appear so you can select a new Icon to display on the MyIconButton.

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

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft