CreateParams.Style Propiedad

Definición

Obtiene o establece una combinación bit a bit de valores de estilo de ventana.

public:
 property int Style { int get(); void set(int value); };
public int Style { get; set; }
member this.Style : int with get, set
Public Property Style As Integer

Valor de propiedad

Combinación bit a bit de los valores de estilo de ventana.

Ejemplos

En el ejemplo de código siguiente se crea una Button clase derivada denominada MyIconButton y se proporciona la implementación necesaria para que el botón muestre un icono en lugar de una imagen. La CreateParams propiedad se extiende y un valor agregado a la Style propiedad que hace que el botón muestre en Icon lugar de .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

Comentarios

La Style propiedad controla la apariencia del control y su estado inicial.

Para obtener más información sobre cómo crear parámetros de control, vea CreateWindow macro, Función CreateWindowEx y estructura CREATESTRUCT.

Nota

Las constantes usadas para establecer las Stylepropiedades , ExStyley ClassStyle se definen en el archivo de encabezado Winuser.h. El SDK de plataforma o Visual Studio instala este archivo.

Se aplica a

Consulte también