优秀代码赏析(c, 消息处理框架)

本代码片断摘录自FreeDos下的Edit软件,实现了类似Windows下的postmessage和sendmessage功能。
#define MAXMESSAGES 100

typedef long PARAM;

typedef enum messages {
	/* 自定义消息 */
	MSG1,
	MSG2,
	MSG3,
	MESSAGECOUNT
} MESSAGE;

/* ---------- message queue --------- */
static struct msgs {
    WINDOW wnd;
    MESSAGE msg;
    PARAM p1;
    PARAM p2;
} MsgQueue[MAXMESSAGES];

static int MsgQueueOnCtr;
static int MsgQueueOffCtr;
static int MsgQueueCtr;


void PostMessage(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
    if (MsgQueueCtr != MAXMESSAGES)    {
        MsgQueue[MsgQueueOnCtr].wnd = wnd;
        MsgQueue[MsgQueueOnCtr].msg = msg;
        MsgQueue[MsgQueueOnCtr].p1 = p1;
        MsgQueue[MsgQueueOnCtr].p2 = p2;
        if (++MsgQueueOnCtr == MAXMESSAGES)
            MsgQueueOnCtr = 0;
        MsgQueueCtr++;
    }
}

int SendMessage(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
    int rtn = TRUE, x, y;

    if (wnd != NULL)
        switch (msg)    {
            case MSG1:
		/* 根据消息做一些必要的处理 */
	        rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
                break;
            default:
                rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
                break;
        }
    /* ----- window processor returned true or the message was sent
        to no window at all (NULL) ----- */
    if (rtn != FALSE)    {
        switch (msg)    {
            case MSG1:
		/* 相关处理 */
                break;
            default:
                break;
        }
    }
    return rtn;
}

BOOL dispatch_message(void)
{
    WINDOW Mwnd, Kwnd;
#if 0 /* 通过条件编译删除该代码 */
/* 本段代码将鼠标、键盘操作转换为Event */
    /* -------- collect mouse and keyboard events ------- */
    collect_events();
/* 从Event队列中取出Event并将Event发往当前取得焦点的窗口 */
    /* --------- dequeue and process events -------- */
    while (EventQueueCtr > 0)  {
        struct events ev;
	ev = EventQueue[EventQueueOffCtr];
        if (++EventQueueOffCtr == MAXMESSAGES)
            EventQueueOffCtr = 0;
        --EventQueueCtr;

        /* ------ get the window in which a
                        keyboard event occurred ------ */
        Kwnd = inFocus; /* inFocus中记录当前取得焦点的窗口 */

        /* -------- send mouse and keyboard messages to the
            window that should get them -------- */
        switch (ev.event)    {
            case EVT1:
	        SendMessage(Kwnd, ev.event, ev.mx, ev.my);
                break;
            default:
                break;
        }
    }
#endif
    /* ------ dequeue and process messages ----- */
    while (MsgQueueCtr > 0)  {
        struct msgs mq;
	mq = MsgQueue[MsgQueueOffCtr];
        if (++MsgQueueOffCtr == MAXMESSAGES)
            MsgQueueOffCtr = 0;
        --MsgQueueCtr;
        SendMessage(mq.wnd, mq.msg, mq.p1, mq.p2);
    }
    return TRUE;
}


 by fervent @ 2013/9/11

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值