Vince
Vince

Reputation: 2646

Better way to set up win32 application using classes?

This is my first project where I'm making a class. Before I had everything under the same file, but now that I'm making an application that requires a lot more functions, it got a little crowded in the file. So I'm in the process of making a Calculator class. When i run my program, the test button I have on the screen keeps flashing. ( my guess is because I keep calling the calc.Initialize() function in the main message loop. How would I fix that problem?

Windows.cpp:

// Create calculator
Calculator basicCalc(hwnd);

// Main message loop
MSG msg;
ZeroMemory(&msg, sizof(msg));
while(msg.message != WM_QUIT)
{
  if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  else
    basicCalc.Initialize();
}

Calculator.h:

#pragma once
#include <Windows.h>
#include <wchar.h>
#include <math.h>
#include "Resource.h"

class Calculator
{
public:
  Calculator(HWND hwnd);
  ~Calculator();
  void Initialize();

private:
  CreateButtons(HWND hwnd);
};

Calculator.cpp

void Calculator::Initialize()
{
    CreateButtons(hwnd);
}

void Calculator::CreateButtons(HWND hwnd)
{
    HWND button = CreateWindowEx(0, L"BUTTON", L"L", WS_CHILD | WS_VISIBLE, 30, 30, 50, 50, hwnd, (HMENU)IDC_BACK, NULL, NULL);
    ShowWindow(button, SW_SHOW);
}

Upvotes: 0

Views: 92

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596582

Call Initialize() once before entering the loop:

// Create calculator
Calculator basicCalc(hwnd);
basicCalc.Initialize();

// Main message loop
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

Upvotes: 1

Related Questions