program_10_6.cpp

来自「清华关于C++ 的程序讲义 值得一看 关于算法」· C++ 代码 · 共 74 行

CPP
74
字号
// Example showing use of the mouse
// The program displays a bitmap in the window at
// the place where the mouse is clicked

#include <bitmap>
#include <cassert>
using namespace std;

// define two non-overlapping windows
SimpleWindow W1("Window One", 15., 12., Position(1., 1.));

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

// define two bitmpas, one for each window
BitMap W1Bmp(W1);
BitMap W2Bmp(W2);

// W1MouseClick(): callback function for window 1
int W1MouseClick(const Position &p) {
	// Erase the bitmap
	W1Bmp.Erase();

	// Set its new position and display it
	W1Bmp.SetPosition(p);
	W1Bmp.Draw();

	return 1;
}

// W2MouseClick(): callback function for window 2
int W2MouseClick(const Position &p) {
	// Erase the bitmap
	W2Bmp.Erase();

	// Set its new position and display it
	W2Bmp.SetPosition(p);
	W2Bmp.Draw();

	return 1;
}


int ApiMain() {
	// Open the windows
	W1.Open();
	assert(W1.GetStatus() == WindowOpen);
	W2.Open();
	assert(W2.GetStatus() == WindowOpen);

	// Load the images
	W1Bmp.Load("me.bmp");
	assert(W1Bmp.GetStatus() == BitMapOkay);
	W2Bmp.Load("bush.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
	W1.SetMouseClickCallback(W1MouseClick);
	W2.SetMouseClickCallback(W2MouseClick);

	return 0;
}

int ApiEnd() {
	W1.Close();
	W2.Close();
	return 0;
}

⌨️ 快捷键说明

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