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

📄 smstalk.cpp

📁 建立一個傳送和接收SMS訊息的應用程式。SMSTalk是一個對話方塊導向的、多執行緒應用程式
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#include <windows.h>                 // For all that Windows stuff
#include <aygshell.h>                // Extended shell defines
#include <tpcshell.h>
#include <sms.h>                     // SMS functions
#include "SMSTalk.h"                 // Program-specific stuff

#define MY_MSGWAITING_STRING  TEXT("SMSMsgReadEvent")
#define EMPTY_MSG_LIST        TEXT("<No new messages>")
#define MAXMESSAGELEN         4096
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("SMSTalk");
const TCHAR szOtherApp[] = TEXT("Another application already \
has the SMS system open.\n\nPlease close the (email?) application");
HINSTANCE hInst;                     // Program instance handle
HWND g_hMain = 0;
HANDLE g_hReadEvent = 0; 
HANDLE g_hQuitEvent = 0;
BOOL g_fContinue = TRUE;
BOOL g_fOnSPhone = FALSE;
PMYMSG_DBASE g_pMsgDB = 0;

// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
    WM_CREATE, DoCreateDialogMain,
    WM_INITDIALOG, DoInitDialogMain,
    WM_COMMAND, DoCommandMain,
    WM_HOTKEY, DoHotKeyMain,
    MYMSG_TELLNOTIFY, DoTellNotifyMain,
};
// Command Message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
    IDD_MSGLIST, DoMainCommandMsgList,
    IDOK, DoMainCommandExit,
    IDCANCEL, DoMainCommandExit,
    ID_CMDREPLY, DoMainCommandReplyMessage,
    ID_CMDNEW, DoMainCommandNewMessage,
    ID_CMDDEL, DoMainCommandDelMessage,
    IDM_ABOUT, DoMainCommandAbout,
};
//======================================================================
// Program entry point
// 
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPWSTR lpCmdLine, int nCmdShow) {
    INT i;
    HWND hWnd;
    HANDLE hThread;
    TCHAR szDlgTemplate[32];
    SMS_HANDLE smshHandle;

    hInst = hInstance;

    // Look to see if another instance of the app is running.
    hWnd = FindWindow (NULL, szAppName);
    // See if we were launched with a command line
    if (*lpCmdLine) {
        // Check to see if app started due to notification.
        if (lstrcmp (lpCmdLine, MY_MSGWAITING_STRING) == 0) {
            if (hWnd) {
                SendMessage (hWnd, MYMSG_TELLNOTIFY, 0, 0);
            }
        } 
    } 
    // Set first instance to the foreground and exit
    if (hWnd) {
        SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));    
        return 0;
    }
    // See if we're running on a smartphone.
    g_fOnSPhone = OnSmartPhone ();

    // Allocate message array
    g_pMsgDB = (PMYMSG_DBASE)LocalAlloc (LPTR, sizeof (MYMSG_DBASE));
    if (g_pMsgDB == 0) {
        ErrorBox (NULL, TEXT("Out of memory"));
        return -1;
    }
    g_pMsgDB->nMsgCnt = 0;

    // Create secondary thread for timer event notification.
    // then try to open an SMS Handle for reading messages.
    g_hQuitEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
    g_hReadEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
    HRESULT hr = SmsOpen (SMS_MSGTYPE_TEXT, SMS_MODE_RECEIVE, 
                          &smshHandle, &g_hReadEvent);
    if (hr == SMS_E_RECEIVEHANDLEALREADYOPEN) {
        ErrorBox (hWnd, (LPCTSTR)szOtherApp);
        return 0;
    } else if (hr != ERROR_SUCCESS) {
        ErrorBox (hWnd, TEXT("SmsOpen fail %x %d"), hr, GetLastError());
        return 0;
    }
    hThread = CreateThread (NULL, 0, MonitorThread, (PVOID)smshHandle, 
                            0, (DWORD *)&i);
    if (hThread == 0)
        return -1;

    // Display dialog box as main window. Use different template if
    // running on the smartphone
    if (g_fOnSPhone)
        _tcscpy (szDlgTemplate, TEXT("SMSTalk_SP"));
    else
        _tcscpy (szDlgTemplate, TEXT("SMSTalk_PPC"));

    DialogBoxParam (hInstance, szDlgTemplate, NULL, MainDlgProc, 
                    (LPARAM)lpCmdLine);
    // Signal notification thread to terminate
    g_fContinue = FALSE;
    SetEvent (g_hQuitEvent);
    WaitForSingleObject (hThread, 1000);
    CloseHandle (hThread);
    CloseHandle (g_hQuitEvent);  // Don't close ReadEvent, SMS does that
    if (g_pMsgDB) LocalFree (g_pMsgDB);
    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;
}
//----------------------------------------------------------------------
// DoCreateDialogMain - Process WM_CREATE message for window.
//
BOOL DoCreateDialogMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {

    if (!g_fOnSPhone) {
        // set up Menu bar for Pocket PC
        SHMENUBARINFO mbi;
    
        memset (&mbi, 0, sizeof(SHMENUBARINFO));
        mbi.cbSize = sizeof(SHMENUBARINFO);
        mbi.hwndParent = hWnd;
        mbi.nToolBarId = ID_MENU_PPC; // IDM_MENU; 
        mbi.hInstRes = hInst;

        // If we could not initialize the dialog box, return an error
        if (!SHCreateMenuBar(&mbi)) {
            ErrorBox (hWnd, TEXT("Menubar failed"));
            DestroyWindow (hWnd);
            return FALSE;
        }
    }
    return TRUE;
}
//----------------------------------------------------------------------
// DoInitDialogMain - Process WM_INITDIALOG message for window.
//
BOOL DoInitDialogMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {
    // Save the window handle
    g_hMain = hWnd;

    // Specify that the dialog box should stretch full screen
    SHINITDLGINFO shidi;
    memset (&shidi, 0, sizeof(shidi));
    shidi.dwMask = SHIDIM_FLAGS;
    shidi.dwFlags = SHIDIF_SIZEDLG ;//SHIDIF_SIZEDLGFULLSCREEN 
    shidi.hDlg = hWnd;
    if(!SHInitDialog(&shidi))
        return FALSE;

    // Create menubar
    SHMENUBARINFO mbi;
    memset (&mbi, 0, sizeof(SHMENUBARINFO));
    mbi.cbSize = sizeof(SHMENUBARINFO);
    mbi.hwndParent = hWnd;
    if (g_fOnSPhone) 
        mbi.nToolBarId = ID_MENU_SP;
    else
        mbi.nToolBarId = ID_MENU_PPC; 
    mbi.hInstRes = hInst;

    // If we could not initialize the dialog box, return an error
    if (!SHCreateMenuBar(&mbi)) {
        ErrorBox (hWnd, TEXT("Menubar failed"));
        DestroyWindow (hWnd);
        return FALSE;
    }

    // This is only needed on the smartphone
    if (g_fOnSPhone) {
        // Override back key since we have an edit control 
        SendMessage (SHFindMenuBar (hWnd), SHCMBM_OVERRIDEKEY, VK_TBACK,
                     MAKELPARAM (SHMBOF_NODEFAULT | SHMBOF_NOTIFY, 
                                 SHMBOF_NODEFAULT | SHMBOF_NOTIFY));
    }
    // set the title bar
    SHSetNavBarText (hWnd, TEXT("SMS Talk"));

    SendDlgItemMessage (hWnd, IDD_MSGLIST, LB_ADDSTRING, 0, 
                        (LPARAM)EMPTY_MSG_LIST);
    SetButtons (hWnd);
    return TRUE;
}
//----------------------------------------------------------------------
// 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;
}
//----------------------------------------------------------------------
// DoHotKeyMain - Process WM_HOTKEY message for window.
//
BOOL DoHotKeyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

    SHSendBackToFocusWindow (wMsg, wParam, lParam); 
    return 0;
}
//----------------------------------------------------------------------
// DoTellNotifyMain - Process MYMSG_TELLNOTIFY message for window.
//
BOOL DoTellNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                       LPARAM lParam) {
    RefreshMessageList (hWnd, lParam);
    SetButtons (hWnd);
    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;
}
//----------------------------------------------------------------------
// DoMainCommandDelMessage - Process Read message button.
//
LPARAM DoMainCommandDelMessage (HWND hWnd, WORD idItem, HWND hwndCtl, 
                                 WORD wNotifyCode) {
    int i, nSel;
    nSel = SendDlgItemMessage (hWnd, IDD_MSGLIST, LB_GETCURSEL,0,0);
    if (nSel != LB_ERR) {
        for (i = nSel; i < g_pMsgDB->nMsgCnt-1; i++) 
            g_pMsgDB->pMsgs[i] = g_pMsgDB->pMsgs[i+1];
        g_pMsgDB->nMsgCnt--;
        RefreshMessageList (hWnd, -1);
    }
    SetButtons (hWnd);
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandReplyMessage - Process Reply message button.
//
LPARAM DoMainCommandReplyMessage (HWND hWnd, WORD idItem, HWND hwndCtl, 
                                  WORD wNotifyCode) {
    int nSel;
    LPCTSTR lpTemplate;
    LPARAM lp = 0;
    nSel = SendDlgItemMessage (hWnd, IDD_MSGLIST, LB_GETCURSEL,0,0);
    if (nSel != LB_ERR)
        lp = (LPARAM)&g_pMsgDB->pMsgs[nSel].smsAddr;

    // Display reply dialog box.
    if (g_fOnSPhone) 
        lpTemplate = TEXT("WriteMsgDlg_SP");
    else
        lpTemplate = TEXT("WriteMsgDlg_PPC");

    DialogBoxParam (hInst, lpTemplate, NULL, WriteDlgProc, lp);
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandNewMessage - Process New message button.
//
LPARAM DoMainCommandNewMessage (HWND hWnd, WORD idItem, HWND hwndCtl, 
                                WORD wNotifyCode) {
    LPCTSTR lpTemplate;
    // Display reply dialog box.
    if (g_fOnSPhone) 
        lpTemplate = TEXT("WriteMsgDlg_SP");
    else
        lpTemplate = TEXT("WriteMsgDlg_PPC");

    DialogBoxParam (hInst, lpTemplate, NULL, WriteDlgProc, 0);
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandAbout - Process About menu item.
//

⌨️ 快捷键说明

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