打破牢笼,跳出控制台

"基于EasyX的C++窗体类"

Posted by QnGhonu on August 8, 2024

C++固然很好,但是只能在cmd的黑框框里运行,大大限制了我们这种“天才”程序员的发挥。 所以我找到了一种可以打破这个框框的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <graphics.h>
#include <stdio.h>
#include <string>

class window {
	private:
		typedef unsigned uint;
		typedef long long lint;
		typedef unsigned long long ulint;
		typedef const int cit;
		uint control;
		uint width, height;
		uint textSize;
		uint textx, texty;
		IMAGE img;
		bool couldFlushBoard;
	public:
		window(int userset = 1) {
			control = userset;
			width = 400 * control;
			height = 300 * control;
			textSize = 20 * control;
			textx = 50 * control;
			texty = 50 * control;
			couldFlushBoard = false;
		}
		struct location {
			uint *_x, *_y;
		};
		void show() {
			initgraph(width, height);
			clearDevice();
		}
		void close() {
			closegraph();
		}
		void loadImage(char* imagePath) {
			loadimage(&img, imagePath);
			if (img.getheight() != 0) couldFlushBoard = true;
		}
	protected:
		uint getControlValue() {
			return control;
		}
		uint getWidth() {
			return width;
		}
		uint getHeight() {
			return height;
		}
		location getTextLocation() {
			location toReturn;
			toReturn._x = &textx;
			toReturn._y = &texty;
			return toReturn;
		}
		void clearDevice() {
			setbkcolor(RGB(255, 255, 255));
			settextcolor(RGB(0, 0, 255));
			settextstyle(textSize, 0, _T("微软雅黑"));
			cleardevice();
			if (couldFlushBoard) putimage(0, 0, &img);
		}
};

class printer : window {
	public:
		void putwords(std::string words) {
			for (unsigned index=0; index<words.size(); index++) {
				char& word = words[index];
				if (word == '\n') {
					*getTextLocation()._y += getControlValue() * 50;
					return;
				}
				outtextxy(*getTextLocation()._x, *getTextLocation()._y, word);
				*getTextLocation()._x += getControlValue() * 20;
			}
		}
};
//MOUSEMSG msg = GetMouseMsg();
//x = (msg.x - 50 + 15) / 30;
//y = (msg.y - 50 + 15) / 30;
int main() {
	window Qmain(2);
	printer qout;
	Qmain.show();

	qout.putwords("Hello,\nNice to meet you!\n");

	getchar();
	Qmain.close();
	return 0;
}

我写了一点小小的示例代码,大家可以尝试运行一下。

PS: EasyX组件的下载与安装请访问 This.