如何将Icon添加到使用Eclipse Galileo C和MinGW构建的应用程序中?

Mar*_*Tik 6 c eclipse

我已经阅读了很多关于如何向使用Visual Studio构建的应用程序添加图标的内容,但我不知道如何使用Eclipse Galileo/C/MinGW执行此操作.

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

Xan*_*ndy 15

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

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

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

#define AppIcon 101
Run Code Online (Sandbox Code Playgroud)

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

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"
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

windres ../src/resources.rc -o ../Resources/resources.o
Run Code Online (Sandbox Code Playgroud)

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

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

Resources/resources.o
Run Code Online (Sandbox Code Playgroud)

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

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

我希望能够通读这一点很清楚.