C++第02课 类与对象 (二)
2021/9/4 9:06:18
本文主要是介绍C++第02课 类与对象 (二),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.类与对象小案例(封装按钮)
#include <graphics.h> #include <iostream> #include <string> using namespace std; class Button { public: Button() = default; Button(int x, int y, int width, int height, COLORREF inColor, COLORREF outColor, string text, COLORREF textColor) : x(x), y(y), width(width), height(height), inColor(inColor), outColor(outColor), text(text), textColor(textColor),curColor(outColor) {}//构造时可以乱序 void draw() { setfillcolor(curColor); fillrectangle(x, y, x + width, y + height); setbkmode(TRANSPARENT); settextstyle(25, 0, "楷体"); settextcolor(textColor); int tW = textwidth(text.c_str()); int tH = textheight(text.data()); int tX = x + (width - tW) / 2; int tY = y + (height - tH) / 2; outtextxy(tX, tY, text.data()); } bool mouseInButton(MOUSEMSG msg) { if (msg.x > x && msg.x<x + width && msg.y>y && msg.y < y + height) { curColor = inColor; return true; } curColor = outColor; return false; } protected: int x; int y; int width; int height; COLORREF inColor; COLORREF outColor; COLORREF curColor; string text; COLORREF textColor; }; int main() { initgraph(800, 600); Button b = { 100,100,150,50,GREEN,WHITE,"BUTTON",BLACK }; Button c(400, 100, 150, 50, GREEN, WHITE, "BUTTON_O", BLACK); MOUSEMSG msg; BeginBatchDraw(); while (1) { b.draw(); c.draw(); msg = GetMouseMsg(); if (b.mouseInButton(msg)&&msg.uMsg==WM_LBUTTONDOWN) { break; } if (c.mouseInButton(msg) && msg.uMsg == WM_LBUTTONDOWN) { break; } FlushBatchDraw(); } EndBatchDraw(); closegraph(); return 0; }
2.图形库的输入
class Button { public: Button() = default; Button(int x, int y, int width, int height, COLORREF inColor, COLORREF outColor, string text, COLORREF textColor) : x(x), y(y), width(width), height(height), inColor(inColor), outColor(outColor), text(text), textColor(textColor), curColor(outColor) {}//构造时可以乱序 void draw() { setfillcolor(curColor); fillrectangle(x, y, x + width, y + height); setbkmode(TRANSPARENT); settextstyle(25, 0, "楷体"); settextcolor(textColor); int tW = textwidth(text.c_str()); int tH = textheight(text.data()); int tX = x + (width - tW) / 2; int tY = y + (height - tH) / 2; outtextxy(tX, tY, text.data()); } bool mouseInButton(MOUSEMSG msg) { if (msg.x > x && msg.x<x + width && msg.y>y && msg.y < y + height) { curColor = inColor; return true; } curColor = outColor; return false; } protected: int x; int y; int width; int height; COLORREF inColor; COLORREF outColor; COLORREF curColor; string text; COLORREF textColor; }; int main() { initgraph(800, 600); Button b = { 100,100,150,50,GREEN,WHITE,"INPUT",BLACK }; char name[20] = ""; MOUSEMSG msg; BeginBatchDraw(); while (1) { b.draw(); msg = GetMouseMsg(); if (b.mouseInButton(msg) && msg.uMsg == WM_LBUTTONDOWN) { InputBox(name, 20);//添加的代码,增加输入框 settextcolor(WHITE); outtextxy(0, 0, name); } FlushBatchDraw(); } EndBatchDraw(); closegraph(); return 0; }
这篇关于C++第02课 类与对象 (二)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-02在 Objective-C 中strong 和 retain有什么区别-icode9专业技术文章分享
- 2024-11-02NSString 中的 hasPrefix 有什么作用-icode9专业技术文章分享
- 2024-11-02在 C 和 Objective-C 中inline的用法是什么-icode9专业技术文章分享
- 2024-11-02文件掩码什么意思?-icode9专业技术文章分享
- 2024-11-02在 Git 提交之前运行 composer cs-fix 命令怎么实现-icode9专业技术文章分享
- 2024-11-02为 Composer 的 cs-fix 命令指定一个目录怎么实现-icode9专业技术文章分享
- 2024-11-02微信公众号开发中怎么获取用户的 unionid-icode9专业技术文章分享
- 2024-11-01lip-sync公司指南:一文读懂主要玩家和技术
- 2024-11-01Anthropic的新RAG方法——提升大型语言模型在特定领域的表现
- 2024-11-01UniApp 中组件的生命周期是多少-icode9专业技术文章分享