CreateParams.Style Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar eller anger en bitvis kombination av fönsterformatvärden.
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
Egenskapsvärde
En bitvis kombination av fönsterformatets värden.
Exempel
I följande kodexempel skapas en Button härledd klass med namnet MyIconButton och den implementering som behövs för knappen för att visa en ikon i stället för en bild. Egenskapen CreateParams utökas och ett värde läggs till i Style egenskapen som gör att knappen visas i Icon stället för en 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
Kommentarer
Egenskapen Style styr kontrollens utseende och dess ursprungliga tillstånd.
Mer information om hur du skapar kontrollparametrar finns i SkapaWindow-makro, SkapaWindowEx-funktion och CREATESTRUCT-struktur.
Note
Konstanterna som används för att ange Styleegenskaperna , ExStyleoch ClassStyle definieras i winuser.h-huvudfilen. Den här filen installeras av Platform SDK eller Visual Studio.