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

📄 program_10_8.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 <cassert>
#include <bitmap>
#include <randint>
using namespace std;

// Define two nonoverlapping windows
SimpleWindow W1("Window One", 15., 9., Position(1., 1.));

SimpleWindow W2("Window Two", 15., 9., Position(8., 12));

// 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., 1.));
	W2Bmp.SetPosition(Position(1., 1.));
	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 + -