PALM开发教程-第七章 列表框和排序 |
| 作者:palmheart 来源:palmheart.net 发布时间:2005-12-21 3:33:26 |
|
ctListForm ); } break; 为了在窗体间切换,而不是弹出窗体,我们使用了函数FrmGotoform()来初始化。这个函数首先调用frmCoseEvent关闭前一个窗体,然后调用frmLoadEvent和frmOpenEvent打开新的窗体。 Contact List窗体事件处理函数 下面我们为Contact List 窗体来添加事件处理函数。首先,在文件的开头加入原型: static Boolean contactListHandleEvent( EventPtr event ); 另外,还需要两个变量表示给列表框分配内存的句柄: // CH.7 Contact list variables static VoidHand hchoices; // CH.7 Handle to packed choices static VoidHand hpchoices; // CH.7 Handle to pointers 下面是事件处理函数: // CH.7 Our Contact List form event handler function static Boolean contactListHandleEvent( EventPtr event ) { FormPtr form; // CH.7 A form structure pointer // CH.7 Get our form pointer form = FrmGetActiveForm(); // CH.7 Parse events switch( event->eType ) { // CH.7 Form open event case frmOpenEvent: { // CH.7 Draw the form FrmDrawForm( form ); // CH.7 Build the list buildList(); } break; // CH.7 Form close event case frmCloseEvent: { // CH.7 Unlock and free things here MemHandleUnlock( hpchoices ); MemHandleFree( hpchoices ); MemHandleUnlock( hchoices ); MemHandleFree( hchoices ); hchoices = 0; } break; // CH.7 Respond to a list selection case lstSelectEvent: { // CH.7 Set the database cursor to the selected contact cursor = event->data.lstSelect.selection; // CH.7 Go to contact details FrmGotoForm( ContactDetailForm ); } break; // CH.7 Respond to a menu event case menuEvent: return( menuEventHandler( event ) ); // CH.7 Respond to the popup trigger case popSelectEvent: { // CH.7 If there is no change, we're done if( sortBy == event->data.popSelect.selection ) return( true ); // CH.7 Modify sort order variable sortBy = event->data.popSelect.selection; // CH.7 Sort the contact database by the new criteria DmQuickSort( contactsDB, (DmComparF*)sortFunc, sortBy ); // CH.7 Rebuild the list buildList(); } break; } // CH.7 End of the event switch statement // CH.7 We're done return( false ); } 事件处理函数以十分标准的形式开始:首先建立了一个窗体指针变量,它将在调用frmOpenEvent时被调用;然后窗体被绘制并调用了一个新的函数buildList(),这个函数在下一部分将详细讨论。 我们调用frmCloseEvent释放用来储存列表框内容的内存。之所以在关闭(close)事件中释放内存,是因为只要列表框可见、窗体在使用的时候,这一部分内存就一直被Palm OS使用。在这一点上,列表控件和其它控件有所不同,一般的控件都有自己的内存,而对于列表控件来说,它需要另外分配内存。 下面,讲述一个新的事件处理函数——lstSelectEvent。当列表的条目被选中时,此事件将被触发。变量selection标明哪一个条目被选中,列表得记录从0开始。这样我们使用起来就十分方便了,只要将当前记录等于列表变量selection的值,然后调用Contact Detail窗体,窗体就会使用前面所设置的变量cursor产生正确的记录。 最后,对本程序菜单事件的处理和对Contact Detail窗体的菜单处理几乎相同。这样,对Contact List窗体的处理程序就算完成了。 函数buildList() 下面我们来讨论这个实用函数buildList(),这个函数通过浏览数据库,为每条记录创建一文本字符串,然后使用这些字符串填充List对象的各个条目。 static void buildList( void ) { FormPtr form; // CH.6 A form structure pointer Int choice; // CH.7 The list choice we're doing CharPtr precord; // CH.7 Pointer to a record Char listChoice[dateStringLength + 1 + // CH.7 We timeStringLength + 1 + // build DB_FIRST_NAME_SIZE + // list DB_LAST_NAME_SIZE]; // choices here // CH.7 The current list choice CharPtr pchoices; // CH.7 Pointer to packed choices UInt offset; // CH.7 Offset into packed strings VoidPtr ppchoices; // CH.7 Pointer to pointers to choices // CH.6 Get our form pointer form = FrmGetActiveForm(); 在声明变量后,函数象往常一样获取窗体指针。这里面最有趣的变量是char array ,它将保证列表字符串的连续性。为使阵列(array)有足够的大小来保存任何Palm OS的系统时间和日期,可以使用Palm OS常量dateStringLength和timeStringLength。 // CH.7 Put the list choices in a packed string for( choice = 0; choice < numRecords; choice++ ) { // CH.7 Get the record hrecord = DmQueryRecord( contactsDB, choice ); precord = MemHandleLock( hrecord ); // CH.7 Get the date and time MemMove( &dateTime, precord + DB_DATE_TIME_START, sizeof( dateTime ) ); 首先,浏览数据库的所有记录,然后获取记录将变量dateTime设置为在所得记录中的时间和日期。注意到我们使用了DmQueryRecord()函数来获取记录句柄。此函数只给我们提供一个只读的记录副本。 下面,我们创建表示记录的字符串: // CH.7 Clear the list choice string *listChoice = '\0'; // CH.7 Add the date string if any if( dateTime.year != NO_DATE ) { DateToAscii( dateTime.month, dateTime.day, dateTime.year, (DateFormatType)PrefGetPreference( prefDateFormat ), listChoice ); StrCat( listChoice, " " ); } // CH.7 Add the time string if any if( dateTime.hour != NO_TIME ) { TimeToAscii( dateTime.hour, dateTime.minute, (TimeFormatType)PrefGetPreference( prefTimeFormat ), listChoice + StrLen( listChoice ) ); StrCat( listChoice, " " ); } // CH.7 Append the first name StrCat( listChoice, precord + DB_FIRST_NAME_START ); StrCat( listChoice, " " ); // CH.7 Appe |
| [] [返回上一页] [打 印] |
|
文章评论 |
