Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14087
  • 博文数量: 2
  • 博客积分: 130
  • 博客等级: 入伍新兵
  • 技术积分: 35
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-21 10:19
文章分类
文章存档

2010年(2)

我的朋友
最近访客

分类:

2010-01-25 11:05:09

支持滚动的左右手势进行图片切换,支持滚动的上下手势进行图片的放大与缩小。
(在模拟器里面上与下正好反了,左与右正好反了,动画的效果还有点问题估计在实际的硬件环境下不会有问题)

//###############################################################################################################
//Main.cpp
//Designed by xqh
//Date:2009--12--29

//#include
#include
#include
#include
#include

//=================================================================================================================
IXRApplication* pApplication = NULL;
IXRVisualHost* pVisualHost = NULL;
IXRImagePtr  CenterImage;
IXRImagePtr  LeftImage;
IXRImagePtr  RightImage;
LPCTSTR   strFilePath[8];

class BtnEventHandler //单独响应按钮操作
{
protected:
   
 IXRStoryboardPtr     sboard;

public:
   BtnEventHandler(IXRStoryboardPtr& storyboard)
 {      
      sboard=storyboard;
 }
    HRESULT Btn_OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)   
 {     
#if  1
  //MessageBox(NULL,TEXT("Btn_OnClick!"),TEXT("Silverlight for Windows Embedded test"),MB_OK);  

        RETAILMSG(1,(TEXT("++++BtnEventHandler::Btn_OnClick( )----\r\n")));
#else
      UINT  exitCode=0;
     // pApplication->StopProcessing(exitCode);
      pVisualHost->EndDialog(0);
#endif
     return S_OK;   
 }
//====================================================================================================
    HRESULT Image_MouseLeftButtonDown(IXRDependencyObject* source,XRMouseButtonEventArgs* args)   
 {     
     static  int  ImageIndex=0;
//先弄一个动画效果
       HRESULT        hr;       
    XRClockState ckstate;
//MessageBox(NULL,TEXT("Image_MouseLeftButtonDown!"),TEXT("Silverlight for Windows Embedded test"),MB_OK);  
 RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_MouseLeftButtonDown( )----\r\n")));

        if (FAILED(hr=sboard->GetCurrentState(&ckstate)))           
   return hr;
        if (ckstate==XRClockState_Stopped)       
  {    
      if (FAILED(hr=sboard->Begin()))               
       return hr;       
  }       
  else       
  {           
      if (FAILED(hr=sboard->Stop()))               
       return hr;       
  }

      if(ImageIndex>6)
    ImageIndex=0;
   else
    ImageIndex++;

 
       IXRBitmapImagePtr    BitmapImage;
//------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);

BitmapImage->SetUriSource(strFilePath[ImageIndex]);

if(LeftImage==NULL)
RETAILMSG(1,(TEXT("++++the LeftImage is NULL!\r\n")));

LeftImage->SetSource(BitmapImage);  //这个地方设置不成功!!!
//--------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);
if(ImageIndex>6)
BitmapImage->SetUriSource(strFilePath[7-ImageIndex]);
else
BitmapImage->SetUriSource(strFilePath[ImageIndex+1]);
CenterImage->SetSource(BitmapImage);

//----------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);
if(ImageIndex>5)
BitmapImage->SetUriSource(strFilePath[ImageIndex-6]);
else
BitmapImage->SetUriSource(strFilePath[ImageIndex+2]);

RightImage->SetSource(BitmapImage);

     return S_OK;   
 }

//====================================================================================================
 HRESULT Image_Gesture(IXRDependencyObject* source,XRGestureEventArgs* args)   
 { 
   DWORD  dwID;
   GESTUREINFO   GestureInfo;
   static   int  iCurImage=0;
   static   float  fScaleX=1.0,fScaleY=1.0; //设置放大/缩小的倍数
   float   fActualX,fActualY;
   float   fWidth,fHeight;

   IXRBitmapImagePtr    BitmapImage;
   HRESULT        hr;       
   XRClockState ckstate;
   IXRScaleTransform*  pRenderTransform;
   GestureInfo=args->GestureInfo;
   dwID=GestureInfo.dwID;
  
   RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----\r\n")));

   switch(dwID)
   {
   case  GID_BEGIN:  //手势开始
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_BEGIN!\r\n")));
 
    break;
   case  GID_END:  //手势结束
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_END!\r\n")));
    break;

   case  GID_PAN:  //滑动----速度慢一点
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_PAN!\r\n")));
    break;
   case  GID_ROTATE:  //旋转----保留没有使用
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_ROTATE!\r\n")));
    break;

   case  GID_SCROLL://滚动---速度要求比PAN快
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_SCROLL!\r\n")));

            switch( GID_SCROLL_DIRECTION(GestureInfo.ullArguments) ) //判断滚动的方向
   {
       case  ARG_SCROLL_NONE:
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_SCROLL--case  ARG_SCROLL_NONE!\r\n")));
    break;
               case  ARG_SCROLL_UP:
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_SCROLL--case  ARG_SCROLL_UP!\r\n")));
//进行图片缩小的处理
if(fScaleX>0.5)
fScaleX-=0.1;
if(fScaleY>0.5)
fScaleY-=0.1;

pApplication->CreateObject(IID_IXRScaleTransform,&pRenderTransform);

CenterImage->GetActualX(&fActualX);  //获取对应的坐标值
CenterImage->GetActualY(&fActualY);
CenterImage->GetWidth(&fWidth); //获取控件的大小
CenterImage->GetHeight(&fHeight); 
pRenderTransform->SetCenterX(fActualX+fWidth/2);
pRenderTransform->SetCenterY(fActualY+fHeight/2);
pRenderTransform->SetScaleX(fScaleX);
pRenderTransform->SetScaleY(fScaleY);
CenterImage->SetRenderTransform(pRenderTransform );

    break;

                case  ARG_SCROLL_DOWN:
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_SCROLL--case  ARG_SCROLL_DOWN!\r\n")));

//进行图片放大的处理
if(fScaleX<2.0)
fScaleX+=0.1;
if(fScaleY<2.0)
fScaleY+=0.1;
pApplication->CreateObject(IID_IXRScaleTransform,&pRenderTransform);

CenterImage->GetActualX(&fActualX);  //获取对应的坐标值
CenterImage->GetActualY(&fActualY);
CenterImage->GetWidth(&fWidth); //获取控件的大小
CenterImage->GetHeight(&fHeight); 
pRenderTransform->SetCenterX(fActualX+fWidth/2);
pRenderTransform->SetCenterY(fActualY+fHeight/2);
pRenderTransform->SetScaleX(fScaleX);
pRenderTransform->SetScaleY(fScaleY);
CenterImage->SetRenderTransform(pRenderTransform );

    break;
                 case  ARG_SCROLL_LEFT:
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_SCROLL--case  ARG_SCROLL_LEFT!\r\n")));
//先弄一个动画效果
/*
        if (FAILED(hr=sboard->GetCurrentState(&ckstate)))  //为什么会是XRClockState_Filling状态?         
   return hr;
        if (ckstate==XRClockState_Stopped)       
  {    
      if (FAILED(hr=sboard->Begin()))  //1S钟就结束             
       return hr;       
  }       
  else       
  {           
      if (FAILED(hr=sboard->Stop()))               
       return hr;       
  }
*/
if(iCurImage>6)
    iCurImage=0;
else
    iCurImage++;

 
//------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);
BitmapImage->SetUriSource(strFilePath[iCurImage]);
LeftImage->SetSource(BitmapImage); 
//--------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);
if(iCurImage>6)
   BitmapImage->SetUriSource(strFilePath[7-iCurImage]);
else
   BitmapImage->SetUriSource(strFilePath[iCurImage+1]);
CenterImage->SetSource(BitmapImage);

//----------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);
if(iCurImage>5)
   BitmapImage->SetUriSource(strFilePath[iCurImage-6]);
else
  BitmapImage->SetUriSource(strFilePath[iCurImage+2]);
RightImage->SetSource(BitmapImage);

    break;
               case  ARG_SCROLL_RIGHT:
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_SCROLL--case  ARG_SCROLL_RIGHT!\r\n")));

//先弄一个动画效果
/*
        if (FAILED(hr=sboard->GetCurrentState(&ckstate)))           
   return hr;
        if (ckstate==XRClockState_Stopped)       
  {    
      if (FAILED(hr=sboard->Begin()))               
       return hr;       
  }       
  else       
  {           
      if (FAILED(hr=sboard->Stop()))               
       return hr;       
  }
*/

if(iCurImage==0)
    iCurImage=7;
else
    iCurImage--;

 IXRBitmapImagePtr    BitmapImage;
//------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);
BitmapImage->SetUriSource(strFilePath[iCurImage]);
LeftImage->SetSource(BitmapImage); 
//--------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);
if(iCurImage>6)
   BitmapImage->SetUriSource(strFilePath[7-iCurImage]);
else
   BitmapImage->SetUriSource(strFilePath[iCurImage+1]);
CenterImage->SetSource(BitmapImage);

//----------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);
if(iCurImage>5)
   BitmapImage->SetUriSource(strFilePath[iCurImage-6]);
else
  BitmapImage->SetUriSource(strFilePath[iCurImage+2]);
RightImage->SetSource(BitmapImage);

    break;
   }
//---------------------------------------
/*
            switch( GID_SCROLL_VELOCITY(GestureInfo.ullArguments) ) //判断滚动的速度
   {

      default:
    break;
             }
//---------------------------------------
            switch( GID_SCROLL_ANGLE(GestureInfo.ullArguments) ) //判断滚动的角度
   {
                default:
    break;
             }
*/
    break;

   case  GID_HOLD://保持
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_HOLD!\r\n")));
    break;

   case  GID_SELECT://单击选择
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_SELECT!\r\n")));
    break;
   case  GID_DOUBLESELECT://双击选择----要求两次单击的时间间隔不要太长
RETAILMSG(1,(TEXT("++++BtnEventHandler::Image_Gesture( )----case  GID_DOUBLESELECT!\r\n")));
    break;

   }
      return S_OK;   
    }

};
void InitPicturePathStr(void)
{

strFilePath[0]=L" Card\\XAMLPERF\\Bmps\\1.png";
strFilePath[1]=L"
Card\\XAMLPERF\\Bmps\\2.png";
strFilePath[2]=L"
Card\\XAMLPERF\\Bmps\\3.png";
strFilePath[3]=L"
Card\\XAMLPERF\\Bmps\\4.png";
strFilePath[4]=L"
Card\\XAMLPERF\\Bmps\\5.png";
strFilePath[5]=L"
Card\\XAMLPERF\\Bmps\\6.png";
strFilePath[6]=L"
Card\\XAMLPERF\\Bmps\\7.png";
strFilePath[7]=L"
Card\\XAMLPERF\\Bmps\\8.png";
}
//==========================================================================================================
INT WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPWSTR lpCmdLine,
    int nCmdShow
    )
{
   int    exitCode = -1;
   BOOL   bRet;
   HRESULT hr;
  
   bRet= XamlRuntimeInitialize();
   GetXRApplicationInstance(&pApplication);
   XRXamlSource SourceXaml;
   SourceXaml.SetFile(L" Card\\XAMLPERF\\Page_PictureBrowser.xaml");

//###############################################################
XRWindowCreateParams WindowParameters;
ZeroMemory(&WindowParameters, sizeof(WindowParameters));
WindowParameters.Style       = WS_POPUP;
WindowParameters.pTitle      = L"Title Name";
WindowParameters.Left        = 0;
WindowParameters.Top         = 0;
//################################################################
//IXRVisualHost* pVisualHost = NULL;

pApplication->CreateHostFromXaml(&SourceXaml, &WindowParameters, &pVisualHost);

if(!pVisualHost)
 RETAILMSG(1,(TEXT("++++the pVisualHost is NULL!\r\n")));
//################################################################
//找到根目录
IXRFrameworkElementPtr root;
if (FAILED(hr=pVisualHost->GetRootElement(&root)))      
return -1;

//################################################################

InitPicturePathStr( );
if (FAILED(hr=root->FindName(TEXT("LeftImage"), &LeftImage)))       
return -1;
if (FAILED(hr=root->FindName(TEXT("CenterImage"), &CenterImage)))       
return -1;
 
if (FAILED(hr=root->FindName(TEXT("RightImage"), &RightImage)))       
return -1;
//=================================================================

IXRStoryboardPtr sboard;
    if (FAILED(hr=root->FindName(TEXT("CenterImageStoryboard"), &sboard)))       
 
  return -1;

//BtnEventHandler handlerImage;
BtnEventHandler handlerImage(sboard);

/*
//注册鼠标左键单击事件
IXRDelegate* clickdelegateImage;
if (FAILED(hr=CreateDelegate(&handlerImage,&BtnEventHandler::Image_MouseLeftButtonDown,&clickdelegateImage)))       
  return -1;

if (FAILED(hr=CenterImage->AddMouseLeftButtonDownEventHandler(clickdelegateImage)))  //注册交互事件     
  return -1;
*/

//注册触摸屏手势事件
IXRDelegate* gesturedelegateImage;
if (FAILED(hr=CreateDelegate(&handlerImage,&BtnEventHandler::Image_Gesture,&gesturedelegateImage)))       
  return -1;

if (FAILED(hr=CenterImage->AddGestureEventHandler(gesturedelegateImage)))  //注册交互事件     
  return -1;
//=================================================================================
IXRBitmapImagePtr    BitmapImage;
//-------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);

BitmapImage->SetUriSource(strFilePath[0]);

LeftImage->SetSource(BitmapImage);
//--------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);

BitmapImage->SetUriSource(strFilePath[1]);
CenterImage->SetSource(BitmapImage);
//----------------------------------------------------------------------
pApplication->CreateObject(IID_IXRBitmapImage,&BitmapImage);

BitmapImage->SetUriSource(strFilePath[2]);

RightImage->SetSource(BitmapImage);

//###############################################################
pVisualHost->StartDialog(NULL);//显示对话框

UINT StartProcessCode = 0;

pApplication->StartProcessing(&StartProcessCode);
//##################################################################
//clickdelegateImage->Release();
gesturedelegateImage->Release();
   return exitCode;

}
//======================================================================================
//相关Page_PictureBrowser.xaml的内容
 xmlns=""
 xmlns:x="
"
 x:Class="SilverlightApplication_PictureBrowser.Page"
 Width="640" Height="480" xmlns:d="
" xmlns:mc="" mc:Ignorable="d">
 
 
  
   
  

 

 
 
 
 
 
 
 
  
   
    
    
    
    
   

  

 
 
 

//==================================================================================
 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xqhrs232/archive/2009/12/30/5105230.aspx
阅读(2299) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~