简体   繁体   English

使用类设置win32应用程序的更好方法?

[英]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. 因此,我正在制作Calculator类。 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? (我的猜测是因为我一直在主消息循环中调用calc.Initialize()函数。如何解决该问题?

Windows.cpp: 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: 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 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);
}

Call Initialize() once before entering the loop: 进入循环之前,请调用一次Initialize()

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM