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

📄 albumdb.cpp

📁 《Windows CE 6.0开发者参考》(《Programming Windows Embedded CE 6.0 Developer Reference》)第四版书中的源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
}
//----------------------------------------------------------------------
// InsertLV - Add an item to the list view control.
//
int InsertLV (HWND hWnd, int nItem, LPTSTR pszName, LPTSTR pszType, 
              int nSize) {
    LVITEM lvi;
    HWND hwndLV = GetDlgItem (hWnd, ID_LISTV);

    lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
    lvi.iItem = nItem;
    lvi.iSubItem = 0;
    lvi.pszText = pszName;
    lvi.iImage = 0;
    lvi.lParam = nItem;
    SendMessage (hwndLV, LVM_INSERTITEM, 0, (LPARAM)&lvi);

    lvi.mask = LVIF_TEXT;
    lvi.iItem = nItem;
    lvi.iSubItem = 1;
    lvi.pszText = pszType;
    SendMessage (hwndLV, LVM_SETITEM, 0, (LPARAM)&lvi);

    return 0;
}
//----------------------------------------------------------------------
// ValidateTime - Trivial error checking of time field
//
BOOL ValidateTime (TCHAR *pStr) {
    BOOL fSep = FALSE;
    TCHAR *pPtr;

    pPtr = pStr;
    // See if field contains only numbers and up to one colon.
    while (*pPtr) {
        if (*pPtr == TEXT (':')) {
            if (fSep)
                return FALSE;
            fSep = TRUE;
        } else if ((*pPtr < TEXT ('0')) || (*pPtr > TEXT ('9'))) 
            return FALSE;
        pPtr++;
    }
    // Reject empty field.
    if (pPtr > pStr)
        return TRUE;
    return FALSE;
}
//----------------------------------------------------------------------
// ErrBox - Displays an error string in a message box
//
int ErrBox (HWND hWnd, LPTSTR lpszFormat, ...) {
    int nBuf;
    TCHAR szBuffer[512];

    va_list args;
    va_start(args, lpszFormat);
    nBuf = vswprintf_s(szBuffer, dim (szBuffer), lpszFormat, args);
    va_end(args);

    MessageBox (hWnd, szBuffer, TEXT("Error"), MB_OK | MB_ICONERROR);
    return 0;
}
//======================================================================
// EditTrack dialog procedure
//
BOOL CALLBACK EditTrackDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam, 
                                LPARAM lParam) {
    static LPTRACKINFO lpti;

    switch (wMsg) {
        case WM_INITDIALOG:
            lpti = (LPTRACKINFO)lParam;
            SendDlgItemMessage (hWnd, IDD_TRACK, EM_SETLIMITTEXT, 
                                sizeof (lpti->szTrack), 0);
            SendDlgItemMessage (hWnd, IDD_TIME, EM_SETLIMITTEXT, 
                                sizeof (lpti->szTime), 0);
            // See if new album or edit of old one.
            if (lstrlen (lpti->szTrack) == 0) {
                SetWindowText (hWnd, TEXT ("New Track"));
            } else {
                SetDlgItemText (hWnd, IDD_TRACK, lpti->szTrack);
                SetDlgItemText (hWnd, IDD_TIME, lpti->szTime);
            }
            return TRUE;

        case WM_COMMAND:
            switch (LOWORD (wParam)) {
                case IDOK:
                    Edit_GetText (GetDlgItem (hWnd, IDD_TRACK), 
                             lpti->szTrack, sizeof (lpti->szTrack));
                    Edit_GetText (GetDlgItem (hWnd, IDD_TIME), 
                              lpti->szTime, sizeof (lpti->szTime));
                    if (ValidateTime (lpti->szTime))
                        EndDialog (hWnd, 1);
                    else 
                        MessageBox (hWnd, TEXT ("Track time must \
                                    be entered in mm:ss format"), 
                                    TEXT ("Error"), MB_OK);
                    return TRUE;
                case IDCANCEL:
                    EndDialog (hWnd, 0);
                    return TRUE;
            }
        break;
    }
    return FALSE;
}
//======================================================================
// EditAlbum dialog procedure
//
BOOL CALLBACK EditAlbumDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam, 
                                LPARAM lParam) {
    static PCEPROPVAL *ppRecord;
    static int nTracks;
    PCEPROPVAL pRecord, pRecPtr;
    TCHAR *pPtr, szTmp[128];
    HWND hwndTList, hwndCombo;
    TRACKINFO ti;
    BOOL fEnable;
    int i, nLen, rc;

    switch (wMsg) {
        case WM_INITDIALOG:
            ppRecord = (PCEPROPVAL *)lParam;
            pRecord = *ppRecord;

            hwndCombo = GetDlgItem (hWnd, IDD_CATEGORY);
            hwndTList = GetDlgItem (hWnd, IDD_TRACKS);

            Edit_LimitText (GetDlgItem (hWnd, IDD_NAME), MAX_NAMELEN);
            Edit_LimitText (GetDlgItem (hWnd, IDD_ARTIST),
                            MAX_ARTISTLEN);
            // Set tab stops on track list box.
            i = 110;
            ListBox_SetTabStops (hwndTList, 1, &i);
            // Initialize category combo box.
            for (i = 0; i < dim(pszCategories); i++) 
                ComboBox_AddString (hwndCombo, pszCategories[i]);
            ComboBox_SetCurSel (hwndCombo, 3);
            nTracks = 0;

            // See if new album or edit of old one.
            if (pRecord == 0) {
                SetWindowText (hWnd, TEXT ("New Album"));
            } else {
                // Copy the data from the record to album structure.
                for (i = 0; i < NUM_DB_PROPS; i++) {
                    switch (pRecord->propid) {
                    case PID_NAME:
                        SetDlgItemText (hWnd, IDD_NAME, 
                                        pRecord->val.lpwstr);
                        break;
                    case PID_ARTIST:
                        SetDlgItemText (hWnd, IDD_ARTIST, 
                                        pRecord->val.lpwstr);
                        break;
                    case PID_CATEGORY:
                        ComboBox_SetCurSel (hwndCombo, 
                                            pRecord->val.iVal);
                        break;
                    case PID_TRACKS:
                        pPtr = (TCHAR *)pRecord->val.blob.lpb;
                        for (i = 0; *pPtr; i++){
                          ListBox_InsertString (hwndTList,i,pPtr);
                            pPtr += lstrlen (pPtr) + 1;
                            nTracks++;
                        }
                        break;
                    }
                    pRecord++;
                }
            }
            // Select first track, or disable buttons if no tracks.
            if (nTracks) 
                ListBox_SetCurSel (GetDlgItem (hWnd, IDD_TRACKS), 3);
            else {
                EnableWindow (GetDlgItem (hWnd, IDD_DELTRACK), 
                              FALSE);
                EnableWindow (GetDlgItem (hWnd, IDD_EDITTRACK), 
                              FALSE);
            }
            return TRUE;
        case WM_COMMAND:
            hwndTList = GetDlgItem (hWnd, IDD_TRACKS);
            hwndCombo = GetDlgItem (hWnd, IDD_CATEGORY);
            pRecord = *ppRecord;
            switch (LOWORD (wParam)) {
                case IDD_TRACKS:
                    switch (HIWORD (wParam)) {
                    case LBN_DBLCLK:
                        PostMessage (hWnd, WM_COMMAND, 
                                   MAKELONG(IDD_EDITTRACK, 0), 0);
                        break;
                    case LBN_SELCHANGE:
                        i = ListBox_GetCurSel (hwndTList);
                        if (i == LB_ERR) 
                            fEnable = FALSE;
                        else
                            fEnable = TRUE;
                        EnableWindow (GetDlgItem (hWnd, 
                                      IDD_DELTRACK), fEnable);
                        EnableWindow (GetDlgItem (hWnd, 
                                      IDD_EDITTRACK), fEnable);
                        break;
                    }
                    return TRUE;

                case IDD_NEWTRACK:
                    memset (&ti, 0, sizeof (ti));
                    rc = DialogBoxParam (hInst, 
                        TEXT ("EditTrackDlg"), hWnd, 
                        EditTrackDlgProc, (LPARAM)&ti);
                    if (rc) {
                        wsprintf (szTmp, TEXT ("%s\t%s"),
                                  ti.szTrack, ti.szTime);
                        i = ListBox_GetCurSel (hwndTList);
                        if (i != LB_ERR)
                            i++;
                        i = ListBox_InsertString (hwndTList, i,
                                                  szTmp);
                        ListBox_SetCurSel (hwndTList, i);
                    }
                    return TRUE;

                case IDD_EDITTRACK:
                    i = ListBox_GetCurSel (hwndTList);
                    if (i != LB_ERR) {
                        ListBox_GetText (hwndTList, i, szTmp);
                        pPtr = szTmp;
                        while ((*pPtr != TEXT ('\t')) &&
                               (*pPtr != TEXT ('\0'))) 
                            pPtr++;
                        if (*pPtr == TEXT ('\t')) 
                            *pPtr++ = TEXT ('\0');

                        StringCchCopy (ti.szTime, dim(ti.szTime), pPtr);
                        StringCchCopy (ti.szTrack, dim(ti.szTrack),
						               szTmp);
                        rc = DialogBoxParam (hInst, 
                                             TEXT ("EditTrackDlg"), 
                                             hWnd, EditTrackDlgProc, 
                                             (LPARAM)&ti);
                        if (rc) {
                            wsprintf (szTmp, TEXT ("%s\t%s"), 
                                      ti.szTrack, ti.szTime);
                            i = ListBox_GetCurSel (hwndTList);
                            ListBox_DeleteString (hwndTList, i);
                            ListBox_InsertString (hwndTList, i,
                                                  szTmp);
                            ListBox_SetCurSel (hwndTList, i);
                        }
                    }
                    return TRUE;

                case IDD_DELTRACK:
                    // Grab the current selection, and remove
                    // it from list box.
                    i = ListBox_GetCurSel (hwndTList);
                    if (i != LB_ERR) {
                        rc = MessageBox (hWnd, 
                                         TEXT ("Delete this item?"),
                                         TEXT ("Track"), MB_YESNO);
                        if (rc == IDYES) {
                            i=ListBox_DeleteString (hwndTList,i);
                            if (i > 0)
                                i--;
                            ListBox_SetCurSel (hwndTList, i);
                        }
                    }
                    return TRUE;

                case IDOK:
                    // Be lazy and assume worst-case size values.
                    nLen = sizeof (CEPROPVAL) * NUM_DB_PROPS + 
                           MAX_NAMELEN + MAX_ARTISTLEN + 
                           MAX_TRACKNAMELEN;
                    // See if prev record, alloc if not.
                    if (pRecord) {
                        // Resize record if necessary.
                        if (nLen > (int)LocalSize (pRecord)) 
                            pRecPtr = 
                               (PCEPROPVAL)LocalReAlloc (pRecord, 
                               nLen, LMEM_MOVEABLE);
                        else
                            pRecPtr = pRecord;
                    } else
                        pRecPtr = (PCEPROPVAL)LocalAlloc (LMEM_FIXED, 
                                                          nLen);
                    if (!pRecPtr)
                        return 0;
                    // Copy the data from the controls to a 
                    // marshaled data block with the structure 
                    // at the front and the data in the back.
                    pRecord = pRecPtr;
                    nTracks = ListBox_GetCount (hwndTList);
                    pPtr = (TCHAR *)((LPBYTE)pRecPtr + 
                             (sizeof (CEPROPVAL) * NUM_DB_PROPS));
                    // Zero structure to start over.
                    memset (pRecPtr, 0, LocalSize (pRecPtr));

                    pRecPtr->propid = PID_NAME;
                    pRecPtr->val.lpwstr = pPtr;
                    GetDlgItemText (hWnd, IDD_NAME, pPtr, 
                                    MAX_NAMELEN);
                    pPtr += lstrlen (pPtr) + 1;
                    pRecPtr++;

                    pRecPtr->propid = PID_ARTIST;
                    pRecPtr->val.lpwstr = pPtr;
                    GetDlgItemText (hWnd, IDD_ARTIST, pPtr, 
                                    MAX_ARTISTLEN);
                    pPtr += lstrlen (pPtr) + 1;
                    pRecPtr++;

                    pRecPtr->propid = PID_RELDATE;
                    pRecPtr->val.iVal = 0;
                    pRecPtr++;

                    pRecPtr->propid = PID_CATEGORY;
                    pRecPtr->val.iVal = 
                                    ComboBox_GetCurSel (hwndCombo);
                    pRecPtr++;

                    pRecPtr->propid = PID_NUMTRACKS;
                    pRecPtr->val.iVal = nTracks;
                    pRecPtr++;

                    pRecPtr->propid = PID_TRACKS;
                    pRecPtr->val.blob.lpb = (LPBYTE)pPtr;

                    // Get the track titles from the list box.
                    rc = MAX_TRACKNAMELEN;
                    for (i = 0; i < nTracks; i++) {
                        // Make sure we have the room in the buff.
                        rc -= ListBox_GetTextLen(hwndTList, i);
                        if (rc) 
                            ListBox_GetText (hwndTList, i, pPtr);
                        else {
                            nTracks = i;
                            break;
                        }
                        pPtr += lstrlen (pPtr) + 1;
                    }
                    *pPtr++ = TEXT ('\0');
                    pRecPtr->val.blob.dwCount = 
                              (LPBYTE)pPtr - pRecPtr->val.blob.lpb;
                    *ppRecord = pRecord;
                    EndDialog (hWnd, 1);
                    return TRUE;

                case IDCANCEL:
                    EndDialog (hWnd, 0);
                    return TRUE;
            }
        break;
    }
    return FALSE;
}
//======================================================================
// About dialog procedure
//
BOOL CALLBACK AboutDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam, 
                            LPARAM lParam) {
    switch (wMsg) {
        case WM_COMMAND:
            switch (LOWORD (wParam)) {
                case IDOK:
                case IDCANCEL:
                    EndDialog (hWnd, 0);
                    return TRUE;
            }
        break;
    }
    return FALSE;
}

⌨️ 快捷键说明

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