为 TinyFileDialogs 运行 Visual Studio C 程序,但最喜欢的颜色弹出窗口不起作用

问题描述 投票:0回答:1

我一直在从事tinyfiledialogs代码项目,我已经设置好了环境并且可以编译代码。

//Number of beeps
void beeper(int num_beeps) {
    char message[100];
    sprintf(message, "Beeping %d times! \n Press OK to initiate.", num_beeps);
    tinyfd_messageBox("Notification", message, "info", "OK", 1);
    
    //beeps
   for (int i = 0; i < num_beeps; i++) {
        Beep(800, 300);  // Frequency and duration of the beep sound
        Sleep(300);      // Pause between beeps (in milliseconds)
    }
    
}
//Favorite color and store RGB values
void mood_ring(struct FavoriteColor* color) {
    char const* options[8] = { "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Pink", "Cancel" };
    int choice = tinyfd_openFileDialog("Pick your favorite color", "Choose a color:", 8, options, NULL, 1);

    //User cancel
    if (choice == -1 || choice == 7) {
        tinyfd_messageBox("Error", "You cancelled the color selection.", "error", "OK", 1);
        return;
    }

    //RGB values
    switch (choice) {
    case 0: // Red
        color->red = 204;
        color->green = 0;
        color->blue = 0;
        break;
    case 1: // Orange
        color->red = 255;
        color->green = 153;
        color->blue = 0;
        break;
    case 2: // Yellow
        color->red = 255;
        color->green = 255;
        color->blue = 0;
        break;
    case 3: // Green
        color->red = 0;
        color->green = 128;
        color->blue = 0;
        break;
    case 4: // Blue
        color->red = 0;
        color->green = 0;
        color->blue = 204;
        break;
    case 5: // Purple
        color->red = 102;
        color->green = 0;
        color->blue = 153;
        break;
    case 6: // Pink
        color->red = 255;
        color->green = 20;
        color->blue = 147;
        break;
    }
}

int main(int argc, char* argv[]) {
    if (argc < 3) {
        return -1;
    }

    //Number of beeps
    int num_beeps = atoi(argv[2]);
    beeper(num_beeps);


    //Favorite color
    struct FavoriteColor person_color;

    strncpy(person_color.name, argv[1], sizeof(person_color.name) - 1);
    person_color.name[sizeof(person_color.name) - 1] = '\0';

    mood_ring(&person_color);

    char message[256];
    printf(message, sizeof(message), "Name: %s\nRed: %d\nGreen: %d\nBlue: %d", person_color.name, person_color.red, person_color.green, person_color.blue);

    tinyfd_messageBox("Favorite Color", message, "info", "OK", 1);

    return 0;
}

编辑! 我的参数数量是错误的,现在它会打印我的名字并打开一个弹出窗口以发出嘟嘟声并发出嘟嘟声!但没有其他内容出现。 对于上下文来说,应该有更多关于最喜欢的颜色的弹出窗口,但这种情况没有发生。命令行打印这个。

╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠MacKenna

我期望的是与此类似的输出:

Input: ./P_MiniProject3 John 5
(Output dialog box appears)
Notification: Beeping 5 times!
(Color selection dialog box appears)
User selects Green as their favorite color.
(Message box appears)
Favorite Color:
Name: John
Red: 0
Green: 255
Blue: 0

或者至少有错误消息,例如:

Input: ./P_MiniProject3 Bob
(Output dialog box appears)
Error: Incorrect number of arguments provided. Please provide a name and the number of beeps.
c visual-studio command-line-arguments popupwindow
1个回答
0
投票

在尝试对代码进行故障排除以将问题归零时,我必须做出一些有根据的猜测并进行一些代码替换,因为我没有使用“tinyfd”小部件集(更熟悉“GTK”)。考虑到这一点,我确实获得了明显的功能,因为它涉及在定义的结构中存储命令行信息和颜色选择。

以下是代码的重构版本,以便复制所需的功能,同时考虑到一些假设和有根据的猜测。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct FavoriteColor {          /* Educated guess of the structure definition as it was not included in the issue */
    char name[100];
    int red;
    int green;
    int blue;
};

// Number of beeps
void beeper(int num_beeps)
{
    char message[100];
    sprintf(message, "Beeping %d times! \nPress OK to initiate.\n", num_beeps);
    //tinyfd_messageBox("Notification", message, "info", "OK", 1);
    printf("%s", message);      /* Substituted this for the message box */

    // Beeps
    for (int i = 0; i < num_beeps; ++i)
    {
        printf("Beep\n");
    }
}

