如何使用Eclipse Galileo C和MinGW构建的应用程序添加图标? [英] How to add an Icon to an application built with Eclipse Galileo C and MinGW?

查看:106
本文介绍了如何使用Eclipse Galileo C和MinGW构建的应用程序添加图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

任何人都可以写一个描述,或给我一个链接描述?

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

推荐答案

在Windows中,图标以及一些其他元素(游标,位图,...)必须在资源文件中指定,一旦编译就会被链接到程序。

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程序的例子,这将说明它在Eclipse中的使用。这是一个简单的程序,只需创建一个窗口,查看我们填写WNDCLASSEX的时间,应用程序的图标在那里引用:

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:

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

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

#define AppIcon 101

下一个文件是资源文件,您可以手动创建或从Eclipse中创建以及在Eclipse中创建它,右键单击您想要的目录(在这种情况下为 src ),然后选择新建 - >文件。写下你想要的名字,然后点击 Finish 。要从Eclipse中编辑它,请单击它并选择打开 - >文本编辑器

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.

资源.rc - 将在此指定图标:

resources.rc - the icon will be specified here:

#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 - 包含程序代码的文件:

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

#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;
}

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

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).

之后,转到项目 - >属性

在那里,转到 C / C ++ Build - >设置 - >构建步骤选项卡。

你会看到预构建步骤 - >命令。您填写的命令将在编译开始之前执行,因此您可以告诉它编译资源文件。当您使用MinGW时,资源编译器为 windres

After that go to Project -> Properties.
There, go to C/C++ Build -> Settings -> Build Steps tab.
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:

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

正如你所见我将编译的资源文件放在名为 Resources 的目录中,您可以将其放在所需的位置(因此文件的名称不需要被命名为 resources.rc )。

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).

现在转到工具设置标签。

在那里,转到 MinGW C Linker - > Miscellaneous ,在其他对象中添加以前创建的对象文件,在此案例你应该添加:

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

Resources/resources.o

由于这是一个Windows应用程序,请将相应选项卡顶部的链接器标记中添加选项 -mwindows

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

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

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.

我希望通过这个阅读清楚。

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

这篇关于如何使用Eclipse Galileo C和MinGW构建的应用程序添加图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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