1.找寻合适的图标
网上的图标资源很多,也有专门的图标资源站点。我是在http://www.easyicon.net找的,很方便。注意图标的让人能读图知意,美观大方,并注意图标的大小尺寸。
2.像WPF中添加图标资源
在工程项目里新建文件夹Resources。右击Resources文件夹,选择添加->新建项,在VC#下选择资源文件模板,后缀名是.resx(VS2013),选择添加。
会出现如图的界面。注意,我用红色椭圆圈起来的部分。
然后,点击添加资源,
选择添加现有文件。
双击图标进行编辑,保存即可。
在xaml代码中引用图标,代码
<Window.Resources>
<ResourceDictionary>
<BitmapImage x:Key="OpenPng"
UriSource="D:\Documents\Visual Studio 2013\Projects\DmdSimAppV1.0\DmdSimAppV1.0\Resources\open.png">
</BitmapImage>
<BitmapImage x:Key="SavePng"
UriSource="D:\Documents\Visual Studio 2013\Projects\DmdSimAppV1.0\DmdSimAppV1.0\Resources\save.png">
</BitmapImage>
<BitmapImage x:Key="MeasurePng"
UriSource="D:\Documents\Visual Studio 2013\Projects\DmdSimAppV1.0\DmdSimAppV1.0\Resources\measure.png">
</BitmapImage>
<BitmapImage x:Key="RunPng"
UriSource="D:\Documents\Visual Studio 2013\Projects\DmdSimAppV1.0\DmdSimAppV1.0\Resources\run.png">
</BitmapImage>
<BitmapImage x:Key="ConfigurePng"
UriSource="D:\Documents\Visual Studio 2013\Projects\DmdSimAppV1.0\DmdSimAppV1.0\Resources\configure.png">
</BitmapImage>
<BitmapImage x:Key="ParaPng"
UriSource="D:\Documents\Visual Studio 2013\Projects\DmdSimAppV1.0\DmdSimAppV1.0\Resources\para.png">
</BitmapImage>
<BitmapImage x:Key="BmpPng"
UriSource="D:\Documents\Visual Studio 2013\Projects\DmdSimAppV1.0\DmdSimAppV1.0\Resources\bmp.png">
</BitmapImage>
</ResourceDictionary>
</Window.Resources>
<ToolBar Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="9" Background="White">
<Image Name="OpenFile" Source="{StaticResource OpenPng}" MouseDown="OpenFile_MouseDown"></Image>
<Image Name="SaveFile" Source="{StaticResource SavePng}"></Image>
<Image Name="ConfigurationFile" Source="{StaticResource ConfigurePng}"></Image>
<Image Name="Measure" Source="{StaticResource MeasurePng}"></Image>
<Image Name="Run" Source="{StaticResource RunPng}"></Image>
</ToolBar>
private void OpenFile_MouseDown(object sender, MouseButtonEventArgs e) {
string file = null;
var dlg = new OpenFileDialog();
dlg.InitialDirectory = "E:";
dlg.Filter = "*.bmp|*.bmp";
Nullable<bool> result = dlg.ShowDialog();
if (result == true) {
file = dlg.FileName;
}
Uri resourceUri = new Uri(file);
ImgInBox.Source = new BitmapImage(resourceUri);
}
3.添加托盘图标
只要修改Window的Icon属性即可在运行时,看到APP在托盘上呈现的图标。
运行结果: