`
hulunberbus
  • 浏览: 857376 次
文章分类
社区版块
存档分类
最新评论

《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之14---BREW精要之事件驱动模型

 
阅读更多

BREW应用程序的模型是基于一个事件驱动的协作式多任务模型。事件处理机制的核心问题是程序应该只处理需要的事件,对于不需要处理的事件,需要返回给系统处理。应用在加载之后可以通过 HandleEvent()函数接收所有输入的事件,然后会通过返回TRUE(已处理)或FALSE(未处理)指示是否处理事件。AEE层存在一个全局的事件队列,所有的事件都存储在该队列中,如果队列中的事件在分发后处理完毕或者无人处理,该事件将被从事件队列中删除。

BREW中的事件主要有三类:系统事件,预定义的事件和。系统事件的事件代码范围为0~7,即从EVT_APP_START EVT_APP_BROWSE_FILE预定义事件的事件代码范围是0x0008-0x4fff,即BREW AEE 层和OEM层预定义或者预留的事件。自定义的事件是指 BREW应用可以自定义自己的事件,自定义事件应该不小于EVT_USER0x5000)。BREW应用可以发送任何事件给应用自身,或者发给在同一进程中的其他应用。如果发送事件给不同进程的应用(主要是针对BREW 4.X及以上版本),这时需要特殊的应用权限。

BREW环境要求及时地处理事件。简而言之,如果应用执行处理事件HandleEvent()调用后没能在合适的时间返回,AEE可能就会关闭应用以保护设备相应其他请求。有些操作,比如说从网络套接字中读取数据,也许耗时过长,无法在一次调用事件处理器内完成。这时就采用回调机制,以便在操作完成之后通知该应用。

4.3.2.1事件处理器

AEE执行环境调用BREW 应用自身的事件处理器来传递关于一系列事件的消息。有过windows编程经历的读者都会清楚这种机制,Windows下消息处理机制:当在交互中进行一个操作(信号,输入,等等),windows将产生相应的事件,通过window的事件分发机制,相应的窗口或者应用得到该事件,从而触发相应的事件处理器进行处理。BREW中事件处理机制与其相似,即BREW环境捕捉到事件后,分发到相应的应用或者控件,由应用或者控件的事件处理器进行处理。

以下是BREW中事件处理器接口的示例:

boolean MyApp_HandleEvent(IApplet * pIApp,

AEEEvent eCode,

uint16 wParam,

uint32 wParam)

在该示例中,变量pIApp实际上指明了应用的结构,也就是AEEApplet的一个指针。许多应用将其结构定义为AEEApplet的超集,而pIApp也能指向该结构。

eCode变量是说明应用接收的事件类型,如EVT_APP_STARTEVT_KEYEVT_ALARM等典型事件。

wParamdwParam参数是依据接收的事件而定义的短数据和长数据值。这些值取决于事件本身,根据事件自身来定义。对于某些事件,短数据和长数据字段中都包含事件数据;而对于另一些事件,长短字段中仅有一个字段,甚至没有字段。两个数据字段均不包含数据的事件有EVT_APP_STARTEVT_APP_STOPEVT_APP_SUSPENDEVT_APP_RESUME。两个数据字段中都包含数据的典型事件有EVT_DIALOG_STARTEVT_COMMAND。仅在短数据字段中包含数据的典型事件有EVT_ALARM,仅在长数据字段中包含数据的典型事件有EVT_NET_STATUSEVT_CTL_CHANGING

按键事件作为EVT_KEY 事件发送给应用。短数据字段包含主键代码;比如说如果用户按下按键符合“2”,就包含AVK_2这一主键代码。AVK_2的值由AEEVCodes.h头文件定义。

Emulator中,与按键符号相对应的主键代码由设备配置文件确定,也可经由设备配置器进行修改。在手机上,主键代码由设备厂商决定。

4.3.2.2事件处理的提示

执行应用时,仅考虑处理应用可能需要处理的事件。许多事件可以被忽略。举例来说,如果执行一个游戏应用时,仅需使用上下左右箭头键,则可忽略接收到的0-9按键事件。

但是如果接收到关键事件,则无论应用处于何种状态都不能忽略。如EVT_START EVT_STOPEVT_SUSPENDEVT_RESUME等系统事件就是在任何情况下都会影响应用的例子,所以不能忽略。需要特别注意的是,无论应用给定状态如何,必须接收所有的关键事件。某些事件在应用特别指示需要此类通知时才会发送。应用必须为这些通知事件注册,可以在MIF编辑器中指定MIF文件的通知事件注册,或者使用ISHELL_RegisterNotify()进行动态注册。

作为一个约定,应用在处理EVT_START分配的任何数据,在处理EVT_STOP时都应该释放出去。但是,在AEEClsCreateInstance()中分配的内存数据,一般必须通过FreeAppData()机制来释放。

4.3.2.3事件分发与代理

当控件激活时,事件应该传递到激活的控件,使控件进行自我更新,又叫做事件代理。例如,如果菜单控件处于激活状态,应该将事件传递到IMENUCTL_HandleEvent();如果文本控件处于激活上,应传递到ITEXTCTL_HandleEvent(),然后控件就会采取相应的操作。以菜单控件来说,它就会改变被选项目,重新绘制菜单。

正如事件处理函数将TRUEFALSE返回AEE执行环境那样,控件返回TRUEFALSE则表示它们处理的事件。每个控件类型只会处理必要的事件。标准菜单控件只处理“上”、“下”和其他部分关键事件,而软键菜单控件则处理“左“、“右”和其他关键事件。如果一个控件从发放的事件中返回的是TRUE,应用就可以早一点从事件处理函数中退出来,但还可以执行额外的处理。如果一个控件从发放的事件中返回的是FALSE,应用一般应该继续处理该事件。如果该控件接收的是从事件处理器返回的FALSEBREW就要执行默认的处理。

当用户按下“选择”键进行选择时,菜单控件使用EVT_COMMAND来通知应用。在这种情况下,一个来自“选择”键下放的EVT_KEY按键事件就由菜单控件处理。此外,对于菜单控件任何视图更新,菜单控件将EVT_COMMAND事件发回给应用。事实上,所有控件类型都能提供代理机制

<!--[if mso & !supportInlineShapes & supportFields]><span style='font-family:"楷体","serif";mso-hansi-font-family:Arial;color:black; layout-grid-mode:line'><span style="mso-element:field-begin;mso-field-lock: yes" mce_style="mso-element:field-begin;mso-field-lock: yes"></span><span style="mso-spacerun:yes" mce_style="mso-spacerun:yes">&nbsp;</span>SHAPE<span style='mso-spacerun:yes'>&nbsp; </span>/* MERGEFORMAT <span style="mso-element:field-separator" mce_style="mso-element:field-separator"></span></span><![endif]--><!--[if gte vml 1]><v:group id="_x0000_s1037" editas="canvas" style='width:243pt;height:145.85pt;mso-position-horizontal-relative:char; mso-position-vertical-relative:line' coordorigin="2362,1233" coordsize="8480,5098"> <o:lock v:ext="edit" aspectratio="t" /> <v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0" /> <v:f eqn="sum @0 1 0" /> <v:f eqn="sum 0 0 @1" /> <v:f eqn="prod @2 1 2" /> <v:f eqn="prod @3 21600 pixelWidth" /> <v:f eqn="prod @3 21600 pixelHeight" /> <v:f eqn="sum @0 0 1" /> <v:f eqn="prod @6 1 2" /> <v:f eqn="prod @7 21600 pixelWidth" /> <v:f eqn="sum @8 21600 0" /> <v:f eqn="prod @7 21600 pixelHeight" /> <v:f eqn="sum @10 21600 0" /> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /> <o:lock v:ext="edit" aspectratio="t" /> </v:shapetype><v:shape id="_x0000_s1038" type="#_x0000_t75" style='position:absolute; left:2362;top:1233;width:8480;height:5098' o:preferrelative="f"> <v:fill o:detectmouseclick="t" /> <v:path o:extrusionok="t" o:connecttype="none" /> <o:lock v:ext="edit" text="t" /> </v:shape><v:group id="_x0000_s1039" style='position:absolute;left:3642;top:1233; width:2465;height:888' coordorigin="521,1026" coordsize="1134,408"> <v:rect id="_x0000_s1040" style='position:absolute;left:521;top:1026;width:1044; height:408;v-text-anchor:middle' fillcolor="#0c9" /> <v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe"> <v:stroke joinstyle="miter" /> <v:path gradientshapeok="t" o:connecttype="rect" /> </v:shapetype><v:shape id="_x0000_s1041" type="#_x0000_t202" style='position:absolute; left:521;top:1071;width:1134;height:288' filled="f" fillcolor="#0c9" stroked="f"> <v:textbox inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style="mso-layout-grid-align:none;text-autospace:none" mce_style="mso-layout-grid-align:none;text-autospace:none"><span style='font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family:"Times","serif"; color:black'>AEE<span style="mso-spacerun:yes" mce_style="mso-spacerun:yes">&nbsp;&nbsp; </span>Shell<o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape></v:group><v:group id="_x0000_s1042" style='position:absolute; left:5910;top:3110;width:2269;height:888' coordorigin="1655,1888" coordsize="1044,408"> <v:rect id="_x0000_s1043" style='position:absolute;left:1655;top:1888; width:1044;height:408;v-text-anchor:middle' fillcolor="#0c9" /> <v:shape id="_x0000_s1044" type="#_x0000_t202" style='position:absolute; left:1808;top:1933;width:862;height:288' filled="f" fillcolor="#0c9" stroked="f"> <v:textbox inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style="mso-layout-grid-align:none;text-autospace:none" mce_style="mso-layout-grid-align:none;text-autospace:none"><span style='font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family:"Times","serif"; color:black'>My App<o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape></v:group><v:group id="_x0000_s1045" style='position:absolute; left:8572;top:4887;width:2270;height:888' coordorigin="3016,2840" coordsize="1044,408"> <v:rect id="_x0000_s1046" style='position:absolute;left:3016;top:2840; width:1044;height:408;v-text-anchor:middle' fillcolor="#0c9" /> <v:shape id="_x0000_s1047" type="#_x0000_t202" style='position:absolute; left:3107;top:2885;width:907;height:288' filled="f" fillcolor="#0c9" stroked="f"> <v:textbox inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style="mso-layout-grid-align:none;text-autospace:none" mce_style="mso-layout-grid-align:none;text-autospace:none"><span style='font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family:"Times","serif"; color:black'>IMenuCtl</span><span style="font-size:8.5pt;mso-bidi-font-size: 10.5pt;font-family:"Times","serif";mso-bidi-font-family:宋体;color:black" mce_style="font-size:8.5pt;mso-bidi-font-size: 10.5pt;font-family:"Times","serif";mso-bidi-font-family:宋体;color:black"><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape></v:group><v:shapetype id="_x0000_t102" coordsize="21600,21600" o:spt="102" adj="12960,19440,14400" path="ar,0@23@3@22,,0@4,0@15@23@1,0@7@2@13l@2@14@22@8@2@12wa,0@23@3@2@11@26@17,0@15@23@1@26@17@22@15xear,0@23@3,0@4@26@17nfe"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="val #0" /> <v:f eqn="val #1" /> <v:f eqn="val #2" /> <v:f eqn="sum #0 width #1" /> <v:f eqn="prod @3 1 2" /> <v:f eqn="sum #1 #1 width" /> <v:f eqn="sum @5 #1 #0" /> <v:f eqn="prod @6 1 2" /> <v:f eqn="mid width #0" /> <v:f eqn="sum height 0 #2" /> <v:f eqn="ellipse @9 height @4" /> <v:f eqn="sum @4 @10 0" /> <v:f eqn="sum @11 #1 width" /> <v:f eqn="sum @7 @10 0" /> <v:f eqn="sum @12 width #0" /> <v:f eqn="sum @5 0 #0" /> <v:f eqn="prod @15 1 2" /> <v:f eqn="mid @4 @7" /> <v:f eqn="sum #0 #1 width" /> <v:f eqn="prod @18 1 2" /> <v:f eqn="sum @17 0 @19" /> <v:f eqn="val width" /> <v:f eqn="val height" /> <v:f eqn="prod height 2 1" /> <v:f eqn="sum @17 0 @4" /> <v:f eqn="ellipse @24 @4 height" /> <v:f eqn="sum height 0 @25" /> <v:f eqn="sum @8 128 0" /> <v:f eqn="prod @5 1 2" /> <v:f eqn="sum @5 0 128" /> <v:f eqn="sum #0 @17 @12" /> <v:f eqn="ellipse @20 @4 height" /> <v:f eqn="sum width 0 #0" /> <v:f eqn="prod @32 1 2" /> <v:f eqn="prod height height 1" /> <v:f eqn="prod @9 @9 1" /> <v:f eqn="sum @34 0 @35" /> <v:f eqn="sqrt @36" /> <v:f eqn="sum @37 height 0" /> <v:f eqn="prod width height @38" /> <v:f eqn="sum @39 64 0" /> <v:f eqn="prod #0 1 2" /> <v:f eqn="ellipse @33 @41 height" /> <v:f eqn="sum height 0 @42" /> <v:f eqn="sum @43 64 0" /> <v:f eqn="prod @4 1 2" /> <v:f eqn="sum #1 0 @45" /> <v:f eqn="prod height 4390 32768" /> <v:f eqn="prod height 28378 32768" /> </v:formulas> <v:path o:extrusionok="f" o:connecttype="custom" o:connectlocs="0,@17;@2,@14;@22,@8;@2,@12;@22,@16" o:connectangles="180,90,0,0,0" textboxrect="@47,@45,@48,@46" /> <v:handles> <v:h position="bottomRight,#0" yrange="@40,@29" /> <v:h position="bottomRight,#1" yrange="@27,@21" /> <v:h position="#2,bottomRight" xrange="@44,@22" /> </v:handles> <o:complex v:ext="view" /> </v:shapetype><v:shape id="_x0000_s1048" type="#_x0000_t102" style='position:absolute; left:4830;top:1991;width:520;height:2430;rotation:-2698153fd;v-text-anchor:middle' fillcolor="#f30"> <v:textbox inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span lang=ZH-CN style="font-size:8.5pt; mso-bidi-font-size:10.5pt;font-family:"Times","serif";mso-bidi-font-family: 宋体;color:black;mso-ansi-language:ZH-CN" mce_style="font-size:8.5pt; mso-bidi-font-size:10.5pt;font-family:"Times","serif";mso-bidi-font-family: 宋体;color:black;mso-ansi-language:ZH-CN"><o:p>&nbsp;</o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1049" type="#_x0000_t102" style='position:absolute; left:7503;top:3902;width:521;height:2429;rotation:-2698153fd;v-text-anchor:middle' fillcolor="#f30"> <v:textbox inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span lang=ZH-CN style="font-size:8.5pt; mso-bidi-font-size:10.5pt;font-family:"Times","serif";mso-bidi-font-family: 宋体;color:black;mso-ansi-language:ZH-CN" mce_style="font-size:8.5pt; mso-bidi-font-size:10.5pt;font-family:"Times","serif";mso-bidi-font-family: 宋体;color:black;mso-ansi-language:ZH-CN"><o:p>&nbsp;</o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1050" type="#_x0000_t102" style='position:absolute; left:8540;top:3569;width:583;height:479;rotation:8178635fd;v-text-anchor:middle' fillcolor="#c90"> <v:textbox style="mso-rotate:270" mce_style="mso-rotate:270" inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span style="font-size:8.5pt;mso-bidi-font-size: 10.5pt;font-family:"Times","serif";mso-bidi-font-family:宋体;color:black" mce_style="font-size:8.5pt;mso-bidi-font-size: 10.5pt;font-family:"Times","serif";mso-bidi-font-family:宋体;color:black"><o:p>&nbsp;</o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1051" type="#_x0000_t102" style='position:absolute; left:6195;top:1878;width:585;height:479;rotation:8178635fd;v-text-anchor:middle' fillcolor="#c90"> <v:textbox style="mso-rotate:270" mce_style="mso-rotate:270" inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span style="font-size:8.5pt;mso-bidi-font-size: 10.5pt;font-family:"Times","serif";mso-bidi-font-family:宋体;color:black" mce_style="font-size:8.5pt;mso-bidi-font-size: 10.5pt;font-family:"Times","serif";mso-bidi-font-family:宋体;color:black"><o:p>&nbsp;</o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1052" type="#_x0000_t202" style='position:absolute; left:2362;top:2911;width:2563;height:770' filled="f" fillcolor="#0c9" stroked="f"> <v:textbox inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style="mso-layout-grid-align:none;text-autospace:none" mce_style="mso-layout-grid-align:none;text-autospace:none"><span lang=ZH-CN style='font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family: 宋体;mso-ascii-font-family:Times;mso-hansi-font-family:Times;mso-bidi-font-family: 宋体;color:black;mso-ansi-language:ZH-CN'>所有的事件</span><span lang=ZH-CN style="font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family:"Times","serif"; mso-bidi-font-family:宋体;color:black;mso-ansi-language:ZH-CN" mce_style="font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family:"Times","serif"; mso-bidi-font-family:宋体;color:black;mso-ansi-language:ZH-CN"><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1053" type="#_x0000_t202" style='position:absolute; left:5419;top:4691;width:2071;height:770' filled="f" fillcolor="#0c9" stroked="f"> <v:textbox inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style="mso-layout-grid-align:none;text-autospace:none" mce_style="mso-layout-grid-align:none;text-autospace:none"><span lang=ZH-CN style='font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family: 宋体;mso-ascii-font-family:Times;mso-hansi-font-family:Times;mso-bidi-font-family: 宋体;color:black;mso-ansi-language:ZH-CN'>按键事件</span><span lang=ZH-CN style="font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family:"Times","serif"; mso-bidi-font-family:宋体;color:black;mso-ansi-language:ZH-CN" mce_style="font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family:"Times","serif"; mso-bidi-font-family:宋体;color:black;mso-ansi-language:ZH-CN"><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1054" type="#_x0000_t102" style='position:absolute; left:8485;top:3985;width:296;height:479;rotation:8178635fd;v-text-anchor:middle' fillcolor="#c90"> <v:textbox style="mso-rotate:270" mce_style="mso-rotate:270" inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span style="font-size:8.5pt;mso-bidi-font-size: 10.5pt;font-family:"Times","serif";mso-bidi-font-family:宋体;color:black" mce_style="font-size:8.5pt;mso-bidi-font-size: 10.5pt;font-family:"Times","serif";mso-bidi-font-family:宋体;color:black"><o:p>&nbsp;</o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1055" type="#_x0000_t202" style='position:absolute; left:7984;top:3859;width:1380;height:1416' filled="f" fillcolor="#0c9" stroked="f"> <v:textbox inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style="mso-layout-grid-align:none;text-autospace:none" mce_style="mso-layout-grid-align:none;text-autospace:none"><span style='font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family:"Times","serif"; color:black'>Ture /False<o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><v:shape id="_x0000_s1056" type="#_x0000_t202" style='position:absolute; left:7194;top:1494;width:2465;height:770' filled="f" fillcolor="#0c9" stroked="f"> <v:textbox inset="1.25975mm,.62986mm,1.25975mm,.62986mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style="mso-layout-grid-align:none;text-autospace:none" mce_style="mso-layout-grid-align:none;text-autospace:none"><span style='font-size:8.5pt;mso-bidi-font-size:10.5pt;font-family:"Times","serif"; color:black'>Ture /False<o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:shape><w:wrap type="none" /> <w:anchorlock /> </v:group><![endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='font-family:"楷体","serif";mso-hansi-font-family:Arial;color:black; layout-grid-mode:line'><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:243pt; height:145.85pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style="mso-element:field-end" mce_style="mso-element:field-end"></span></span><![endif]-->

<!--[if supportFields]><span style="mso-element:field-begin" mce_style="mso-element:field-begin"></span><span style='mso-spacerun:yes'>&nbsp;</span>TOC /h /z /c &quot;<span lang=ZH-CN style='font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family: "Times New Roman"'>图</span>&quot; <span style="mso-element:field-separator" mce_style="mso-element:field-separator"></span><![endif]--> 4-7 BREW中的事件分发示例 <!--[if supportFields]><span style='mso-element:field-end'></span><![endif]-->

事件的分发代理机制非常灵活,你可以在消息循环中按照你的需要自由处理(图。

<!--[if mso & !supportInlineShapes & supportFields]><span style='font-family:"楷体","serif";mso-hansi-font-family:Arial;color:black; layout-grid-mode:line'><span style="mso-element:field-begin;mso-field-lock: yes" mce_style="mso-element:field-begin;mso-field-lock: yes"></span><span style="mso-spacerun:yes" mce_style="mso-spacerun:yes">&nbsp;</span>SHAPE<span style='mso-spacerun:yes'>&nbsp; </span>/* MERGEFORMAT <span style="mso-element:field-separator" mce_style="mso-element:field-separator"></span></span><![endif]--><!--[if gte vml 1]><v:group id="_x0000_s1026" editas="canvas" style='width:351pt;height:190pt;mso-position-horizontal-relative:char; mso-position-vertical-relative:line' coordorigin="2362,12033" coordsize="11374,6166"> <o:lock v:ext="edit" aspectratio="t" /> <v:shape id="_x0000_s1027" type="#_x0000_t75" style='position:absolute;left:2362; top:12033;width:11374;height:6166' o:preferrelative="f"> <v:fill o:detectmouseclick="t" /> <v:path o:extrusionok="t" o:connecttype="none" /> <o:lock v:ext="edit" text="t" /> </v:shape><v:rect id="_x0000_s1028" style='position:absolute;left:2362;top:12033; width:3122;height:1045;v-text-anchor:middle' fillcolor="#069"> <v:textbox inset="1.3809mm,.69044mm,1.3809mm,.69044mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span lang=ZH-CN style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN" mce_style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN">用户按下某一个键<o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:rect><v:rect id="_x0000_s1029" style='position:absolute;left:4135;top:13286; width:4191;height:1046;v-text-anchor:middle' fillcolor="#069"> <v:textbox inset="1.3809mm,.69044mm,1.3809mm,.69044mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span lang=ZH-CN style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN" mce_style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN">与该键相关的事件被传送<o:p></o:p></span></p> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span lang=ZH-CN style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN" mce_style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN">给您的事件处理函数<o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:rect><v:rect id="_x0000_s1030" style='position:absolute;left:7787;top:15796; width:4570;height:1043;v-text-anchor:middle' fillcolor="#069"> <v:textbox inset="1.3809mm,.69044mm,1.3809mm,.69044mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span lang=ZH-CN style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN" mce_style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN">可以将它交给<o:p></o:p></span></p> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:Times;color:white" mce_style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:Times;color:white">IMENUCTL_HandleEvent()<o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:rect><v:rect id="_x0000_s1031" style='position:absolute;left:6014;top:14541; width:3478;height:1045;v-text-anchor:middle' fillcolor="#069"> <v:textbox inset="1.3809mm,.69044mm,1.3809mm,.69044mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span lang=ZH-CN style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN" mce_style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN">可以选择先处理它<o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:rect><v:rect id="_x0000_s1032" style='position:absolute;left:9666;top:17154; width:3478;height:1045;v-text-anchor:middle' fillcolor="#069"> <v:textbox inset="1.3809mm,.69044mm,1.3809mm,.69044mm"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=center style="text-align:center;mso-layout-grid-align: none;text-autospace:none" mce_style="text-align:center;mso-layout-grid-align: none;text-autospace:none"><span lang=ZH-CN style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN" mce_style="mso-bidi-font-size:9.0pt; font-family:宋体;mso-bidi-font-family:宋体;color:white;mso-ansi-language:ZH-CN">也可以再次处理它<o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> </v:rect><v:shapetype id="_x0000_t13" coordsize="21600,21600" o:spt="13" adj="16200,5400" path="m@0,l@0@1,0@1,0@2@0@2@0,21600,21600,10800xe"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="val #0" /> <v:f eqn="val #1" /> <v:f eqn="sum height 0 #1" /> <v:f eqn="sum 10800 0 #1" /> <v:f eqn="sum width 0 #0" /> <v:f eqn="prod @4 @3 10800" /> <v:f eqn="sum width 0 @5" /> </v:formulas> <v:path o:connecttype="custom" o:connectlocs="@0,0;0,10800;@0,21600;21600,10800" o:connectangles="270,180,90,0" textboxrect="0,@1,@6,@2" /> <v:handles> <v:h position="#0,#1" xrange="0,21600" yrange="0,10800" /> </v:handles> </v:shapetype><v:shape id="_x0000_s1033" type="#_x0000_t13" style='position:absolute; left:5992;top:13071;width:627;height:314;rotation:2248108fd;mso-wrap-style:none; v-text-anchor:middle' fillcolor="#ccf" /> <v:shape id="_x0000_s1034" type="#_x0000_t13" style='position:absolute;left:7788; top:14228;width:626;height:313;rotation:2248108fd;mso-wrap-style:none; v-text-anchor:middle' fillcolor="#ccf" /> <v:shape id="_x0000_s1035" type="#_x0000_t13" style='position:absolute;left:9458; top:15482;width:626;height:313;rotation:2248108fd;mso-wrap-style:none; v-text-anchor:middle' fillcolor="#ccf" /> <v:shape id="_x0000_s1036" type="#_x0000_t13" style='position:absolute;left:11336; top:16840;width:626;height:314;rotation:2248108fd;mso-wrap-style:none; v-text-anchor:middle' fillcolor="#ccf" /> <w:wrap type="none" /> <w:anchorlock /> </v:group><![endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='font-family:"楷体","serif";mso-hansi-font-family:Arial;color:black; layout-grid-mode:line'><v:shape id="_x0000_i1026" type="#_x0000_t75" style='width:351pt; height:190pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style="mso-element:field-end" mce_style="mso-element:field-end"></span></span><![endif]-->

<!--[if supportFields]><span style='mso-element:field-begin'></span><span style="mso-spacerun:yes" mce_style="mso-spacerun:yes">&nbsp;</span>TOC /h /z /c &quot;<span lang=ZH-CN style="font-family:宋体;mso-ascii-font-family: "Times New Roman";mso-hansi-font-family:"Times New Roman"" mce_style="font-family:宋体;mso-ascii-font-family: "Times New Roman";mso-hansi-font-family:"Times New Roman"">图</span>&quot; <span style='mso-element:field-separator'></span><![endif]--> 4-8 BREW中的事件代理机制 <!--[if supportFields]><span style='mso-element:field-end'></span><![endif]-->

BREW下的消息处理机制与Windows下的消息处理机制的区别在于:BREW的体系结构采用了COM方式,即具有面向对象的类层次结构,从而其具体的事件处理机制也是作为各个接口外露的接口函数的形式被运用。一个应用本质上来说就是一个实例化的IAPPLET类,所以这样就统一了所有在应用中运用的事件处理机制都是各个接口外露接口函数的说法。具体而言,这些事件处理器还是有区别的,主要是IAPPLET_HandleEvent和其他接口的HandleEvent的区别。

IAPPLET_Handleevent是通过在AEEClsCreateInstance中的AEEApplet_New函数被注册实例化的,AEEApplet_New函数实例化应用同时也通过传入USERAPP_HandleEvent参数实例化了IAPPLET_Handleevent

除了IAPPLET具有handleevent外,所有的继承IControl的接口也具有事件处理函数,允许处理事件。这些各种具体的IControl_handleEvent有两种方式被调用。一种是在应用的handleevent中由开发者显式的调用,如:

switch (eCode)

{

case EVT_APP_START:

return(TRUE);

case EVT_APP_STOP:

……….

Case EVT_KEY:

IMENU_Handleevent.

ItextCtl_Handleevent.

}

另一种是当这些控件包含于对话框中,且处于聚焦状态时,这些事件处理函数的触发是隐式的,是由AEE机制自动触发的,无需在代码中显式的调用这些handleevent IDialog接口没有外露的handleevent接口函数,但是允许通过IDialog_seteventhandle来注册一个该对话框的事件处理函数。需要注意的是该事件处理函数是何时被触发的:一旦当一个对话框处于激活时,AEE层将会把所有的事件直接发往该对话框,该对话框会自动的调用处于焦点控件的handleevent来处理该事件,只有当该控件没有处理该事件时,对话框注册的事件处理函数才会被调用。

BREW运行后,首先操作系统中的ui 任务会捕捉到各种事件,此时ui 任务通过aee_dispatch将事件分发至BREW环境中。BREW环境再通过aee_sentevent具体分发事件至目的地,在两种不同的情况下将走不同的流程。

如果当前没有激活的对话框,则紧接着IAPPLET_HandleeventBREW自动调用来处理事件,而此时调用的IAPPLET_Handleevent其实就是用户注册的app_handleevent。从而实现了允许用户的应用捕捉到事件并处理的机制。在用户的app_handleevent中,用户可以将事件继续下发,比如通过调用IMENU_handleevent等将事件下发给各种控件处理。

如果当前有激活的对话框,则基于对话框的事件被BREW自动调用,从而使得事件被对话框最先截获,而对话框之后的处理是检查包含的控件中哪个处于焦点,并将事件下发给它的handleevent来处理,同时根据其返回值来判断其是否已经处理了该事件,当其返回False后,对话框将该事件继续转发至该对话框注册的handleevent(如果有的话),如果该handleevent仍然返回falseBREW继续将该事件转发至app_handleevent。这种机制使得当以对话框方式来创建应用时,各种事件被自动的处理,从而简化了代码量,但也使得事件流程更加晦涩,用户的应用程序不能直接的控制它。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics