Very imple GUI variable replacement

Hi,

I need a way to (using a GUI) have a window with 12 buttons, each of which (when clicked), will first insert a set line of text into a variable, then disappear and continue on to the next part of the program.

The problem is that I have no idea how to do this, and cant find any usefull help anywhere else.

Could anybody please help me?

Thanks,
Stephen
Let me know if this is what you mean, also let me know if you have any questions, or if you want me to add in comments into the code:

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <Windows.h>
#define ErrorMessageBox(a,b) MessageBox(a,b,"Error:",MB_ICONWARNING)
#define WS_BUTTON WS_CHILD|WS_VISIBLE|WS_BORDER
#define BUTTON_1 701
#define BUTTON_2 702
#define BUTTON_3 703
#define BUTTON_4 704
#define BUTTON_5 705
#define BUTTON_6 706
#define BUTTON_7 707
#define BUTTON_8 708
#define BUTTON_9 709
#define BUTTON_10 710
#define BUTTON_11 711
#define BUTTON_12 712
char *cpText;
bool SetUpWindowClass (char*);
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
class CPaintClass
{
	PAINTSTRUCT ps;
	HDC hDC;
	HWND hParent;
	public:
	CPaintClass (HWND hWnd)
	{
		hParent = hWnd;
		hDC = BeginPaint (hParent, &ps);
		TextOut (hDC, 5, 50, cpText, strlen (cpText));
	}
	~CPaintClass ()
	{
		EndPaint (hParent, &ps);
	}
};
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpsCmdLine, int iCmdShow)
{
	if (!SetUpWindowClass ("1"))
	{
		ErrorMessageBox (NULL, "Window class \"1\" failed");
		return 0;
	}
	HWND hWnd = CreateWindow ("1", "Title Here", WS_OVERLAPPEDWINDOW, 0, 115, 1366, 480, NULL, NULL, hInstance, NULL);
	if (!hWnd)
	{
		ErrorMessageBox (NULL, "Window handle \"hWnd\" is NULL");
		return 0;
	}
	ShowWindow (hWnd, iCmdShow);
	MSG uMsg;
	while (GetMessage (&uMsg, NULL, 0, 0) > 0)
	{
		TranslateMessage (&uMsg);
		DispatchMessage (&uMsg);
	}
	return 0;
}
bool SetUpWindowClass (char *cpClassName)
{
	WNDCLASSEX WindowClass;
	WindowClass.cbClsExtra = 0;
	WindowClass.cbWndExtra = 0;
	WindowClass.cbSize = sizeof (WNDCLASSEX);
	WindowClass.style = 0;
	WindowClass.lpszClassName = cpClassName;
	WindowClass.lpszMenuName = NULL;
	WindowClass.lpfnWndProc = WindowProcedure;
	WindowClass.hInstance = GetModuleHandle (NULL);
	WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
	WindowClass.hbrBackground = CreateSolidBrush (RGB (255, 255, 255));
	WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
	WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
	if (RegisterClassEx (&WindowClass)) return true;
	else return false;
}
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uiMsg)
	{
		case WM_CLOSE:
			DestroyWindow (hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage (0);
			break;
		case WM_PAINT:
			{
				CPaintClass qPaintClass (hWnd);
			}
			break;
		case WM_CREATE:
			{
				HINSTANCE hInstance = GetModuleHandle (NULL);
				HMENU hmaMenu [13] = {
					(HMENU) BUTTON_1, (HMENU) BUTTON_2, (HMENU) BUTTON_3,
					(HMENU) BUTTON_4, (HMENU) BUTTON_5, (HMENU) BUTTON_6,
					(HMENU) BUTTON_7, (HMENU) BUTTON_8, (HMENU) BUTTON_9,
					(HMENU) BUTTON_10, (HMENU) BUTTON_11, (HMENU) BUTTON_12
				};
				char *cpaButtonName [13] = {
					"Button 1",
					"Button 2",
					"Button 3",
					"Button 4",
					"Button 5",
					"Button 6",
					"Button 7",
					"Button 8",
					"Button 9",
					"Button 10",
					"Button 11",
					"Button 12"
				};
				int iXButton = 0;
				for (int i = 0; i < 12; i++, iXButton += 110)
				{
					CreateWindow ("button", cpaButtonName [i], WS_BUTTON, iXButton, 5, 100, 30, hWnd, (HMENU) hmaMenu [i], hInstance, NULL);
				}
			}
			break;
		case WM_COMMAND:
			switch (LOWORD (wParam))
			{
				case BUTTON_1: cpText = "Button 1"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_2: cpText = "Button 2"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_3: cpText = "Button 3"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_4: cpText = "Button 4"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_5: cpText = "Button 5"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_6: cpText = "Button 6"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_7: cpText = "Button 7"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_8: cpText = "Button 8"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_9: cpText = "Button 9"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_10: cpText = "Button 10"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_11: cpText = "Button 11"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_12: cpText = "Button 12"; InvalidateRect (hWnd, NULL, TRUE); break;
			}
			break;
	}
	return DefWindowProc (hWnd, uiMsg, wParam, lParam);
}
Last edited on

