CreateParams.Style 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定視窗樣式值的位元組合。
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
屬性值
視窗樣式值的位元組合。
範例
下列程式碼範例會 Button 建立名為 的 MyIconButton
衍生類別,並提供按鈕顯示圖示而非影像所需的實作。 屬性 CreateParams 已擴充,且已將值新增至 Style 屬性,導致按鈕顯示 Icon ,而不是 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
備註
屬性 Style 會控制控制項的外觀及其初始狀態。
如需建立控制項參數的詳細資訊,請參閱 CreateWindow 宏、 CreateWindowEx 函式和 CREATESTRUCT 結構。
注意
用來設定 、 ExStyle 和 ClassStyle 屬性的 Style 常數定義于 Winuser.h 標頭檔中。 此檔案是由 Platform SDK 或 Visual Studio 所安裝。