Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CreateParams.Style Property

Gets or sets a bitwise combination of window style values.

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

public int Style { get; set; }
/** @property */
public int get_Style ()

/** @property */
public void set_Style (int value)

public function get Style () : int

public function set Style (value : int)

Property Value

A bitwise combination of the window style values.

The Style property controls the appearance of the control and its initial state.

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

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.