Hi friends,
Can anyone tell me how can we get message boxes (e.g, with buttons OK, Cancel) in C ?
Please guide.
Thanks,
Neelu

Re: Message Box in C 80 80

Try this sample code

#include<windows.h>
int main()
{
MessageBox(0,"Hello","Welcome Message",1);
return 0;
}

Change the last parameter to see various options.

Re: Message Box in C 80 80

Try this sample code

#include<windows.h>
int main()
{
MessageBox(0,"Hello","Welcome Message",1);
return 0;
}

Change the last parameter to see various options.

Hi sunnypalsingh,
Thank you very much for your kind attention and reply to my query. This code will work fine with Windows. But I am working in Linux. In Linux, can you please tell me if there is an option to get this dialog box without GTK ?
Thanks a lot.

Re: Message Box in C 80 80

This code will work fine with Windows. But I am working in Linux. In Linux, can you please tell me if there is an option to get this dialog box without GTK ?

You should mention such requirements with original question.

I have no idea of linux. Maybe someone else can help you out in this.

Re: Message Box in C 80 80

Its not as easily accomplished in *nix -- you have to do lots and lots of programming to get that to work in graphics mode. Motif open source package helps a little. Or you might use console-based curses library functions.

Re: Message Box in C 80 80

Hi sunnypalsingh,
Thank you very much for your kind attention and reply to my query. This code will work fine with Windows. But I am working in Linux. In Linux, can you please tell me if there is an option to get this dialog box without GTK ?
Thanks a lot.

Well, without gtk and without writing lots of code after becoming an expert of X programming, you could use xmessage which is included in almost every distro... It's not an elegant way to do what you want, but it's easy and it works. For more info: man xmessage

Anyway here is an example of a MsgBox portable in win & linux

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

#if defined(WIN32) || defined(WINDOWS)

#include <windows.h>

void MsgBox(char *s){
    MessageBox(0, s, "Message", 1);
}

#else

#include <unistd.h>

void MsgBox(char *s){
    char cmd[1024];
    sprintf(cmd, "xmessage -center \"%s\"", s);
    if(fork()==0){
        close(1); close(2);
        system(cmd);
        exit(0);
    }
}

#endif
commented: Thank you guys! +0
Re: Message Box in C 80 80

Sudo: did you notice that you answered a year-old thread?? :mrgreen: :mrgreen:

Re: Message Box in C 80 80

2 years after your reply: dragon, I'm glad he did answer - it's a nice solution for my problem :-)

Marc

Re: Message Box in C 80 80

This is what worked for me in Visual Studio:-
MessageBox(NULL,(LPCWSTR)L"Hello",(LPCWSTR)L"Welcome Message",MB_OK);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.