您现在的位置: 中国IT实验室 >> 移动设备开发 >> PLAM >> 文章正文
PALM开发教程-第三章 文本框中的乐趣(下)

ChinaITLab 收集整理  2005-3-6  保存本文  推荐给好友  QQ上看本站  收藏本站



  6.当你创建完菜单,双击打开Contact Detail窗体。在Menu Bar ID属性条中输入菜单的ID号(可能1000)。
  
  7.以上工作完成了菜单条资源的创建,它看起来像前面例子中的一个标准的Edit菜单。
  为了使用你刚才创建的菜单,必须在Contacts.c中增加一些代码。为了处理菜单事件,你必须增加一个叫MemuHandleEvent()的函数调用,在事件循环中处理菜单事件。
  
  //CH.2 Handle system events
  if (SysHandleEvent(&event))
  continue;
  //CH.3 Handle menu events
  if(MenuHandleEvent(NULL,&event,&error))
  contiue;
  //CH.2 Handle form events
  FrmDispatchEvent(&event);
  
  在你事件处理器的循环中,以menuEvent函数调用替换ctlSelectEvent调用。使用它再去调用一个名为menuEventHandler()的函数。现在你新的事件处理器看起来像下面这样:
  
  //CH.2 Our form handler function
  static Boolean myHandlerEvent(EventType* event)
  {
  //CH.3 Parse menu events
  if(event->eType= =menuEvent)
  return(menuEventHandler(event));
  //CH.2 We’re done
  return(false);
  }
  
  现在来写menuEventHandler()函数。首先,你必须发信号给用户界面,报告菜单事件已经被接收:
  
  //CH.3 Handle menu events
  Boolean menuEventHandler(EventPtr event)
  {
  FormPtr form //CH.3 A pointer to our form structure
  Word index //CH.2 A general purpose control index
  FieldPtr field; //CH.3 Used for manipulating fields
  //CH.3 Get our form pointer
  form=FrmGetActiveForm();
  //CH.3 Erase the menu status from the display
  MenuEraseStatus(NULL);
  
  提供单独的输入区帮助。这是因为输入区帮助应该是可用的,不管文本框当前是否被选中。
  
  //CH.3 Handle graffiti help
  if(event->data.menu.itemID= =EditGraffitiHelp)
  {
  //CH.3 Pop up the graffiti reference based on
  //the graffiti state
  SysGraffitiReferenceDialog(referenceDefault);
  return(true);
  }
  
  下一步,你得到了文本框指针后就可以调用那些非常好的编辑命令了。下面的例子显示了一个用最普通的方法来得到文本框指针,这个方法我们在先前的c ase frmOpenEvent:中用到过。无论窗体上有多少文本框,它都可以工作。
  
  //CH.3 Get the index of our field
  index=FrmGetFocus(form);
  //CH.3 If there is no field selected,we’re done
  if(index= =noFocus)
  return(false);
  //CH.3 Get the pointer of our field
  field=FrmGetObjectPtr(form,index);
  
  现在我们可以执行edit命令了。调用这些编辑函数非常简单并且它们处理每一件事都比较恰当。如执行Select All命令,你只要把整个字符串传递给FldSetSelection()函数就可以了。
  
  //CH.3 Do the edit command
  switch(event->data.menu.itemID)
  {
  //CH.3 Undo
  case EditUndo;
  FldUndo(field);
  break;
  
  //CH.3 Cut
  case EditCut;
  FldCut(field);
  break;
  
  //CH.3 Copy
  case EditCopy;
  FldCopy(field);
  break;
  
  //CH.3 Paste
  case EditPaste;
  FldPaste (field);
  break;
  
  //CH.3 Select All
  case EditSelect All;
  {
  //CH.3 Get the length of the string in the field
  Word length=FldGetTextLength(field);
  
  //CH.3 Select the whole string
  FldSetSelection(field,0,length);
  }
  break;
  //CH.3 Bring up the kdyboard tool
  case EditKeyBoard;
  SysKeyBoardDialog (kbdDefault);
  break;
  }
  //CH.3 We’re done
  rerurn(true);
  }
  
  经过以上修改,make,debug,并运行你的应用程序。你就可以使用已创建的菜单和快捷方式了。
  
  支持各种不同版本的Palm OS
  事实上,上面的代码如果拿到Pilot 1000或者Pilot 1500这些使用Palm OS1.0版本的系统上运行的话就会使系统崩溃。这是因为在1.0版本中,SysKeyboardDialog()是一个于现在不同的函数调用。但也有好几种方法可以解决这个问题。首选就是换成调用函数S ysKeyboardDialogV10(),这是一个向后兼容的函数。除了最新的系统命令外,如果你还想了解更多的话,你就有必要检查一下OS的版本,这样可以基于O S版本来正确的调用函数。
  
  如果我们多次遭遇由于OS版本不同带来的严重后果的话,我们在编程时将变得更加老练。现在就用SysKeyboardDialogV10()替换这个SysKe yboardDialog()函数调用。
  
  //CH.3 Bring up the keyboard tool
  case EditKeyboard;
  SysKeyboardDialogV10();
  break;
  
  错误和警告
  Contacts程序已经扩展了它的使用范围,用户对它做的某些操作可能导致错误发生。现在是一个极好的时机来讨论出错处理。
  
  如果你执行编辑命令,就可以发现如果你做一些无意义的事,譬如在没有选中任何文本的时候复制,编辑函数也会产生一个警告声。不只是S elect All如此,因为你已经调用了一个普通函数,即便没有错误,它也会发出声音的。为了使Select All符合其它函数的发音标准,你可以加入如下的一个SndPlaySystemSound()函数:
  
  //CH.3 Select All
  case EditSelectAll
  {
  //CH.3 Get the length of the String in the field
  Word length=FldGetTextLength(field);
  //CH.3 Sound an error if appropriate
  if(legth= =0)
  {
  SndPlaySystemSound(sndError);
  return(false);
  }
  //CH.3 Select the whole string
  FldSetSelection(field,0,length);
  }
  
  当用户的输入超越了被调函数的职能时,就应该提示用户他们可能做错了什么。对此,有一个很好的解决方法,就是使用Alerts。Alerts `是由操作系统控制的袖珍型窗体,创建和使用都非常的方便。
  
  作一个试验:创建一个Alert资源,来显示Select All命令的错误信息:
  1.启动资源构造器。
  2.从资源类型列表中选中Alert。
  3.按Ctrl-K来创建一个新的警告。
  4.双击打开警告。
  5.改变消息属性像“There was no text to select。”
  6.改变Error的标题属性。
  7.改变Error的警告类型属性。
  为了调用这些Alert,我们加入FrmAlert()函数。你可以从Contacts_res.h文件中得到警告ID的变量名。
  
  //CH.3 Pop up an error if appropriate
  if(length= =0)
  {
  SndPlaySystemSound(sndError);
  FrmAlert(SelectAllErrorAlert);
  return(false);
  }
  
  加入上面的代码后,make,debug,并运行应用程序。可以发现,自从我们给错误设了警告类型,就有两次嘟嘟声:一个来自SndPlaySyst emSound(),一个来自警告。
  
  另一个我们要放置Alert,至少是一次系统嘟嘟声的地方,就是当我们检查焦点的时候。事实上,当休眠状态的应用程序被唤醒的时候,文本框是获得焦点的首选控件。在一般情况下我们就想要在此时发一个经典的系统嘟嘟声来通知用户。
  由于Select All的错误提示并不是经常会出现的,就让我们移走这个Alert资源。在以后的章节中,我们再使用Alert通知用户各种不同的错误信号吧。
  
  下一步是什么?
  在下一章中,我们将研究如何在你的应用程序中拥有多个窗体。
  
  程序清单
  这里是完整的Contacts.c程序清单。从hello.c改变的行都用//CH.3注解。
  //CH.2 The super-include for the Palm OS
  #include
  
  //CH.3 Our resource file
  #include “Contacts_res.h”
  
  // CH.2 prototypes for our event handler functions
  static Boolean myHandleEvent(EventPtr enent);
  static Boolean menuEventHandler(EventPtr event);
  
  //CH.3 Our field memory handle
  static Handle htext; //CH.3 Handle to the text in our edit field
  #define HTEXT_SIZE 81 // CH.3 Size of our edit field
  
  //CH.2 The main entry point
  Dword PilotMain (Word emd,Ptr,Word)
  {
  FormPtr form; //CH.2 A pointer to our form structure*/
  Handle hsrc; //CH.3 Handle to the string resource
  Charptr psrc; //CH.3 Points to the text in the resource
  Charptr ptext; //CH.3 Points to the text in the edit field
  Word index; //CH.3 A general purpose index
  FieldPtr field; //CH.3 Used for manipulating fields
  EventType event; //CH.2 Our eent structure
  Word error; //CH.3 Error word for menu event handler
  
  //CH.2 If this in not a normal launch,don’t launch
  if (cmd!=sysAppLaunchCmdNormalLaunch)
  return(0);
  
  //CH.3 Get the initialization string resource handle
  hsrc=DmGetResource(strRsc, FieldInitString);




 相关文章  热门文章
Palm Powered设备上的增强型音频
编写Palm J2ME红外线“聊天”程序
PALM开发教程-…
PALM开发教程-…
PALM开发教程-…
PALM开发教程-…
PALM开发教程-…
Palm OS上发送和&…
Palm OS上常用编&…
Palm OS上如何利&…

 文章评论


认证培训
热门专题       more
相关下载
论坛新帖
博 客