📄 albumdb.cpp
字号:
break;
// Double click indicates edit
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 lookup 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);
if (rc == 0) {
ErrBox (hWnd, TEXT ("Couldn\'t delete database. rc=%d"),
GetLastError());
g_hDB = OpenDB (hWnd, NULL); // Open the database.
return 0;
}
g_hDB = INVALID_HANDLE_VALUE;
g_oidDB = 0;
}
ListView_SetItemCount (GetDlgItem (hWnd, ID_LISTV), 0);
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
SendMessage (hWnd, WM_CLOSE, 0, 0);
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandNew - Process Program New command.
//
LPARAM DoMainCommandNew (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
PCEPROPVAL pcepv;
int i, rc;
CEOID oid;
HWND hwndLV = GetDlgItem (hWnd, ID_LISTV);
// Display the new/edit dialog.
pcepv = 0;
rc = DialogBoxParam (hInst, TEXT ("EditAlbumDlg"), hWnd,
EditAlbumDlgProc, (LPARAM)&pcepv);
if (rc == 0)
return 0;
// Write the record.
oid = CeWriteRecordProps(g_hDB, 0, NUM_DB_PROPS, pcepv);
if (!oid)
ErrBox (hWnd, TEXT ("Write Rec fail. rc=%d"), GetLastError());
ClearCache (); // Clear the lv cache.
i = ListView_GetItemCount (hwndLV) + 1; // Increment list view
// count.
ListView_SetItemCount (hwndLV, i);
InvalidateRect (hwndLV, NULL, TRUE); // Force redraw.
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandEdit - Process Program Edit command.
//
LPARAM DoMainCommandEdit (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
PCEPROPVAL pcepv = 0;
int nSel, rc;
WORD wProps = 0;
DWORD dwRecSize, dwIndex;
CEOID oid;
HWND hwndLV = GetDlgItem (hWnd, ID_LISTV);
nSel = ListView_GetSelectionMark (hwndLV);
if (nSel == -1)
return 0;
// Seek to the necessary record.
oid = CeSeekDatabase (g_hDB, CEDB_SEEK_BEGINNING, nSel, &dwIndex);
if (oid == 0) {
ErrBox (hWnd, TEXT ("Db item not found. rc=%d"), GetLastError());
return 0;
}
// Read all properties for the record. Have the system
// allocate the buffer containing the data.
oid = CeReadRecordPropsEx (g_hDB, CEDB_ALLOWREALLOC, &wProps, NULL,
(LPBYTE *)&pcepv, &dwRecSize, 0);
if (oid == 0) {
ErrBox (hWnd, TEXT ("Db item not read. rc=%d"), GetLastError());
return 0;
}
// Display the edit dialog.
rc = DialogBoxParam (hInst, TEXT ("EditAlbumDlg"), hWnd,
EditAlbumDlgProc, (LPARAM)&pcepv);
if (rc == 0)
return 0;
// Write the record.
oid = CeWriteRecordProps(g_hDB, oid, NUM_DB_PROPS, pcepv);
if (!oid)
ErrBox (hWnd, TEXT ("Write Rec fail. rc=%d"), GetLastError());
LocalFree ((LPBYTE)pcepv);
ClearCache (); // Clear the lv cache.
InvalidateRect (hwndLV, NULL, TRUE); // Force redraw.
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandDelete - Process Program Delete command.
//
LPARAM DoMainCommandDelete (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
HWND hwndLV;
TCHAR szText[64];
DWORD dwIndex;
int i, nSel;
CEOID oid;
hwndLV = GetDlgItem (hWnd, ID_LISTV);
nSel = ListView_GetSelectionMark (hwndLV);
if (nSel != -1) {
wsprintf (szText, TEXT ("Delete this item?"));
i = MessageBox (hWnd, szText, TEXT ("Delete"), MB_YESNO);
if (i != IDYES)
return 0;
// Seek to the necessary record.
oid = CeSeekDatabase (g_hDB, CEDB_SEEK_BEGINNING, nSel, &dwIndex);
CeDeleteRecord (g_hDB, oid);
// Reduce the list view count by one and force redraw.
i = ListView_GetItemCount (hwndLV) - 1;
ListView_SetItemCount (hwndLV, i);
ClearCache (); // Clear the lv cache.
InvalidateRect (hwndLV, NULL, TRUE);
}
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandSort - Process the Sort commands.
//
LPARAM DoMainCommandSort(HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
int nSort;
switch (idItem) {
case IDM_SORTNAME:
nSort = 0;
break;
case IDM_SORTARTIST:
nSort = 1;
break;
case IDM_SORTCATEGORY:
nSort = 2;
break;
}
if (nSort == g_nLastSort)
return 0;
ReopenDatabase (hWnd, nSort); // Close and reopen the database.
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandAbout - Process the Help | About menu command.
//
LPARAM DoMainCommandAbout(HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
// Use DialogBox to create modal dialog.
DialogBox (hInst, TEXT ("aboutbox"), hWnd, AboutDlgProc);
return 0;
}
//----------------------------------------------------------------------
// 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 |
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 = 150;
lvc.pszText = TEXT ("Name");
lvc.iSubItem = 0;
SendMessage (hwndLV, LVM_INSERTCOLUMN, 0, (LPARAM)&lvc);
lvc.mask |= LVCF_SUBITEM;
lvc.pszText = TEXT ("Artist");
lvc.cx = 100;
lvc.iSubItem = 1;
SendMessage (hwndLV, LVM_INSERTCOLUMN, 1, (LPARAM)&lvc);
lvc.mask |= LVCF_SUBITEM;
lvc.pszText = TEXT ("Category");
lvc.cx = 100;
lvc.iSubItem = 2;
SendMessage (hwndLV, LVM_INSERTCOLUMN, 2, (LPARAM)&lvc);
}
return hwndLV;
}
//----------------------------------------------------------------------
// 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;
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 ("\\Albums"));
if (g_hDB == INVALID_HANDLE_VALUE) {
rc = GetLastError();
if (rc == ERROR_FILE_NOT_FOUND) {
g_oidDB = CeCreateDatabaseEx2 (&g_guidDB, &g_diex);
if (g_oidDB == 0) {
ErrBox (hWnd, TEXT ("Database create failed. rc=%d"),
GetLastError());
return 0;
}
g_hDB = OpenDB (hWnd, NULL);
}
}
if (CeOidGetInfo (g_oidDB, &oidinfo))
*pnRecords = oidinfo.infDatabase.wNumRecords;
else
*pnRecords = 0;
return g_hDB;
}
//----------------------------------------------------------------------
// 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;
}
//----------------------------------------------------------------------
// 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);
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;
}
//----------------------------------------------------------------------
// Get the album 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;
int i;
// See if the item requested was the previous one. If so,
// just use the old data.
if ((nItem == g_nLastItem) && (g_pLastRecord))
pRecord = (PCEPROPVAL)g_pLastRecord;
else {
// Seek to the necessary record.
oid = CeSeekDatabase (g_hDB, CEDB_SEEK_BEGINNING, nItem, &dwIndex);
if (oid == 0) {
ErrBox (NULL, TEXT ("Db item 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) {
ErrBox (NULL, TEXT ("Db item 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 album structure.
for (i = 0; i < wProps; i++) {
switch (pRecord->propid) {
case PID_NAME:
StringCchCopy (pcd->Album.szName,
dim (pcd->Album.szName),
pRecord->val.lpwstr);
break;
case PID_ARTIST:
StringCchCopy (pcd->Album.szArtist,
dim (pcd->Album.szArtist),
pRecord->val.lpwstr);
break;
case PID_CATEGORY:
pcd->Album.sCategory = pRecord->val.iVal;
break;
case PID_NUMTRACKS:
pcd->Album.sNumTracks = pRecord->val.iVal;
break;
}
pRecord++;
}
return 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -