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

标签 c eclipse

我已经阅读了很多有关如何将图标添加到使用 Visual Studio 构建的应用程序的信息,但我不知道如何使用 Eclipse Galileo/C/MinGW 执行此操作。

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

最佳答案

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

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

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

#define AppIcon 101

下一个文件是资源文件,您可以手动创建它,也可以从 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"

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

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

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

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

如你所见,我将把编译后的资源文件放在一个名为Resources 的目录中,你可以将它留在你想要的地方(因此文件名,它没有将被命名为 resources.rc)。

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

Resources/resources.o

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

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

我希望读完这篇文章已经足够清楚了。

关于c - 如何向使用 Eclipse Galileo C 和 MinGW 构建的应用程序添加图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1411040/

相关文章:

c - 声明为两种类型的 Typedef 结构 : "typedef struct x { .. } X, *XPointer"

c - 如何实现我的变位词和回文函数来检查用户输入的单词?

c - 有没有更快的方法来查找数据?

php - 使 Eclipse PHP 代码格式化程序适应 Symfony 编码标准

java - "discouraged reference"在Java中是什么意思?

java - 将现有文件夹复制到 Eclipse 工作区中新创建的项目中

c - 如果您已经知道输出,找到函数输入的最佳方法是什么?

c - 学习C#define语句(MACROS)

java - 使用 Eclipse 的单人项目的版本控制?

Android:自定义操作栏主题错误 -(未找到匹配的资源......)