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

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

问题描述

我已经读了很多关于如何将一个图标添加到使用Visual Studio构建的应用程序,但我不知道如何使用Eclipse伽利略/ 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.

任何人都可以写一个说明,或者给我一个TA描述的链接?

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目录),然后选择新建 - >文件。还有写你想要的名称,然后单击完成。在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.

resources.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 - 包含程序的code中的文件:

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 ++编译 - >设置 - >生成步骤标签

在那里,你将看到 pre-生成步骤 - >命令。编译开始前你在里面填写的指令被执行,所以你会告诉它来编译资源文件。当您使用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.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的Ç链接器 - >其他,并在其他对象添加之前创建的目标文件,在这种情况下,你应该增加:

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 添加到链接标志。

完成,建设项目时,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伽利略C和MinGW的构建的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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