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

📄 cechat.cpp

📁 Win CE 下的EVC串口聊天工具,附源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		return SHHandleWMActivate(hWnd, wParam, lParam, &sai, 0);
#endif 
	return 0;
}
//----------------------------------------------------------------------
// DoFocusMain - Process WM_SETFOCUS message for window.
//
LRESULT DoSetFocusMain (HWND hWnd, UINT wMsg, WPARAM wParam,
						LPARAM lParam) {
	SetFocus (GetDlgItem (hWnd, ID_SENDTEXT));
	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;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
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) {
			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;
}
//----------------------------------------------------------------------
// FillComComboBox - Fills the COM port combo box
//
int FillComComboBox (HWND hWnd) {
	int rc;
	WIN32_FIND_DATA fd;
	HANDLE hFind;

	hFind = FindFirstFileEx (TEXT ("COM?:"), FindExInfoStandard, &fd, 
							 FindExSearchLimitToDevices, NULL, 0);
	if (hFind != INVALID_HANDLE_VALUE) {
		do {
			SendDlgItemMessage (GetDlgItem (hWnd, IDC_CMDBAR),
								IDC_COMPORT, CB_INSERTSTRING,
								-1, (LPARAM)fd.cFileName);
			rc = FindNextFile (hFind, &fd);
		} while (rc);

		rc = FindClose (hFind);
	}
	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;
	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.

	hLocal = CreateFile (pszDevName, GENERIC_READ | GENERIC_WRITE,
						 0, NULL, OPEN_EXISTING, 0, NULL);

	if (hLocal != INVALID_HANDLE_VALUE) {
		// Configure port.
		dcb.DCBlength = sizeof (dcb);
		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);

		// 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
//
DWORD WINAPI SendThread (PVOID pArg) {
	HWND hWnd, hwndSText;
	int rc;
	DWORD cBytes;
	TCHAR szText[TEXTSIZE];

	hWnd = (HWND)pArg;
	hwndSText = GetDlgItem (hWnd, ID_SENDTEXT);
	while (1) {
		rc = WaitForSingleObject (g_hSendEvent, INFINITE);
		if (rc == WAIT_OBJECT_0) {
			if (!fContinue)
				break;
			// Disable send button while sending.
			EnableWindow (GetDlgItem (hWnd, ID_SENDBTN), FALSE);
			GetWindowText (hwndSText, szText, dim(szText));
			lstrcat (szText, TEXT ("\r\n"));
			rc = WriteFile (hComPort, szText, 
							lstrlen (szText)*sizeof (TCHAR),&cBytes, 0);
			if (rc) {
				// Copy sent text to output window. 
				SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0,
									(LPARAM)TEXT (" >"));
				SetWindowText (hwndSText, TEXT (""));  // Clear text box
			} else {
				// Else, print error message.
				wsprintf (szText, TEXT ("Send failed rc=%d\r\n"), 
						  GetLastError());
				DWORD dwErr = 0;
				COMSTAT Stat;
				
				if (ClearCommError (hComPort, &dwErr, &Stat)) {
					printf ("fail\n");
				}
			}
			// Put text in receive text box.
			SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0,
								(LPARAM)szText);
			EnableWindow (GetDlgItem (hWnd, ID_SENDBTN), TRUE);
		} else
			break;
	}
	return 0;
}
//======================================================================
// ReadThread - Receives characters from the serial port
//
DWORD WINAPI ReadThread (PVOID pArg) {
	HWND hWnd;
	DWORD 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 problems 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 + -