#define declare_message_map() /
private: /
static const afx_msgmap_entry _messageentries[]; /
protected: /
static afx_data const afx_msgmap messagemap; /
virtual const afx_msgmap* getmessagemap() const; /
#define begin_message_map(theclass, baseclass) /
const afx_msgmap* theclass::getmessagemap() const /
{ return &theclass::messagemap; } /
afx_comdat afx_datadef const afx_msgmap theclass::messagemap = /
{ &baseclass::messagemap, &theclass::_messageentries[0] }; /
afx_comdat const afx_msgmap_entry theclass::_messageentries[] = /
{ /
#define on_command(id, memberfxn) /
{ wm_command, 0, (word)id, (word)id, afxsig_vv, (afx_pmsg)memberfxn },
#define end_message_map() /
{0, 0, 0, 0, afxsig_end, (afx_pmsg)0 } /
}; /
#define declare_message_map() /
private: /
static const afx_msgmap_entry _messageentries[]; /
protected: /
static afx_data const afx_msgmap messagemap; /
virtual const afx_msgmap* getmessagemap() const; /
#define begin_message_map(theclass, baseclass) /
const afx_msgmap* theclass::getmessagemap() const /
{ return &theclass::messagemap; } /
afx_comdat afx_datadef const afx_msgmap theclass::messagemap = /
{ &baseclass::messagemap, &theclass::_messageentries[0] }; /
afx_comdat const afx_msgmap_entry theclass::_messageentries[] = /
{ /
#define on_command(id, memberfxn) /
{ wm_command, 0, (word)id, (word)id, afxsig_vv, (afx_pmsg)memberfxn },
#define end_message_map() /
{0, 0, 0, 0, afxsig_end, (afx_pmsg)0 } /
}; /
嘿嘿,就这么几个宏,就构造出一个消息数组来.
例四、用c宏,智者思维的火花说了半天了,嘴皮子都干了,举个例子大家轻松一下——看看人家老外是怎么用宏的。这个例子摘自《c专家编程》。 根据位模式构建图形图标(icon)或者图形(glyph),是一种小型的位模式映射于屏幕产生的图像。一个位代表图像上的一个像素。如果一个位被设置,那么它所代表的像素就是“亮”的。如果一个位被清除,那么它所代表的像素就是“暗”的。所以,一系列的整数值能够用于为图像编码。类似iconedit这样的工具就是用于绘图的,他们所输出的是一个包含一系列整型数的ascii文件,可以被一个窗口程序所包含。它所存在的问题是程序中的图标只是一串十六进制数。在c语言中,典型的16x16的黑白图形可能如下:
static unsigned short stopwatch[] = {
0x07c6,
0x1ff7,
0x383b,
0x600c,
0x600c,
0xc006,
0xc006,
0xdf06,
0xc106,
0xc106,
0x610c,
0x610c,
0x3838,
0x1ff0,
0x07c0,
0x0000
};
正如所看到的那样,这些c语言常量并未有提供有关图形实际模样的任何线索。这里有一个惊人的#define定义的优雅集合,允许程序建立常量使它们看上去像是屏幕上的图形。
#define x )*2+1
#define _ )*2
#define s ((((((((((((((((0 /* for building glyphs 16 bits wide */
定义了它们之后,只要画所需要的图标或者图形等,程序会自动创建它们的十六进制模式。使用这些宏定义,程序的自描述能力大大加强,上面这个例子可以转变为:
static unsigned short stopwatch[] =
{
s _ _ _ _ _ x x x x x _ _ _ x x _ ,
s _ _ _ x x x x x x x x x _ x x x ,
s _ _ x x x _ _ _ _ _ x x x _ x x ,
s _ x x _ _ _ _ _ _ _ _ _ x x _ _ ,
s _ x x _ _ _ _ _ _ _ _ _ x x _ _ ,
s x x _ _ _ _ _ _ _ _ _ _ _ x x _ ,
s x x _ _ _ _ _ _ _ _ _ _ _ x x _ ,
s x x _ x x x x x _ _ _ _ _ x x _ ,
s x x _ _ _ _ _ x _ _ _ _ _ x x _ ,
s x x _ _ _ _ _ x _ _ _ _ _ x x _ ,
s _ x x _ _ _ _ x _ _ _ _ x x _ _ ,
s _ x x _ _ _ _ x _ _ _ _ x x _ _ ,
s _ _ x x x _ _ _ _ _ x x x _ _ _ ,
s _ _ _ x x x x x x x x x _ _ _ _ ,
s _ _ _ _ _ x x x x x _ _ _ _ _ _ ,
s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
};
显然,与前面的代码相比,它的意思更为明显。标准的c语言具有八进制、十进制和十六进制常量,但没有二进制常量,否则的话倒是一种更为简单的绘制图形模式的方法。
如果抓住书的右上角,并斜这看这一页,可能会猜测这是一个用于流行窗口系统的“cursor busy”小秒表图形。我是在几年前从usenet comp.lang.c新闻组学到这个技巧的。
千万不要忘了在绘图结束后清除这些宏定义,否这很可能会给你后面的代码带来不可预测的后果。
好了,今天的废话就到这里了。水能载舟,亦能覆舟,把握好手中的双刃剑,让它好好的为你服务吧,别割破了手。