非常简单的例子,从代码中添加一个小盾牌图标和按钮文本。按钮应该在XAML中以名称“OkButton”添加。
[DllImport("user32.dll")]
static extern IntPtr LoadImage(
IntPtr hinst,
string lpszName,
uint uType,
int cxDesired,
int cyDesired,
uint fuLoad);
public MainWindow()
{
InitializeComponent();
var image = LoadImage(IntPtr.Zero, "#106", 1, SystemInformation.SmallIconSize.Width, SystemInformation.SmallIconSize.Height, 0);
var imageSource = Imaging.CreateBitmapSourceFromHIcon(image, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
var sp = new StackPanel
{
Orientation = Orientation.Horizontal,
};
sp.Children.Add(new Image { Source = imageSource, Stretch = Stretch.None });
sp.Children.Add(new TextBlock { Text = "OK", Margin = new Thickness(5, 0, 0, 0) });
OkButton.Content = sp;
}