C++控制台应用程序复选框

发布于 2025-02-10 01:25:31 字数 329 浏览 0 评论 0 原文

我正在Visual Studio C ++中编写控制台应用程序,并想像此C# application

“

是否有C ++的类似解决方案?

I am writing a console application in visual studio c++ and want to make a checkbox like in this c# application.

Snapshot

Is there any similar solution for C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巨坚强 2025-02-17 01:25:31

我设法通过Alex f注释的_KBHIT函数创建了一个最小的解决方案。

每次制作一些货币时,我只是打印出控制台。就在打印之前,我要清理控制台。使用_khbit,我正在检查用户是否按下正确的键盘输入之一。如果是否检查了复选框,我将保存在矢量中。 (仅在Windows!)

复选框

#pragma once
#include <iostream>
#include <vector>
#include <conio.h>
#include <stdio.h>
#include <chrono>
#include <thread>
#include <Windows.h>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
using namespace std;

#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KEY_W 119
#define KEY_S 115
#define KEY_ENTER 13
#define KEY_SPACE 32

class Checkbox
{
public:
    ~Checkbox();
    Checkbox(string headline, std::vector<string> lineText);
    std::vector<int> run();
private:
    string headline;
    int lineCount;
    unsigned int line = 0;
    std::vector<int> checkboxesValue;
    std::vector<string> checkboxesText;
    int counter;
    bool finish = false;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    void processInput(uint8_t input);
    char getInput();
    void tickLine();
    void printLines();
};

。HCECKBOX.CPP

#include "stdafx.h"
#include "checkbox.h"

Checkbox::Checkbox(string headline, std::vector<string> lineText)
{
    this->headline = headline;
    this->checkboxesText = lineText;
    this->lineCount = lineText.size();
    for (size_t i = 0; i < lineText.size(); i++)
    {
        this->checkboxesValue.push_back(0);
    }
}


Checkbox::~Checkbox()
{
}

std::vector<int> Checkbox::run()
{
    printLines();
    while (1)
    {
        while (!_kbhit())
        {
        }
        processInput(_getch());
        printLines();
        if (finish)
        {
            return checkboxesValue;
        }
    }
}

void Checkbox::processInput(uint8_t input)
{
    switch (input) {
    case KEY_UP:
    case KEY_W:
        // W and up key: up
        line = line - 1;
        if (line == -1)
        {
            line = lineCount -1;
        }
        break;
    case KEY_DOWN:
    case KEY_S:
        // S and down key: down
        line = (line + 1) % lineCount;
        break;
    case KEY_ENTER:
        // enter
        finish = true;
        break;
    case KEY_SPACE:
        // space: select
        tickLine();
        break;
    }
}

char Checkbox::getInput()
{
    while (!_kbhit())
    {
    }
    char input = _getch();
    return input;
}

void Checkbox::tickLine()
{
    if (checkboxesValue[line])
    {
        checkboxesValue[line] = 0;
    }
    else
    {
        checkboxesValue[line] = 1;
    }
}

void Checkbox::printLines()
{
    system("cls");
    SetConsoleTextAttribute(hConsole, 7);
    cout << headline.c_str() << endl;
    cout << "(Use arrow keys to navigate up and down, space bar to select and enter to submit.)" << endl;
    for (size_t i = 0; i < checkboxesValue.size(); i++)
    {
        if (i == line)
        {
            SetConsoleTextAttribute(hConsole, 4);
        }
        else
        {
            SetConsoleTextAttribute(hConsole, 7);
        }
        cout << "[";
        if (checkboxesValue[i])
        {
            cout << "x";
        }
        else
        {
            cout << " ";
        }
        cout << "] " << checkboxesText[i].c_str() << endl;
    }
}

使用它,只需提供一个字符串的向量,它将返回int的向量,其中0(未检查)或1(检查)。

#include "stdafx.h"
#include "checkbox.h"
#include <string> 
int main()
{
    std::vector<string> lineTexts;
    lineTexts.push_back("first line");
    lineTexts.push_back("second line");
    lineTexts.push_back("third line");
    string headline = "Choose one of the following";

    Checkbox checkbox(headline, lineTexts);
    std::vector<int> result = checkbox.run();

    cout << "Checkboxes: " << endl;
    for (size_t i = 0; i < result.size(); i++)
    {
        cout << "line " << i << " " << result[i] << endl;
    }
}

I managed to create a minimal solution with the _kbhit function from Alex F comment.

I am just printing out the console every time some chnage is made. Right before the printing I am clearing the console. With _khbit I am checking if the user is pressing one of the correct keyboard input. If a checkbox is checked or not, i am saving inside a vector. (Works only on windows!)

checkbox.h

#pragma once
#include <iostream>
#include <vector>
#include <conio.h>
#include <stdio.h>
#include <chrono>
#include <thread>
#include <Windows.h>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
using namespace std;

#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KEY_W 119
#define KEY_S 115
#define KEY_ENTER 13
#define KEY_SPACE 32

class Checkbox
{
public:
    ~Checkbox();
    Checkbox(string headline, std::vector<string> lineText);
    std::vector<int> run();
private:
    string headline;
    int lineCount;
    unsigned int line = 0;
    std::vector<int> checkboxesValue;
    std::vector<string> checkboxesText;
    int counter;
    bool finish = false;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    void processInput(uint8_t input);
    char getInput();
    void tickLine();
    void printLines();
};

checkbox.cpp

#include "stdafx.h"
#include "checkbox.h"

Checkbox::Checkbox(string headline, std::vector<string> lineText)
{
    this->headline = headline;
    this->checkboxesText = lineText;
    this->lineCount = lineText.size();
    for (size_t i = 0; i < lineText.size(); i++)
    {
        this->checkboxesValue.push_back(0);
    }
}


Checkbox::~Checkbox()
{
}

std::vector<int> Checkbox::run()
{
    printLines();
    while (1)
    {
        while (!_kbhit())
        {
        }
        processInput(_getch());
        printLines();
        if (finish)
        {
            return checkboxesValue;
        }
    }
}

void Checkbox::processInput(uint8_t input)
{
    switch (input) {
    case KEY_UP:
    case KEY_W:
        // W and up key: up
        line = line - 1;
        if (line == -1)
        {
            line = lineCount -1;
        }
        break;
    case KEY_DOWN:
    case KEY_S:
        // S and down key: down
        line = (line + 1) % lineCount;
        break;
    case KEY_ENTER:
        // enter
        finish = true;
        break;
    case KEY_SPACE:
        // space: select
        tickLine();
        break;
    }
}

char Checkbox::getInput()
{
    while (!_kbhit())
    {
    }
    char input = _getch();
    return input;
}

void Checkbox::tickLine()
{
    if (checkboxesValue[line])
    {
        checkboxesValue[line] = 0;
    }
    else
    {
        checkboxesValue[line] = 1;
    }
}

void Checkbox::printLines()
{
    system("cls");
    SetConsoleTextAttribute(hConsole, 7);
    cout << headline.c_str() << endl;
    cout << "(Use arrow keys to navigate up and down, space bar to select and enter to submit.)" << endl;
    for (size_t i = 0; i < checkboxesValue.size(); i++)
    {
        if (i == line)
        {
            SetConsoleTextAttribute(hConsole, 4);
        }
        else
        {
            SetConsoleTextAttribute(hConsole, 7);
        }
        cout << "[";
        if (checkboxesValue[i])
        {
            cout << "x";
        }
        else
        {
            cout << " ";
        }
        cout << "] " << checkboxesText[i].c_str() << endl;
    }
}

To use it just provide a vector of string and it will return a vector of int, with either 0 (not checked) or 1 (checked).

#include "stdafx.h"
#include "checkbox.h"
#include <string> 
int main()
{
    std::vector<string> lineTexts;
    lineTexts.push_back("first line");
    lineTexts.push_back("second line");
    lineTexts.push_back("third line");
    string headline = "Choose one of the following";

    Checkbox checkbox(headline, lineTexts);
    std::vector<int> result = checkbox.run();

    cout << "Checkboxes: " << endl;
    for (size_t i = 0; i < result.size(); i++)
    {
        cout << "line " << i << " " << result[i] << endl;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文