EasyX按键和下拉框手动实现,兼容性问题

本文展示了一个使用EasyX图形库在VS2022环境下创建五子棋游戏图形界面的示例,包括绘制按钮、下拉框以及处理鼠标事件来切换下拉框状态和开始游戏的功能。代码中遇到了类型不兼容的问题,通过调整项目属性中的字符集和符合模式得以解决。
摘要由CSDN通过智能技术生成

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" 类型的形参不兼容“。 

解决:项目 》属性 》高级 》字符集 》选择“使用多字节字符集”。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值