如何向使用 Eclipse Galileo C 和 MinGW 构建的应用程序添加图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1411040/
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-19 14:00:33  来源:igfitidea点击:

How to add an Icon to an application built with Eclipse Galileo C and MinGW?

ceclipse

提问by Marcus Tik

I've read a lot about how to add an icon to an application built with Visual Studio, but I have no idea how to do this with Eclipse Galileo / C / MinGW.

我已经阅读了很多关于如何向使用 Visual Studio 构建的应用程序添加图标的内容,但我不知道如何使用 Eclipse Galileo / C / MinGW 来做到这一点。

Can anyone write a description, or give me a link ta a description ?

谁能写一个描述,或者给我一个链接ta一个描述?

回答by Xandy

In Windows, the icons as well as some other elements (cursors, bitmaps, ...) have to be specified in a resource file, which once compiled will be linked to the program.

在 Windows 中,必须在资源文件中指定图标以及其他一些元素(光标、位图等),该文件一旦编译将链接到程序。

First an example on how to add an icon to a Windows program which will illustrate it's use within Eclipse. Here is a simple program that just creates a window, look at the time we fill the WNDCLASSEX, the icon of the application is referenced there:

首先是一个关于如何向 Windows 程序添加图标的示例,该示例将说明它在 Eclipse 中的使用。下面是一个简单的程序,只是创建了一个窗口,看看我们填入WNDCLASSEX的时候,应用的图标在那里被引用:

resources.h- this file may be used to assign a value to a resource identifier, and so use the value instead:

resources.h- 此文件可用于为资源标识符分配值,因此请改用该值:

#define AppIcon 101

The next file is the resources file, you may create it manually or from within Eclipse as well, to create it in Eclipse, right click the directory you want it to be (in this case is src) and select New -> File. There write the name you want and click Finish. To edit it from within Eclipse right click it and select Open with -> Text Editor.

下一个文件是资源文件,您可以手动创建它,也可以从 Eclipse 中创建它,要在 Eclipse 中创建它,请右键单击您想要的目录(在本例中为src)并选择New -> File。在那里写下你想要的名字并点击Finish。要在 Eclipse 中对其进行编辑,请右键单击它并选择Open with -> Text Editor.

resources.rc- the icon will be specified here:

resources.rc- 将在此处指定图标:

#include "resources.h"

// The icon path I used will be needed by Eclipse.
// If you want to use back-slashes you have to scape them (\ instead of \):
AppIcon ICON "../src/icon.ico"

demoicon.c- the file containing the code of the program:

demoicon.c- 包含程序代码的文件:

#include <windows.h>
#include "resources.h"

const char *ClassName = "DemoIcon";

// Declaration of the window procedure, to be used in the WNDCLASSEX struct:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd) {

    WNDCLASSEX wc;
    HWND hWnd;
    MSG msg;

    // Filling the structure:
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    // Remember this just loads 32x32, use LoadImage() instead for other dimensions (16x16, 48x48, ...):
    wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(AppIcon));
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = ClassName;
    // Here we'll use LoadImage, as we need a 16x16 mini icon:
    wc.hIconSm = LoadImage(hInstance,MAKEINTRESOURCE(AppIcon),IMAGE_ICON,16,16, LR_DEFAULTCOLOR);

    // Registering the class:
    if(!RegisterClassEx(&wc)) {
        MessageBox(NULL,
                   "Could not register window.",
                   "Error",
                   MB_ICONEXCLAMATION | MB_OK);
        return -1;
    }

    // Create the window using the "MainWindow" class:
    hWnd = CreateWindowEx(WS_EX_WINDOWEDGE,
                          ClassName,
                          "Demo Icon",
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          200,
                          150,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    // If the window was not created show error and exit:
    if(hWnd == NULL) {
        MessageBox(NULL,
                   "Could not create window.",
                   "Error",
                   MB_ICONEXCLAMATION | MB_OK);
        return -1;
    }

    // Set the windows show state, to show it:
    ShowWindow(hWnd, nShowCmd);
    // Draw the window:
    UpdateWindow(hWnd);

    // Retrieve messages from the message queue:
    while(GetMessage(&msg, NULL, 0, 0) > 0) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

// Implementation of the window procedure, will handle the messages:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

    switch(uMsg) {
        case WM_CLOSE:
            DestroyWindow(hWnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

    return 0;
}

Now, in your Eclipse project source directory make sure you have all the files (in the example the 3 files mentioned before and the icon file).

现在,在您的 Eclipse 项目源目录中,确保您拥有所有文件(在示例中是前面提到的 3 个文件和图标文件)。

After that go to Project -> Properties.
There, go to C/C++ Build -> Settings -> Build Stepstab.
There you'll see Pre-build steps -> Command. The command you fill in there will be executed before the compilation starts, so you'll tell it to compile the resource file. As you are using MinGW the resource compiler is windres:

之后去Project -> Properties
在那里,转到C/C++ Build -> Settings -> Build Steps选项卡。
在那里你会看到Pre-build steps -> Command。您在那里填写的命令将在编译开始之前执行,因此您将告诉它编译资源文件。当您使用 MinGW 时,资源编译器是windres

windres ../src/resources.rc -o ../Resources/resources.o

As you can see I'll be placing the compiled resource file in a directory called Resources, you may leave it where you want (and so the name of the file, it doesn't have to be named resources.rc).

正如您所看到的,我会将编译后的资源文件放在一个名为 的目录中Resources,您可以将其保留在您想要的位置(因此文件的名称,不必命名为resources.rc)。

Now go to the Tool Settingstab.
There, go to MinGW C Linker -> Miscellaneous, and in other objects add the object file created before, in this case you should add:

现在转到Tool Settings选项卡。
在那里,转到MinGW C Linker -> Miscellaneous,并在其他对象中添加之前创建的对象文件,在这种情况下,您应该添加:

Resources/resources.o

As this is a Windows app, add the option -mwindowsto the linker flags at the top of the same tab.

由于这是一个 Windows 应用程序,因此将选项添加-mwindows到同一选项卡顶部的链接器标志。

Done, when building your project Eclipse will compile the resource file first and then link the generated object as any other object file of your project.

完成后,在构建项目时,Eclipse 将首先编译资源文件,然后将生成的对象链接为项目的任何其他对象文件。

I hope it's clear enough to read through this.

我希望通读一遍就足够清楚了。

回答by Inigo Selwood

The way I did it was by creating a file

我这样做的方法是创建一个文件

icon.rc

图标.rc

#define AppIcon 101
AppIcon ICON "../icon.ico"

Then invoke windresvia command prompt with

然后通过命令提示符调用windres

windres my.rc -O coff -o my.res

It'll compile several files for you -- keep the one called icon.resand rename it as libicon.a. Finally include it in your program by adding it as a library under

它将为您编译多个文件——保留一个名为icon.res 的文件并将其重命名为libicon.a。最后通过将其作为库添加到您的程序中

Project -> Properties -> Build -> Settings -> C++ Linker -> Libraries

项目 -> 属性 -> 构建 -> 设置 -> C++ 链接器 -> 库

(make sure you tell Eclipse where to find the file using the library paths section underneath).

(确保使用下面的库路径部分告诉 Eclipse 在哪里可以找到文件)。

Hope this helps!

希望这可以帮助!

回答by The Andyman

Eclipse isn't set to look at resource files. BUG. So they also have steps to add it to processing. Here are the steps for June. [http://wiki.eclipse.org/CDT/User/FAQ#How_to_handle_Resource-Script-Files_.27.2A.rc.27_with_CDT.3F][1]

Eclipse 未设置为查看资源文件。漏洞。因此,他们也有将其添加到处理中的步骤。这是六月的步骤。[http://wiki.eclipse.org/CDT/User/FAQ#How_to_handle_Resource-Script-Files_.27.2A.rc.27_with_CDT.3F][1]

How to handle Resource-Script-Files '*.rc' with CDT? Currently handling of windres.exe with CDT is not possible. You can not add the .rc file to the project to be compiled and linked with automatically. This is already raised as a bug in bugzilla.

如何使用 CDT 处理资源脚本文件“*.rc”?当前无法使用 CDT 处理windres.exe。您不能将 .rc 文件添加到要自动编译和链接的项目中。这已经作为 bugzilla 中的一个错误提出。

One way is to create a Pre-Build Step. Under menue Project | Properties | C/C++-Build | Settings | Build Steps | Pre-Build Steps fill in the command-line: windres --use-temp-file -i..\MyProject.rc -o..\MyProject_rc\MyProject_rc.o Make the object known to the linker. Under menue Project | Properties | C/C++-Build | Settings Tool Settings | MinGW C++ Linker | Miscellaneous | Other Objects click the icon 'Add', fill in the line: "C:\MyWorkspace\MyProject\MyProject_rc\MyProject_rc.o" 'MyWorkspace' and 'MyProject' replace with whatever is fitting for your purpose.

一种方法是创建一个预构建步骤。在菜单项目下| 属性 | C/C++-构建 | 设置 | 构建步骤 | Pre-Build Steps 在命令行中填写:windres --use-temp-file -i..\MyProject.rc -o..\MyProject_rc\MyProject_rc.o 使链接器知道该对象。在菜单项目下| 属性 | C/C++-构建 | 设置工具设置| MinGW C++ 链接器 | 杂项 | 其他对象单击图标“添加”,填写行:“C:\MyWorkspace\MyProject\MyProject_rc\MyProject_rc.o”“MyWorkspace”和“MyProject”替换为适合您目的的任何内容。

You have to add the folder .\MyProject_rc before you build.

您必须在构建之前添加文件夹 .\MyProject_rc。

The path to windres.exe must be known to eclipse.

eclipse 必须知道windres.exe 的路径。

回答by Martin Sansone - MiOEE

I zip up all of the icons I want to use within the project first. Then Renamethe .zip to a .jar

我首先压缩了我想在项目中使用的所有图标。然后 .zip重命名为 .jar

Create a resource folder if you havent got one already (I use "lib") Then place the jar file inside the resource folder.

如果您还没有资源文件夹,请创建一个资源文件夹(我使用“lib”)然后将 jar 文件放在资源文件夹中。

Then one simple addition to the project properties:

然后对项目属性进行一个简单的添加:

by right clicking and Configure "Java Build Path" - select the Libraries tab. Click on the Add JARsbutton and add the icons.jar to the libraries - then save.

通过右键单击并配置“Java 构建路径” - 选择库选项卡。单击Add JARs按钮并将 icons.jar 添加到库中 - 然后保存

Now its easy to allocate the desired image icon inside the Window Builder editorfor example as the jar containing your icons appears within the Image Selection mode chooser within the Classpath resource list. Everything works as it should and compiles fine.

现在可以很容易地在Window Builder 编辑器中分配所需的图像图标,例如,包含您的图标的 jar 出现在类路径资源列表中的图像选择模式选择器中。一切正常,并且编译正常。