hello all...i need help with the code below..

#include <stdio.h>
#include <windows.h>
main()
{
   
        MessageBox(0,"hello world","hello world",0);
        fflush(stdin);
        getchar();

}

now i know it is perfectly clean code...but the problem is if i put 2 messagebox functions
linke this

#include <stdio.h>
#include <windows.h>
main()
{
   
        MessageBox(0,"hello world","hello world",0);
        MessageBox(0,"hello world","hello world",0);
        fflush(stdin);
        getchar();

}

i get the same box repeating itself twice...my question is how can i open 2 boxes...
(I am still a n00b @ "c")

Recommended Answers

All 15 Replies

>>i get the same box repeating itself twice
It only appears to be the same box because you have the same identical text in each one. Change the text in one of the boxes and you should see the difference.

You can't show them both at the same time. Message Boxes blocks your program from running until one of its buttons is pressed.

well thanks on the info but i am sure about it you can there is some way right..i meen it cant be impossible..for god's sake it's "c" anythings possible.

Silly -- that isn't C, its win32 api, where you are at the mercy of Microsoft programming staff. There is a way to do it, but not by using MessageBox(). You have to create your own modeless dialog boxes. The only way I have done that is with MFC (Microsoft Foundation Class), but I'm certain it can be done with just win32 api functions as well. google for it and you might find example code.

ive been trying to google it but i am not finding what i am looking for..also i am not the best when it comes to programming windows programs ..

ive been trying to google it but i am not finding what i am looking for..also i am not the best when it comes to programming windows programs ..

Unless you use modeless dialog boxes, as suggested by Ancient Dragon, you need to have multiple threads in order to display multiple message boxes simultaneously by means of the MessageBox() function.

Unless you use modeless dialog boxes, as suggested by Ancient Dragon, you need to have multiple threads in order to display multiple message boxes simultaneously by means of the MessageBox() function.

Good sugestion

#include <windows.h>


DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    MessageBox(0,"Hello World From Thread", "HelloWorld",MB_OK);
    return 0;
}

int main()
{
    DWORD dwThreadID = 0;
    CreateThread(0,0, ThreadProc, 0,0,&dwThreadID);   
    MessageBox(0,"Hello World From Main Program", "HelloWorld",MB_OK);

}

so if i want to do three boxes to i change the thread id to 1..???

so if i want to do three boxes to i change the thread id to 1..???

No. You call CreateThread() for as many message boxes as you want to appear at the same time. The CreateThread() function will populate the dwThreadID integer with the appropriate number -- note the last parameter is a pointer.

sorry...to ask but can i have an example because i am a total newb in "c" and i know your kindness will help..please..

I already did.

i mean can you please show me an example of making 3 msgboxes cuzz ive tried doing it but only to open up so i just want a example so i can learn from it..

i mean can you please show me an example of making 3 msgboxes cuzz ive tried doing it but only to open up so i just want a example so i can learn from it..

A minimal example of displaying three message boxes, each message box is displayed by a dedicated thread ...

#include <windows.h>

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    MessageBox(0,"Hello World From a Thread", "Hello World",MB_OK);
    return 0;
}

int main()
{
    DWORD dwThreadID = 0;

    // first thread ...
    CreateThread(0, 0, ThreadProc, 0, 0, &dwThreadID);   

    // second thread ...
    CreateThread(0, 0, ThreadProc, 0, 0, &dwThreadID);

    // and the third thread ...
    const HANDLE hThread3 = CreateThread(0, 0, ThreadProc, 0, 0, &dwThreadID);

    // the program waits on the hThread3 handle, i.e.
    // as soon as the message box displayed by the
    // thread #3 has been closed, the program will terminate
    WaitForSingleObject(hThread3, INFINITE);

    return 0;
}

Note that there is no error checking whatsoever, but maybe you get the idea. Also note the WaitForSingleObject() , without it, you probably would not get to see any of the message boxes.

The other alternative using modeless dialog boxes, would probably be easier for you (i.e. you can display as many message boxes as you want without multiple threads).

after debugging it and compiling it i just see a screen flicker for a milisecond and the whole program exits..but i sorta get the idea..does any one have links to websites that explain this thoroughly because ive been searching the dungeons of google and i get nothing..

If you leave the 3d message box in the main thread then you don't have to call that WaitForSingleObject() function, and the program don't exit immediatey. But the program will exit immediately if you press the Ok button in the MessageBox that appears in the main thread. Note that you can create as many message boxes as you want with the posted code. If you want four MessageBoxes then just call CreateThread() another time.

As I said in my previous post -- I already showed you how to do it. All you have to do is ass more CreateThread() function calls.

yeah i know..but how can i make them open all at the same time..

Be a part of the DaniWeb community

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