?? albumdb.c
字號:
//======================================================================
// AlbumDB - A Windows CE database
//
// Written for the book Programming Windows CE
// Copyright (C) 1998 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
#include <windowsx.h> // For Window Controls macros
#include <commctrl.h> // Command bar includes
#include "AlbumDB.h" // Program-specific stuff
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("AlbumDB");
HINSTANCE hInst; // Program instance handle
HANDLE g_hDB = 0; // Handle to album database
CEOID g_oidDB = 0; // Handle to album database
INT g_nLastSort = PID_NAME; // Last sort order used
// These two variables represent a one item cache for
// the list view control.
int g_nLastItem = -1;
LPBYTE g_pLastRecord = 0;
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_SIZE, DoSizeMain,
WM_COMMAND, DoCommandMain,
WM_NOTIFY, DoNotifyMain,
WM_DESTROY, DoDestroyMain,
DB_CEOID_CHANGED, DoDbNotifyMain,
DB_CEOID_CREATED, DoDbNotifyMain,
DB_CEOID_RECORD_DELETED, DoDbNotifyMain,
};
// Command message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
IDM_DELDB, DoMainCommandDelDB,
IDM_EXIT, DoMainCommandExit,
IDM_NEW, DoMainCommandNew,
IDM_EDIT, DoMainCommandEdit,
IDM_DELETE, DoMainCommandDelete,
IDM_SORTNAME, DoMainCommandSort,
IDM_SORTARTIST, DoMainCommandSort,
IDM_SORTCATEGORY, DoMainCommandSort,
IDM_ABOUT, DoMainCommandAbout,
};
// Album category strings; must be alphabetical.
const TCHAR *pszCategories[] = {TEXT ("Classical"), TEXT ("Country"),
TEXT ("New Age"), TEXT ("Rock")};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
HWND hwndMain;
MSG msg;
int rc = 0;
// Initialize application.
rc = InitApp (hInstance);
if (rc) return rc;
// Initialize this instance.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0)
return 0x10;
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitApp - Application initialization
//
int InitApp (HINSTANCE hInstance) {
WNDCLASS wc;
INITCOMMONCONTROLSEX icex;
// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = NULL; // Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 1;
// Load the command bar common control class.
icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
icex.dwICC = ICC_BAR_CLASSES | ICC_TREEVIEW_CLASSES |
ICC_LISTVIEW_CLASSES;
InitCommonControlsEx (&icex);
return 0;
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow){
HWND hWnd;
// Save program instance handle in global variable.
hInst = hInstance;
// Create main window.
hWnd = CreateWindow (szAppName, TEXT ("AlbumDB"), WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
// Close the opened database.
if (g_hDB)
CloseHandle (g_hDB);
// Free the last db query if saved.
ClearCache ();
return nDefRC;
}
//======================================================================
// Message handling procedures for MainWindow
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HWND hwndCB, hwndChild;
INT nHeight, nCnt;
RECT rect;
LPCREATESTRUCT lpcs;
// Convert lParam into pointer to create structure.
lpcs = (LPCREATESTRUCT) lParam;
// Create a minimal command bar that only has a menu and an
// exit button.
hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
// Insert the menu.
CommandBar_InsertMenubar (hwndCB, hInst, ID_MENU, 0);
// Add exit button to command bar.
CommandBar_AddAdornments (hwndCB, 0, 0);
nHeight = CommandBar_Height (hwndCB);
// 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, 0, nHeight, lpcs->cx, lpcs->cy - nHeight);
hwndChild = CreateLV (hWnd, &rect);
// Destroy frame if window not created.
if (!IsWindow (hwndChild)) {
DestroyWindow (hWnd);
return 0;
}
ListView_SetItemCount (hwndChild, nCnt);
return 0;
}
//----------------------------------------------------------------------
// DoSizeMain - Process WM_SIZE message for window.
//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam){
HWND hwndLV;
RECT rect;
hwndLV = GetDlgItem (hWnd, ID_LISTV);
// Adjust the size of the client rect to take into account
// the command bar height.
GetClientRect (hWnd, &rect);
rect.top += CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR));
SetWindowPos (hwndLV, NULL, rect.left, rect.top,
(rect.right - rect.left), rect.bottom - rect.top,
SWP_NOZORDER);
return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
WORD idItem, wNotifyCode;
HWND hwndCtl;
INT i;
// Parse the parameters.
idItem = (WORD) LOWORD (wParam);
wNotifyCode = (WORD) HIWORD (wParam);
hwndCtl = (HWND) lParam;
// Call routine to handle control message.
for (i = 0; i < dim(MainCommandItems); i++) {
if (idItem == MainCommandItems[i].Code)
return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
wNotifyCode);
}
return 0;
}
//----------------------------------------------------------------------
// DoNotifyMain - Process DB_CEOID_xxx messages for window.
//
LRESULT DoDbNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
switch (wMsg) {
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;
}
return 0;
}
//----------------------------------------------------------------------
// DoNotifyMain - Process WM_NOTIFY message for window.
//
LRESULT DoNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
int idItem, i;
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.Album.szName);
break;
case 1:
lstrcpy (pLVdi->item.pszText, data.Album.szArtist);
break;
case 2:
lstrcpy (pLVdi->item.pszText,
pszCategories[data.Album.sCategory]);
break;
}
}
break;
// Ignore cache hinting for db example.
case LVN_COLUMNCLICK:
i = ((NM_LISTVIEW *)lParam)->iSubItem + IDM_SORTNAME;
PostMessage (hWnd, WM_COMMAND, MAKELPARAM (i, 0), 0);
break;
// Ignore cache hinting for db example.
case NM_DBLCLK:
PostMessage (hWnd, WM_COMMAND, MAKELPARAM (IDM_EDIT, 0), 0);
break;
// Ignore cache hinting for db example.
case LVN_ODCACHEHINT:
break;
case LVN_ODFINDITEM:
// We should do a reverse look up here to see if
// an item exists for the text passed.
return -1;
}
}
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandDelDB - Process Program Delete command.
//
LPARAM DoMainCommandDelDB (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
int i, rc;
i = MessageBox (hWnd, TEXT ("Delete the entire database?"),
TEXT ("Delete"), MB_YESNO);
if (i != IDYES)
return 0;
if (g_oidDB) {
CloseHandle (g_hDB);
rc = CeDeleteDatabase (g_oidDB);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -