⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ir chat.cpp

📁 windows mobile 下的数据库操作的小程序。不是采用的ADO方式
💻 CPP
字号:
#include <windows.h>
#include <aygshell.h>
#include "resource.h"
#include <tpcshell.h>
#pragma comment(lib,"aygshell")
#include "Ir Chat.h"

const TCHAR szAppName[]=TEXT("DB Engine");
const char chzAppName[] = "IrComm";
HWND hMain;
HINSTANCE	hInst;
RECORDINFO1 ri[]={{TEXT("76"),TEXT("69"),TEXT("47"),TEXT("200504030412")},
					{TEXT("35"),TEXT("74"),TEXT("74"),TEXT("200502060926")},
					{TEXT("78"),TEXT("38"),TEXT("46"),TEXT("200502070334")}};

// About Database------------------
HANDLE g_hDB = INVALID_HANDLE_VALUE; // Handle to album database
CEOID g_oidDB = 0;					 // Object ID of the album database
CEGUID g_guidDB;					 // Guid for database volume
CENOTIFYREQUEST cenr;				 // Notify request structure.

int g_nLastSort = 0;				 // Last sort order used
CEDBASEINFOEX g_diex;				 // Sort order array

// These two variables represent a one-item cache for
// the list view control.
int g_nLastItem = -1;
LPBYTE g_pLastRecord = 0;
//---------------------------------

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
                   LPWSTR lpCmdLine,int nCmdShow){
//	MSG msg;
	HWND hWnd=0;
	hInst = hInstance;

//About Database---------------------
	// Create a guid for the database Ex functions that points
	// to the object store.
//	CREATE_SYSTEMGUID(&g_guidDB);
	memset (&cenr, 0, sizeof (cenr)); // Initialize the notify request.

	// Initialize database info structure.
	memset (&g_diex, 0, sizeof (g_diex));
	g_diex.wVersion = CEDBASEINFOEX_VERSION;
	g_diex.dwFlags = CEDB_VALIDNAME |CEDB_VALIDTYPE | CEDB_VALIDSORTSPEC;
	lstrcpy (g_diex.szDbaseName, TEXT ("\\MMMDB"));
	g_diex.dwDbaseType = 0;//????
	g_diex.wNumSortOrder = 1;//????

	// Create sort property array
	int i = 0;
	g_diex.rgSortSpecs[i].wVersion = SORTORDERSPECEX_VERSION;
	g_diex.rgSortSpecs[i].wNumProps = 2;//???
	g_diex.rgSortSpecs[i].rgPropID[0] = PID_DATE;
	g_diex.rgSortSpecs[i].rgdwFlags[0] = CEDB_SORT_DESCENDING;
	g_diex.rgSortSpecs[i].rgPropID[1] = PID_PULSE;
	g_diex.rgSortSpecs[i].rgdwFlags[1] = CEDB_SORT_DESCENDING;

//------------------------------------------------

	hWnd = FindWindow(NULL,szAppName);
	if(hWnd)
	{
		SetForegroundWindow((HWND)(((DWORD)hWnd)|0x01));    
		return 0;
    }

	DialogBoxParam(hInst,TEXT("DLG_MAIN"),NULL,Main_DlgProc,LPARAM(lpCmdLine));

	return 0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HWND InitDialog(HWND hWnd){
//	HANDLE hThread;
	SHINITDLGINFO shidi;
	memset(&shidi, 0, sizeof(shidi));
	shidi.dwMask=SHIDIM_FLAGS;
	shidi.dwFlags=SHIDIF_SIZEDLGFULLSCREEN;
	shidi.hDlg=hWnd;
	if(!SHInitDialog(&shidi))
        return FALSE;

	 return hWnd;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
BOOL ShowMenubar(HWND hwnd,int menuid){
	 SHMENUBARINFO mbi;
     memset(&mbi, 0, sizeof(SHMENUBARINFO));
     mbi.cbSize=sizeof(SHMENUBARINFO);
     mbi.hwndParent=hwnd;
     mbi.nToolBarId=menuid;
     mbi.hInstRes=hInst;
     if (!SHCreateMenuBar(&mbi))
		 return FALSE;
     return TRUE;
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
BOOL CALLBACK Main_DlgProc(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
	int rc,nCnt;
	RECT rect;
	HWND hwndChild, hwndLV = GetDlgItem (hWnd, ID_LISTV);

	switch(msg){
		case WM_INITDIALOG:
			InitDialog(hWnd);
			ShowMenubar(hWnd,MB_MAIN);
			// Open the album database. If one doesn't exist, create it.
			g_hDB = OpenCreateDB (hWnd, &nCnt);
			if (g_hDB == INVALID_HANDLE_VALUE) {		
				MessageBox (hWnd, TEXT ("Could not open database."), szAppName, MB_OK);
				DestroyWindow (hWnd);
				return 0;
			}
			// Create the list view control in right pane.
			SetRect (&rect, 9, 53, 170, 170);
			hwndChild = CreateLV (hWnd, &rect);
			if (!IsWindow (hwndChild)) {
				DestroyWindow (hWnd);
				return 0;
			}
			else
				SetFocus(hwndChild);
			insertrecord(hWnd);
			ReopenDatabase (hWnd, -1);
//			ListView_SetItemCount (hwndChild, nCnt);
			return TRUE;
		case WM_HOTKEY:
			SHSendBackToFocusWindow(msg,wp,lp);
			return TRUE;
		case WM_NOTIFY:
			DoNotifyMain (hWnd,msg,wp,lp);
			return TRUE;
		case WM_DBNOTIFICATION:
			DoDbNotifyMain (hWnd,msg,wp,lp);
			return TRUE;
		case WM_COMMAND:
			switch(LOWORD(wp)){
				case ID_CREATE:
					int  nCnt;
					OpenCreateDB (hWnd, &nCnt);
					return TRUE;
				case ID_DELETE:
					if (g_oidDB) 
					{
						CloseHandle (g_hDB);
						rc = CeDeleteDatabaseEx (&g_guidDB,g_oidDB);
						if (rc == 0) 
						{
							Add2List (hWnd, TEXT ("Couldn\'t delete database. rc=%d"),GetLastError());
							g_hDB = OpenDB (hWnd, NULL);// Open the database.
							return 0;
						}
						else
							Add2List (hWnd, TEXT ("Database has been deleted."));
						g_hDB = INVALID_HANDLE_VALUE;
						g_oidDB = 0;
						CeUnmountDBVol(&g_guidDB);
					}
					ListView_SetItemCount (GetDlgItem (hWnd, ID_LISTV), 0);

					return TRUE;
				case ID_LIST:
					int nSort;
					nSort = 0;
					ReopenDatabase (hWnd, nSort);
					return TRUE;
				case IDM_OK:
					PostQuitMessage(0);
					return TRUE;
			}
			return TRUE;
	}
	return FALSE;
}

void insertrecord(HWND hWnd){
	int i,j;
	PCEPROPVAL pcepv, pRecPtr;
	CEOID oid;
	TCHAR *pPtr;
	int nLen = sizeof (CEPROPVAL) * NUM_DB_PROPS + 
						   MAX_PULSE + MAX_HBP + 
						   MAX_LBP + MAX_DATE;
	HWND hwndLV = GetDlgItem (hWnd, ID_LISTV);
	for(j=0;j<3;j++){		
		pRecPtr = (PCEPROPVAL)LocalAlloc (LMEM_FIXED, nLen);
		pcepv = (PCEPROPVAL)LocalAlloc (LMEM_FIXED, nLen);
		pRecPtr = pcepv;
		pPtr = (TCHAR *)((LPBYTE)pRecPtr + (sizeof (CEPROPVAL) * NUM_DB_PROPS));
		memset (pRecPtr, 0, LocalSize (pRecPtr));
		pRecPtr->propid=PID_PULSE;
		pPtr=ri[j].pulse;
		pRecPtr->val.lpwstr=pPtr;
		pPtr+=lstrlen (pPtr)+1;
		pRecPtr++;

		pRecPtr->propid=PID_HBP;
		pPtr=ri[j].hbp;
		pRecPtr->val.lpwstr=pPtr;
		pPtr+=lstrlen (pPtr)+1;
		pRecPtr++;

		pRecPtr->propid=PID_LBP;
		pPtr=ri[j].lbp;
		pRecPtr->val.lpwstr=pPtr;
		pPtr+=lstrlen (pPtr)+1;
		pRecPtr++;

		pRecPtr->propid=PID_DATE;
		pPtr=ri[j].date;
		pRecPtr->val.lpwstr=pPtr;
		pPtr+=lstrlen (pPtr)+1;
		pRecPtr++;

		oid = CeWriteRecordProps(g_hDB, 0, NUM_DB_PROPS, pcepv);
		if (oid){
			Add2List (hWnd, TEXT ("A new record has been added."));
			ClearCache ();// Clear the lv cache.
			i = ListView_GetItemCount (hwndLV) + 1; // Increment list view
			ListView_SetItemCount (hwndLV, i);
			InvalidateRect (hwndLV, NULL, TRUE);
		}
	}
}

// Add2List - Add string to the report list box.
void Add2List (HWND hWnd, LPTSTR lpszFormat, ...) {
    int i, nBuf;
    TCHAR szBuffer[512];
   
    va_list args;
    va_start(args, lpszFormat);
   
    nBuf = _vstprintf(szBuffer, lpszFormat, args);
   
    i = SendDlgItemMessage (hWnd, IDLB_Received, LB_ADDSTRING, 0,
                            (LPARAM)(LPCTSTR)szBuffer);
    if (i != LB_ERR)
        SendDlgItemMessage (hWnd, IDLB_Received, LB_SETTOPINDEX, i,
                            (LPARAM)(LPCTSTR)szBuffer);
    va_end(args);
}

// MySetWindowText - Set window title to passed printf style string.
void MySetWindowText (HWND hWnd, LPTSTR lpszFormat, ...) {
    int nBuf;
    TCHAR szBuffer[512];
   
    va_list args;
    va_start(args, lpszFormat);
   
    nBuf =_vstprintf(szBuffer, lpszFormat, args);
   
    SetWindowText (hWnd, szBuffer);
    va_end(args);
}

//----------------------------------------------------------------------
// CreateLV - Creates the list view control
//
HWND CreateLV (HWND hWnd, RECT *prect) {
	HWND hwndLV;
	LVCOLUMN lvc;

	// Create album list window.  
	hwndLV = CreateWindowEx (0, WC_LISTVIEW, TEXT (""), 
						 WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_HSCROLL |
						 WS_TABSTOP |LVS_OWNERDATA | WS_BORDER | LVS_REPORT, 
						 prect->left, prect->top,
						 prect->right - prect->left,
						 prect->bottom - prect->top,
						 hWnd, (HMENU)ID_LISTV, 
						 hInst, NULL);

	// Add columns.
	if (hwndLV) {
		lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT | LVCF_SUBITEM;
		lvc.fmt = LVCFMT_LEFT;
		lvc.cx = 50;
		lvc.pszText = TEXT ("Pulse");
		lvc.iSubItem = 0;
		SendMessage (hwndLV, LVM_INSERTCOLUMN, 0, (LPARAM)&lvc);

		lvc.mask |= LVCF_SUBITEM;
		lvc.pszText = TEXT ("HBP");
		lvc.cx = 50;
		lvc.iSubItem = 1;
		SendMessage (hwndLV, LVM_INSERTCOLUMN, 1, (LPARAM)&lvc);

		lvc.mask |= LVCF_SUBITEM;
		lvc.pszText = TEXT ("LBP");
		lvc.cx = 50;
		lvc.iSubItem = 2;
		SendMessage (hwndLV, LVM_INSERTCOLUMN, 2, (LPARAM)&lvc);

		lvc.mask |= LVCF_SUBITEM;
		lvc.pszText = TEXT ("Date");
		lvc.cx = 50;
		lvc.iSubItem = 3;
		SendMessage (hwndLV, LVM_INSERTCOLUMN, 3, (LPARAM)&lvc);
	}
	return hwndLV;
}

// Get data from the database for the requested lv item.
//
int GetItemData (int nItem, PLVCACHEDATA pcd) {
	static WORD wProps;
	DWORD dwIndex;
	CEOID oid;
	PCEPROPVAL pRecord = NULL;
	DWORD dwRecSize;

	// Seek to the necessary record.
	oid = CeSeekDatabase (g_hDB, CEDB_SEEK_BEGINNING, nItem, &dwIndex);
	if (oid == 0) {
		Add2List (NULL, TEXT ("Datebase not found. rc=%d"), GetLastError());
		return 0;
	}
	// Read all properties for the record.	Have the system
	// allocate the buffer containing the data.
	oid = CeReadRecordProps (g_hDB, CEDB_ALLOWREALLOC, &wProps, NULL,
							(LPBYTE *)&pRecord, &dwRecSize);
	if (oid == 0) {
		Add2List (NULL, TEXT ("Datebase not read. rc=%d"), GetLastError());
		return 0;
	}
	// Free old record, and save the newly read one.
	if (g_pLastRecord)
		LocalFree (g_pLastRecord);
	g_nLastItem = nItem;
	g_pLastRecord = (LPBYTE)pRecord;
	// Copy the data from the record to the record structure.
	for (int i = 0; i < wProps; i++) {
		switch (pRecord->propid) {
		case PID_PULSE:
			lstrcpy (pcd->record.pulse, pRecord->val.lpwstr);
			break;
		case PID_LBP:
			lstrcpy (pcd->record.lbp, pRecord->val.lpwstr);
			break;
		case PID_HBP:
			lstrcpy (pcd->record.hbp, pRecord->val.lpwstr);
			break;
		case PID_DATE:
			lstrcpy (pcd->record.date, pRecord->val.lpwstr);
			break;
		}
		pRecord++;
	}
	return 1;
}

//----------------------------------------------------------------------
// OpenDB - Open database.

HANDLE OpenDB (HWND hWnd, LPTSTR lpszName) {
	// Reinitialize the notify request structure.
	cenr.dwSize = sizeof (cenr);
	cenr.hwnd = hWnd;
	cenr.dwFlags = CEDB_EXNOTIFICATION;

	if (lpszName)
		g_oidDB = 0;
	CeMountDBVol(&g_guidDB,TEXT ("\\Storage\\MMMvol.vol"),OPEN_ALWAYS);
	return CeOpenDatabaseEx2 (&g_guidDB, &g_oidDB, lpszName, 
							  &g_diex.rgSortSpecs[g_nLastSort], 
							  0, &cenr);
}

//----------------------------------------------------------------------
// OpenCreateDB - Open database, create if necessary.
HANDLE OpenCreateDB (HWND hWnd, int *pnRecords) {
	int rc;
	CEOIDINFO oidinfo;

	g_oidDB = 0;
	g_hDB = OpenDB (hWnd, TEXT ("\\MMMDB"));
	if (g_hDB == INVALID_HANDLE_VALUE) {
		rc = GetLastError();
		if (rc == ERROR_FILE_NOT_FOUND) {
			CeMountDBVol(&g_guidDB,TEXT ("\\Storage\\MMMvol.vol"),OPEN_ALWAYS);
			g_oidDB = CeCreateDatabaseEx2 (&g_guidDB, &g_diex);
			if (g_oidDB != 0)
				Add2List (hWnd, TEXT ("database has been created."));
			g_hDB = OpenDB (hWnd, TEXT ("\\MMMDB"));
			if (g_hDB != INVALID_HANDLE_VALUE){
				Add2List (hWnd, TEXT ("database has been opened."));
			}
		}
	} 
	else
		Add2List (hWnd, TEXT ("database has been opened."));
	CeOidGetInfo (g_oidDB, &oidinfo);
	*pnRecords = oidinfo.infDatabase.wNumRecords;
	return g_hDB;
}

// ReopenDatabase - Closes and reopens the database 
void ReopenDatabase (HWND hWnd, int nNewSort) {
	int nCnt; 

	if (nNewSort != -1)
		g_nLastSort = nNewSort;

	if (g_hDB)
		CloseHandle (g_hDB);
	CeUnmountDBVol(&g_guidDB);
	ClearCache ();						  // Clear the lv cache.

	g_hDB = OpenCreateDB (hWnd, &nCnt);

	ListView_SetItemCount (GetDlgItem (hWnd, ID_LISTV), nCnt);
	InvalidateRect (GetDlgItem (hWnd, ID_LISTV), NULL, 0);
	return;

}

// ClearCache - Clears the one-item cache for the list view control
void ClearCache (void) {

	if (g_pLastRecord)
		LocalFree (g_pLastRecord);
	g_pLastRecord = 0;			  
	g_nLastItem = -1;
	return;
}

// DoNotifyMain - Process WM_NOTIFY message for window.
LRESULT DoNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
					  LPARAM lParam) {
	int idItem;
	LPNMHDR pnmh;
	LPNMLISTVIEW pnmlv;
	NMLVDISPINFO *pLVdi;
	LVCACHEDATA data;
	HWND hwndLV;

	// Parse the parameters.
	idItem = (int) wParam;
	pnmh = (LPNMHDR)lParam;
	hwndLV = pnmh->hwndFrom;

	if (idItem == ID_LISTV) {
		pnmlv = (LPNMLISTVIEW)lParam;

		switch (pnmh->code) {
		case LVN_GETDISPINFO:
			pLVdi = (NMLVDISPINFO *)lParam;

			// Get a pointer to the data either from the cache
			// or from the actual database.
			GetItemData (pLVdi->item.iItem, &data);

			if (pLVdi->item.mask & LVIF_IMAGE) 
				pLVdi->item.iImage = 0;

			if (pLVdi->item.mask & LVIF_PARAM) 
				pLVdi->item.lParam = 0;

			if (pLVdi->item.mask & LVIF_STATE) 
				pLVdi->item.state = 0;
			if (pLVdi->item.mask & LVIF_TEXT) {
				switch (pLVdi->item.iSubItem) {
				case 0:
					lstrcpy (pLVdi->item.pszText, data.record.pulse);
					break;
				case 1:
					lstrcpy (pLVdi->item.pszText, data.record.hbp);
					break;
				case 2:
					lstrcpy (pLVdi->item.pszText, data.record.lbp);
					break;
				case 3:
					lstrcpy (pLVdi->item.pszText, data.record.date);
					break;
				}
			}
			break;

		// Ignore cache hinting for db example.
		case LVN_ODCACHEHINT:
			break;

		case LVN_ODFINDITEM:
			// We should do a reverse lookup here to see if
			// an item exists for the text passed.
			return -1;
		}
	}
	return 0;
}

// DoNotifyMain - Process DB_CEOID_xxx messages for window.
LRESULT DoDbNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
						LPARAM lParam) {
	CENOTIFICATION *pcen = (CENOTIFICATION *)lParam;
	switch (pcen->uType) {
	case DB_CEOID_CHANGED:
		InvalidateRect (GetDlgItem (hWnd, ID_LISTV), NULL, TRUE);
		break;
	case DB_CEOID_CREATED:
		ReopenDatabase (hWnd, -1);
		break;
	case DB_CEOID_RECORD_DELETED:
		ReopenDatabase (hWnd, -1);
		break;
	}
	CeFreeNotification (&cenr, pcen);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -