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

📄 objectstoreit.cpp

📁 《Windows CE 权威指南》(作者:(美)CHRIS MUENCH
💻 CPP
字号:
// ObjectStoreIt.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "ObjectStoreIt.h"
#include <commctrl.h>


// <BOOK_ADDON Chapter 9.2.2> ******************************************
#include <objidl.h>
#include <rapi.h>
// </BOOK_ADDON Chapter 9.2.2> ******************************************


#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE			hInst;			// The current instance
HWND				hwndCB;			// The command bar handle

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass	(HINSTANCE hInstance, LPTSTR szWindowClass);
BOOL				InitInstance	(HINSTANCE, int);
LRESULT CALLBACK	WndProc			(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About			(HWND, UINT, WPARAM, LPARAM);

// <BOOK_ADDON Chapter 8.3.1> ******************************************
void AccessObjectStore (void)
{
// Variables for Step1 - Open/Create a database
HANDLE hDataBase;          
CEOID	CeOid=0;
DWORD	dwErr;

// Variable for Step2 - Writing a record
CEPROPVAL	NewRecProps[4],*Fields;
SYSTEMTIME  SystemTime;
FILETIME    FileTime;
WCHAR       *lpStr;	  // <BOOK_ADDON Chapter 9.2.2/> *****       	
LPVOID      lpBlob;

// Variables for Step3 - Reading a record
DWORD		dwIndex;
LPBYTE	pData=NULL;
CEPROPID	PropId=1;
WORD		cPropID = 1;
DWORD		cbData = 0;

	// Step1 - Open/Create a database
	// *********************************************
	CeRapiInit();												// <BOOK_ADDON Chapter 9.2.2/> *****       	
	hDataBase = CeOpenDatabase(&CeOid,L"Book922",0,0,NULL);		// <BOOK_ADDON Chapter 9.2.2/> *****       	
	if (hDataBase == INVALID_HANDLE_VALUE)
	{
		dwErr = GetLastError ();
		if (dwErr == ERROR_NOT_ENOUGH_MEMORY)
		{  }  // Handle not enough memory  
		else
		{	// Database does not exist - create it.
			CeOid = CeCreateDatabase(L"Book922", 0,0,NULL);		// <BOOK_ADDON Chapter 9.2.2/> *****       	
			//wprintf(TEXT(" : New Database Created\n\r"));
			if (CeOid != NULL)
				hDataBase = CeOpenDatabase(&CeOid,L"Book922",0, 0,NULL); // <BOOK_ADDON Chapter 9.2.2/> *****       	
			else
				return;		// Handle Open Failure
		}
	}
	//wprintf(TEXT("Step1 Complete: Database Opened\n\r"));
	
	// Step2 - Writing a record
	// *********************************************
	NewRecProps[0].propid = MAKELONG( CEVT_I4, 0);
	NewRecProps[0].wLenData = 0;
	NewRecProps[0].wFlags = 0;
	NewRecProps[0].val.lVal = 0xffffffff;

	GetSystemTime(&SystemTime);
	SystemTimeToFileTime(&SystemTime,&FileTime);
	NewRecProps[1].propid = MAKELONG( CEVT_FILETIME, 1);
	NewRecProps[1].wLenData = 0;
	NewRecProps[1].wFlags = 0;
	NewRecProps[1].val.filetime = FileTime;
	
	lpStr = (WCHAR*)LocalAlloc(LMEM_FIXED, 50 * sizeof(WCHAR));	// <BOOK_ADDON Chapter 9.2.2/> *****       	
	if ( lpStr )
		wcscpy(lpStr, L"This is a string property");			// <BOOK_ADDON Chapter 9.2.2/> *****       	
	NewRecProps[2].propid = MAKELONG( CEVT_LPWSTR, 2);
	NewRecProps[2].wLenData = 0;
	NewRecProps[2].wFlags = 0;
	NewRecProps[2].val.lpwstr = lpStr;

	lpBlob = (LPVOID)LocalAlloc(LMEM_FIXED, 50  * sizeof(WCHAR));  // <BOOK_ADDON Chapter 9.2.2/> *****       	
	if ( lpBlob )
		wcscpy((WCHAR*)lpBlob,L"This is a blob property");		// <BOOK_ADDON Chapter 9.2.2/> *****       	
	NewRecProps[3].propid = MAKELONG( CEVT_BLOB, 3);
	NewRecProps[3].wLenData = 0;
	NewRecProps[3].wFlags = 0;
	NewRecProps[3].val.blob.dwCount = 50  * sizeof(WCHAR);		// <BOOK_ADDON Chapter 9.2.2/> *****       	
	NewRecProps[3].val.blob.lpb  = (LPBYTE)lpBlob;

    if(!CeWriteRecordProps(hDataBase, 0, 4, NewRecProps) )
	{ return; }		// Handle Write Failure 
	//wprintf(TEXT("Step2 Complete: New Record Written\n\r"));

	if( lpBlob )	LocalFree(lpBlob);
	if( lpStr )		LocalFree(lpStr);

	// Step3 - Reading a record
	// *********************************************
	if (!CeSeekDatabase(hDataBase, CEDB_SEEK_BEGINNING, 0, &dwIndex) )
	{ return; }		// Handle Seek Failure

	if(!CeReadRecordProps(hDataBase, CEDB_ALLOWREALLOC,&cPropID,NULL, &pData,&cbData)) // <BOOK_ADDON Chapter 9.2.2/> *****       	
	{ return; }		// Handle Read Failure
	
	Fields=(CEPROPVAL*)pData;
	//wprintf(TEXT("Step3 Complete: Read %d records\n\r"),cPropID);
	//for (int i=0;i<cPropID;i++)
		//wprintf(TEXT(" : Prop/Field %d of Type %d\n\r"),i+1,LOWORD(Fields[i].propid));

	// Step4 - Closing the Database
	CeCloseHandle(hDataBase);									// <BOOK_ADDON Chapter 9.2.2/> *****       	
	CeRapiUninit();												// <BOOK_ADDON Chapter 9.2.2/> *****       	
}
// </BOOK_ADDON Chapter 8.3.1> ******************************************

int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
	MSG msg;
	HACCEL hAccelTable;

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_OBJECTSTOREIT);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS	wc;

    wc.style			= CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc		= (WNDPROC) WndProc;
    wc.cbClsExtra		= 0;
    wc.cbWndExtra		= 0;
    wc.hInstance		= hInstance;
    wc.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_OBJECTSTOREIT));
    wc.hCursor			= 0;
    wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName		= 0;
    wc.lpszClassName	= szWindowClass;

	return RegisterClass(&wc);
}

//
//  FUNCTION: InitInstance(HANDLE, int)
//
//  PURPOSE: Saves instance handle and creates main window
//
//  COMMENTS:
//
//    In this function, we save the instance handle in a global variable and
//    create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND	hWnd;
	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text
	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name

	hInst = hInstance;		// Store instance handle in our global variable
	// Initialize global strings
	LoadString(hInstance, IDC_OBJECTSTOREIT, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance, szWindowClass);

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{	
		return FALSE;
	}
	
// <BOOK_ADDON Chapter 8.3.1> ******************************************
	AccessObjectStore();
// </BOOK_ADDON Chapter 8.3.1> ******************************************

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
#ifdef UNDER_CE // <BOOK_ADDON Chapter 9.2.2/> *****
	if (hwndCB)
		CommandBar_Show(hwndCB, TRUE);
#endif  // <BOOK_ADDON Chapter 9.2.2/> *****
	return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	TCHAR szHello[MAX_LOADSTRING];

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_HELP_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_FILE_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_CREATE:
#ifdef UNDER_CE // <BOOK_ADDON Chapter 9.2.2/> *****
			hwndCB = CommandBar_Create(hInst, hWnd, 1);			
			CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
			CommandBar_AddAdornments(hwndCB, 0, 0);
#endif // <BOOK_ADDON Chapter 9.2.2/> *****
			break;
		case WM_PAINT:
			RECT rt;
			hdc = BeginPaint(hWnd, &ps);
			GetClientRect(hWnd, &rt);
			LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
			DrawText(hdc, szHello, lstrlen(szHello), &rt, 
				DT_SINGLELINE | DT_VCENTER | DT_CENTER);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
#ifdef UNDER_CE // <BOOK_ADDON Chapter 9.2.2/> *****
			CommandBar_Destroy(hwndCB);
#endif // <BOOK_ADDON Chapter 9.2.2/> *****
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	RECT rt, rt1;
	int DlgWidth, DlgHeight;	// dialog width and height in pixel units
	int NewPosX, NewPosY;

	switch (message)
	{
		case WM_INITDIALOG:
			// trying to center the About dialog
			if (GetWindowRect(hDlg, &rt1)) {
				GetClientRect(GetParent(hDlg), &rt);
				DlgWidth	= rt1.right - rt1.left;
				DlgHeight	= rt1.bottom - rt1.top ;
				NewPosX		= (rt.right - rt.left - DlgWidth)/2;
				NewPosY		= (rt.bottom - rt.top - DlgHeight)/2;
				
				// if the About box is larger than the physical screen 
				if (NewPosX < 0) NewPosX = 0;
				if (NewPosY < 0) NewPosY = 0;
				SetWindowPos(hDlg, 0, NewPosX, NewPosY,
					0, 0, SWP_NOZORDER | SWP_NOSIZE);
			}
			return TRUE;

		case WM_COMMAND:
			if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

⌨️ 快捷键说明

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