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

📄 smstalk.cpp

📁 建立一個傳送和接收SMS訊息的應用程式。SMSTalk是一個對話方塊導向的、多執行緒應用程式
💻 CPP
📖 第 1 页 / 共 2 页
字号:
LPARAM DoMainCommandAbout (HWND hWnd, WORD idItem, HWND hwndCtl, 
                           WORD wNotifyCode) {
    TCHAR szAbout[] = TEXT("SMS Talk\nCopyright 2003\nDouglas Boling");

    // Display about information in a message box.
    MessageBox (hWnd, szAbout, TEXT("About"), MB_OK | MB_ICONASTERISK);
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandMsgList - Process message list listbox.
//
LPARAM DoMainCommandMsgList (HWND hWnd, WORD idItem, HWND hwndCtl, 
                             WORD wNotifyCode) {
    if (wNotifyCode == LBN_SELCHANGE) 
        SetButtons (hWnd);
    return 0;
}
//----------------------------------------------------------------------
// RefreshMessageList - Fill in message listbox from message array
//
int RefreshMessageList (HWND hWnd, int nSel) {
    TCHAR szStr[256];
    int i;
    
    SendDlgItemMessage (hWnd, IDD_MSGLIST, LB_RESETCONTENT, 0, 0);
    for (i = 0; i < g_pMsgDB->nMsgCnt; i++) {
        wsprintf (szStr, TEXT("%d/%02d/%02d %d:%02d:%02d  %s"),
                  g_pMsgDB->pMsgs[i].stMsg.wMonth,
                  g_pMsgDB->pMsgs[i].stMsg.wDay,
                  g_pMsgDB->pMsgs[i].stMsg.wYear%100,
                  g_pMsgDB->pMsgs[i].stMsg.wHour,
                  g_pMsgDB->pMsgs[i].stMsg.wMinute,
                  g_pMsgDB->pMsgs[i].stMsg.wSecond, 
                  g_pMsgDB->pMsgs[i].wcMessage);
        SendDlgItemMessage (hWnd, IDD_MSGLIST, LB_ADDSTRING, 0, 
                            (LPARAM)szStr);
    }
    if (g_pMsgDB->nMsgCnt == 0)
        SendDlgItemMessage (hWnd, IDD_MSGLIST, LB_ADDSTRING, 0, 
                            (LPARAM)EMPTY_MSG_LIST);
    else { 
        if (nSel != -1)
            SendDlgItemMessage (hWnd, IDD_MSGLIST, LB_SETCURSEL,0,nSel);
        else
            SendDlgItemMessage (hWnd, IDD_MSGLIST, LB_SETCURSEL, 0, i-1);
    }
    return 0;
}
//----------------------------------------------------------------------
// SetButtons - Utility function to compute enabled state of btns
//
int SetButtons (HWND hWnd) {
    int nSel;
    BOOL fReply = FALSE;
    TCHAR szText[128];
    LPTSTR pMsg = TEXT(""), pNum = TEXT("");

    nSel = SendDlgItemMessage (hWnd, IDD_MSGLIST, LB_GETCURSEL, 0, 0);
    if (nSel != LB_ERR) {
        SendDlgItemMessage (hWnd, IDD_MSGLIST, LB_GETTEXT, nSel, 
                            (LPARAM)szText);
        if (_tcscmp (szText, EMPTY_MSG_LIST)) {
            fReply = TRUE;
            pNum = g_pMsgDB->pMsgs[nSel].smsAddr.ptsAddress;
            pMsg = g_pMsgDB->pMsgs[nSel].wcMessage;
        }
    }
    EnableWindow (GetDlgItem (hWnd, ID_CMDREPLY), fReply);

    // Set the text in the number and message fields
    SetWindowText (GetDlgItem (hWnd, IDD_MSGADDR), pNum);
    SetWindowText (GetDlgItem (hWnd, IDD_MSGTEXT), pMsg);

    // Disable the menu bar button if necessary
    TBBUTTONINFO tbi;
    HWND hwndMB = SHFindMenuBar (hWnd);
    memset (&tbi, 0, sizeof (tbi));
    tbi.cbSize = sizeof (tbi);
    tbi.dwMask = TBIF_STATE;
    if(SendMessage (hwndMB, TB_GETBUTTONINFO, IDPOP, (LPARAM)&tbi)) {
        if (fReply)
            tbi.fsState |= TBSTATE_ENABLED;
        else
            tbi.fsState &= ~TBSTATE_ENABLED;
        SendMessage (hwndMB, TB_SETBUTTONINFO, IDPOP, (LPARAM)&tbi);
    }
    return nSel;
}
//----------------------------------------------------------------------
// SendSmsMessage - Send an SMS message
//
HRESULT SendSmsMessage (HWND hWNd, SMS_ADDRESS smsDest, LPTSTR pMsg) {
    HRESULT hr;
    SMS_HANDLE smshHandle;
    TEXT_PROVIDER_SPECIFIC_DATA tpsd;
    SMS_MESSAGE_ID smsmidMessageID = 0;

    // try to open an SMS Handle
    hr = SmsOpen(SMS_MSGTYPE_TEXT, SMS_MODE_SEND, &smshHandle, NULL);
    if (hr != ERROR_SUCCESS) 
        return hr;

    // Set up provider specific data
    tpsd.dwMessageOptions = PS_MESSAGE_OPTION_NONE;
    tpsd.psMessageClass = PS_MESSAGE_CLASS0;
    tpsd.psReplaceOption = PSRO_NONE;
    tpsd.dwHeaderDataSize = 0;

    // Send the message, indicating success or failure
    hr = SmsSendMessage (smshHandle, NULL, &smsDest, NULL, (PBYTE)pMsg, 
                         lstrlen(pMsg) * sizeof (TCHAR), 
                         (PBYTE) &tpsd, 12, SMSDE_OPTIMAL, 
                         SMS_OPTION_DELIVERY_NONE, &smsmidMessageID);
    SmsClose (smshHandle);
    return hr;
}
//======================================================================
// WriteMsg Dialog procedure
//
BOOL CALLBACK WriteDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam, 
                            LPARAM lParam) {
    SHINITDLGINFO shidi;
    static SMS_ADDRESS smsDest;
    TCHAR szMsg[SMS_DATAGRAM_SIZE+2];
    HRESULT hr;

    SHInputDialog (hWnd, wMsg, wParam);
    switch (wMsg) {
    case WM_INITDIALOG:
        // Specify that the dialog box should stretch full screen
        memset (&shidi, 0, sizeof(shidi));
        shidi.dwMask = SHIDIM_FLAGS;
        shidi.dwFlags = SHIDIF_SIZEDLGFULLSCREEN;
        if (!g_fOnSPhone) shidi.dwFlags |= SHIDIF_DONEBUTTON;
        shidi.hDlg = hWnd;
        if(!SHInitDialog(&shidi))
            return FALSE;

        // This is only needed on the smartphone
        if (g_fOnSPhone) {
            // Create softkeybar
            SHMENUBARINFO mbi;
            memset (&mbi, 0, sizeof(SHMENUBARINFO));
            mbi.cbSize = sizeof(SHMENUBARINFO);
            mbi.hwndParent = hWnd;
            mbi.nToolBarId = ID_DLGMENU_SP;
            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;
            }

            // 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 input mode of number field to numbers
            SendDlgItemMessage (hWnd, IDD_MSGADDR, EM_SETINPUTMODE, 0, 
                                EIM_NUMBERS);
        }

        SendDlgItemMessage (hWnd, IDD_MSGTEXT, EM_LIMITTEXT, 
                            SMS_DATAGRAM_SIZE, 0);
        // If there is a reply address passed, place it in control
        if (lParam) {
            // Copy dest address
            smsDest = *(SMS_ADDRESS *)lParam;
            SetDlgItemText (hWnd, IDD_MSGADDR, smsDest.ptsAddress);
            SetFocus (GetDlgItem (hWnd, IDD_MSGTEXT));
            return FALSE;
        } else {
            smsDest.smsatAddressType = SMSAT_INTERNATIONAL;
            memset (smsDest.ptsAddress, 0, 
                    sizeof (smsDest.ptsAddress));
        }
        return TRUE;

    case WM_HOTKEY:
        SHSendBackToFocusWindow (wMsg, wParam, lParam); 
        return TRUE;

    case WM_COMMAND:
        switch (LOWORD (wParam)) {
            case ID_CMDSEND:
                GetDlgItemText (hWnd,IDD_MSGADDR, smsDest.ptsAddress, 
                                dim (smsDest.ptsAddress));
                GetDlgItemText (hWnd,IDD_MSGTEXT, szMsg, dim (szMsg));
                hr = SendSmsMessage (hWnd, smsDest, szMsg);
                if (hr == 0)
                    MessageBox (hWnd, TEXT("Message Sent"), szAppName,
                                MB_OK | MB_ICONASTERISK);
                else
                    ErrorBox (hWnd, TEXT ("Send message fail %x %d"),
                              hr, GetLastError());
            case IDOK:
            case IDCANCEL:
                EndDialog (hWnd, 0);
                return TRUE;
        }
    break;
    }
    return FALSE;
}
//----------------------------------------------------------------------
// ErrorBox - Displays a message box with a formatted string
//
void ErrorBox (HWND hWnd, LPCTSTR lpszFormat, ...) {
    int nBuf;
    TCHAR szBuffer[512];

    va_list args;
    va_start(args, lpszFormat);

    nBuf = _vstprintf(szBuffer, lpszFormat, args);
    MessageBox (hWnd, szBuffer, TEXT("Error Msg"), MB_OK);
    va_end(args);
}
//----------------------------------------------------------------------
// OnSmartPhone - Determines if we're running on a smartphone
//
BOOL OnSmartPhone (void) {
    TCHAR szPlat[128];
    int rc;
    rc = SystemParametersInfo(SPI_GETPLATFORMTYPE, dim(szPlat),szPlat,0);
    if (rc) {
        if (lstrcmpi (szPlat, TEXT("Smartphone")) == 0)
            return TRUE;
    }
    return FALSE;
}
//======================================================================
// MonitorThread - Monitors event for timer notificaiton 
//
DWORD WINAPI MonitorThread (PVOID pArg) {
    TEXT_PROVIDER_SPECIFIC_DATA tpsd;
    SMS_HANDLE smshHandle = (SMS_HANDLE)pArg;
    PMYMSG_STRUCT pNextMsg;
    BYTE bBuffer[MAXMESSAGELEN];
    PBYTE pIn;
    SYSTEMTIME st;
    HANDLE hWait[2];
    HRESULT hr;
    int rc;
    DWORD dwInSize, dwSize, dwRead = 0;

    hWait[0] = g_hReadEvent;        // Need two events since it isn't
    hWait[1] = g_hQuitEvent;        // allowed for us to signal SMS event. 

    while (g_fContinue) {
        rc = WaitForMultipleObjects (2, hWait, FALSE, INFINITE);
        if (!g_fContinue || (rc != WAIT_OBJECT_0))
            break;
        // Point to the next free entry in the array
        pNextMsg = &g_pMsgDB->pMsgs[g_pMsgDB->nMsgCnt];

        // Get the message size
        hr = SmsGetMessageSize (smshHandle, &dwSize);
        if (hr != ERROR_SUCCESS) continue;

        // Check for message larger than std buffer
        if (dwSize > sizeof (pNextMsg->wcMessage)) {
            if (dwSize > MAXMESSAGELEN) 
                continue;
            pIn = bBuffer;
            dwInSize = MAXMESSAGELEN;
        } else {
            pIn = (PBYTE)pNextMsg->wcMessage;
            dwInSize = sizeof (pNextMsg->wcMessage);
        }
        // Set up provider specific data
        tpsd.dwMessageOptions = PS_MESSAGE_OPTION_NONE;
        tpsd.psMessageClass = PS_MESSAGE_CLASS0;
        tpsd.psReplaceOption = PSRO_NONE;
        tpsd.dwHeaderDataSize = 0;
        // Read the message
        hr = SmsReadMessage (smshHandle, NULL, &pNextMsg->smsAddr, &st,
                             (PBYTE)pIn, dwInSize, (PBYTE)&tpsd, 
                             sizeof(TEXT_PROVIDER_SPECIFIC_DATA),
                             &dwRead);
        if (hr == ERROR_SUCCESS) {
            // Convert GMT message time to local time
            FILETIME ft, ftLocal;
            SystemTimeToFileTime (&st, &ft);
            FileTimeToLocalFileTime (&ft, &ftLocal);
            FileTimeToSystemTime (&ftLocal, &pNextMsg->stMsg);

            // If using alt buffer, copy to std buff 
            if ((DWORD)pIn == (DWORD)pNextMsg->wcMessage) {
                pNextMsg->nSize = (int) dwRead;
            } else {
                memset (pNextMsg->wcMessage, 0, 
                        sizeof(pNextMsg->wcMessage));
                memcpy (pNextMsg->wcMessage, pIn, 
                        sizeof(pNextMsg->wcMessage)-2);
                pNextMsg->nSize = sizeof(pNextMsg->wcMessage);
            }
            // Increment message count
            if (g_pMsgDB->nMsgCnt < MAX_MSGS-1) {
                if (g_hMain)
                    PostMessage (g_hMain, MYMSG_TELLNOTIFY, 1, 
                                 g_pMsgDB->nMsgCnt);
                g_pMsgDB->nMsgCnt++;
            }
        } else {
            ErrorBox (g_hMain, TEXT("Error %x (%d) reading msg"), 
                      hr, GetLastError());
            break;
        }
    }
    SmsClose (smshHandle);
    return 0;
}

⌨️ 快捷键说明

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