![]() | Programming Guide | WideStudio/MWT Index Table of contents |
标签事件过程范例
通过鼠标选择标签
以下是事件过程中最基本的鼠标动作的范例。每次鼠标被单击时,在标签上就显示被点击数。#include "WSDmouse.h" //Set this to a label Instance with WSEV_MOUSE_PRESS // (MOUSE-PRESS trigger) void cbop(WSCbase* object){ //(0) Which the mouse button is pressed? // btn1 -> fire. btn2 or the other -> return. if ( (WSGIappMouse()->getMouseStatus() & WS_MOUSE_BTN1) == 0){ return; } //(A)Get the value of the property: WSNuserValue long value = object->getProperty(WSNuserValue); //(B)Count it up. value++; //(C)Display the value. object->setProperty(WSNlabelString,value); //(D)Store the counted value into the property: // WSNuserValue for the next time. object->setProperty(WSNuserValue,value); }首先,在这个事项过程,对WSCvbtn,WSCvlabel 等拥有WSNlabelString 属性的项目进行MOUSE-PRESS 触发器设定。每次鼠标键单击该项目时,该事项过程函数将被启动。 事项过程被启动后,以(0)进行鼠标键的判断。如果鼠标键为1以外,则按钮回车。请参照鼠标键判断的处理方法。
然后,(A)处取得WSNuserValue 的属性数字。 WSNuserValue 属性初始值为0,用户可自由指定初期值并进行保持。在这里,让项目记忆该数字是为了当多项项目被处理时,不发生各自的标签数字混乱。反过来,使用静态变量等制作的话,全部的标签可利用同一个计数器进行显示。
然后,(B)处追加点击数,并在(C)进行显示。在(D)中将显示的数字作为WSNuserValue的 属性数进行保存,预备下一个事项过程的启动。
如果触发器适用MOUSE-IN的话,应该可以显示鼠标出入回数。
制作可以鼠标选择的标签
以下,我们试着制作可以选择的标签。当标签被选择时,颜色将发生变化。这次,使用set/getUserData(函数)代替WSNuserValue 属性来保持选择状态。//Set this to a label Instance with WSEV_MOUSE_PRESS // (MOUSE-PRESS trigger) void cbop(WSCbase* object){ //(A) Get the value with getUserData() long value = (long)object->getUserData("STATUS"); //(B) it makes the Instance selected if value is 0, // and unselected if 1. if (value == 0){ //(C) Store the backcolor(which is string type) into WSNuserString WSCvariant color = object->getProperty(WSNbackColor); object->setProperty(WSNuserString,color); //(D)Set the backcolor to the selected color. object->setProperty(WSNbackColor,"slategray4"); //(E)Store the state with setUserData(). value = 1; object->setUserData("STATUS",(void*)value); }else{ //(F)Get the original backcolor from WSNuserString. WSCvariant color = object->getProperty(WSNuserString); //(G)Store it to WSNbackColor to display with the original color. object->setProperty(WSNbackColor,color); //(H)Store the state with setUserData(). value = 0; object->setUserData("STATUS",(void*)value); } }在这个事项过程,对WSCvbtn,WSCvlabel 等具用WSNlabelString属性的项目,设定MOUSE-PRESS 触发器。
事项过程被启动时,在(A)处取得以setUserData函数记忆的数字。set/getUserData函数的初始值为0,用户可自由指定或保持其值。可使用喜欢的名称,用void*进行保持。
然后,在(B)处辨别该值,在(C)处将原来的颜色保存于属性 WSNuserString,以便下次显示原来颜色的使用。(D)处,显示所选择的颜色。(E)处,再次重新保持选择状态。
将颜色恢复到原来状态时,首先在(F)处取得保持的原来颜色,然后在(G)处显示该颜色。最后在(H)处再次保持选择状态。制作加亮鼠标标签
以下制作通过鼠标的进入及离开,显示加亮标签的事项过程。当鼠标进入项目上时,标签加亮,而当鼠标离开项目时,标签显示恢复。
这里重要的步骤是,设定鼠标进入时发生的触发器和鼠标出来时发生的触发器。
当事项过程被初始化启动时,可通过设定新事项过程的方法,实现一个事项过程中设定复数事项过程的处理。//a sub-procedure with WSEV_MOUSE_IN trigger void subop1(WSCbase* object){ //(A)Store the original back-color to WSNuserString WSCvariant color = object->getProperty(WSNbackColor); object->setProperty(WSNuserString,color); //(B)highlight the Instance. object->setProperty(WSNbackColor,"slategray4"); } //a sub-procedure with WSEV_MOUSE_OUT trigger void subop1(WSCbase* object){ //(C)Get the original back-color from WSNuserString WSCvariant color = object->getProperty(WSNuserString); //(D)Store the original back color. object->setProperty(WSNbackColor,color); } //a main-procedure with WSEV_INITIALIZE trigger void cbop(WSCbase* object){ //If executed,it add the sub-procedures to the Instance. //(E) Setup a sub-procedure:WSEV_MOUSE_IN. //ProcedureName="Highlight1" Trigger=WSEV_MOUSE_IN Function=subop1 WSCprocedure* ac1 = new WSCprocedure("Highlight1",WSEV_MOUSE_IN); ac1->setFunction(subop1,"subop1"); object->addProcedure(ac1); //(F) Setup a sub-procedure:WSEV_MOUSE_OUT. //ProcedureName="Highlight2" Trigger=WSEV_MOUSE_OUT Function=subop2 WSCprocedure* ac2 = new WSCprocedure("Highlight2",WSEV_MOUSE_OUT); ac2->setFunction(subop2,"subop2"); object->addProcedure(ac2); }通过子项目EP1,将鼠标界内的触发器启动,把标签的背景显示颜色做为加亮颜色。子项目EP2,鼠标出界的触发器启动,把标签的背景显示颜色恢复为原来的颜色。事项过程的实体,被初始化触发器设定在项目中,并通过(E),(F)项目设定子项目EP1,2。以后,当鼠标点入时,设定在新程序中的子项目EP将被实行。制作可用鼠标选择的组化标签
对于同样区域上可配置的标签,可做成用鼠标选择可能的组化标签事项过程。在该选择状态里用鼠标选择标签,即可保持该区域中被选择的标签。//An event procedure with WSEV_MOUSE_PRESS trigger void cbop(WSCbase* object){ //(A)Use the value of WSNuserValue as "Instance identifier" long val = object->getProperty(WSNuserValue); //(B)Make the Instance selected: WS_SHADOW_IN state. object->setProperty(WSNshadowType,WS_SHADOW_IN); //(C)Get the last selected Instance which is memorized with setUserData() of the parent Instance. WSCbase* parent = object->getParent(); WSCbase* target = (WSCbase*)parent->getUserData("SelectedItem"); //(D)Make it not selected: WS_SHADOW_OUT state. if (target != NULL){ target->setProperty(WSNshadowType,WS_SHADOW_OUT); } if (target == object){ //(E)When clicking the selected Instance twice, // clear the selected state. parent->setUserData("GroupValue",(void*)0); parent->setUserData("SelectedItem",(void*)0); }else{ //(E)The other,store the selected Instance to the parent Instance. parent->setUserData("GroupValue",(void*)val); //Instance identifier parent->setUserData("SelectedItem",(void*)object); //selected Instance } }对组化标签,各自需具有识别的ID,在此ID使用设定在WSNuserValue 属性上的设定值。
(A) 取得WSNuserValue 属性上设定的 ID 。
(B)为了表现可进入选择,显示选择状态。
(C)取得被选择的项目。任何项目均可以,不过,这个范例中,首先选择组化标签共有的父对象。
(D)将被选择项目的显示状态恢复到已选择状态。
(E)被选择的项目(target)和,已经被选择的项目(object)相等的情况,即选择2次时,选择状态被解除。父对象中所设定的值和项目将初期化。
(F)将被重新选择的项目和其值设定到父对象。
Copyright(C) WideStudio/MWT Development Team, 1999-2005 | Last modified: Jul 31, 2005 |