EasyX 是针对 C++ 的图形库,可查阅 EasyX 的文档和示例代码来了解更多用法和功能。EasyX 文档 - 基本说明
VS2022,以五子棋图形界面为例。
#include<iostream>
#include <graphics.h> // 引用图形库头文件
//下拉框项的高度
#define ITEM_HEIGHT 26
//下拉框是否展开的标志
bool isRoleDown = false;
//当前选中的下拉框项索引
int selectedRoleIndex = 0;
// 下拉框项的内容
const char* role[2] = { "电脑先手", "玩家先手" };
//绘图的参数
struct Param
{
int x, y, w, h;
};
Param board = {40,40,560,560}; //用于棋盘
Param start = { 620,50,150,50 }; //用于开局
Param roleParm = { 620, 120, 150, 30 }; //用于先手
//绘制按钮
void drawButton(Param param, TCHAR* text)
{
setfillcolor(LIGHTBLUE);
fillrectangle(param.x, param.y, param.x + param.w, param.y + param.h); // 画按钮
settextstyle(20, 0,_T("黑体"));
int tx = param.x + (param.w - textwidth(text)) / 2;
int ty = param.y + (param.h - textheight(text)) / 2;
outtextxy(tx, ty, text); //输出按钮内容
}
//绘制下拉框
void drawDropDownBox(Param param, const char** items,int count,bool isDown,int itemIndex)
{
setfillcolor(LIGHTBLUE);
fillrectangle(param.x, param.y, param.x + param.w, param.y + param.h); //画下拉框
settextstyle(18, 0, _T("黑体"));
if (isDown)
{
for (int i = 0; i < count; i++)
{
fillrectangle(param.x, param.y + param.h + i * ITEM_HEIGHT, param.x + param.w, param.y + param.h + (i + 1) * ITEM_HEIGHT); //画下拉框项
outtextxy(param.x + 40, param.y + param.h + i * ITEM_HEIGHT + 5, items[i]); //输出下拉框项的内容
}
}
else
{
if (itemIndex >= 0 && itemIndex < count)
{
outtextxy(param.x + 40, param.y + 5, items[itemIndex]); // 输出当前选中的下拉框项的内容
}
}
}
// 处理鼠标事件
void HandleMouseEvent()
{
MOUSEMSG mouse;
HWND hnd = GetHWnd();
while (1)
{
mouse = GetMouseMsg();
if (WM_LBUTTONUP == mouse.uMsg)
{
int x = mouse.x;
int y = mouse.y;
//指定先手
if (x >= roleParm.x && x <= roleParm.x + roleParm.w && y >= roleParm.y && y <= roleParm.y + roleParm.h) // 点击下拉框外框,切换展开/收起状态
{
isRoleDown = !isRoleDown;
clearrectangle(roleParm.x - 1, roleParm.y, roleParm.x + roleParm.w, roleParm.y + roleParm.h + ITEM_HEIGHT * 2);
drawDropDownBox(roleParm, role, 2, isRoleDown, selectedRoleIndex);
}
else if (isRoleDown && x >= roleParm.x && x <= roleParm.x + roleParm.w && y >= roleParm.y + roleParm.h && y <= roleParm.y + roleParm.h + ITEM_HEIGHT * 2)// 点击下拉框展开后的选项
{
selectedRoleIndex = (y - roleParm.y - roleParm.h) / ITEM_HEIGHT;
isRoleDown = false;
clearrectangle(roleParm.x - 1, roleParm.y, roleParm.x + roleParm.w, roleParm.y + roleParm.h + ITEM_HEIGHT * 2);
drawDropDownBox(roleParm, role, 2, isRoleDown, selectedRoleIndex);
}
//开局
if (x >= start.x && x <= start.x + start.w && y >=start.y && y <=start.y + start.h)
{
MessageBox(hnd, "已开局", "提示", MB_YESNO);
break;
}
}
}
}
int main()
{
initgraph(800, 750); // 创建绘图窗口,大小为800x750像素
HWND hnd = GetHWnd();
SetWindowText(hnd, "五子棋");//设置窗口标题
setbkmode(TRANSPARENT); //设置图案填充和文字输出时的背景透明的
setbkcolor(BLUE); //设置背景颜色
cleardevice(); //使用当前背景色清空绘图设备
fillrectangle(board.x, board.y, board.x + board.w, board.y + board.h);//画棋盘
setlinecolor(BLACK);
setlinestyle(0, 2);
for (int i = 0; i < 15; i++)
{
line(board.x + board.x * i, board.y, board.x + board.x * i, board.y + board.h);
line(board.x, board.y + board.y * i, board.x + board.w, board.y + board.y * i);
}
for (int i = 0; i < 15; i++)//画棋盘标号
{
RECT rbuttom = { board.x + board.x * i,board.y+ board.h,40+board.x + board.x * i,40+board.y + board.h };
TCHAR s[5];
_stprintf_s(s, _T("%d"), i + 1);
drawtext(s, &rbuttom, DT_SINGLELINE);//用于在指定区域内以指定格式输出字符串
RECT rleft = {20 ,board.y + board.y * i,board.x ,40+board.y + board.y * i };
_stprintf_s(s, _T("%d"), 15-i);
drawtext(s, &rleft, DT_LEFT);
}
drawButton(start, _T("开局"));//画按钮
drawDropDownBox(roleParm,role,2,isRoleDown,selectedRoleIndex);//画下拉框
HandleMouseEvent() // 处理鼠标事件
system("pause");
closegraph(); // 关闭绘图窗口
return 0;
}
结果显示
👎 error: "const char *" 类型的实参与 "TCHAR *" 类型的形参不兼容”。
解决:项目 》属性 》C/C++ 》语言 》符合模式 》选则”否“。
👎 error: “没有与参数列表匹配的重载函数 "outtextxy" 实例 ” 同时报错 "const char *" 类型的实参与 "LPCWSTR" 类型的形参不兼容“。
解决:项目 》属性 》高级 》字符集 》选择“使用多字节字符集”。