Freigeben über


CreateParams-Klasse

Kapselt die für die Erstellung eines Steuerelements benötigten Informationen.

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

Syntax

'Declaration
Public Class CreateParams
'Usage
Dim instance As CreateParams
public class CreateParams
public ref class CreateParams
public class CreateParams
public class CreateParams

Hinweise

Mit den Informationen in CreateParams können Informationen zum ursprünglichen Zustand und zur Darstellung eines Steuerelements übergeben werden. Die meisten von Control abgeleiteten Steuerelemente überschreiben die CreateParams-Eigenschaft, um die geeigneten Werte zu übergeben oder zusätzliche Informationen aufzunehmen, die in CreateParams enthalten sind.

Weitere Informationen zum Erstellen von Steuerelementparametern finden Sie in der Dokumentation zur CreateWindow-Funktion, zur CreateWindowEx-Funktion und zur CREATESTRUCT-Struktur in der Windows Platform SDK-Referenz der MSDN Library.

Hinweis

Die Konstanten zum Festlegen der Style-Eigenschaft, der ExStyle-Eigenschaft und der ClassStyle-Eigenschaft werden in der Headerdatei Winuser.h definiert. Diese Datei wird vom Platform SDK oder von Visual Studio .NET installiert.

Beispiel

Im folgenden Codebeispiel wird die von Button abgeleitete Klasse MyIconButton erstellt, und die Implementierung, die erforderlich ist, damit die Schaltfläche ein Symbol statt eines Bilds anzeigt, wird bereitgestellt. Die CreateParams-Eigenschaft wird erweitert, und der Style-Eigenschaft wird ein Wert hinzugefügt, der bewirkt, dass die Schaltfläche ein Icon statt eines Image anzeigt.

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

Im folgenden Codebeispiel werden eine Instanz des Button-Standardsteuerelements und eine Instanz des im obigen Beispiel erstellten abgeleiteten MyIconButton-Steuerelements erstellt. In diesem Beispiel muss die Icon-Datei Default.ico sich am selben Speicherort befinden wie die Anwendung. Wenn die Anwendung gestartet wird, wird das Default-Symbol auf der MyIconButton-Schaltfläche angezeigt. Wenn das Default-Symbol nicht vorhanden ist, ist die Schaltflächenoberseite leer. Durch das Klicken auf den Standard-Button wird ein OpenFileDialog-Feld angezeigt, sodass Sie ein neues Symbol zum Anzeigen auf dem MyIconButton auswählen können.

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

Vererbungshierarchie

System.Object
  System.Windows.Forms.CreateParams

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

CreateParams-Member
System.Windows.Forms-Namespace
Control.CreateParams-Eigenschaft