(EasyX入门与实战)第十章 按钮

您所在的位置:网站首页 按下按钮怎么写 (EasyX入门与实战)第十章 按钮

(EasyX入门与实战)第十章 按钮

#(EasyX入门与实战)第十章 按钮| 来源: 网络整理| 查看: 265

        熟悉EasyX的用户应该都知道EasyX虽然有丰富的画图函数,但是也有很多好用的组件是不具备的,比如按钮,这对于我们进行游戏开发来说是个非常重要的交互组件,这篇文章就用c++语法写一个简易的按钮组件方便大家用C++在开发游戏和学习中使用。

        

涉及C++知识点:函数、数组、结构体。

1、绘制按钮边框

#include #include struct Button { int x1; int x2; int y1; int y2; }; void setVertexAndShowButton(Button btn,int x1,int x2,int y1,int y2) { //设置顶点坐标 btn.x1 = x1; btn.x2 = x2; btn.y1 = y1; btn.y2 = y2; //绘制按钮边框 rectangle(btn.x1, btn.y1, btn.x2, btn.y2); } int main() { Button btns[3]; //初始化按钮结构体数组 initgraph(640, 480);//初始化界面 setVertexAndShowButton(btns[0], 280, 360, 220, 260); setVertexAndShowButton(btns[1], 280, 360, 270, 310); setVertexAndShowButton(btns[2], 280, 360, 320, 360); _getch();//等待用户输入 return 0; }

输出:

2、设置按钮文字

 

#include #include #include #include #include using namespace std; struct Button { int x1; int x2; int y1; int y2; }; void setVertexAndShowButton(Button btn,int x1,int x2,int y1,int y2, TCHAR text[1000],int tlength,int twidth,int tsetoff,string color) { //设置顶点坐标 btn.x1 = x1; btn.x2 = x2; btn.y1 = y1; btn.y2 = y2; //绘制按钮边框 rectangle(btn.x1, btn.y1, btn.x2, btn.y2); //设置按钮文字 setbkmode(TRANSPARENT);//设置透明字体 if(color=="red"|| color == "RED")settextcolor(RED);//设置字体颜色为红色 else if(color == "green"|| color == "GREEN")settextcolor(GREEN);//设置字体颜色为红色 else if(color == "blue"|| color == "BLUE")settextcolor(BLUE);//设置字体颜色为红色 else if(color == "yellow"|| color == "YELLOW")settextcolor(YELLOW);//设置字体颜色为黄色 else if (color == "black" || color == "BLACK")settextcolor(BLACK);//设置字体颜色为黑色 else if (color == "white" || color == "WHITE")settextcolor(WHITE);//设置字体颜色为白色 else { int x; x = MessageBox(GetForegroundWindow(), TEXT("请检查字体颜色是否设置正确,目前字体颜色包含:red、green、blue、yellow、black、white。"), TEXT("字体颜色设置错误"), 1); cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3