2014-02-21 32 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); 
} 
相關問題