C++ 为我的程序设置一个 exe 图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2393863/
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
Set an exe icon for my program
提问by Danny
I'm using Microsoft Visual Studio 2008 and I'm trying to set an exe icon for my program.
我正在使用 Microsoft Visual Studio 2008 并且我正在尝试为我的程序设置一个 exe 图标。
I've searched this site and found this: How do I set the icon for my application in visual studio 2008?
我搜索了这个网站,发现了这个:如何在 Visual Studio 2008 中为我的应用程序设置图标?
But I can't find this "Resource View". I've looked everywhere in MVS08
但是我找不到这个“资源视图”。我在 MVS08 到处都看过
How may I access Resource View and set the main icon for my application?
如何访问资源视图并为我的应用程序设置主图标?
回答by CaptainBli
Ben had a good start to the question along with the help of Tymek, but you need to go to the next step. Once you have added your icon to your resources, you will also need a resource file .rc
in order to set the icon as the main icon of your application.
在 Tymek 的帮助下,Ben 有了一个很好的开始,但您需要进行下一步。将图标添加到资源后,您还需要一个资源文件.rc
,以便将图标设置为应用程序的主图标。
If you don't know if you already have a resource file, open your Resource View for the project: go to View->Resource Viewor Ctrl+Shift+E(in Visual Studio 2012/2013 View->Other Windows->Resource Viewor you can type Ctrl+W,Rto get it to open and in 2015 Ctrl+Shift+E). Check to see if you have a MyProject->MyProject.rc. This is where you should
Add
your icon. It should then create a resource file for you.Now Ctrl+Shift+F to search the whole solution. Search for your icon filename so that you can open the actual
.rc
file. When you do so it may suggest that you already have it open just accept.Now find a place to add the following line to the
.rc
file:MAINICON ICON "foo.ico"
如果你不知道你是否已经有一个资源文件,打开你的资源视图项目为:去查看- >资源视图或Ctrl+ Shift+ E(在Visual Studio 2012/2013查看- >其他Windows的>资源视图或您可以输入Ctrl+ W,R让它在 2015 Ctrl+ Shift+ 中打开E)。检查您是否有 MyProject->MyProject.rc。这是您应该放置
Add
图标的地方。然后它应该为您创建一个资源文件。现在 Ctrl+Shift+F 搜索整个解决方案。搜索您的图标文件名,以便您可以打开实际
.rc
文件。当您这样做时,它可能会建议您已将其打开,只需接受即可。现在找到一个地方将以下行添加到
.rc
文件中:MAINICON ICON "foo.ico"
The next time you compile you can browse to the folder with the executable and see that you have the icon associated with the app and when you run it from there it will use the icon.
下次编译时,您可以浏览到包含可执行文件的文件夹,看到您有与应用程序关联的图标,当您从那里运行它时,它将使用该图标。
Note:
笔记:
- Sometimes when running from Visual Studio you will not get the icon while debugging.
- You need to use an
.ico
file. You cannot use a PNG image file for your executable's icon, it will not work. You must use.ico
. There are web utilities that convert images to .ico files. as quoted from bobobobo.
- 有时从 Visual Studio 运行时,您在调试时不会获得图标。
- 您需要使用一个
.ico
文件。您不能为可执行文件的图标使用 PNG 图像文件,它不起作用。您必须使用.ico
. 有一些网络实用程序可以将图像转换为 .ico 文件。引用自bobobobo。
回答by Ben
Right-click on your project, and select Add
-> Resource
. Then select Icon
and click New
. Edit your icon, and you should be good to go.
右键单击您的项目,然后选择Add
-> Resource
。然后选择Icon
并单击New
。编辑你的图标,你应该很高兴。
回答by Shaggy
See resource.rc in your application.
请参阅您的应用程序中的 resource.rc。
#define IDI_ICON_1 102
#define IDI_ICON_2 103
// Icon with lowest ID value placed first to ensure application icon // remains consistent on all systems.
// 首先放置具有最低 ID 值的图标以确保应用程序图标 // 在所有系统上保持一致。
IDI_ICON_1 ICON "icoMainApp.ico"
IDI_ICON_2 ICON "iconSecond.ico"
回答by Sajjad Hosen
Create a resources.h file under the project folder and the new resource folder will be created automatically. And resources.h can be found in the resource file below the source files.
在项目文件夹下创建一个resources.h文件,新的资源文件夹会自动创建。而resources.h可以在源文件下面的资源文件中找到。
Icon.ico file put in the project file or another location select the location folder.
Icon.ico 文件放在项目文件或其他位置选择位置文件夹。
Write the following code
编写以下代码
#ifndef _resource_rc
#define _resource_rc
MAINICON ICON "icon.ico" //If you can set any name
#endif // _resource_rc
Save and rebuild this project. Happy coding
保存并重建这个项目。快乐编码
回答by user287107
try to add a resource file to your project, then you should be able to open the rc file and add an icon
尝试将资源文件添加到您的项目中,然后您应该可以打开 rc 文件并添加图标