codeblock图形界面编程(七)基于ege库的交互设计1

19 篇文章 0 订阅
10 篇文章 2 订阅

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坐标和鼠标滚动值
        }
    }

运行结果

当鼠标左键按下时,显示如下:

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的银行家算法C语言实现的交互界面,使用CodeBlocks编写: ```c #include <stdio.h> #define MAX_RESOURCES 10 int available[MAX_RESOURCES]; int allocation[MAX_RESOURCES][MAX_RESOURCES]; int need[MAX_RESOURCES][MAX_RESOURCES]; int processes_count; void init() { printf("Enter the number of processes: "); scanf("%d", &processes_count); printf("Enter the number of available resources: "); int resources_count; scanf("%d", &resources_count); printf("Enter the available resources:\n"); for (int i = 0; i < resources_count; i++) { scanf("%d", &available[i]); } printf("Enter the allocation matrix:\n"); for (int i = 0; i < processes_count; i++) { for (int j = 0; j < resources_count; j++) { scanf("%d", &allocation[i][j]); need[i][j] = allocation[i][j]; available[j] -= allocation[i][j]; } } printf("Initialization complete.\n"); } int request_resources(int process_id, int request[]) { for (int i = 0; i < MAX_RESOURCES; i++) { if (request[i] > need[process_id][i]) { printf("Request exceeds need.\n"); return 0; } if (request[i] > available[i]) { printf("Request exceeds available resources.\n"); return 0; } } for (int i = 0; i < MAX_RESOURCES; i++) { available[i] -= request[i]; allocation[process_id][i] += request[i]; need[process_id][i] -= request[i]; } printf("Request granted.\n"); return 1; } int release_resources(int process_id, int release[]) { for (int i = 0; i < MAX_RESOURCES; i++) { if (release[i] > allocation[process_id][i]) { printf("Release exceeds allocation.\n"); return 0; } } for (int i = 0; i < MAX_RESOURCES; i++) { available[i] += release[i]; allocation[process_id][i] -= release[i]; need[process_id][i] += release[i]; } printf("Resources released.\n"); return 1; } void print_state() { printf("Available resources:\n"); for (int i = 0; i < MAX_RESOURCES; i++) { printf("%d ", available[i]); } printf("\n"); printf("Allocation matrix:\n"); for (int i = 0; i < processes_count; i++) { for (int j = 0; j < MAX_RESOURCES; j++) { printf("%d ", allocation[i][j]); } printf("\n"); } printf("Need matrix:\n"); for (int i = 0; i < processes_count; i++) { for (int j = 0; j < MAX_RESOURCES; j++) { printf("%d ", need[i][j]); } printf("\n"); } } int main() { init(); while (1) { printf("\nEnter command (request, release, state, quit): "); char command[10]; scanf("%s", command); if (strcmp(command, "request") == 0) { int process_id; printf("Enter process id: "); scanf("%d", &process_id); int request[MAX_RESOURCES]; printf("Enter request: "); for (int i = 0; i < MAX_RESOURCES; i++) { scanf("%d", &request[i]); } request_resources(process_id, request); } else if (strcmp(command, "release") == 0) { int process_id; printf("Enter process id: "); scanf("%d", &process_id); int release[MAX_RESOURCES]; printf("Enter release: "); for (int i = 0; i < MAX_RESOURCES; i++) { scanf("%d", &release[i]); } release_resources(process_id, release); } else if (strcmp(command, "state") == 0) { print_state(); } else if (strcmp(command, "quit") == 0) { break; } else { printf("Invalid command.\n"); } } return 0; } ``` 该程序实现了银行家算法的初始化、请求资源、释放资源和打印当前状态等功能。用户可以通过命令行输入不同的命令与程序进行交互
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cyjbj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值