I have a Window
class and a MainMenu
class. In the Window
class, I create the window itself, and in the MainMenu
class I create the controls for the Window
(like in C# with form and user-control).
Do I need to define, let's say #define EXIT_BUTTON 1
, in Window.cpp
and MainMenu.cpp
for button events to work, or is there a better way?
exit = CreateWindow(L"Button", L"Exit", style, monitor.right / 2 - 100, 150, 200, 100, m_hWnd, **HMENU(EXIT_BUTTON)**, NULL, NULL);
Window.cpp
#include "Window.h"
#define EXIT_BUTTON 1
MainMenu* mainMenu1;
Window::Window() : m_hInst(GetModuleHandle(nullptr)) //creates the window
{
WNDCLASS wc = {};
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = m_hInst;
wc.lpszClassName = ClassName;
wc.lpfnWndProc = WindProc;
RegisterClass(&wc);
DWORD style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
GetWindowRect(GetDesktopWindow(), &monitor);
m_hWnd = CreateWindow(ClassName, WindowTitle, style, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
mainMenu1 = new MainMenu(m_hWnd, monitor);
ShowWindow(m_hWnd, SW_MAXIMIZE);
}
Window::~Window()
{
UnregisterClass(ClassName, m_hInst);
delete mainMenu1;
}
LRESULT CALLBACK WindProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) // gets input from user
{
switch (msg) {
case WM_CREATE:
AddControls();
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch (wp) {
case EXIT_BUTTON:
PostQuitMessage(0);
break;
}
case WM_WINDOWPOSCHANGED:
std::cout << "1";
break;
default:
return DefWindowProcW(hwnd, msg, wp, lp);
}
return 1;
}
bool Window::ProcessMessage()
{
MSG msg = {};
while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
if (msg.message == WM_QUIT)
return false;
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return true;
}
void AddControls()
{
mainMenu1->Initialize();
}
MainMenu.cpp
#include "MainMenu.h"
#define EXIT_BUTTON 1
MainMenu::MainMenu(HWND hWnd, RECT monitor)
{
this->monitor = monitor;
m_hWnd = hWnd;
}
MainMenu::~MainMenu()
{
}
void MainMenu::Initialize()
{
DWORD style = WS_VISIBLE | WS_CHILD | SS_CENTER;
title = CreateWindow(L"static", L"Welcome", style, monitor.right / 2 - 100, 100, 200, 100, m_hWnd, NULL, NULL, NULL);
SendMessage(title, WM_SETFONT, WPARAM(CreateFont(50, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial")), true);
exit = CreateWindow(L"Button", L"Exit", style, monitor.right / 2 - 100, 150, 200, 100, m_hWnd, HMENU(EXIT_BUTTON), NULL, NULL);
}
void MainMenu::Hide()
{
}
void MainMenu::Show()
{
}
You should create app_common.h header file and write define to it.
app_common.h
#pragma once
#define EXIT_BUTTON 1
and update your Window.cpp
#include "app_common.h"
#include "Window.h"
....
and then update your MainMenu.cpp
#include "app_common.h"
#include "MainMenu.h"
.....