项目里的PDA程序(zhuan)

今天终于把pda版本重新做好,最近好多东西要烦,烦这烦那,没时间做这个东西,今天总算弄好了,可以安心开始看书了!
这个pda版本把早段时间做的跨平台socket库用上,所以基本上底层网络基本不用怎么做,直接用就好了,然后再加上原来那些上层代码复用,所以功能很快就实现,主要可能是一些Pocket pc不兼容的函数或者控件需要考虑,记录一下:

1)如果忘记定义析构函数,有可能导致程序在pocket pc上会无端端退出。
2)代码记录:

添加菜单代码:
void CPDACtrlDlg::initMenu()
{
 hwndCB = CommandBar_Create(::AfxGetInstanceHandle(),this->GetSafeHwnd(),1);
 CommandBar_InsertMenubar (hwndCB,::AfxGetInstanceHandle(), IDR_MAIN_MENU, 0);
 CommandBar_AddAdornments (hwndCB, WM_HELP , 0);
}
全屏设置代码:
void CPDACtrlDlg::initGUI()
{
 RECT rc;
 GetWindowRect(&rc);
 rc.top-=26;
 MoveWindow(rc.left,rc.top,rc.right,rc.bottom,FALSE); //上移26像素
 SHFullScreen(this->m_hWnd,SHFS_HIDETASKBAR); //隐藏任务栏

 initMenu();

}

菜单check项修改:
HMENU hmenu = ::CommandBar_GetMenu(hwndCB,0);

 UINT state = ::GetMenuState(hmenu,ID_PUSHPLAY,MF_BYCOMMAND);
 ASSERT(state != 0xFFFFFFFF);

 if (state & MF_CHECKED)
 {
  pBaseServer->unloadService(pCallPlayResp->getRespType());
  CheckMenuItem(hmenu,ID_PUSHPLAY, MF_UNCHECKED | MF_BYCOMMAND);
 } 
 else
 {
  pBaseServer->loadService(pCallPlayResp);
  CheckMenuItem(hmenu,ID_PUSHPLAY, MF_CHECKED | MF_BYCOMMAND);
 }


3)wince选择一个或多个文件,利用CFileDialog试过不行,所以干脆自己做了个简单的,利用CTreeCtrl
和CListCtrl一起,做了一个像wince中的资源管理器的东西,代码通过递归所有文件夹目录,在CListCtrl中
显示当前目录下的文件(非文件夹),可以从CListCtrl中选择多个文件,记录文件路径
代码:

DirDlg头文件

  1. #pragma once   
  2.   
  3.   
  4. // CDirDialog 对话框   
  5. #include<string>   
  6. #include<vector>   
  7. #include "afx.h"   
  8.   
  9. using   namespace  std;  
  10.   
  11. class  CDirDialog :  public  CDialog  
  12. {  
  13.     DECLARE_DYNAMIC(CDirDialog)  
  14.   
  15. public :  
  16.     CDirDialog(int  MAXFILENUM = 500,CWnd* pParent = NULL);    // 标准构造函数   
  17.     virtual  ~CDirDialog();  
  18.   
  19. // 对话框数据   
  20.     enum  { IDD = IDD_LISTDLG };  
  21. private :  
  22.     void  initImageList();  
  23.     void  clrImageList();  
  24.     void  initDirTree();  
  25.     void  clrFileList(CListCtrl *pList);  
  26.     void  clrTreeItem(CTreeCtrl *pTree,HTREEITEM item);  
  27.   
  28.     void  initGUI();  
  29.     void  clrGUI();  
  30.   
  31.     vector<WIN32_FIND_DATA> findFile(string dir);  
  32.     void  getDirFile(CListCtrl *pList,CTreeCtrl *pTree,HTREEITEM root);  
  33.     string getPath(CTreeCtrl *pTree,HTREEITEM item);  
  34.     void  recurDir(string dir,CTreeCtrl *pTree,HTREEITEM root);  
  35. public :  
  36.     vector<string> getSelFile();                  //返回路径   
  37.   
  38. private :  
  39.     CListCtrl *pFileList;  
  40.     CTreeCtrl *pDirTree;  
  41.     CImageList *pImageList;  
  42.     vector<string>vecSelPath;  
  43.     vector<string>vecTmp;  
  44.     HWND  hwndCB;  
  45.   
  46.     int  iMaxFileNum;                 //最大存储显示文件数目   
  47.   
  48. protected :  
  49.     virtual   void  DoDataExchange(CDataExchange* pDX);     // DDX/DDV 支持   
  50.     DECLARE_MESSAGE_MAP()  
  51. public :  
  52.     virtual   BOOL  OnInitDialog();  
  53. public :  
  54.     afx_msg void  OnTvnSelchangedDir(NMHDR *pNMHDR,  LRESULT  *pResult);  
  55. public :  
  56.     afx_msg void  OnClose();  
  57. public :  
  58.     afx_msg void  OnBnClickedOk();  
  59. public :  
  60.     afx_msg void  OnBnClickedCancel();  
  61. public :  
  62.     afx_msg void  OnActivate( UINT  nState, CWnd* pWndOther,  BOOL  bMinimized);  
  63. public :  
  64.     afx_msg void  OnSettingChange( UINT  uFlags,  LPCTSTR  lpszSection);  
  65. };  

