PALM开发教程-第九章 分类和查找 |
| 作者:palmheart 来源:palmheart.net 发布时间:2005-12-21 3:36:55 |
|
the date string if any if( dateTime.year != NO_DATE ) { DateToAscii( dateTime.month, dateTime.day, dateTime.year, (DateFormatType)PrefGetPreference( prefDateFormat ), textRecord ); StrCat( textRecord, " " ); } // CH.9 Add the time string if any if( dateTime.hour != NO_TIME ) { TimeToAscii( dateTime.hour, dateTime.minute, (TimeFormatType)PrefGetPreference( prefTimeFormat ), textRecord + StrLen( textRecord ) ); StrCat( textRecord, " " ); } // CH.9 Append the first name StrCat( textRecord, precord + DB_FIRST_NAME_START ); StrCat( textRecord, " " ); // CH.9 Append the last name StrCat( textRecord, precord + DB_LAST_NAME_START ); StrCat( textRecord, " " ); // CH.9 Append the phone number StrCat( textRecord, precord + DB_PHONE_NUMBER_START ); // CH.9 Unlock the record MemHandleUnlock( hrecord ); 深入 取消查找 这个例子中,在查找过程中不能被取消,但添加这个功能也十分简单。可以调用函数EvtSysEventAvail()来实现,将其放在for()循环顶部。 在Find窗体中,我们需要指定字符串的格式,这里使用小写。然后使用函数FindStrInStr()来查找匹配的记录。从它的参数中可以看到,strToFind并不是一个小写的字符串。如果你想建立自己的函数来查找,也需要规定自己的字符串格式。 // CH.9 Copy and convert to lower case StrToLower( lcText, textRecord ); // CH.9 If there's no match, move on if( (FindStrInStr( lcText, findParams->strToFind, &offset )) == false ) continue; 如果找到了一条匹配记录并且屏幕上有空间,那么就在屏幕上显示,然后继续查找下一条记录。 // CH.9 Send it to find // CH.9 If there's no more room, return if( (FindSaveMatch( findParams, cursor, offset, 0, NULL, cardNum, dbID )) == true ) break; // CH.9 Get the rectangle for our line of text FindGetLineBounds( findParams, &bounds ); // CH.9 Truncate the string if necessary width = bounds.extent.x; len = StrLen( textRecord ); noFit = false; FntCharsInWidth( textRecord, &width, &len, &noFit ); // CH.9 Draw the text WinEraseRectangle( &bounds, 0 ); WinDrawChars( textRecord, len, bounds.topLeft.x, bounds.topLeft.y ); // We used a line in the find dialog (findParams->lineNumber)++; } // CH.9 Close the database DmCloseDatabase( contactsDB ); // CH.9 We're done return; } 当查找完所有的记录后,关闭数据库。 调试 我主要调试了查找函数中我所传递的参数,并看看能不能将其修改。例如,不要相信strToFind就是你要找的字符串。另外,注意Palm OS不允许将lineNumber值增加。图9-6所示是查找函数运行后的结果。 下一步做什么 这是介绍Palm OS基础知识的最后一章,在下面的章节中将介绍一些有关软件设计的内容。第十章介绍了如何设计Palm OS的用户操作界面,第十一章介绍了建立Palm OS应用程序所使用的一些工具,第十二章介绍了如何组织和修改代码来加强它的可重用性。 程序列表 下面是最新版本的Contacts.c,包括了我们在这一章中所有的修改。 // CH.2 The super-include for the Palm OS #include <Pilot.h> // CH.5 Added for the call to GrfSetState() #include <Graffiti.h> // CH.3 Our resource file #include "Contacts_res.h" // CH.4 Prototypes for our event handler functions static Boolean contactDetailHandleEvent( EventPtr event ); static Boolean aboutHandleEvent( EventPtr event ); static Boolean enterTimeHandleEvent( EventPtr event ); static Boolean contactListHandleEvent( EventPtr event ); static Boolean menuEventHandler( EventPtr event ); // CH.4 Constants for ROM revision #define ROM_VERSION_2 0x02003000 #define ROM_VERSION_MIN ROM_VERSION_2 // CH.5 Prototypes for utility functions static void newRecord( void ); static VoidPtr getObject( FormPtr, Word ); static void setFields( void ); static void getFields( void ); static void setText( FieldPtr, CharPtr ); static void getText( FieldPtr, VoidPtr, Word ); static void setDateTrigger( void ); static void setTimeTrigger( void ); static void setTimeControls( void ); static Int sortFunc( CharPtr, CharPtr, Int ); static void drawTable( void ); static void drawCell( VoidPtr table, Word row, Word column, RectanglePtr bounds ); static void initIndexes( void ); static void scrollIndexes( Int amount ); static UInt findIndex( UInt scrollValue ); static void find( Ptr params ); // CH.5 Our open database reference static DmOpenRef contactsDB; static ULong numRecords; static UInt cursor; static Boolean isDirty; static VoidHand hrecord; // CH.5 Constants that define the database record #define DB_ID_START 0 #define DB_ID_SIZE (sizeof( ULong )) #define DB_DATE_TIME_START (DB_ID_START +\ DB_ID_SIZE) #define DB_DATE_TIME_SIZE (sizeof( DateTimeType )) #define DB_FIRST_NAME_START (DB_DATE_TIME_START +\ DB_DATE_TIME_SIZE) #define DB_FIRST_NAME_SIZE 16 #define DB_LAST_NAME_START (DB_FIRST_NAME_START +\ DB_FIRST_NAME_SIZE) #define DB_LAST_NAME_SIZE 16 #define DB_PHONE_NUMBER_START (DB_LAST_NAME_START +\ DB_LAST_NAME_SIZE) #define DB_PHONE_NUMBER_SIZE 16 #define DB_RECORD_SIZE (DB_PHONE_NUMBER_START +\ DB_PHONE_NUMBER_SIZE) // CH.6 Storage for the record's date and time in expanded form static DateTimeType dateTime; static Word timeSelect; #define NO_DATE 0 #define NO_TIME 0x7fff // CH.7 The error exit macro #define errorExit(alert) { ErrThrow( alert ); } // CH.7 The sort order variable and constants |
| [] [返回上一页] [打 印] |
|
文章评论 |
