Search code examples
c++messagebox

dynamic MessageBox using variables (c++)


I want to create a dynamic MessageBox using a char array, but i have some problem with the uType (UINT)..

If you watch my code, i first have a char string with | delimiters and it's parsed into the array "a". Then i convert my char into UINT but i cant get it work, it just doesnt show any messagebox. Thanks for helping :-)

char str[] ="Testing message|Title Message|MB_OK|MB_ICONINFORMATION";
char * pch;
char * a[4];
int i = 0;
pch = strtok (str,"|");
while (pch != NULL)
{
    a[i] = pch;
    pch = strtok (NULL, "|");
    //cout << a[i];
    i++;
}
char test[1000] = "";
strcat_s (test,a[2]);
strcat_s (test,"|");
strcat_s (test,a[3]);

UINT y;
stringstream s;
s << test;
s >> y;


MessageBox(0,a[0],a[1],y);
Sleep(10000);

Solution :

                        UINT x;
                            if(!strcmp(a[3],"MB_ICONERROR")){
                                x = 0x10;
                            }else if(!strcmp(a[3],"MB_ICONEXCLAMATION")){
                                x = 0x30;
                            }else if(!strcmp(a[3],"MB_ICONINFORMATION")){
                                x = 0x40;
                            }else if(!strcmp(a[3],"MB_ICONQUESTION")){
                                x = 0x20;
                            }
                            if(!strcmp(a[2],"MB_OK")){
                                x = x + 0;
                            }else if(!strcmp(a[2],"MB_OKCANCEL")){
                                x = x + 1;
                            }else if(!strcmp(a[2],"MB_YESNO")){
                                x = x + 4;
                            }else if(!strcmp(a[2],"MB_YESNOCANCEL")){
                                x = x + 3;
                            }else if(!strcmp(a[2],"MB_RETRYCANCEL")){
                                x = x + 5;
                            }else if(!strcmp(a[2],"MB_ABORTRETRYIGNORE")){
                                x = x + 2;
                            }
                            MessageBox(0, a[0], a[1], x);

Solution

  • MB_OK|MB_ICONINFORMATION
    

    Those are 'enumerations' and not actual strings. In your program (outside of the string), they would get replaced by numbers. What you're doing, is trying to convert a plain string into a number, which obviously doesn't yield the expected result, since the system doesn't recognize the enumerations any more then.

    I suggest using some sort of mask, or inputting the 'plain' value into the string. For your example, that's 0x40, as can be seen here.

    So, try to use a string like this:

    char str[] ="Testing message|Title Message|0x40";
    

    And make sure, that the last element is treated as Integer, and not as 'string'.