// Favorite color and store RGB values
void mood_ring(struct FavoriteColor* color)
{
    char const* options[8] = { "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Pink", "Cancel" };

    //int choice = tinyfd_openFileDialog("Pick your favorite color", "Choose a color:", 8, options, NULL, 1);       /* Suspect line of code */

    //char *selection = tinyfd_openFileDialog("Pick your favorite color", "Choose a color:", 8, options, NULL, 1);  /* Likely format for using this dialog box */

    int choice;

    char *selection = "Purple";                 /* Simulate making a selection from the dialog box  */

    for (choice = 0; choice < 8; choice++)      /* Evaluating the selection to an integer value     */
    {
        if (strcmp(selection, options[choice]) == 0)
            break;
    }

    // User cancel
    if (choice == -1 || choice == 7)
    {
        printf("You cancelled the color selection\n");
    }

    // RGB values
    switch (choice)
    {
    case 0: // Red
        color->red = 204;
        color->green = 0;
        color->blue = 0;
        break;
    case 1: // Orange
        color->red = 255;
        color->green = 153;
        color->blue = 0;
        break;
    case 2: // Yellow
        color->red = 255;
        color->green = 255;
        color->blue = 0;
    case 3: // Green
        color->red = 0;
        color->green = 128;
        color->blue = 0;
        break;
    case 4: // Blue
        color->red = 0;
        color->green = 0;
        color->blue = 204;
        break;
    case 5: // Purple
        color->red = 102;
        color->green = 0;
        color->blue = 153;
        break;
    case 6: // Pink
        color->red = 255;
        color->green = 20;
        color->blue = 147;
        break;

    default:                /* Default - such as when cancel is selected */
        color->red = 128;
        color->green = 128;
        color->blue = 128;
        break;
    }
}

int main(int argc, char* argv[])
{
    if (argc < 3)           /* Noted correction in the comments */
    {
        return -1;
    }

    // Number of beeps
    int num_beeps = atoi(argv[2]);
    beeper(num_beeps);

    // Favorite color
    struct FavoriteColor person_color;

    strncpy(person_color.name, argv[1], sizeof(person_color.name) - 1);
    person_color.name[sizeof(person_color.name) - 1] = '\0';

    mood_ring(&person_color);

    //char message[256];        /* This string variable was not being populated and could cause undefined behaviour */
    //printf(message, sizeof(message), "Name: %s\nRed: %d\nGreen: %d\nBlue: %d", person_color.name, person_color.red, person_color.green, person_color.blue);

    printf("Name: %s\nRed: %d\nGreen: %d\nBlue: %d\n", person_color.name, person_color.red, person_color.green, person_color.blue); /* In lieu of message box */

    //tinyfd_messageBox("Favorite Color", message, "info", "ok", 1);

    return 0;
}

首先,我很抱歉包含完整的代码,但重构的各个部分分散在整个程序中。以下是要点。

  • 您的代码示例中没有“FavoriteColor”的结构定义,其定义至少允许捕获程序中所需的信息。
  • 如前所述,任何对“tinyfd”小部件(消息、对话框等)的引用都被注释掉,并替换了打印或模拟输入数据的等效方法,从而保持核心功能完整。
  • 参考“tinyMessageBox”小部件应该返回的值类型,包含注释掉的代码行作为重构程序以赋予其适当功能的可能建议。
  • 正如其他支持者最初的好评中所指出的,参数计数检查已更正以测试命令行参数的正确数量。
  • 此外,在“main”函数中似乎有可能在“message”字符数组的定义中出现未定义的行为,因此该位被停用(不知道这是否是由于您的代码中缺少一些代码而导致的)样品)。

在审查了您编辑的代码和关于问题所在的注释之后,我确实对“tinyfd_messageBox”函数的使用进行了一些研究,并注意到该函数返回一个字符数组(字符串)来代替整数,这就是您的方式定义了您的代码行。将字符串值返回到整数变量很可能是您的争论点,因为此时您可能正在经历未定义的行为。

接下来是带有终端输出的示例运行(紫色是测试选择的颜色)。

craig@Vera:~/C_Programs/Console/Beeper/bin/Release$ ./Beeper Craig 5
Beeping 5 times! 
Press OK to initiate.
Beep
Beep
Beep
Beep
Beep
Name: Craig
Red: 102
Green: 0
Blue: 153

这里需要注意的是,可能需要进一步研究“tinyfd”工具包的正确使用,以及可能进一步使用一些“C”教程文献,因为它涉及变量定义和用法。

希望您能够分析此示例代码以及解释某些替换原因的附加注释。

© www.soinside.com 2019 - 2024. All rights reserved.