⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.cpp

📁 c++程序设计讲义 cppprograming 含源码
💻 CPP
字号:
// example showing use of timer events
// the program displays a bitmap in the window at
// a random location when the timer goes off

#include <assert.h>
#include <bitmap.h>
#include <randint.h>

// define two nonoverlapping windows
SimpleWindow W1("Window One", 15.0, 9.0, Position(1.0, 1.0));
SimpleWindow W2("Window Two", 15.0, 9.0, Position(8.0, 12.0));
    
// define two bitmaps, one for each window
BitMap W1Bmp(W1);
BitMap W2Bmp(W2);
    
// redisplay(): move bitmap to a new location
void Redisplay(SimpleWindow &W, BitMap &B) {
    // erase the bitmap
    B.Erase();
    
    // compute a new position and display the bitmap
    // make sure the bitmap is completely in the window
    // initialize random number generator. then create a
    // random number generator for the X position
    EzRandomize();
    RandomInt X(1, (int) W.GetWidth());
    int XCoord = X.Draw();
    if (XCoord + B.GetWidth() > W.GetWidth())
        XCoord = XCoord - B.GetWidth();
        
    // create RandomInt object the Y position
    RandomInt Y(1, (int) W.GetHeight());
    int YCoord = Y.Draw();
    if (YCoord + B.GetHeight() > W.GetHeight())
        YCoord = YCoord - B.GetHeight();
    B.SetPosition(Position(XCoord, YCoord));
    B.Draw();
}

// W1TimerEvent(): callback function for window 1
int W1TimerEvent() {
    Redisplay(W1, W1Bmp);
    return 1;
}

// W2TimerEvent(): callback function for window 2
int W2TimerEvent() {
    Redisplay(W2, W2Bmp);
    return 1;
}

// ApiMain(): open the windows and start the timers
int ApiMain() {
	// open the windows
	W1.Open();
	assert(W1.GetStatus() == WindowOpen);
	W2.Open();
	assert(W2.GetStatus() == WindowOpen);
	
	// load the images
	W1Bmp.Load("c1.bmp");
	assert(W1Bmp.GetStatus() == BitMapOkay);
	W2Bmp.Load("c2.bmp");
	assert(W2Bmp.GetStatus() == BitMapOkay);

	// display the bitmaps at a starting position
	W1Bmp.SetPosition(Position(1.0, 1.0));
	W2Bmp.SetPosition(Position(1.0, 1.0));
	W1Bmp.Draw();
	W2Bmp.Draw();
	
	// register the callbacks for each window
	// and start the timers to go off every 500 ms
	W1.SetTimerCallback(W1TimerEvent);
	W2.SetTimerCallback(W2TimerEvent);
	
	bool TimerStatus1 = W1.StartTimer(500);
	bool TimerStatus2 = W2.StartTimer(500);
	
	assert(TimerStatus1 && TimerStatus2);
	
	return 0;
}

int ApiEnd() {
	// stop the timers and close the windows
	W1.StopTimer();
	W2.StopTimer();
	W1.Close();
	W2.Close();
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -