2014-02-21 34 views
0

这是我第一个上课的项目。在我将所有内容都放在同一个文件之前,但现在我正在制作一个需要更多功能的应用程序之前,它在文件中有点拥挤。所以我正在制作一个Calculator类。当我运行我的程序时,屏幕上的测试按钮不断闪烁。 (我猜是因为我一直在打主消息循环calc.Initialize()函数,我将如何解决这个问题使用类设置win32应用程序的更好方法?

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); 
} 

回答

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); 
} 
相关问题