Open a new window (win32) c++

Hello! This is my first post so...

I want a simple program to open a new window on a button click, and close the old window. The code is below.

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <windows.h>
#include "resource.h"

using namespace std;

bool clicked = false;
HBITMAP background = NULL;
HBITMAP g_ball = NULL;
HBITMAP g_ballmask = NULL;
HINSTANCE hThisInstance;

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "First GUI";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = (HICON)LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    wincl.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);                 /* menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Good Window Title",           /* Title Text */
           WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           675,                 /* The programs width */
           365,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
	HDC hdcMem, hdcMem2;
	HBITMAP hbmMask;
	BITMAP bm;

	GetObject(hbmColour, sizeof(BITMAP), &bm);
	hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);

	hdcMem = CreateCompatibleDC(0);
	hdcMem2 = CreateCompatibleDC(0);

	SelectObject(hdcMem, hbmColour);
	SelectObject(hdcMem2, hbmMask);

	SetBkColor(hdcMem, crTransparent);

	BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

	BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);

	DeleteDC(hdcMem);
	DeleteDC(hdcMem2);

	return hbmMask;
}

BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:

        return TRUE;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_OK:
                    EndDialog(hwnd, ID_OK);
                break;
                case ID_THATSCOOL:
                    EndDialog(hwnd, ID_THATSCOOL);
                break;
            }
        break;
        default:
            return FALSE;
    }
    return TRUE;
}
LRESULT CALLBACK WindowProcedure(HWND hWnd, int nCmdShow, UINT msg)
{
    switch(msg)
   case WM_CREATE:
if(clicked)
{
// create the window and use the result as the handle
hWnd = CreateWindowEx(NULL,
"WindowClass1", // name of the window class
"no", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
300, // y-position of the window
500, // width of the window
400, // height of the window
HWND_DESKTOP, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hThisInstance, // application handle
NULL); // used with multiple windows, NULL
    // display the window on the screen
    ShowWindow(hWnd, nCmdShow);
}

    return TRUE;
}


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{                                    /*Parameters for main window*/
    switch (message)                  /* handle the messages */
    {

        case WM_PAINT:
        {
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hwnd, &ps);
			EndPaint(hwnd, &ps);
        }
		break;

        case WM_CREATE:
        {
        background = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BACKGROUND));
			if(background == NULL)
				MessageBox(hwnd, "Could not load background.", "Error", MB_OK | MB_ICONEXCLAMATION);

        g_ball = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
			if(g_ball == NULL)
				MessageBox(hwnd, "Could not load ball!", "Error", MB_OK | MB_ICONEXCLAMATION);

        g_ballmask = CreateBitmapMask(g_ball, RGB(0, 0, 0));
			if(g_ballmask == NULL)
				MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION);
        }
        break;

        case WM_COMMAND:

        switch(LOWORD(wParam))
            {

                case ID_FILE_SAVEAS:

                MessageBox(hwnd, "This id is ID_FILE_SAVEAS", "id", MB_OK);

                break;

                case ID_FILE_NEW:

                MessageBox(hwnd, "This id is ID_FILE_NEW", "id", MB_OK);

                break;

                case ID_FILE_OPEN:

//                WindowProcedure(hwnd,UINT msg,  );

                break;

                case ID_FILE_SAVE:

                MessageBox(hwnd, "This id is ID_FILE_SAVE", "id", MB_OK);

                break;

                case ID_FILE_EXIT:

                PostMessage(hwnd, WM_CLOSE, 0, 0);

                break;

                case ID_STUFF_GO:

                MessageBox(hwnd, "go", "go", MB_OK);

                break;

                case ID_ABOUT:

            int ret = DialogBox(GetModuleHandle(NULL),
                MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
                if (ret == ID_THATSCOOL){
                MessageBox(hwnd, "I understand.", "First GUI",
                    MB_OK);
            }
            else if(ret == -1){
                MessageBox(hwnd, "Dialog failed!", "Error",
                    MB_OK | MB_ICONINFORMATION);
                                }
                        }
            break;


        case WM_CLOSE:
            {
            DestroyWindow(hwnd);
            }
            break;

        case WM_DESTROY:
        {
            DeleteObject(g_ball);
            DeleteObject(g_ballmask);
			PostQuitMessage(0);       /* send a WM_QUIT to the message queue */
        }
            break;

        case WM_LBUTTONDOWN:
			{
				clicked=true;
//				goto CREATE;
				return 0;
			} break;

        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


Sorry for the length.

Thank You!
What is the question, if you please?

Also a few more pointers:

-If you are getting compilation errors, post those errors here.
-If you need help with the actual logic, make sure you say so.
-If your program compiles OK but doesn't work as expected, then make sure you state what the incorrect behavior is and the correct behavior you want.
-If you want the experts to help with a particular area, point out the line numbers you think are the key to the problem.

In general, don't expect someone is going to try to compile your project and just give it back fixed. You should always be explicit about what you have, what you are experiencing and what you need.
Last edited on
Ok, im was too lazy to actually go through your code and rewrite what it should be, but I will tell you the general process of what you are trying to do. In your "WindowsProcedure" function, when your button is clicked, open up the dialog, then use the "DestroyWindow(hwnd)" function. Then just use the dialog/other window as your main "hwnd"
Topic archived. No new replies allowed.