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

📄 cechat.c

📁 WinCE下的网络聊天试验程序
💻 C
📖 第 1 页 / 共 2 页
字号:
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {
    PostQuitMessage (0);
    return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
                          WORD wNotifyCode) {
    SendMessage (hWnd, WM_CLOSE, 0, 0);
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandComPort - Process the COM port combo box commands.
//
LPARAM DoMainCommandComPort (HWND hWnd, WORD idItem, HWND hwndCtl,
                             WORD wNotifyCode) {
    INT i;
    TCHAR szDev[32];

    if (wNotifyCode == CBN_SELCHANGE) {
        i = SendMessage (hwndCtl, CB_GETCURSEL, 0, 0);
        if (i != nLastDev) {
            nLastDev = i;
            SendMessage (hwndCtl, CB_GETLBTEXT, i, (LPARAM)szDev);
            InitCommunication (hWnd, szDev);
            SetFocus (GetDlgItem (hWnd, ID_SENDTEXT));
        }
    }
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandSendText - Process the Send text button.
//
LPARAM DoMainCommandSendText (HWND hWnd, WORD idItem, HWND hwndCtl,
                              WORD wNotifyCode) {

    // Set event so that sender thread will send the text.
    SetEvent (g_hSendEvent);
    SetFocus (GetDlgItem (hWnd, ID_SENDTEXT));
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandAbout - Process the Help | About menu command.
//
LPARAM DoMainCommandAbout(HWND hWnd, WORD idItem, HWND hwndCtl,
                          WORD wNotifyCode) {
    // Use DialogBox to create modal dialog.
    DialogBox (hInst, TEXT ("aboutbox"), hWnd, AboutDlgProc);
    return 0;
}
//======================================================================
// 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;
}
//----------------------------------------------------------------------
// GetRawIrDeviceName - Returns the device name for the RawIR com port
//
INT GetRawIrDeviceName (LPTSTR pDevName) {
    DWORD dwSize, dwType, dwData;
    HKEY hKey;

    *pDevName = TEXT ('\0');
    // Open the IrDA key.
    if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT ("Comm\\IrDA"), 0,
                      0, &hKey) == ERROR_SUCCESS) {

        // Query the device number.
        dwSize = sizeof (dwData);
        if (RegQueryValueEx (hKey, TEXT ("Port"), 0, &dwType,
                             (PBYTE)&dwData, &dwSize) == ERROR_SUCCESS)

            // Check for valid port number. Assume buffer > 5 chars.
            if (dwData < 10)
                wsprintf (pDevName, TEXT ("COM%d:"), dwData);

        RegCloseKey (hKey);
    }
    return lstrlen (pDevName);
}
//----------------------------------------------------------------------
// GetIrCommDeviceName - Returns the device name for the IrComm port
//
INT GetIrCommDeviceName (LPTSTR pDevName) {
    DWORD dwSize, dwType, dwData;
    HKEY hKey;

    *pDevName = TEXT ('\0');
    // Open the IrDA key.
    if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
                      TEXT ("Drivers\\BuiltIn\\IrCOMM"), 0,
                      0, &hKey) == ERROR_SUCCESS) {

        // Query the device number.
        dwSize = sizeof (dwData);
        if (RegQueryValueEx (hKey, TEXT ("Index"), 0, &dwType,
                             (PBYTE)&dwData, &dwSize) == ERROR_SUCCESS)

            // Check for valid port number. Assume buffer > 5 chars.
            if (dwData < 10)
                wsprintf (pDevName, TEXT ("COM%d:"), dwData);

        RegCloseKey (hKey);
    }
    return lstrlen (pDevName);
}
//----------------------------------------------------------------------
// FillComComboBox - Fills the com port combo box
//
int FillComComboBox (HWND hWnd) {
    TCHAR szDev[64];

    lstrcpy (szDev, TEXT ("Serial Port   COM1:"));
    SendDlgItemMessage (GetDlgItem (hWnd, IDC_CMDBAR),
                        IDC_COMPORT, CB_INSERTSTRING,
                        -1, (LPARAM)szDev);

    lstrcpy (szDev, TEXT ("IrComm Port   "));
    GetIrCommDeviceName (&szDev[lstrlen (szDev)]);
    SendDlgItemMessage (GetDlgItem (hWnd, IDC_CMDBAR),
                        IDC_COMPORT, CB_INSERTSTRING,
                        -1, (LPARAM)szDev);

    lstrcpy (szDev, TEXT ("Raw IR Port   "));
    GetRawIrDeviceName (&szDev[lstrlen (szDev)]);
    SendDlgItemMessage (GetDlgItem (hWnd, IDC_CMDBAR),
                        IDC_COMPORT, CB_INSERTSTRING,
                        -1, (LPARAM)szDev);
    SendDlgItemMessage (GetDlgItem (hWnd, IDC_CMDBAR), IDC_COMPORT,
                        CB_SETCURSEL, 0, 0);
    return 0;
}
//----------------------------------------------------------------------
// InitCommunication - Open and initialize selected COM port.
//
HANDLE InitCommunication (HWND hWnd, LPTSTR pszDevName) {
    DCB dcb;
    INT i;
    TCHAR szDbg[128];
    COMMTIMEOUTS cto;
    HANDLE hLocal;
    DWORD dwTStat;

    hLocal = hComPort;
    hComPort = INVALID_HANDLE_VALUE;

    if (hLocal != INVALID_HANDLE_VALUE)
        CloseHandle (hLocal);  // This causes WaitCommEvent to return.

    // The com port name is the last 5 characters of the string.
    i = lstrlen (pszDevName);
    hLocal = CreateFile (&pszDevName[i-5], GENERIC_READ | GENERIC_WRITE,
                           0, NULL, OPEN_EXISTING, 0, NULL);

    if (hLocal != INVALID_HANDLE_VALUE) {
        // Configure port.
        GetCommState (hLocal, &dcb);
        dcb.BaudRate = nSpeed;
        dcb.fParity = FALSE;
        dcb.fNull = FALSE;
        dcb.StopBits = ONESTOPBIT;
        dcb.Parity = NOPARITY;
        dcb.ByteSize = 8;
        SetCommState (hLocal, &dcb);

        // Set the timeouts.  Set infinite read timeout.
        cto.ReadIntervalTimeout = 0;
        cto.ReadTotalTimeoutMultiplier = 0;
        cto.ReadTotalTimeoutConstant = 0;
        cto.WriteTotalTimeoutMultiplier = 0;
        cto.WriteTotalTimeoutConstant = 0;
        SetCommTimeouts (hLocal, &cto);

        wsprintf (szDbg, TEXT ("Port %s opened\r\n"), pszDevName);
        SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0,
                            (LPARAM)szDbg);
        // Really bad hack to determine which is the raw IR selection.
        // We need to enable IR on the raw IR port in case port is
        // shared with the standard serial port.
        if (*pszDevName == TEXT ('R')) {
            if (!EscapeCommFunction (hLocal, SETIR)) {
                wsprintf (szDbg, TEXT ("Set IR failed. rc %d\r\n"),
                          GetLastError());
                SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL,
                                    0, (LPARAM)szDbg);
            }
        }
        // Start read thread if not already started.
        hComPort = hLocal;
        if (!GetExitCodeThread (hReadThread, &dwTStat) ||
            (dwTStat != STILL_ACTIVE)) {
            hReadThread = CreateThread (NULL, 0, ReadThread, hWnd,
                                        0, &dwTStat);
            if (hReadThread)
                CloseHandle (hReadThread);
        }
    } else {
        wsprintf (szDbg, TEXT ("Couldn\'t open port %s. rc=%d\r\n"),
                  pszDevName, GetLastError());
        SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL,
                            0, (LPARAM)szDbg);
    }
    return hComPort;
}
//======================================================================
// SendThread - Sends characters to the serial port
//
int SendThread (PVOID pArg) {
    HWND hWnd, hwndSText;
    INT cBytes, nGoCode;
    TCHAR szText[TEXTSIZE];

    hWnd = (HWND)pArg;
    hwndSText = GetDlgItem (hWnd, ID_SENDTEXT);
    while (1) {
        nGoCode = WaitForSingleObject (g_hSendEvent, INFINITE);
        if (nGoCode == WAIT_OBJECT_0) {
            if (!fContinue)
                break;
            GetWindowText (hwndSText, szText, dim(szText));
            lstrcat (szText, TEXT ("\r\n"));
            WriteFile (hComPort, szText, lstrlen (szText)*sizeof (TCHAR),
                       &cBytes, 0);
            SetWindowText (hwndSText, TEXT (""));  // Clear out text box
        } else
            break;
    }
    return 0;
}
//======================================================================
// ReadThread - Receives characters from the serial port
//
int ReadThread (PVOID pArg) {
    HWND hWnd;
    INT cBytes, i;
    BYTE szText[TEXTSIZE], *pPtr;
    TCHAR tch;

    hWnd = (HWND)pArg;
    while (fContinue) {
        tch = 0;
        pPtr = szText;
        for (i = 0; i < sizeof (szText)-sizeof (TCHAR); i++) {

            while (!ReadFile (hComPort, pPtr, 1, &cBytes, 0))
                if (hComPort == INVALID_HANDLE_VALUE)
                    return 0;

            // This syncs the proper byte order for Unicode.
            tch = (tch << 8) & 0xff00;
            tch |= *pPtr++;
            if (tch == TEXT ('\n'))
                break;
        }
        *pPtr++ = 0;  // Avoid alignment probs by addressing as bytes.
        *pPtr++ = 0;

        // If out of byte sync, move bytes down one.
        if (i % 2) {
            pPtr = szText;
            while (*pPtr || *(pPtr+1)) {
                *pPtr = *(pPtr+1);
                pPtr++;
            }
            *pPtr = 0;
        }
        SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0,
                            (LPARAM)szText);
    }
    return 0;
}

⌨️ 快捷键说明

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