Прочитать на английском

Поделиться через


CreateParams.Style Свойство

Определение

Получает или задает побитовое сочетание значений стилей окон.

C#
public int Style { get; set; }

Значение свойства

Побитовое сочетание значений стилей окон.

Примеры

В следующем примере кода создается производный Button класс с именем MyIconButton и предоставляется реализация, необходимая для отображения значка, а не изображения. Свойство CreateParams расширяется, и к свойству Style добавляется значение, которое приводит к отображению кнопки , Icon а не Image.

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

Комментарии

Свойство Style управляет внешним видом элемента управления и его начальным состоянием.

Дополнительные сведения о создании параметров элемента управления см. в разделах Создание макроса Windows, функции CreateWindowEx и структуры CREATESTRUCT.

Примечание

Константы, используемые для задания Styleсвойств , и ClassStyle , ExStyleопределяются в файле заголовка Winuser.h. Этот файл устанавливается пакетом SDK платформы или Visual Studio.

Применяется к

Продукт Версии
.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, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

См. также раздел