如何更改 WPF 窗口中的标题栏图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5101895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:36:54  来源:igfitidea点击:

How to change title bar image in WPF Window?

wpf

提问by KMC

How to change the titile bar image (the top-left most icon) in WPF?

如何更改 WPF 中的标题栏图像(最左上角的图标)?

回答by kamaci

The Icon attribute of Window is used to set Icon of a window.

Window 的 Icon 属性用于设置窗口的图标。

<Window x:Class="WindowSample.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="WPF Window Sample" Height="350" Width="525"

Name="FirstWindow" Icon="Icon1.ico" >

The Icon property of Window class represents a window's icon at run-time. This property takes an ImageSource variable.

Window 类的 Icon 属性表示运行时窗口的图标。此属性采用 ImageSource 变量。

The following code snippet uses BitmapFrame.Create method to create an ImageSource and sets the Icon property of a Window.

以下代码片段使用 BitmapFrame.Create 方法创建 ImageSource 并设置 Window 的 Icon 属性。

Uri iconUri = new Uri("pack://application:,,,/Icon1.ico", UriKind.RelativeOrAbsolute);

this.Icon = BitmapFrame.Create(iconUri);

You can read more from here

你可以从这里阅读更多

回答by Praveen Gopal

Easy way to add image to title bar:

将图像添加到标题栏的简单方法:

In your Project, Select - Properties - Application - Resources - Icon and Manifest - select the .ico image(always convert your image to .ico)

在您的项目中,选择 - 属性 - 应用程序 - 资源 - 图标和清单 - 选择 .ico 图像(始终将图像转换为 .ico)

Add this line(icon) in WPF Main window:

在 WPF 主窗口中添加此行(图标):

Title="xxxxx" **Icon="xxxxxx.ico"**>

回答by Jens

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SDKSample.MainWindow"
Icon="WPFIcon1.ico">
</Window>

or in code

或在代码中

// Set an icon using code
Uri iconUri = new Uri("pack://application:,,,/WPFIcon2.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri);

Source: Window.Icon Property

来源:Window.Icon 属性

回答by mgear

This one worked for me (using Visual Studio 2017)

这个对我有用(使用 Visual Studio 2017)

  • Select menu Project/[yourprojectname] Properties
  • Click Application tab (its the first one on top)
  • Here you can browse for Icon, and it will be copied to your project
  • 选择菜单项目/[您的项目名称] 属性
  • 单击应用程序选项卡(顶部的第一个)
  • 在这里您可以浏览 Icon,它将被复制到您的项目中

回答by Krzysztof Gapski

  1. Add ico file to project Resourcesand check as Embedded Resource
  2. Set Project->Properties->Iconand choose from Resources
  3. Run Project in ReleaseMode or start without debugging.
  1. 将 ico 文件添加到项目资源并检查为嵌入式资源
  2. 设置项目->属性->图标并从资源中选择
  3. 发布模式下运行项目或在不调试的情况启动

回答by Mesam Mujtaba

 <Window Icon="youricon.ico"></Window>