Export (0) Print
Expand All

CreateParams Class

Encapsulates the information needed when creating a control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public class CreateParams
public class CreateParams
public class CreateParams

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 .NET.

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.

import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
import System.Runtime.InteropServices.*;
import System.Diagnostics.*;
import System.IO.*;
import System.Security.Permissions.*;

/** @attribute SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)
 */
public class MyIconButton extends Button
{
    private Icon icon;

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

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

        // Size the button to 4 pixels larger than the icon.
        this.set_Height(icon.get_Height() + 4);
        this.set_Width(icon.get_Width() + 4);
    } //MyIconButton

    /** @property 
     */
    protected CreateParams get_CreateParams()
    {
        // Extend the CreateParams property of the Button class.
        CreateParams cp = super.get_CreateParams();

        // Update the button Style.
        cp.set_Style(cp.get_Style() | 0x40); // BS_ICON value
        return cp;
    } //get_CreateParams

    /** @property 
     */
    public Icon get_Icon()
    {
        return icon;
    } //get_Icon

    /** @property 
     */
    public void set_Icon(Icon value)
    {
        icon = value;
        UpdateIcon();

        // Size the button to 4 pixels larger than the icon.
        this.set_Height(icon.get_Height() + 4);
        this.set_Width(icon.get_Width() + 4);
    } //set_Icon

    protected void OnHandleCreated(EventArgs e)
    {
        super.OnHandleCreated(e);

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

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

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

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

    // Import the SendMessage method of the User32 DLL.   
    /** @attribute DllImport("user32.dll", CharSet = CharSet.Auto)
     */
    public static native IntPtr SendMessage(IntPtr hWnd, int msg, 
        int wParam, int lParam);
} //MyIconButton

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 MyApplication extends Form
{
    private MyIconButton myIconButton;
    private Button stdButton;
    private OpenFileDialog openDlg;

    public static void main(String[] args)
    {
        Application.Run(new MyApplication());
    } //main

    public MyApplication()
    {
        try {
            // Create the button with the default icon.
            myIconButton = new MyIconButton(new Icon(Application.
                get_StartupPath() + "\\Default.ico"));
        }
        catch (System.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.add_Click(new EventHandler(this.myIconButton_Click));
            stdButton.add_Click(new EventHandler(this.stdButton_Click));

           // Set the location, text and width of the standard button.
           stdButton.set_Location(new Point(myIconButton.get_Location().
               get_X(), myIconButton.get_Location().get_Y() 
               + myIconButton.get_Height() + 20));
           stdButton.set_Text("Change Icon");
           stdButton.set_Width(100);

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

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

    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.set_InitialDirectory(Application.get_StartupPath());
        openDlg.set_Filter("Icon files (*.ico)|*.ico");
        openDlg.set_Multiselect(false);
        openDlg.ShowDialog();
        if (!(openDlg.get_FileName().Equals(""))) {
            myIconButton.set_Icon(new Icon(openDlg.get_FileName()));
        }
    } //stdButton_Click
} //MyApplication

System.Object
  System.Windows.Forms.CreateParams

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft