首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Brew >

新手问个BREW MP中GridWidget的有关问题

2012-02-23 
新手问个BREW MP中GridWidget的问题最近开始学做BREW MP,基础太差,许多东西还是一头雾水。昨天开始写个Grid

新手问个BREW MP中GridWidget的问题
最近开始学做BREW MP,基础太差,许多东西还是一头雾水。昨天开始写个Grid Widget的例子,想简单的只显示个Static就好,可水平太差不知道怎么写。在Wizard生成的代码的基础上参考了 http://topic.csdn.net/u/20091123/18/3c67c251-79c0-45cf-b799-08ca40a839a5.html 一帖中两位仁兄的代码,现写出如下代码,请各位大哥看看小弟哪里写的有问题,谢谢了。

/*=============================================================================
FILE: GridWidgetSample.c
=============================================================================*/

/*-----------------------------------------
Includes and Variable Definitions
-----------------------------------------*/
#include "AEEModGen.h"
#include "AEEAppGen.h"
#include "AEEShell.h"

#include "GridWidgetSample.bid"
#include "GridWidgetSample_res.h"

/* some property definition, like AEERect, ect. */
#include "AEEWProperties.h"

#include "AEERootContainer.h"
#include "AEECLSID_ROOTCONTAINER.bid"

#include "AEEDisplayCanvas.h"
#include "AEECLSID_DISPLAYCANVAS.bid"

#include "AEEListWidget.h"
#include "AEECLSID_GRIDWIDGET.bid"

#include "AEEStaticWidget.h"
#include "AEECLSID_STATICWIDGET.bid"

#include "AEEIDecorator.h"

#include "AEEIVectorModel.h"
#include "AEECLSID_VECTORMODEL.bid"

#ifndef RELEASEIF
#define RELEASEIF(p) ReleaseIf((IBase**)(void*)&p)

//#define ERR_TRY(x) do { nErr = (x); if (nErr) goto ERR_CATCH; } while(0)

static void ReleaseIf(IBase **ppif) {
  if (*ppif) {
  IBASE_Release(*ppif); 
  *ppif = 0;
  }
}
#endif

/*-----------------------------------------
Applet Structure
-----------------------------------------*/
typedef struct _GridWidgetSample {
  AEEApplet applet;
  IDisplay * piDisplay;
  IShell * piShell;
  AEEDeviceInfo deviceInfo;
  // Add your own variables here...
IRootContainer *picRootContainer;
IWidget *piwRootWidget;

} GridWidgetSample;

/*-----------------------------------------
Function Prototypes
-----------------------------------------*/
static boolean GridWidgetSample_HandleEvent(GridWidgetSample* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam);
boolean GridWidgetSample_InitAppData(GridWidgetSample* pMe);
void GridWidgetSample_FreeAppData(GridWidgetSample* pMe);
static void GridWidgetSample_DrawScreen(GridWidgetSample * pMe);

/*-----------------------------------------
Function Definitions
-----------------------------------------*/

/*=============================================================================
FUNCTION: AEEClsCreateInstance
=============================================================================*/
int AEEClsCreateInstance(AEECLSID ClsId, IShell * piShell, IModule * piModule, 
void ** ppObj)
{
  *ppObj = NULL;

  if( AEECLSID_GRIDWIDGETSAMPLE == ClsId ) {
if( TRUE == AEEApplet_New(sizeof(GridWidgetSample),
  ClsId,
  piShell,
  piModule,
  (IApplet**)ppObj,
  (AEEHANDLER)GridWidgetSample_HandleEvent,
  (PFNFREEAPPDATA)GridWidgetSample_FreeAppData) ) {
 

if(TRUE == GridWidgetSample_InitAppData((GridWidgetSample*)*ppObj)) {


return AEE_SUCCESS; // Data initialized successfully.
}
else {
  IApplet_Release((IApplet*)*ppObj);
  return AEE_EFAILED;
  }
  } // End AEEApplet_New
  }
  return AEE_EFAILED;
}


/*=============================================================================
FUNCTION: GridWidgetSample_InitAppData
=============================================================================*/
boolean GridWidgetSample_InitAppData(GridWidgetSample * pMe)
{
int nErr;
  // Save local copy for easy access:
  pMe->piDisplay = pMe->applet.m_pIDisplay;
  pMe->piShell = pMe->applet.m_pIShell;

  // Get the device information for this handset.
  pMe->deviceInfo.wStructSize = sizeof(pMe->deviceInfo);
  ISHELL_GetDeviceInfo(pMe->applet.m_pIShell,&pMe->deviceInfo);
   
  // Insert your code here for initializing or allocating resources...
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_ROOTCONTAINER, (void *)(&pMe->picRootContainer));
if (AEE_SUCCESS != nErr) {
return FALSE;
}

nErr = IROOTCONTAINER_QueryInterface(pMe->picRootContainer, AEEIID_WIDGET, (void *)(&pMe->piwRootWidget));
if (AEE_SUCCESS != nErr) {
return FALSE;
}

  return TRUE;// No failures up to this point, so return success.
}


