如何将图标添加到C++控制台应用程序



我正在尝试向我的C++控制台应用程序添加一个图标,但是当我尝试编译以下代码时,我收到这些错误。第一个是我的 resources.rc,第二个文件是我的主.cpp。

资源脚本:
#ifndef RESOURCE_RC_INCLUDED
#define RESOURCE_RC_INCLUDED
MAINICON  ICON  "icon.ico"
#endif // RESOURCE_RC_INCLUDED`
C++来源:
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <windows.h>
#include <fstream>
#include "resources.rc"
using namespace std;
void SetWindow(int Width, int Height) {
    _COORD coord;
    coord.X = Width;
    coord.Y = Height;
    _SMALL_RECT Rect;
    Rect.Top = 0;
    Rect.Left = 0;
    Rect.Bottom = Height - 1;
    Rect.Right = Width - 1;
    HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);      // Get Handle
    SetConsoleScreenBufferSize(Handle, coord);            // Set Buffer Size
    SetConsoleWindowInfo(Handle, TRUE, &Rect);            // Set Window Size
}

int main() {
    SetWindow(105, 30);
    SetConsoleTitle("Shima Command Line");
    int x;
    char file;
    string name, cmd, selPrg, title;
    string cmds[] = {"(help)", "(information)", "(exit)", "(tell me a joke)", "(open)", "(start)", "(talk dirty to me)", "(what is your favorite color)", "(what is your favourite colour)", "(cls)", "(clear)", "(title)"};
    string jokes[] = {"If your cold you should sit in a corner, it's 90 degrees.", "Knock KnocknWho's there?nJustin.nJustin who?nJustin time for dinner.", "Why did the chicken cross the road?nTo get to the other side.", "Jokes aren't really my thing.", "I don't know any good jokes.", "What do you call a cow that just had a baby?nDe-calf-inated. You know like coffee.", "What do you get from a pampered cow?nSpoiled milk.", "What do you get if you divide the circumference of a pumpkin by it's diameter?nPumpkin Pi.", "Did you hear about the two antennas that got married?nThe ceremony was long and boring, but the reception was great!"};
    string prg[] = {"chrome", "opera", "notepad++", "notepad", "firefox", "ie", "codeblocks", "audacity", "cmd"};
    cout << "What is your name?" << endl;
    getline(cin, name);
    cout << "Hello, " << name << ", my name is Shima Josei. " << endl;
    x = 10;
    do {
        cout << "How may I help you?" << endl;
        getline(cin, cmd);
        if (cmd == cmds[0] || cmd == cmds[1] || cmd == cmds[2] || cmd == cmds[3] || cmd == cmds[4] || cmd == cmds[5] || cmd == cmds[6] || cmd == cmds[7] || cmd == cmds[8] || cmd == cmds[9] || cmd == cmds[10] || cmd == cmds[11]) {
            if (cmd == cmds[0]) {
                cout << "nnclear - Shima will clear all text." << endl;
                cout << "cls - Shima will clear the screen." << endl;
                cout << "exit - Shima will exit the program you are currently using." << endl;
                cout << "help - Shima will display the help menu." << endl;
                cout << "information - Shima will display information about her." << endl;
                cout << "open - Shima will open the specified program." << endl;
                cout << "start - Shima will start the specified program." << endl;
                cout << "talk dirty to me - Shima will talk dirty to you." << endl;
                cout << "tell me a joke - Shima will tell you a joke." << endl;
                cout << "title - Shima will change the title." << endl;
                cout << "what is your favorite color - Shima will display her favorite color." << endl;
                cout << "what is your favourite colour - Shima will display her favourite colour in United Kingdom English.nn" << endl;
            }
            if (cmd == cmds[1]) {
                cout << "nnShima Command Line was developed atnGigaboy Web Designs by Adam Oates.nCopyright (C) 2016 Shima Command Line. All rights reserved.nn" << endl;
            }
            if (cmd == cmds[2]) {
                exit(0);
            }
            if (cmd == cmds[3]) {
                srand(time(0));
                cout << "nn" << jokes[rand() % 9] << "nn" << endl;
            }
            if (cmd == cmds[4] || cmd == cmds[5]) {
                cout << "nnWhat program would you like to open?" << endl;
                getline(cin, selPrg);
                if (selPrg == prg[0] || selPrg == prg[1] || selPrg == prg[2] || selPrg == prg[3] || selPrg == prg[4] || selPrg == prg[5] || selPrg == prg[6] || selPrg == prg[7] || selPrg == prg[8]) {
                    if (selPrg == prg[0]) {
                        cout << "nnOpening Google Chrome..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Google Chrome.nn" << endl;
                    }
                    if (selPrg == prg[1]) {
                        cout << "nnOpening Opera..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files/Opera/launcher.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Opera.nn" << endl;
                    }
                    if (selPrg == prg[2]) {
                        cout << "nnOpening Notepad++..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files (x86)/Notepad++/notepad++.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Notepad++.nn" << endl;
                    }
                    if (selPrg == prg[3]) {
                        cout << "nnOpening Notepad..." << endl;
                        ShellExecute(NULL, "open", "C:/Windows/notepad.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Notepad.nn" << endl;
                    }
                    if (selPrg == prg[4]) {
                        cout << "nnOpening Mozilla Firefox..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Mozilla Firefox.nn" << endl;
                    }
                    if (selPrg == prg[5]) {
                        cout << "nnOpening Internet Explorer..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files/Internet Explorer/iexplorer.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Internet Explorer.nn" << endl;
                    }
                    if (selPrg == prg[6]) {
                        cout << "nnOpening Code::Blocks..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files (x86)/CodeBlocks/codeblocks.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Code::Blocks.nn" << endl;
                    }
                    if (selPrg == prg[7]) {
                        cout << "nnOpening Audacity..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files (x86)/Audacity/audacity.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Audacity.nn" << endl;
                    }
                    if (selPrg == prg[8]) {
                        cout << "nnOpening Command Prompt..." << endl;
                        ShellExecute(NULL, "open", "C:/Windows/System32/cmd.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Command Prompt.nn" << endl;
                    }
                } else {
                    cout << "nnI'm sorry, " << name << ", but I don't recognize that program name." << endl;
                }
            }
            if (cmd == cmds[6]) {
                srand(time(0));
                cout << "nnDirt, soil earth, rocks, and mud.nn" << endl;
            }
            if (cmd == cmds[7]) {
                cout << "nnMy favorite color is... well I guess I've never had a favorite color.nn" << endl;
            }
            if (cmd == cmds[8]) {
                cout << "nnMy favourite colour is... well I guess I have never had a favourite colour.nn" << endl;
            }
            if (cmd == cmds[9] || cmd == cmds[10]) {
                system("CLS");
            }
            if (cmd == cmds[11]) {
                cout << "nnWhat would you like the title to be?" << endl;
                getline(cin, title);
                SetConsoleTitle(title.c_str());
                cout << "nn" << endl;
            }
        } else {
            cout << "nnI'm sorry, " << name << ", but I don't understand that command.nn" << endl;
        }
    } while (x = 10);
    return 0;
}

这些是我得到的错误。

||=== 构建:在志摩调试(编译器:GNU GCC 编译器(===|C:\Users\Adam\Desktop\C++ Tutorial\Shima\resources.rc|4|error:"MAINICON"未命名类型|C:\用户\亚当\桌面\C++教程\志摩\主.cpp||在函数 'int main((' 中:|C:\用户\亚当\桌面\C++教程\志摩\main.cpp|35|错误: 在此作用域中未声明"字符串"|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|35|注意:建议的替代方法:|C:\Program Files (x86(\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stringfwd.h|65|note: 'std::string'|C:\用户\亚当\桌面\C++ 教程\志摩\main.cpp|35|错误:在"名称"之前应为";"|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|36|错误:在"cmds"之前应为";"|C:\用户\亚当\桌面\C++ 教程\志摩\main.cpp|37|错误:在"笑话"之前预期";"|C:\用户\亚当\桌面\C++ 教程\志摩\main.cpp|38|错误:在"prg"之前应为";"|C:\用户\亚当\桌面\C++ 教程\志摩\main.cpp|39|错误: 在此范围内未声明"cout"|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|39|注意:建议的替代方法:|C:\Program Files (x86(\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iostream|62|note: 'std::cout'|C:\用户\亚当\桌面\C++ 教程\志摩\main.cpp|39|错误: 在此范围内未声明"endl"|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|39|注意:建议的替代方法:|C:\Program Files (x86(\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|562|note: 'std::endl'|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|40|错误: 在此范围内未声明"cin"|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|40|注意:建议的替代方案:|C:\Program Files (x86(\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iostream|61|note: 'std::cin'|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|40|错误: 在此范围内未声明"名称"|C:\用户\亚当\桌面\C++教程\志摩\main.cpp|40|错误: 在此范围内未声明"getline"|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|40|注意:建议的替代方案:|C:\Program Files (x86(\codeblocks\mingw\bin\..\lib\GCC\MingW32\4.7.1\include\C++\bits\basic_string.h|2792|note: 'std::getline'|C:\用户\亚当\桌面\C++教程\志摩\主.cpp|46|错误: 在此范围内未声明"cmd"|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|47|错误: "cmds"未在此范围内声明|C:\用户\亚当\桌面\C++教程\志摩\main.cpp|74|错误:在此范围内未声明"笑话"|C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|79|error: 'selPrg' 未在此范围内声明|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|80|错误: 在此范围内未声明"prg"|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|158|错误: 在此范围内未声明"标题"|C:\用户\亚当\桌面\C++教程\志摩\主.cpp|165|警告:建议将赋值用作真值的括号 [-括号]|C:\用户\亚当\桌面\C++ 教程\志摩\主.cpp|34|警告:未使用的变量"文件"[-Wunused-变量]|||=== 构建失败:17 个错误、2 个警告(0 分钟、1 秒(===|

在源文件中,必须包含头文件resource.h,而不是资源脚本resource.rc。然后重新编译项目。

头文件:

#define 主101 

源C++文件:

#include <iostream>
#include ...
#include "resource.h"
using namespace std;

相关内容

  • 没有找到相关文章

最新更新