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

📄 albumdb.cpp

📁 《Windows CE 6.0开发者参考》(《Programming Windows Embedded CE 6.0 Developer Reference》)第四版书中的源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//======================================================================
// AlbumDB - A Windows CE database
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//======================================================================
#include <windows.h>                 // For all that Windows stuff
#include <windowsx.h>                // For Windows controls macros
#include <commctrl.h>                // Common control includes

#include "AlbumDB.h"                 // Program-specific stuff

// The file is specified here so there is no special project settings
// for windows mobile compiles.
#if defined(WIN32_PLATFORM_PSPC)
#include <aygshell.h>
#pragma comment( lib, "aygshell" )   // Link shell lib for menu bar.
#endif

//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("AlbumDB");
HINSTANCE hInst;                     // Program instance handle
HANDLE g_hDB = INVALID_HANDLE_VALUE; // Handle to album database
CEOID g_oidDB = 0;                   // Object ID of the album database
CEGUID g_guidDB;                     // Guid for database volume
CENOTIFYREQUEST cenr;                // Notify request structure.

int g_nLastSort = 0;                 // Last sort order used
CEDBASEINFOEX g_diex;                // Sort order array

// 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,
    WM_DBNOTIFICATION, 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 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);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
// 
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow){
    HWND hWnd;
    WNDCLASS wc;
    INITCOMMONCONTROLSEX icex;

#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
    // For Windows Mobile devices, allow only one instance of the app
    hWnd = FindWindow (szAppName, NULL);
    if (hWnd) {
        SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));    
        return (HWND)-1;
    }
#endif    
    // Save program instance handle in global variable.
    hInst = hInstance;
    // Create a guid for the database Ex functions that points
    // to the object store.
    CREATE_SYSTEMGUID(&g_guidDB);
    memset (&cenr, 0, sizeof (cenr)); // Initialize the notify request.

    // Initialize database info structure.
    memset (&g_diex, 0, sizeof (g_diex));
    g_diex.wVersion = CEDBASEINFOEX_VERSION;
    g_diex.dwFlags = CEDB_VALIDNAME | CEDB_VALIDTYPE | 
                 CEDB_VALIDSORTSPEC;
    StringCchCopy (g_diex.szDbaseName, dim(g_diex.szDbaseName), 
	               TEXT ("\\Albums"));
    g_diex.dwDbaseType = 0;
    g_diex.wNumSortOrder = 3;

    // Create sort property array
    int i = 0;
    g_diex.rgSortSpecs[i].wVersion = SORTORDERSPECEX_VERSION;
    g_diex.rgSortSpecs[i].wNumProps = 2;
    g_diex.rgSortSpecs[i].rgPropID[0] = PID_NAME;
    g_diex.rgSortSpecs[i].rgdwFlags[0] = 0;
    g_diex.rgSortSpecs[i].rgPropID[1] = PID_CATEGORY;
    g_diex.rgSortSpecs[i].rgdwFlags[1] = 0;
    i++;
    g_diex.rgSortSpecs[i].wVersion = SORTORDERSPECEX_VERSION;
    g_diex.rgSortSpecs[i].wNumProps = 2;
    g_diex.rgSortSpecs[i].rgPropID[0] = PID_ARTIST;
    g_diex.rgSortSpecs[i].rgdwFlags[0] = 0;
    g_diex.rgSortSpecs[i].rgPropID[1] = PID_NAME;
    g_diex.rgSortSpecs[i].rgdwFlags[1] = 0;
    i++;
    g_diex.rgSortSpecs[i].wVersion = SORTORDERSPECEX_VERSION;
    g_diex.rgSortSpecs[i].wNumProps = 3;
    g_diex.rgSortSpecs[i].rgPropID[0]= PID_CATEGORY;
    g_diex.rgSortSpecs[i].rgdwFlags[0] = 0;
    g_diex.rgSortSpecs[i].rgPropID[1] = PID_ARTIST;
    g_diex.rgSortSpecs[i].rgdwFlags[1] = 0;
    g_diex.rgSortSpecs[i].rgPropID[2] = PID_NAME;
    g_diex.rgSortSpecs[i].rgdwFlags[2] = 0;

    // 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 = LoadCursor (NULL, IDC_ARROW);// Default cursor
    wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wc.lpszMenuName =  NULL;                  // Menu name
    wc.lpszClassName = szAppName;             // Window class name

    if (RegisterClass (&wc) == 0) return 0;

    // Load the command bar common control class.
    icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_BAR_CLASSES | ICC_TREEVIEW_CLASSES | 
                 ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx (&icex);

    // Create main window.
    hWnd = CreateWindowEx (0, 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 != INVALID_HANDLE_VALUE)
        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;
    // Convert lParam to pointer to create structure.
    LPCREATESTRUCT lpcs = (LPCREATESTRUCT) lParam;

#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
    SHMENUBARINFO mbi;                      // For WinMobile, create
    memset(&mbi, 0, sizeof(SHMENUBARINFO)); // menu bar so that we
    mbi.cbSize = sizeof(SHMENUBARINFO);     // have a sip button.
    mbi.hwndParent = hWnd;
    mbi.dwFlags = SHCMBF_EMPTYBAR;          // No menu
    SHCreateMenuBar(&mbi);
    SetWindowPos (hWnd, 0, 0, 0, lpcs->cx,lpcs->cy - 26, 
                  SWP_NOMOVE | SWP_NOZORDER);
#endif
    // Convert lParam to pointer to create structure.
    lpcs = (LPCREATESTRUCT) lParam;

    // Create a minimal command bar that has only 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);

    CommandBar_AlignAdornments(GetDlgItem (hWnd, IDC_CMDBAR));
    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) {
    CENOTIFICATION *pcen = (CENOTIFICATION *)lParam;
    switch (pcen->uType) {
    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;
    }
    CeFreeNotification (&cenr, pcen);
    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:
                    StringCchCopy (pLVdi->item.pszText, 
					               pLVdi->item.cchTextMax, 
					               data.Album.szName);
                    break;
                case 1:
                    StringCchCopy (pLVdi->item.pszText, 
					               pLVdi->item.cchTextMax, 
					               data.Album.szArtist);
                    break;
                case 2:
                    StringCchCopy (pLVdi->item.pszText, 
					         pLVdi->item.cchTextMax, 
                             pszCategories[data.Album.sCategory]);
                    break;
                }
            }
            break;

        // Sort by column
        case LVN_COLUMNCLICK:
            i = ((NM_LISTVIEW *)lParam)->iSubItem + IDM_SORTNAME;
            PostMessage (hWnd, WM_COMMAND, MAKELPARAM (i, 0), 0);

⌨️ 快捷键说明

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