/*=============================================================================
FUNCTION: GridWidgetSample_FreeAppData
=============================================================================*/
void GridWidgetSample_FreeAppData(GridWidgetSample * pMe)
{
  // Insert your code here for freeing any resources you have allocated...

  // Example to use for releasing each interface:
  // if ( NULL != pMe->piMenuCtl ) { // check for NULL first
  // IMenuCtl_Release(pMe->piMenuCtl)// release the interface
  // pMe->piMenuCtl = NULL; // set to NULL so no problems later
  // }
  //
}


/*=============================================================================
FUNCTION: GridWidgetSample_HandleEvent
=============================================================================*/
static boolean GridWidgetSample_HandleEvent(GridWidgetSample* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{  
  switch (eCode) {

  case EVT_APP_START:
  GridWidgetSample_DrawScreen(pMe); // Draw text on display screen.
  return TRUE;  

  case EVT_APP_STOP:
  return TRUE;

  case EVT_APP_SUSPEND:
  return TRUE;

  case EVT_APP_RESUME:
  GridWidgetSample_DrawScreen(pMe); 
  return TRUE;

  case EVT_APP_MESSAGE:
  return TRUE;

  case EVT_KEY:
  return FALSE;

  case EVT_FLIP:
  return TRUE;
   
  case EVT_KEYGUARD:
  return TRUE;

  default:
  break;
  }
  return FALSE; // Event wasn't handled.
}


/*=============================================================================
FUNCTION: GridWidgetSample_DrawScreen
=============================================================================*/
static void GridWidgetSample_DrawScreen(GridWidgetSample * pMe)
{
AEERect rcRoot;
ICanvas *piCanvas;
IWidget *piw, *pisw;


IVectorModel *pivm;
WidgetPos wpos;
WidgetExtent weStatic;
  AECHARpwszText[64] = {0};
  intnStrLen = 0;
int nErr;

/* Set Canvas for RootContainer */
SETAEERECT(&(rcRoot), 0, 0, pMe->deviceInfo.cxScreen, pMe->deviceInfo.cyScreen);
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_DISPLAYCANVAS, (void**)&(piCanvas));
if (AEE_SUCCESS != nErr) 
{
return;
}

nErr = IDISPLAYCANVAS_SetDisplay((IDisplayCanvas*)(void *)piCanvas, pMe->piDisplay);
if (AEE_SUCCESS != nErr) 
{
return;
}

IROOTCONTAINER_SetCanvas(pMe->picRootContainer, piCanvas, &(rcRoot));

RELEASEIF(piCanvas);

nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_GRIDWIDGET, (void**)&(piw));
if (AEE_SUCCESS != nErr) 
{
return;
}

/* Create the item widget that will be wrapped by the list widget. */
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_STATICWIDGET, (void**)&(pisw));
if (AEE_SUCCESS != nErr) 
{
return;
}

/* Identify the value model that is to be associated with this widget. */
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_VECTORMODEL, (void**)&(pivm));
if (AEE_SUCCESS != nErr) 
{
return;
}

/* Specify the text to be displayed within the static widget */
  nStrLen = ISHELL_LoadResString(pMe->piShell, 
  GRIDWIDGETSAMPLE_RES_FILE,
  IDS_STRING_1001, pwszText, sizeof(pwszText));

if (pwszText)
{
IValueModel *piValueModel = 0;
if (AEE_SUCCESS == IWidget_GetModel(pisw, AEEIID_VALUEMODEL, (IModel **)(void**) &(piValueModel)))
{
IVALUEMODEL_SetText(piValueModel, pwszText, -1);
IValueModel_Release(piValueModel);
}
}

/* Attach the appropriate model to the list. */
nErr = IWidget_SetModel(piw, IVectorModel_to_IModel(pivm));
if (AEE_SUCCESS != nErr) 
{
return;
}

/* Wrap the list widget around the item widget, use staticWidget as 'item' widget. */
IDecorator_SetWidget((IDecorator *)(void*) piw, pisw);

IWidget_SetRows(piw, 2);
IWidget_SetCols(piw, 3);

IWidget_GetPreferredExtent(pisw, &weStatic);

/* Set the width and height of the list widget. */
IWidget_SetItemWidth(piw, weStatic.width);
IWidget_SetItemHeight(piw, weStatic.height);

weStatic.width = 200;
weStatic.height = 240;
IWidget_SetExtent(piw, &weStatic);

IWidget_SetBGColor(pisw, MAKE_RGB(0,0,255));

wpos.x = 0;
wpos.y = 0;
wpos.bVisible = TRUE;

IROOTCONTAINER_Insert( pMe->picRootContainer, piw, WIDGET_ZNORMAL, &wpos );

RELEASEIF(piw);
RELEASEIF(pisw);
RELEASEIF(pivm);

}

[解决办法]
我大概看了一下你的代码,你的widget应该是已经画上去了,但是文字应该没有显示。不知道是不是这样,文字显示是需要你要给static widget做一个set的动作的,添加一个ValueModel的Listener当值发生改变的时候给staitc widget设置一下文字。
[解决办法]
两点哦:
1. 2行3列的GridWidget没有任何内容哦,因为你的pivm Vectormodel里面没有任何数据
2. 就是楼上说的GridWidget每一个item里面的STATICWIDGET没有数据, 解决方法是给ValueModel设置一个Adapte

最好用bmp的例子先看明白哦, 这个理解还是有点复杂的~

热点排行