这是我第一个上课的项目。在我将所有内容放在同一个文件之前,但现在我正在创建一个需要更多功能的应用程序,它在文件中有点拥挤。所以我正在制作一个计算器课程。当我运行我的程序时,我在屏幕上的测试按钮一直闪烁。 (我的猜测是因为我一直在主消息循环中调用calc.Initialize()函数。我该如何解决这个问题?
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);
}
答案 0 :(得分:1)
在进入循环之前调用Initialize()
一次:
// Create calculator
Calculator basicCalc(hwnd);
basicCalc.Initialize();
// Main message loop
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}