codeblock图形界面编程(七)基于ege库的交互设计
鼠标的交互
EGE库定义
和鼠标的交互主要就是获取鼠标的基本信息,包括当前位置,左键、中键和右键、滚轮的状态,在EGE库中,使用鼠标消息结构体来定义鼠标消息结构体定义如下
typedef struct mouse_msg {
int x;
int y;
mouse_msg_e msg;
unsigned int flags;
int wheel;
//成员函数
bool is_left() { return (flags & mouse_flag_left) != 0; }
bool is_right() { return (flags & mouse_flag_right) != 0; }
bool is_mid() { return (flags & mouse_flag_mid) != 0; }
bool is_down() { return msg == mouse_msg_down; }
bool is_up() { return msg == mouse_msg_up; }
bool is_move() { return msg == mouse_msg_move; }
bool is_wheel() { return msg == mouse_msg_wheel; }
}mouse_msg;
这几种鼠标消息,在EGE中并不区分开来发送,都是保存到一个鼠标消息结构体里,通过一些标志位来区分不同的消息,并且包含产生消息时鼠标的状态, 比如鼠标位置,鼠标左右键、中键有没有按下,滚轮是否滑动,向哪边滑动等,其中flags 是鼠标消息标志位,说明鼠标消息是左键、右键、中键(滚轮)哪个被按下,响应flag置位。
typedef enum mouse_flag_e {
mouse_flag_left = 1,
mouse_flag_right = 2,
mouse_flag_mid = 4,
mouse_flag_shift = 0x100,
mouse_flag_ctrl = 0x200,
}mouse_flag_e;
代码实现
获取鼠标信息的实现代码如下:
//鼠标消息处理:
int x,y,flag=0;;
mousepos(&x, &y); //获取鼠标消息当前坐标点
mouse_msg msg = {0};//声明鼠标消息的结构
for ( ; is_run(); delay_fps(60))
{
while (mousemsg()) 获取鼠标消息,这个函数会等待,等待到有消息为止
{
msg = getmouse();//获取鼠标消息信息
if(msg.is_down()&&msg.is_left())flag=1;//左键按下
if(msg.is_down()&&msg.is_right())flag=2;//右键按下
if(msg.is_down()&&msg.is_mid())flag=3;//中键
if(msg.is_up()&&msg.is_left())flag=11;//左键放开
if(msg.is_up()&&msg.is_right())flag=21;//右键放开
if(msg.is_up()&&msg.is_mid())flag=31;//中键放开
xyprintf(0, 0, "x = %10d y = %10d,wheel =%10d flag = %10d",msg.x, msg.y, msg.wheel,flag);//获取鼠标的x,y坐标和鼠标滚动值
}
}
运行结果
当鼠标左键按下时,显示如下: