Message Box in C++

This is a discussion on Message Box in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi. I have a question, I like the MessageBox in my my program but not to much windows. I created ...

  1. #1
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133

    Message Box in C++

    Hi.
    I have a question, I like the MessageBox in my my program but not to much windows. I created this little program in a couple of minuts but I have a problem: I really like that before the program starts that he ask if you want to you it, that's quite simple but I want the MessageBox who ask if you want to use it, displaying first before the black Win 32 Console screen (*.exe) displays (shows up) and that if you click on "No" that he quits ( exit (0); ) but if you quit the Win 32 Console still mussn't be displayed and "Yes", then it starts:
    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int one;
    int two;
    int three;
    int four;
    
    int main()
    {
        cout<<"Hi there, typ in 100: ";
        cin>> one;
        cout<<endl;
        
        if ( one == 100 ){
        cout<<endl;
        cout<<"Good, thanks for using this software."<<endl;
        cout<<endl;
        
        Sleep(3000);
     	three = MessageBox( 0, "Do you want to quit?", "Quit", 4 );
        
     	if (three == IDNO){
        main();
        }
        }
        
    	else{
        cout<<endl;
        cout<<"You need to typ 100 not something else!"<<endl;
        cout<<endl;
        cout<<"Do you want to try again or quit? Press 1 to try again, 2 to stop: ";
        cin>> two;
        cout<<endl;
        }
        
        if ( two == 1 ){
        main();
        }
        
        else{
        four = MessageBox( 0, "Thank you for using this software!", "Bye", MB_ICONEXCLAMATION );
        
        if (four == IDOK){
        cout<<"BYEEEEEEEEEEEEEEEEEEEEEEEEEE!!!!!!!!!!!!!!!!!!!!"<<endl;
        cout<<endl;
        Sleep(1000);
        exit(0);
        }
        }
    }
    I don't know much about all that windows code (only MB, a bit), maybe it is possible in C++ but if it just works it's fine.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    #include <iostream>
    #include <windows.h>
    
    
    int main()
    {
    	int iRet = ::MessageBox(0, "rawr", "rawr", MB_YESNO);
    
    	if(iRet == IDYES) std::cout << "You pressed yes";
    	else std::cout << "You pressed no probably";
    
    	return 0;
    }
    It's self explanitory. Resources used to make this code: http://msdn.microsoft.com/library/de...messagebox.asp I suggest always checking MSDN for your win32 needs. Check the return value table for other MessageBox returns for other style types. "Not knowing much about windows code" is no excuse to ignore the vast resources at your fingertips!

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int main()
    {
        ...
    
        main();
    }
    Don't do that! Assuming calling main is even valid, you'll eventually blow the stack. Look into loops (while).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    1. If I use your code (Tonto) it still opens the Win 32 Console screen and a Message Box but what I want is that he first opens the Message Box and if you click on "Yes" then he opens the Win 32 screen whith the rest of the program and if you click on "No" then he just quits.
    2. I don't get it, what is wrong whith "main();" please explain me better, I am still learning.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    1) Okay. I do not actually know how this could be done with a console window without using a 'hackish' method like ShowWindow(hwnd, SW_HIDE); further hacking stuff up with a FindWindow to obtain the console window handle, and then conditionally re-shwoing it once the user clicks. I mean if that's fine for you, whatev, however, I highly suggest getting into fullblown win32 programming. This tutorial" http://www.functionx.com/win32/ is very in detail. Then you can put the MessageBox before everything, and then CreateWindow[Ex] afterwards. This makes the particular task much easier.

    2) May I quote a nice article? It explains the asm RET and CALL instruction very well.

    Quote Originally Posted by http://www.enderunix.org/docs/eng/bof-eng.txt
    ...However, ix86 processor family provides us with two instructions: CALL and
    RET, making our lives easy! the CALL instruction writes that "next instruction
    to be executed after function returns" (from then on, we'll call this
    as the "return address") to the stack. It PUSHes it onto the stack, and writes
    the address of the function to EIP. Thus, a function call is made. The RET
    instruction, on the other hand, POPs the "return address" from the stack, and
    writes that address in the EIP. Thus we'll safely return from the function,
    and continue with the program's next thread of execution.
    Because you are calling main recursively however, you just keep going down down down into the memory, and you never return from all the call's that you've been making. It breaks one of the fundamental thingies of recursion, that you have a "check" so that when a condition is met you pop back you of all the frames you've "allocated". So if you call main() recursively like you do enough times, it would just like make the stack and computer explode, and your pets would die, and your socialife would fall apart, etc.

  6. #6
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    How do I need to use "ShowWindow(hwnd, SW_HIDE);" if I use it it doens't do anything if I use it like this:
    Code:
        HWND hwnd;
        ShowWindow(hwnd, SW_HIDE);
    Thanks anyway but it would really help if it worked.

  7. #7
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Well yeah it's not going to work, that HWND variable isn't assigned to anything yet.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  8. #8
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    So... how does it work then?

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Technique described at gamedev or codeguru I think. Dunno. Very very ugly and hackish.

    Code:
    #include <windows.h>
    #include <iostream>
    int main(int argc, char* argv[])
    {
    	char szOld[200];
    	char szNew[200] = "hello";
    	int iRet;
    
    	::GetConsoleTitle(szOld, 200);
    	::SetConsoleTitle(szNew);
    	::Sleep(100);
    
    
    
    	HWND hwnd = ::FindWindow(NULL, szNew);
    	if(!hwnd)
    	{
    		::MessageBox(0, "Window handle not obtained", "wtf", MB_OK);
    		return 0;
    	}
    
    	::SetConsoleTitle(szOld);
    	::ShowWindow(hwnd, SW_HIDE);
    
    	if((iRet = ::MessageBox(0, "I'm asking you a question ..........", "rwawrwarr", MB_YESNO)) == IDYES)
    	{
    		::ShowWindow(hwnd, SW_SHOWNORMAL);
    	}
    	
    	return 0;
    }
    I eluded to this without providing the code in my post above saying:

    Quote Originally Posted by Me
    ...further hacking stuff up with a FindWindow to obtain the console window handle...

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    According to the C++ standard you should not call main from anywhere in your program.

  11. #11
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Ok, thanks it works it still shows up for a little time but that doesn't care.

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Then do it properly and write a windows program that allocates its own console when necessary. Look up AllocConsole()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Desperate Over Ecs!
    By cookie in forum C Programming
    Replies: 17
    Last Post: 07-16-2008, 02:25 PM
  2. Annoying Message Box Problem
    By Necrofear in forum C++ Programming
    Replies: 14
    Last Post: 07-02-2006, 12:25 PM
  3. input/output
    By dogbert234 in forum Windows Programming
    Replies: 11
    Last Post: 01-26-2005, 06:57 AM
  4. Positioning Message Box MFC
    By UnclePunker in forum Windows Programming
    Replies: 2
    Last Post: 08-12-2004, 05:14 AM
  5. Dialog In Message Box
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2002, 10:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21