今天终于把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头文件
- #pragma once
- // CDirDialog 对话框
- #include<string>
- #include<vector>
- #include "afx.h"
- using namespace std;
- class CDirDialog : public CDialog
- {
- DECLARE_DYNAMIC(CDirDialog)
- public :
- CDirDialog(int MAXFILENUM = 500,CWnd* pParent = NULL); // 标准构造函数
- virtual ~CDirDialog();
- // 对话框数据
- enum { IDD = IDD_LISTDLG };
- private :
- void initImageList();
- void clrImageList();
- void initDirTree();
- void clrFileList(CListCtrl *pList);
- void clrTreeItem(CTreeCtrl *pTree,HTREEITEM item);
- void initGUI();
- void clrGUI();
- vector<WIN32_FIND_DATA> findFile(string dir);
- void getDirFile(CListCtrl *pList,CTreeCtrl *pTree,HTREEITEM root);
- string getPath(CTreeCtrl *pTree,HTREEITEM item);
- void recurDir(string dir,CTreeCtrl *pTree,HTREEITEM root);
- public :
- vector<string> getSelFile(); //返回路径
- private :
- CListCtrl *pFileList;
- CTreeCtrl *pDirTree;
- CImageList *pImageList;
- vector<string>vecSelPath;
- vector<string>vecTmp;
- HWND hwndCB;
- int iMaxFileNum; //最大存储显示文件数目
- protected :
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- DECLARE_MESSAGE_MAP()
- public :
- virtual BOOL OnInitDialog();
- public :
- afx_msg void OnTvnSelchangedDir(NMHDR *pNMHDR, LRESULT *pResult);
- public :
- afx_msg void OnClose();
- public :
- afx_msg void OnBnClickedOk();
- public :
- afx_msg void OnBnClickedCancel();
- public :
- afx_msg void OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized);
- public :
- afx_msg void OnSettingChange( UINT uFlags, LPCTSTR lpszSection);
- };
DirDlg实现文件
- // DirDialog.cpp : 实现文件
- //
- #include "../stdafx.h"
- #include "../DlgSrc/PDACtrl.h"
- #include "DirDialog.h"
- // CDirDialog 对话框
- IMPLEMENT_DYNAMIC(CDirDialog, CDialog)
- CDirDialog::CDirDialog(int MAXFILENUM,CWnd* pParent /*=NULL*/ )
- : CDialog(CDirDialog::IDD, pParent)
- {
- this ->iMaxFileNum = MAXFILENUM;
- }
- CDirDialog::~CDirDialog()
- {
- }
- void CDirDialog::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CDirDialog, CDialog)
- ON_WM_CREATE()
- ON_NOTIFY(TVN_SELCHANGED, IDC_DIR, &CDirDialog::OnTvnSelchangedDir)
- ON_WM_CLOSE()
- ON_BN_CLICKED(IDC_OK, &CDirDialog::OnBnClickedOk)
- ON_BN_CLICKED(IDC_CANCEL, &CDirDialog::OnBnClickedCancel)
- ON_WM_ACTIVATE()
- ON_WM_SETTINGCHANGE()
- END_MESSAGE_MAP()
- // CDirDialog 消息处理程序
- void CDirDialog::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); //隐藏任务栏
- hwndCB = CommandBar_Create(::AfxGetInstanceHandle(),this ->GetSafeHwnd(),1);
- CommandBar_AddAdornments (hwndCB, WM_HELP, 0);
- }
- void CDirDialog::clrGUI()
- {
- ::CommandBar_Destroy(hwndCB);
- }
- void CDirDialog::initDirTree()
- {
- pFileList = (CListCtrl*)this ->GetDlgItem(IDC_FILELIST);
- pDirTree = (CTreeCtrl*)this ->GetDlgItem(IDC_DIR);
- pFileList->SetImageList(pImageList,LVSIL_SMALL);
- pDirTree->SetImageList(pImageList,TVSIL_NORMAL);
- int iIndex = 0;
- HTREEITEM root = pDirTree->InsertItem(L"." ,1,1);
- recurDir("." ,pDirTree,root);
- }
- void CDirDialog::initImageList()
- {
- pImageList = new CImageList();
- pImageList->Create(16,16,ILC_COLOR,2,6);
- CBitmap *pBmp = new CBitmap();
- pBmp->LoadBitmapW(IDB_FILEBMP);
- pImageList->Add(pBmp,(CBitmap*)NULL);
- delete pBmp;
- pBmp = new CBitmap();
- pBmp->LoadBitmapW(IDB_FOLDERBMP);
- pImageList->Add(pBmp,(CBitmap*)NULL);
- delete pBmp;
- }
- void CDirDialog::clrImageList()
- {
- delete pImageList;
- }
- BOOL CDirDialog::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // TODO: 在此添加额外的初始化
- initImageList();
- initDirTree();
- return TRUE; // return TRUE unless you set the focus to a control
- // 异常: OCX 属性页应返回 FALSE
- }
- vector<WIN32_FIND_DATA> CDirDialog::findFile(string dir)
- {
- vector<WIN32_FIND_DATA>vecRet;
- WIN32_FIND_DATA ffd;
- HANDLE hFind;
- USES_CONVERSION;
- hFind = FindFirstFile(A2W(dir.c_str()), &ffd);
- if (hFind == INVALID_HANDLE_VALUE)
- {
- //no file found
- }
- else
- {
- do {
- vecRet.push_back(ffd);
- }while (FindNextFile(hFind, &ffd));
- }
- FindClose(hFind);
- return vecRet;
- }
- void CDirDialog::recurDir(string dir,CTreeCtrl *pTree,HTREEITEM root)
- {
- USES_CONVERSION;
- vector<WIN32_FIND_DATA> vecFile;
- dir += "//*" ;
- vecFile = findFile(dir);
- if (vecFile.size() == 0)
- return ;
- else
- {
- int iIndex = 0;
- string strPath = getPath(pTree,root);
- for ( int i = 0;i<vecFile.size();i++)
- {
- if (vecFile[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- HTREEITEM cItem = pDirTree->InsertItem(vecFile[i].cFileName,1,1,root);
- string strCurDir = strPath + (string)W2A(vecFile[i].cFileName);
- recurDir(strCurDir,pTree,cItem);
- }
- }
- }
- }
- string CDirDialog::getPath(CTreeCtrl *pTree,HTREEITEM item)
- {
- USES_CONVERSION;
- string strPath("" );
- string curDir = (string)W2A(pDirTree->GetItemText(item));
- while ((item = pDirTree->GetParentItem(item)) != NULL)
- {
- CString cs = pDirTree->GetItemText(item);
- strPath = (string)W2A(cs) +"//" + strPath;
- }
- strPath = strPath + curDir + "//" ;
- return strPath; // 返回 路径 + / eg ./root/test
- }
- void CDirDialog::clrFileList(CListCtrl *pList)
- {
- pList->DeleteAllItems();
- }
- void CDirDialog::clrTreeItem(CTreeCtrl *pTree,HTREEITEM hDelItem)
- {
- HTREEITEM hChildItem = pTree->GetChildItem(hDelItem);
- while (hChildItem != NULL)
- {
- pTree->DeleteItem(hChildItem);
- hChildItem = pTree->GetChildItem(hDelItem);
- }
- }
- void CDirDialog::getDirFile(CListCtrl *pList,CTreeCtrl *pTree,HTREEITEM root)
- {
- clrFileList(pList);
- vecTmp.clear();
- vecTmp.reserve(iMaxFileNum);
- USES_CONVERSION;
- string strPath = getPath(pTree,root);
- string dir = strPath + "//*" ;
- vector<WIN32_FIND_DATA> vecFile;
- vecFile = findFile(dir);
- int iIndex = 0;
- int jL(0);
- for ( int i = 0;i<vecFile.size();i++)
- {
- if (!(vecFile[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
- {
- string strFile = strPath + (string)W2A(vecFile[i].cFileName);
- vecTmp.push_back(strFile);
- pFileList->InsertItem(jL,vecFile[i].cFileName,0);
- pFileList->SetItemData(jL,(DWORD )&vecTmp[jL]);
- jL++;
- }
- }
- }
- void CDirDialog::OnTvnSelchangedDir(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMTREEVIEW pNMTreeView = reinterpret_cast <LPNMTREEVIEW>(pNMHDR);
- // TODO: 在此添加控件通知处理程序代码
- HTREEITEM selItem = pDirTree->GetSelectedItem();
- if (selItem == NULL)
- return ;
- getDirFile(pFileList,pDirTree,selItem);
- *pResult = 0;
- }
- void CDirDialog::OnClose()
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- clrImageList();
- CDialog::OnClose();
- }
- vector<string> CDirDialog::getSelFile()
- {
- return vecSelPath;
- }
- void CDirDialog::OnBnClickedOk()
- {
- // TODO: 在此添加控件通知处理程序代码
- POSITION pos = pFileList->GetFirstSelectedItemPosition();
- int index(0);
- while (pos)
- {
- index = pFileList->GetNextSelectedItem(pos);
- string strPath = (*(string*)pFileList->GetItemData(index));
- vecSelPath.push_back(strPath);
- }
- EndDialog(IDOK);
- }
- void CDirDialog::OnBnClickedCancel()
- {
- // TODO: 在此添加控件通知处理程序代码
- EndDialog(IDCANCEL);
- }
- void CDirDialog::OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized)
- {
- CWnd::OnActivate(nState, pWndOther, bMinimized);
- SHFullScreen( this ->m_hWnd, SHFS_HIDETASKBAR);
- // TODO: 在此处添加消息处理程序代码
- }
- void CDirDialog::OnSettingChange( UINT uFlags, LPCTSTR lpszSection)
- {
- //CDialog::OnSettingChange(uFlags, lpszSection);
- // TODO: 在此处添加消息处理程序代码
- }
效果
4)程序运行效果图: