?? albumdb.c
字號:
if (rc == 0) {
TCHAR szDbg[128];
rc = GetLastError();
wsprintf (szDbg, TEXT ("Couldn\'t delete db. rc=%d"), rc);
MessageBox (hWnd, szDbg, szAppName, MB_OK);
g_hDB = CeOpenDatabase (&g_oidDB, NULL, g_nLastSort,
0, hWnd);
return 0;
}
g_hDB = 0;
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) {
TCHAR szText[64];
rc = GetLastError ();
wsprintf (szText, TEXT ("Write Rec fail. Error %d (%x)"),
rc, rc);
MessageBox (hWnd, szText, TEXT ("Error"), MB_OK);
}
ClearCache (); // Clear the lv cache.
i = ListView_GetItemCount (hwndLV) + 1; // Increment list view
// count.
ListView_SetItemCount (hwndLV, i);
InvalidateRect (hwndLV, NULL, TRUE); // Force list view
// 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) {
TCHAR szTxt[64];
INT rc = GetLastError();
wsprintf (szTxt, TEXT ("Db item not found. rc = %d (%x)"),
rc, rc);
MessageBox (NULL, szTxt, TEXT ("err"), MB_OK);
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)pcepv, &dwRecSize);
if (oid == 0) {
TCHAR szTxt[64];
INT rc = GetLastError();
wsprintf (szTxt, TEXT ("Db item not read. rc = %d (%x)"),
rc, rc);
MessageBox (NULL, szTxt, TEXT ("err"), MB_OK);
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) {
TCHAR szText[64];
rc = GetLastError ();
wsprintf (szText, TEXT ("Write Rec fail. Error %d (%x)"),
rc, rc);
MessageBox (hWnd, szText, TEXT ("Error"), MB_OK);
}
LocalFree ((LPBYTE)pcepv);
ClearCache (); // Clear the lv cache.
InvalidateRect (hwndLV, NULL, TRUE); // Force list view
// 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;
CEOID oid;
int nSel;
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 = PID_NAME;
break;
case IDM_SORTARTIST:
nSort = PID_ARTIST;
break;
case IDM_SORTCATEGORY:
nSort = PID_CATEGORY;
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;
}
//----------------------------------------------------------------------
// OpenCreateDB - Open database, create if necessary.
//
HANDLE OpenCreateDB (HWND hWnd, int *pnRecords) {
INT i, rc;
CEOIDINFO oidinfo;
SORTORDERSPEC sos[4];
g_oidDB = 0;
g_hDB = CeOpenDatabase (&g_oidDB, TEXT ("\\Albums"),
g_nLastSort, 0, hWnd);
if (g_hDB == INVALID_HANDLE_VALUE) {
rc = GetLastError();
if (rc == ERROR_FILE_NOT_FOUND) {
i = 0;
sos[i].propid = PID_NAME;
sos[i++].dwFlags = 0;
sos[i].propid = PID_ARTIST;
sos[i++].dwFlags = 0;
sos[i].propid = PID_CATEGORY;
sos[i++].dwFlags = 0;
g_oidDB = CeCreateDatabase (TEXT ("\\Albums"), 0, 3,
sos);
if (g_oidDB == 0) {
TCHAR szErr[128];
wsprintf (szErr, TEXT ("Database create failed. \
rc %d"), GetLastError());
MessageBox (hWnd, szErr, szAppName, MB_OK);
return 0;
}
g_hDB = CeOpenDatabase(&g_oidDB,NULL, g_nLastSort, 0, hWnd);
}
}
CeOidGetInfo (g_oidDB, &oidinfo);
*pnRecords = oidinfo.infDatabase.wNumRecords;
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) {
TCHAR szTxt[64];
INT rc = GetLastError();
wsprintf (szTxt, TEXT ("Db item not found. rc = %d (%x)"),
rc, rc);
MessageBox (NULL, szTxt, TEXT ("err"), MB_OK);
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) {
TCHAR szTxt[64];
INT rc = GetLastError();
wsprintf (szTxt, TEXT ("Db item not read. rc = %d (%x)"),
rc, rc);
MessageBox (NULL, szTxt, TEXT ("err"), MB_OK);
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:
lstrcpy (pcd->Album.szName, pRecord->val.lpwstr);
break;
case PID_ARTIST:
lstrcpy (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 + -