Message box

I want to make a message box without an 'X' button pop up.

I already have this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <windows.h>

/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
	switch(Message) {
		case WM_CREATE: {
			::MessageBox(hwnd,"Click the OK button to continue.","Hello there!",MB_OK); // here is the message box
			break;
		}
		
		/* Upon destruction, tell the main thread to stop */
		case WM_DESTROY: {
			PostQuitMessage(0);
			break;
		}
		
		/* All other messages (a lot of them) are processed using default procedures */
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wc; /* A properties struct of our window */
	HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
	MSG Msg; /* A temporary location for all messages */
	/* zero out the struct and set the stuff we want to modify */
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = WndProc; /* This is where we will send messages to */
	wc.hInstance	 = hInstance;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "WindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		640, /* width */
		480, /* height */
		NULL,NULL,hInstance,NULL);

	if(hwnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	/*
		This is the heart of our program where all input is processed and 
		sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
		this loop will not produce unreasonably high CPU usage
	*/
	while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
		TranslateMessage(&Msg); /* Translate key codes to chars if present */
		DispatchMessage(&Msg); /* Send it to WndProc */
	}
	return Msg.wParam;
}


The message box is on line 7.
Last edited on
I want to make a message box without an 'X' button pop up.
I'm not sure what you mean by 'X' button pop up, but you can specify a number of flags that customize a MessageBox's appearance.
Like for the browser you're looking at right now, on the top right side there is a minimize, restore down/maximize, and close button. I don't want the close button to appear.
I don't think there's a way to do that with MessageBox. Though why would you want to?

The only way I could think to do this is to create your own dialog box and pop it up instead of calling MessageBox.


Also... side note... you're using the wrong character type:
http://www.cplusplus.com/forum/windows/106683/
I want to troll my friend. LOL
Yeah but... he can just click whatever button is on the message box. You can't make a messagebox without any buttons... so what difference does it make if there's an X button in the corner?


EDIT:

Also... FWIW... while you can't remove the corner X, you can disable it by choosing some of the button combos. I think any combo without a 'cancel' button will do the trick (since the X is treated the same as the Cancel button).

IE...
 
MessageBoxA(NULL,"test","test",MB_YESNO);


Will have Yes and No buttons, but will disable the corner X.
Last edited on
No, I only want him to click the OK button, so I can do something after that.

EDIT:

Yes! It works! Is there an option for only one button?

I mean something like only "Yes"?
Last edited on
What difference does it make whether he presses OK or the X? Your program will run the same regardless.

EDIT:
I mean something like only "Yes"?


Just use MB_OK. Him pressing the OK button is exactly the same as him pressing the corner X. There literally is no reason to remove the corner X. Your program will run exactly the same regardless.

Try it:

1
2
3
4
5
6
7
8
9

#include <Windows.h>

int main()
{
    MessageBoxA(NULL,"test","test",MB_OK);
    
    MessageBoxA(NULL,"This will pop up regardless of whether or not you press the X on the previous box","test",MB_OK);
}
Last edited on
I want another message box to pop up after he clicks "OK". And display a different message.
Last edited on
I want another message box to pop up after he clicks "OK". And display a different message.


Yes... you can do that with MB_OK. Even if he presses the corner X.

When you use MB_OK... the corner X is treated just like the OK button. Pressing either of them literally does the exact same thing.
@ Disch: Regarding the post in your link: it was a very pointed and informative rant but is there a reason that you omitted BSTR's? I know they are functionally wchar_t's, but due to how they're formed you can't treat them the same.
BSTRs are a COM thing that I'm not really familiar with (I haven't dealt with COM very much)
Oh! OK! Thanks Disch. Is there an option for no button then? (only the 'X' button)
No. All the options are available on this page:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505%28v=vs.85%29.aspx

But again, you don't need to remove any buttons. MB_OK will do what you want (user only has 1 option)
Thanks!
This will make it more fun

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <Windows.h>
#pragma warning(default:4716)

int repeate() {
	MessageBoxA(NULL, "test", "test", MB_OK);
	MessageBoxA(NULL, "This will pop up regardless of whether or not you press the X on the previous box", "test", MB_OK);
	repeate();

}

int main()
{

HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth, 0);
repeate();

}


It will repeate over and over and plus that black box wont show up :) but if you know what your doing you can still kill the program.

Last edited on
It will repeate over and over


Until it crashes because you ran out of stack space due to your infinite recursion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <Windows.h>
#pragma warning (default:4716); 

int repeate() {

	switch (MessageBoxA(NULL, "test", "test", MB_OK))
	{
	case IDOK:
		switch (MessageBoxA(NULL, "This will pop up regardless of whether or not you press the X on the previous box", "test", MB_OK)) {

		case IDOK:
			repeate();
		}

	}
}

	


	int main()
{

	HWND Stealth;
	AllocConsole();
	Stealth = FindWindowA("ConsoleWindowClass", NULL);
	ShowWindow(Stealth, 0);
	repeate();


} 


Will this do it then?
Cause shouldn't this wait until you have pressed ok before it moves on to the next message box?

EDIT if this will also cause it to crash then you can use another program to check if this one is running and if it isint have that program start this one again.
Last edited on
That's exactly the same.

repeate() is calling repeate() endlessly. Each time you call a function, the current function info gets pushed to the stack. It gets popped when you return from the called function.

However, You never return. You keep calling and calling infinitely. So you keep using more and more stack space. Eventually you'll run out and the program will explode.


If you want to loop, use a loop:

1
2
3
4
while(true)
{
    // this will loop infinitely
}
Topic archived. No new replies allowed.