I need a way to (using a GUI) have a window with 12 buttons, each of which (when clicked), will first insert a set line of text into a variable, then disappear and continue on to the next part of the program.


First, you can't put something into nothing, and since you are talking character buffer memory, you need to allocate it either through a memory allocation, or even simpler by creating a buffer of the required size by declaring a char array variable, i.e.,

char szBuffer[256];

then, in your button click procedure, you copy the text you want to put in szBuffer to that variable with strcpy() or one of its relatives. Clear as mud, huh?
Let me know if this is what you mean, also let me know if you have any questions, or if you want me to add in comments into the code:


Yes, comment codes would be very helpful, but this seems to be very close to what i need.
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <Windows.h> // Include all the code in the Windows.h file into this file.
#define ErrorMessageBox(a,b) MessageBox(a,b,"Error:",MB_ICONWARNING) // Define all calls to ErrorMesageBox as MessageBox (a, b, "Error:", MB_ICONWARNING)
#define WS_BUTTON WS_CHILD|WS_VISIBLE|WS_BORDER // Define WS_BUTTON as WS_CHILD|WS_VISIBLE|WS_BORDER
// Define some more things
#define BUTTON_1 701
#define BUTTON_2 702
#define BUTTON_3 703
#define BUTTON_4 704
#define BUTTON_5 705
#define BUTTON_6 706
#define BUTTON_7 707
#define BUTTON_8 708
#define BUTTON_9 709
#define BUTTON_10 710
#define BUTTON_11 711
#define BUTTON_12 712
// Global character pointer to hold the text
char *cpText;
// Function prototypes. Function info is at the function body (below WinMain)
bool SetUpWindowClass (char*);
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
// Class to paint text to the window
class CPaintClass
{
	// Private members:
	// Paint structure, I believe this holds some information on the text/paint that will be edited in the BeginPaint function
	PAINTSTRUCT ps;
	// Handle to the device context
	HDC hDC;
	// Parent window
	HWND hParent;
	// Public members
	public:
	// Constructor, this takes a window handle, which is what window to print the text to
	CPaintClass (HWND hWnd)
	{
		hParent = hWnd; // hParent = hWnd, so my EndPaint call can use the right arguments
		hDC = BeginPaint (hParent, &ps); // Begin the paint
		TextOut (hDC, 5, 50, cpText, strlen (cpText)); // Print the text
	}
	// Destructor, this is called when the created object falls out of scope.
	~CPaintClass ()
	{
		EndPaint (hParent, &ps); // End the paint
	}
};
// Entry point
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpsCmdLine, int iCmdShow)
{
	if (!SetUpWindowClass ("1")) // Call the SetUpWindow class function, I have a function because I usually creat multiple window classes, and I was just used to it :P
	{
		ErrorMessageBox (NULL, "Window class \"1\" failed"); // If it didn't work then print an error message box
		return 0; // Exit the program
	}
	// Create the window
	HWND hWnd = CreateWindow ("1", "Title Here", WS_OVERLAPPEDWINDOW, 0, 115, 1366, 480, NULL, NULL, hInstance, NULL);
	// If the window didn't work
	if (!hWnd)
	{
		ErrorMessageBox (NULL, "Window handle \"hWnd\" is NULL");
		return 0;
	}
	ShowWindow (hWnd, iCmdShow); // Show the window
	MSG uMsg; // Declaire a message structure
	// Message loop
	while (GetMessage (&uMsg, NULL, 0, 0) > 0)
	{
		TranslateMessage (&uMsg);
		DispatchMessage (&uMsg);
	}
	// Exit the program
	return 0;
}
// SetUpWindowClass function
// char *cpClassName - Name of the window class
// returns true if the class was registered, false if something was bad.
bool SetUpWindowClass (char *cpClassName)
{
	// Not going to explain these, just google them.
	WNDCLASSEX WindowClass;
	WindowClass.cbClsExtra = 0;
	WindowClass.cbWndExtra = 0;
	WindowClass.cbSize = sizeof (WNDCLASSEX);
	WindowClass.style = 0;
	WindowClass.lpszClassName = cpClassName;
	WindowClass.lpszMenuName = NULL;
	WindowClass.lpfnWndProc = WindowProcedure;
	WindowClass.hInstance = GetModuleHandle (NULL);
	WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
	WindowClass.hbrBackground = CreateSolidBrush (RGB (255, 255, 255));
	WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
	WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
	// Register the class and return true if it did register, else if not
	if (RegisterClassEx (&WindowClass)) return true;
	else return false;
}
// Window procedure
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uiMsg)
	{
		case WM_CLOSE: // If we close the window
			DestroyWindow (hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage (0);
			break;
		case WM_PAINT: // If we want to paint something
			{
				CPaintClass qPaintClass (hWnd); // Create an object and pass in hWnd to the default constructor
			}
			break;
		case WM_CREATE: // If we want to create something
			{
				HINSTANCE hInstance = GetModuleHandle (NULL); // hInstance is needed for CreateWindow function, and hInstance is passed into WinMain
								// so I needed to make it here so I could use it
				// Array of menu IDs
				HMENU hmaMenu [13] = {
					(HMENU) BUTTON_1, (HMENU) BUTTON_2, (HMENU) BUTTON_3,
					(HMENU) BUTTON_4, (HMENU) BUTTON_5, (HMENU) BUTTON_6,
					(HMENU) BUTTON_7, (HMENU) BUTTON_8, (HMENU) BUTTON_9,
					(HMENU) BUTTON_10, (HMENU) BUTTON_11, (HMENU) BUTTON_12
				};
				// Array of button titles
				char *cpaButtonName [13] = {
					"Button 1",
					"Button 2",
					"Button 3",
					"Button 4",
					"Button 5",
					"Button 6",
					"Button 7",
					"Button 8",
					"Button 9",
					"Button 10",
					"Button 11",
					"Button 12"
				};
				// int to hold the X axos
				int iXButton = 0;
				// Create all the buttons
				for (int i = 0; i < 12; i++, iXButton += 110)
				{
					CreateWindow ("button", cpaButtonName [i], WS_BUTTON, iXButton, 5, 100, 30, hWnd, (HMENU) hmaMenu [i], hInstance, NULL);
				}
			}
			break;
		case WM_COMMAND: // If a command (such as a key press, mouse click, or BUTTON PRESS was used) was used
			switch (LOWORD (wParam))
			{
				// there is a case statement for each button
				// 1) Set the cpText variable to what we want
				// 2) Invalidate the rect, making the WM_PAINT message get send again, making the window re-paint. This is updating the window
				case BUTTON_1: cpText = "Button 1"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_2: cpText = "Button 2"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_3: cpText = "Button 3"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_4: cpText = "Button 4"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_5: cpText = "Button 5"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_6: cpText = "Button 6"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_7: cpText = "Button 7"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_8: cpText = "Button 8"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_9: cpText = "Button 9"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_10: cpText = "Button 10"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_11: cpText = "Button 11"; InvalidateRect (hWnd, NULL, TRUE); break;
				case BUTTON_12: cpText = "Button 12"; InvalidateRect (hWnd, NULL, TRUE); break;
			}
			break;
	}
	// Handle all the other messages that we didn't
	return DefWindowProc (hWnd, uiMsg, wParam, lParam);
}
Last edited on
Topic archived. No new replies allowed.