DirDlg实现文件

  1. // DirDialog.cpp : 实现文件   
  2. //   
  3.   
  4. #include "../stdafx.h"   
  5. #include "../DlgSrc/PDACtrl.h"   
  6. #include "DirDialog.h"   
  7.   
  8.   
  9. // CDirDialog 对话框   
  10.   
  11. IMPLEMENT_DYNAMIC(CDirDialog, CDialog)  
  12.   
  13. CDirDialog::CDirDialog(int  MAXFILENUM,CWnd* pParent  /*=NULL*/ )  
  14.     : CDialog(CDirDialog::IDD, pParent)  
  15. {  
  16.     this ->iMaxFileNum = MAXFILENUM;  
  17. }  
  18.   
  19. CDirDialog::~CDirDialog()  
  20. {  
  21. }  
  22.   
  23. void  CDirDialog::DoDataExchange(CDataExchange* pDX)  
  24. {  
  25.     CDialog::DoDataExchange(pDX);  
  26. }  
  27.   
  28.   
  29. BEGIN_MESSAGE_MAP(CDirDialog, CDialog)  
  30.     ON_WM_CREATE()  
  31.     ON_NOTIFY(TVN_SELCHANGED, IDC_DIR, &CDirDialog::OnTvnSelchangedDir)  
  32.     ON_WM_CLOSE()  
  33.     ON_BN_CLICKED(IDC_OK, &CDirDialog::OnBnClickedOk)  
  34.     ON_BN_CLICKED(IDC_CANCEL, &CDirDialog::OnBnClickedCancel)  
  35.     ON_WM_ACTIVATE()  
  36.     ON_WM_SETTINGCHANGE()  
  37. END_MESSAGE_MAP()  
  38.   
  39.   
  40. // CDirDialog 消息处理程序   
  41. void  CDirDialog::initGUI()  
  42. {  
  43.     RECT rc;  
  44.     GetWindowRect(&rc);  
  45.     rc.top-=26;  
  46.     MoveWindow(rc.left,rc.top,rc.right,rc.bottom,FALSE); //上移26像素   
  47.     SHFullScreen(this ->m_hWnd,SHFS_HIDETASKBAR);  //隐藏任务栏   
  48.   
  49.     hwndCB = CommandBar_Create(::AfxGetInstanceHandle(),this ->GetSafeHwnd(),1);  
  50.     CommandBar_AddAdornments (hwndCB, WM_HELP, 0);  
  51. }  
  52.   
  53. void  CDirDialog::clrGUI()  
  54. {  
  55.     ::CommandBar_Destroy(hwndCB);  
  56. }  
  57.   
  58. void  CDirDialog::initDirTree()  
  59. {  
  60.     pFileList = (CListCtrl*)this ->GetDlgItem(IDC_FILELIST);  
  61.     pDirTree = (CTreeCtrl*)this ->GetDlgItem(IDC_DIR);  
  62.     pFileList->SetImageList(pImageList,LVSIL_SMALL);  
  63.     pDirTree->SetImageList(pImageList,TVSIL_NORMAL);  
  64.   
  65.     int  iIndex = 0;  
  66.     HTREEITEM root = pDirTree->InsertItem(L"." ,1,1);  
  67.     recurDir("." ,pDirTree,root);  
  68. }  
  69. void  CDirDialog::initImageList()  
  70. {  
  71.     pImageList = new  CImageList();  
  72.     pImageList->Create(16,16,ILC_COLOR,2,6);  
  73.     CBitmap *pBmp = new  CBitmap();  
  74.     pBmp->LoadBitmapW(IDB_FILEBMP);  
  75.     pImageList->Add(pBmp,(CBitmap*)NULL);  
  76.     delete  pBmp;  
  77.     pBmp = new  CBitmap();  
  78.     pBmp->LoadBitmapW(IDB_FOLDERBMP);  
  79.     pImageList->Add(pBmp,(CBitmap*)NULL);  
  80.     delete  pBmp;  
  81. }  
  82.   
  83. void  CDirDialog::clrImageList()  
  84. {  
  85.     delete  pImageList;  
  86. }  
  87. BOOL  CDirDialog::OnInitDialog()  
  88. {  
  89.     CDialog::OnInitDialog();  
  90.   
  91.     // TODO:  在此添加额外的初始化   
  92.     initImageList();  
  93.     initDirTree();  
  94.   
  95.     return  TRUE;   // return TRUE unless you set the focus to a control   
  96.     // 异常: OCX 属性页应返回 FALSE   
  97. }  
  98. vector<WIN32_FIND_DATA> CDirDialog::findFile(string dir)  
  99. {  
  100.     vector<WIN32_FIND_DATA>vecRet;  
  101.   
  102.     WIN32_FIND_DATA ffd;  
  103.     HANDLE  hFind;  
  104.     USES_CONVERSION;  
  105.   
  106.     hFind = FindFirstFile(A2W(dir.c_str()), &ffd);  
  107.     if  (hFind == INVALID_HANDLE_VALUE)   
  108.     {  
  109.         //no file found   
  110.     }   
  111.     else    
  112.     {  
  113.         do {  
  114.             vecRet.push_back(ffd);  
  115.   
  116.         }while (FindNextFile(hFind, &ffd));  
  117.     }  
  118.     FindClose(hFind);  
  119.     return  vecRet;  
  120. }  
  121. void  CDirDialog::recurDir(string dir,CTreeCtrl *pTree,HTREEITEM root)  
  122. {  
  123.     USES_CONVERSION;  
  124.     vector<WIN32_FIND_DATA> vecFile;  
  125.     dir += "//*" ;  
  126.     vecFile = findFile(dir);  
  127.   
  128.     if (vecFile.size() == 0)  
  129.         return ;  
  130.     else   
  131.     {  
  132.         int  iIndex = 0;  
  133.         string strPath = getPath(pTree,root);  
  134.   
  135.         for ( int  i = 0;i<vecFile.size();i++)  
  136.         {  
  137.             if (vecFile[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  
  138.             {  
  139.                 HTREEITEM cItem = pDirTree->InsertItem(vecFile[i].cFileName,1,1,root);  
  140.                 string strCurDir = strPath + (string)W2A(vecFile[i].cFileName);  
  141.                 recurDir(strCurDir,pTree,cItem);  
  142.             }     
  143.         }  
  144.     }  
  145. }  
  146.   
  147. string CDirDialog::getPath(CTreeCtrl *pTree,HTREEITEM item)  
  148. {  
  149.     USES_CONVERSION;  
  150.     string strPath("" );  
  151.     string curDir = (string)W2A(pDirTree->GetItemText(item));  
  152.   
  153.     while ((item = pDirTree->GetParentItem(item)) != NULL)  
  154.     {  
  155.         CString cs = pDirTree->GetItemText(item);  
  156.         strPath = (string)W2A(cs) +"//"  + strPath;  
  157.   
  158.     }  
  159.     strPath = strPath + curDir + "//" ;  
  160.     return  strPath;                              // 返回 路径 + / eg ./root/test   
  161. }  
  162.   
  163. void  CDirDialog::clrFileList(CListCtrl *pList)  
  164. {  
  165.     pList->DeleteAllItems();  
  166. }  
  167.   
  168. void  CDirDialog::clrTreeItem(CTreeCtrl *pTree,HTREEITEM hDelItem)  
  169. {  
  170.     HTREEITEM hChildItem = pTree->GetChildItem(hDelItem);  
  171.     while  (hChildItem != NULL)  
  172.     {  
  173.         pTree->DeleteItem(hChildItem);  
  174.         hChildItem = pTree->GetChildItem(hDelItem);  
  175.     }  
  176. }  
  177.   
  178. void  CDirDialog::getDirFile(CListCtrl *pList,CTreeCtrl *pTree,HTREEITEM root)  
  179. {  
  180.     clrFileList(pList);  
  181.     vecTmp.clear();  
  182.     vecTmp.reserve(iMaxFileNum);  
  183.     USES_CONVERSION;  
  184.   
  185.     string strPath = getPath(pTree,root);  
  186.     string dir = strPath + "//*" ;  
  187.   
  188.     vector<WIN32_FIND_DATA> vecFile;  
  189.     vecFile = findFile(dir);  
  190.   
  191.     int  iIndex = 0;  
  192.     int  jL(0);  
  193.   
  194.     for ( int  i = 0;i<vecFile.size();i++)  
  195.     {  
  196.         if (!(vecFile[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))  
  197.         {  
  198.             string strFile = strPath + (string)W2A(vecFile[i].cFileName);  
  199.             vecTmp.push_back(strFile);  
  200.             pFileList->InsertItem(jL,vecFile[i].cFileName,0);  
  201.             pFileList->SetItemData(jL,(DWORD )&vecTmp[jL]);  
  202.             jL++;  
  203.         }  
  204.     }  
  205. }  
  206.   
  207. void  CDirDialog::OnTvnSelchangedDir(NMHDR *pNMHDR,  LRESULT  *pResult)  
  208. {  
  209.     LPNMTREEVIEW pNMTreeView = reinterpret_cast <LPNMTREEVIEW>(pNMHDR);  
  210.     // TODO: 在此添加控件通知处理程序代码   
  211.     HTREEITEM selItem = pDirTree->GetSelectedItem();  
  212.     if (selItem == NULL)  
  213.         return ;  
  214.   
  215.     getDirFile(pFileList,pDirTree,selItem);  
  216.   
  217.   
  218.     *pResult = 0;  
  219. }  
  220.   
  221. void  CDirDialog::OnClose()  
  222. {  
  223.     // TODO: 在此添加消息处理程序代码和/或调用默认值   
  224.     clrImageList();  
  225.   
  226.     CDialog::OnClose();  
  227. }  
  228.   
  229.   
  230. vector<string> CDirDialog::getSelFile()  
  231. {  
  232.     return  vecSelPath;  
  233. }  
  234.   
  235.   
  236. void  CDirDialog::OnBnClickedOk()  
  237. {  
  238.     // TODO: 在此添加控件通知处理程序代码   
  239.     POSITION pos = pFileList->GetFirstSelectedItemPosition();  
  240.   
  241.     int  index(0);  
  242.     while (pos)  
  243.     {  
  244.         index = pFileList->GetNextSelectedItem(pos);  
  245.         string strPath = (*(string*)pFileList->GetItemData(index));  
  246.         vecSelPath.push_back(strPath);  
  247.     }  
  248.   
  249.     EndDialog(IDOK);  
  250. }  
  251.   
  252. void  CDirDialog::OnBnClickedCancel()  
  253. {  
  254.     // TODO: 在此添加控件通知处理程序代码   
  255.     EndDialog(IDCANCEL);  
  256. }  
  257.   
  258. void  CDirDialog::OnActivate( UINT  nState, CWnd* pWndOther,  BOOL  bMinimized)  
  259. {  
  260.     CWnd::OnActivate(nState, pWndOther, bMinimized);  
  261.     SHFullScreen( this ->m_hWnd, SHFS_HIDETASKBAR);  
  262.     // TODO: 在此处添加消息处理程序代码   
  263. }  
  264.   
  265. void  CDirDialog::OnSettingChange( UINT  uFlags,  LPCTSTR  lpszSection)  
  266. {  
  267.     //CDialog::OnSettingChange(uFlags, lpszSection);   
  268.   
  269.     // TODO: 在此处添加消息处理程序代码   
  270. }  

效果

4)程序运行效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值