本文整理匯總了C#中System.Windows.Forms.CreateParams.Style屬性的典型用法代碼示例。如果您正苦於以下問題:C# CreateParams.Style屬性的具體用法?C# CreateParams.Style怎麽用?C# CreateParams.Style使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類System.Windows.Forms.CreateParams
的用法示例。
在下文中一共展示了CreateParams.Style屬性的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: MyIconButton
//引入命名空間
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Security.Permissions;
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
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
{
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
// 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;
}
}
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
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);
}