天天看點

項目裡的PDA程式

今天終于把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: 在此處添加消息處理程式代碼

}

效果

項目裡的PDA程式

4)程式運作效果圖:

項目裡的PDA程式
項目裡的PDA程式
項目裡的PDA程式
項目裡的PDA程式