I am learning the Win32 API. In my main / first window I would like to have an open window 2 button that then opens a new Window. So the idea is, when the button is pressed a function is called to open window 2. Yet I am not sure how to call this function here:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
switch(wParam) {
case OPEN_WINDOW2_BUTTON:
{} // <------------------------- what do I put here?
}
}
case WM_CREATE:{
AddControls1(hwnd);
break;
}
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
And here is my entire code:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <iostream>
#include <tchar.h>
#include <windows.h>
#define OPEN_WINDOW2_BUTTON 1
using namespace std;
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK windowprocessforwindow2(HWND handleforwindow2,UINT message,WPARAM wParam,LPARAM lParam);
void AddControls1(HWND);
void createwindow2 (WNDCLASSEX wincl_2, HWND& hwnd, HINSTANCE hThisInstance, int nCmdShow);
HWND hMainWindow, hwnd, hHeader;
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain(HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
bool endprogram=false;
//create window 1
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl_1; /* Data structure for the windowclass */
/* The Window structure */
wincl_1.hInstance = hThisInstance;
wincl_1.lpszClassName = szClassName;
wincl_1.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl_1.style = CS_DBLCLKS; /* Catch double-clicks */
wincl_1.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl_1.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl_1.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl_1.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl_1.lpszMenuName = NULL; /* No menu */
wincl_1.cbClsExtra = 0; /* No extra bytes after the window class */
wincl_1.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl_1.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
//----
if (!RegisterClassEx (&wincl_1))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Code::Blocks Template Windows App"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
1800, /* The programs width */
920, /* 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;
}
void createwindow2 (WNDCLASSEX wincl_2, HWND& hwnd, HINSTANCE hThisInstance, int nCmdShow)
{
if (!RegisterClassEx (&wincl_2))
wincl_2.hInstance = hThisInstance;
wincl_2.lpszClassName = szClassName;
wincl_2.lpfnWndProc = (WNDPROC)windowprocessforwindow2; /* This function is called by windows */
wincl_2.style = CS_DBLCLKS; /* Catch double-clicks */
wincl_2.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl_2.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl_2.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl_2.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl_2.lpszMenuName = NULL; /* No menu */
wincl_2.cbClsExtra = 0; /* No extra bytes after the window class */
wincl_2.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl_2.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
HWND handleforwindow2 = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Code::Blocks Template Windows App"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
330, /* The programs width */
320, /* 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(handleforwindow2,nCmdShow);
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
switch(wParam) {
case OPEN_WINDOW2_BUTTON:
{} // <------------------------- what do I put here?
}
}
case WM_CREATE:{
AddControls1(hwnd);
break;
}
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK windowprocessforwindow2(HWND handleforwindow2,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_COMMAND:
{
switch(wParam) {
}
}
case WM_CREATE:{
AddControls1(hwnd);
break;
}
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc(handleforwindow2,msg,wParam,lParam);
}
return 0;
}
void AddControls1(HWND hwnd)
{
CreateWindowW(L"Button",L" Open Window 2", WS_VISIBLE | WS_CHILD, 215,285,300,30,hwnd, (HMENU)OPEN_WINDOW2_BUTTON,NULL,NULL);
}
Any help would be greatly appreciated. Thanks!