如何为没有VISUAL STUDIO或任何其他ide的已完成的c#项目设置图标 [英] How to set an icon for finished c# project WITHOUT VISUAL STUDIO or any other ide

查看:56
本文介绍了如何为没有VISUAL STUDIO或任何其他ide的已完成的c#项目设置图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是14 ..编码世界的新手...很抱歉,如果这个问题很愚蠢。

i刚刚使用winforms创建了一个计算器。我为此设置了一个图标那......我的意思是可执行文件的图标和窗口左上角的图标。

i我没有使用任何ide ...只是文本编辑器... plz help

i am just 14.. new to the world of coding... so sorry if this question is stupid.
i just created a calculator using winforms now..how do i set an icon for that.. i mean the icon of the executable and the icon at the top left of the window.
i am not using any ide...just text editor... plz help

推荐答案

不适用于C#:你需要将图标文件添加到项目中,并将其引用放在资源文件中。请参阅 https://msdn.microsoft.com/en-us/library/aa380599.aspx [ ^ ]了解更多信息。



您可能会发现从https://www.visualstudio.com/products/visual-studio-express-vs [ ^ ]。
Not applicable to C#: You need to add the icon file to your project, and put its reference in the resource file. See https://msdn.microsoft.com/en-us/library/aa380599.aspx[^] for more information.

You may find it easier to get hold of one of Microsoft's free copies of Visual Studio from https://www.visualstudio.com/products/visual-studio-express-vs[^].


这是一个非常好的主意。有时你确实需要它。



在我回答你的问题之前,我将解释如何以通常的方式处理图标和位图。



事实上,我不建议通过Visual Studio编辑器添加图标和位图资源,即使它非常适合某些人。使用单独的文件更易于维护。甚至可以通过Visual Studio单独编辑文件并保存,但是在目标项目之外。



您可以创建.resx节点并使用使用现有文件 。您选择的文件将首先添加到您选择的项目目录中,并在项目文件中引用。它的复制属性将设置为无。然后它将在.resx文件中引用,但不会嵌入其中。当然,这意味着文件将在构建期间嵌入到生成的资源中。



注意:文件将从原始文件复制,所以练习一些预防措施,以避免重复。最好在项目之外使用原始文件ether(以后可以将其删除),或者在项目文件结构中您希望复制的确切位置。



此外,具有C#自动生成代码的节点将显示为.resx节点的子节点。生成的代码可以使用了。在文本编辑器中打开此文件,找到一些名称接近原始图像文件名称的静态属性。只需使用它。



除此之外,我建议避免在任何表格/窗口的客户区放置任何图标。它应该是位图(与图标相对),或者在WPF的情况下,应该是基于Canvas元素的XAML矢量图像。如果您阅读有关符合标准的图标,您将看到它们有多大。您应该将不同像素大小的多个不同位图打包成一个标准图标,这样可以轻松地使您的图标比所有装配的整个输出目录大小更大。请参阅我的文章中我解释的部分: 带声音激活的实用录音机, 3.4。一些有趣的特点 。另请参阅图标,以了解您应提供的像素大小的位图数量。如果你没有全部完成,某些模式下的某些操作系统元素会以非常丑陋的方式自动重新设置你的图标。



所以,保留你的图标专属两种情况1)应用程序图标(请参阅项目的属性),2)窗口/表单图标。如果您还为一个或所有窗户或表格重复使用窗口/表格图标的应用程序图标,那将是最好的。



现在,我可以回答你的问题。



首先,创建一些简单的示例项目并完成我上面推荐的所有步骤。然后删除所有非源文件:所有bin和obj目录,* .user等。查看您的项目文件。找到您的图标和您需要的其他内容的参考。了解如何在文本中插入它们。



此外,您可以使用MSBuild API编写可安全操作项目文件的代码: https://msdn.microsoft.com/en-us/library/microsoft.build.tasks.msbuild.aspx [ ^ ]。



编写自己的实用程序以在项目中注入一些预定义资源并使用它执行构建将非常容易。您甚至可以将此实用程序嵌入到构建本身中,这可以通过开发自定义任务类来完成,然后可以在某个项目中使用该类。您可以创建一些主项目,它使用一些自定义任务来操作构建中涉及的其他项目/解决方案。请特别注意:

https:// msdn.microsoft.com/en-us/library/microsoft.build.utilities.task.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/t9883dzc.aspx [ ^ ]。



-SA
This is a very good idea. Sometimes you really need it.

Before I answer your question, I'll explain how to deal with icons and bitmaps in a usual way.

As a matter of fact, I do not recommend to add icon and bitmap resources via the Visual Studio editor even when it it quite suitable for some. Using a separate file is way more maintainable. One can even edit the file via Visual Studio separately and save it, but outside of the target project.

You can create a .resx node and use "Use existing file". The file you chose will first be added to the of of the project directories of your choice and referenced in the project file. It's "Copy" property will be set to "None". Then it will be referenced in the .resx file, but not embedded in it. Of course, it means that the file will be embedded in the resulting resource during the build.

Pay attention: the file will be copied from the original one, so exercise some precautions to avoid duplication. It's the best to have the original file ether outside of the project (you can later removed it), or at the exact location in your project file structure where you expect the copy.

Moreover, the node with C# auto-generated code will appear as a child node of your .resx node. The generated code is ready to use. Open this file in a text editor and locate some static property with the name close to the name of your original image file. Just use it.

On top of that, I recommend to avoid putting any icons in the client area of any forms/windows. It should be either bitmap (as opposed to icon) or, in case of WPF, XAML vector image based on Canvas element. If you read about standard-compliant icons, you will see how big they are. You are supposed to pack several different bitmap in different pixel sizes into a standard icon, which can easily make your icon way bigger than your whole output directory size with all your assemblies. Please see the section of my article where I explain it: Practical Sound Recorder with Sound Activation, 3.4. Some Funny Peculiarities. See also the icon, to find out how many bitmaps of what pixel sizes you are supposed to provide. If you fail to make them all, some OS elements in some modes will re-sample your icons automatically, in quite an ugly way.

So, reserve your icons exclusively to two cases 1) application icon (see your project's Properties), 2) window/form icons. It would be the best if you also reuse your application icon for the window/form icon for one or all of your windows or forms.

Now, I can answer your question.

First of, create some simple sample project and do all the steps I recommended above. Then remove all non-source files: all "bin" and "obj" directories, *.user, and so on. Look at your project files. Locate the references to your icon and other stuff you need. Learn how to insert them in text.

Also, you can use MSBuild API to write code which would safely manipulate your project files: https://msdn.microsoft.com/en-us/library/microsoft.build.tasks.msbuild.aspx[^].

I will be pretty easy to write your own utility to inject some predefined resources in your projects and perform the build with it. You can even embed this utility into the build itself, which is done via development of the custom Task class which can then be used in some project. You can create some "master project" which uses some custom Tasks to manipulate other projects/solutions involved in your build. Please see, in particular:
https://msdn.microsoft.com/en-us/library/microsoft.build.utilities.task.aspx[^],
https://msdn.microsoft.com/en-us/library/t9883dzc.aspx[^].

—SA


我最近使用msbuild迁移到wpf和im来编译我的项目....

所以有没有办法设置图标使用msbuild?

必须有办法通过.csproj文件来做到这一点
i have recently migrated to wpf nd im using msbuild to compile my projects....
so is there a way to set the icon using msbuild?
there must be a way to do that through the .csproj file


这篇关于如何为没有VISUAL STUDIO或任何其他ide的已完成的c#项目设置图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