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

📄 mynotify.c

📁 上传的代码是WindowsCEProgramming第一版的源代码
💻 C
字号:
//======================================================================
// MyNotify - Demonstrates the Windows CE Notification API
//
// Written for the book Programming Windows CE
// Copyright (C) 1998 Douglas Boling
//======================================================================
#include <windows.h>                 // For all that Windows stuff
#include <notify.h>                  // For notification defines
#include "MyNotify.h"                // Program-specific stuff

//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("MyNotify");
HINSTANCE hInst;                     // Program instance handle

CE_USER_NOTIFICATION g_ceun;         // User notification structure
TCHAR szDlgTitle[128] = TEXT ("Notification Demo");
TCHAR szDlgText[128] = TEXT ("Times Up!");
TCHAR szSound[MAX_PATH] = TEXT ("alarm1.wav");

// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
    WM_INITDIALOG, DoInitDialogMain,
    WM_COMMAND, DoCommandMain,
    MYMSG_TELLNOTIFY, DoTellNotifyMain,
};
// Command Message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
    IDOK, DoMainCommandExit,
    IDCANCEL, DoMainCommandExit,
    IDD_ADDUSERNOT, DoMainCommandAddUserNotification,
    IDD_CFGUSERNOT, DoMainCommandConfigUserNotification,
    IDD_ADDSYSNOT, DoMainCommandAddSysNotification, 
    IDD_ADDTIMENOT, DoMainCommandAddTimerNotification, 
};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPWSTR lpCmdLine, int nCmdShow) {
    INT i;
    TCHAR szText[MAX_PATH];
    WCHAR *pPtr;
    HANDLE hNotify;
    HWND hWnd;

    hInst = hInstance;

    if (*lpCmdLine) {
        pPtr = lpCmdLine;
        // Parse the first word of the command line.
        for (i = 0; (i < dim(szText)-1) && (*pPtr > TEXT (' ')); i++)
            szText[i] = *pPtr++;
        szText[i] = TEXT ('\0');

        // Check to see if app started due to notification.
        if (lstrcmp (szText, APP_RUN_TO_HANDLE_NOTIFICATION) == 0) {
            // Ack the notification
            GetModuleFileName (hInst, szText, sizeof (szText));
            CeHandleAppNotifications (szText);

            // Get handle of command line.
            hNotify = (HANDLE)_wtol (pPtr);

            // Look to see if another instance of the app is running.
            hWnd = FindWindow (NULL, szAppName);
            if (hWnd) {
                SendMessage (hWnd, MYMSG_TELLNOTIFY, 0, 
                             (LPARAM)hNotify);
                // I should terminate this app here, but I don't so you
                // can see what happens.
                // return 0;
            }
        } 
    }
    // Do a little initialization of CE_USER_NOTIFICATION.
    memset (&g_ceun, 0, sizeof (g_ceun));
    g_ceun.ActionFlags = PUN_DIALOG;
    g_ceun.pwszDialogTitle = szDlgTitle;
    g_ceun.pwszDialogText = szDlgText;
    g_ceun.pwszSound = szSound;
    g_ceun.nMaxSound = sizeof (szSound);

    // Display dialog box as main window.
    DialogBoxParam (hInstance, szAppName, NULL, MainDlgProc, 
                    (LPARAM)lpCmdLine);
    return 0;

}
//======================================================================
// Message handling procedures for main window
//----------------------------------------------------------------------
// MainDlgProc - Callback function for application window
//
BOOL CALLBACK MainDlgProc (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 FALSE;
}
//----------------------------------------------------------------------
// DoInitDialogMain - Process WM_INITDIALOG message for window.
//
BOOL DoInitDialogMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {

    if (*(LPTSTR)lParam)
        Add2List (hWnd, (LPTSTR)lParam);
    return FALSE;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
BOOL 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) {
            (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl, 
                                       wNotifyCode);
            return TRUE;
        }
    }
    return FALSE;
}
//----------------------------------------------------------------------
// DoTellNotifyMain - Process MYMSG_TELLNOTIFY message for window.
//
BOOL DoTellNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                       LPARAM lParam) {
    Add2List (hWnd, TEXT ("Notification %d reported"), lParam);
    return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl, 
                          WORD wNotifyCode) {
    EndDialog (hWnd, 0);
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandAddUserNotification - Process Add User Notify button.
//
LPARAM DoMainCommandAddUserNotification (HWND hWnd, WORD idItem, 
                                       HWND hwndCtl, WORD wNotifyCode) {
    SYSTEMTIME st;
    TCHAR szExeName[MAX_PATH], szText[128];
    HANDLE hNotify;

    // Initialize time structure with local time.
    GetLocalTime (&st);
    // Do a trival amount of error checking.
    if (st.wMinute == 59) {
        st.wHour++;
        st.wMinute = 0;
    } else
        st.wMinute++;

    GetModuleFileName (hInst, szExeName, sizeof (szExeName));
    // Set the notification.
    hNotify = CeSetUserNotification (0, szExeName, &st, &g_ceun);
    if (hNotify) {
        wsprintf (szText, TEXT ("User notification set for %d:%02d"),
                  st.wHour, st.wMinute);
        MessageBox (hWnd, szText, szAppName, MB_OK);
    }
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandConfigUserNotification - Process Config user
// notification button.
//
LPARAM DoMainCommandConfigUserNotification (HWND hWnd, WORD idItem, 
                                       HWND hwndCtl, WORD wNotifyCode) {
    CeGetUserNotificationPreferences (hWnd, &g_ceun);
    return 0;
}
//----------------------------------------------------------------------
//  DoMainCommandAddSysNotification - Process Add Sys notify button.
//
LPARAM DoMainCommandAddSysNotification (HWND hWnd, WORD idItem, 
                                      HWND hwndCtl,  WORD wNotifyCode) {

    DialogBox (hInst, TEXT ("SysNotifyConfig"), hWnd, 
               SetEventNotifyDlgProc);
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandAddTimerNotification - Process add timer notify button.
//
LPARAM DoMainCommandAddTimerNotification (HWND hWnd, WORD idItem, 
                                      HWND hwndCtl,  WORD wNotifyCode) {
    SYSTEMTIME st;
    TCHAR szExeName[MAX_PATH], szText[128];

    // Initialize time structure with local time.
    GetLocalTime (&st);
    // Do a trivial amount of error checking.
    if (st.wMinute == 59) {
        st.wHour++;
        st.wMinute = 0;
    } else
        st.wMinute++;

    GetModuleFileName (hInst, szExeName, sizeof (szExeName));
    // Set the notification.
    if (CeRunAppAtTime (szExeName, &st)) { 
        wsprintf (szText, TEXT ("Timer notification set for %d:%d"),
                  st.wHour, st.wMinute);
        MessageBox (hWnd, szText, szAppName, MB_OK);
    }
    return 0;
}
//----------------------------------------------------------------------
// Add2List - Add string to the report list box.
//
void Add2List (HWND hWnd, LPTSTR lpszFormat, ...) {
    int i, nBuf;
    TCHAR szBuffer[512];

    va_list args;
    va_start(args, lpszFormat);

    nBuf = _vstprintf(szBuffer, lpszFormat, args);
    i = SendDlgItemMessage (hWnd, IDD_OUTPUT, WM_SETTEXT, 0, 
                            (LPARAM)(LPCTSTR)szBuffer);
    va_end(args);
}
//======================================================================
// SetEventNotifyDlgProc - Callback function for Event dialog box
//
BOOL CALLBACK SetEventNotifyDlgProc (HWND hWnd, UINT wMsg,
                                     WPARAM wParam,
                                     LPARAM lParam) {
    LONG lEvent;
    TCHAR szExeName[MAX_PATH];

    switch (wMsg) {
    case WM_COMMAND:
        {
            WORD idItem = LOWORD (wParam);
            switch (idItem) {
            case IDOK:
                lEvent = 0;

                // IsDlgButtonChecked isn't defined in Win CE, so 
                // a macro has been defined.
                if (IsDlgButtonChecked (hWnd, IDC_SYNC_END) == 1)
                    lEvent |= NOTIFICATION_EVENT_SYNC_END;

                if (IsDlgButtonChecked (hWnd, IDC_SERIAL_DETECT) == 1)
                    lEvent |= NOTIFICATION_EVENT_RS232_DETECTED;

                if (IsDlgButtonChecked (hWnd, IDC_DEVICE_CHANGE) == 1)
                    lEvent |= NOTIFICATION_EVENT_DEVICE_CHANGE;

                if (IsDlgButtonChecked (hWnd, IDC_TIME_CHANGE) == 1)
                    lEvent |= NOTIFICATION_EVENT_TIME_CHANGE;

                if (IsDlgButtonChecked (hWnd, IDC_RESTORE_END) == 1)
                    lEvent |= NOTIFICATION_EVENT_RESTORE_END;
 
                // Set the notification.
                GetModuleFileName (hInst, szExeName, 
                   sizeof (szExeName));
                CeRunAppAtEvent (szExeName, lEvent); 

                EndDialog (hWnd, 1); 
                return TRUE;

            case IDCANCEL:
                EndDialog (hWnd, 0); 
                return TRUE;
            }
        }
        break;
    }
    return FALSE;
}

⌨️ 快捷键说明

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