本文整理汇总了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);
}