本代码片断摘录自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