home *** CD-ROM | disk | FTP | other *** search
- /*+==========================================================================
- File: SendEdit.cs
-
- Summary: Extends the default functionality of the DataEdit control
- by forwarding characters typed by the user to a Socket.
-
- Classes: SendEdit
-
- Functions: SetClientConnection, WndProc, OnChar
-
- ----------------------------------------------------------------------------
- This file is part of the Microsoft NGWS Samples.
-
- Copyright (C) 1998-2000 Microsoft Corporation. All rights reserved.
- ==========================================================================+*/
-
- using System;
- using System.Core;
- using System.WinForms;
- using System.Net;
- using System.Net.Sockets;
-
- public class SendEdit : DataEdit
- {
- protected Socket ClientConnection = null;
-
- /*****************************************************************************
- Function: SetClientConnection
-
- Abstract: Sets the socket used for sending keystroke information to.
-
- Input Parameters: s(Socket)
-
- Returns: void
- ******************************************************************************/
- public void SetClientConnection(Socket s)
- {
- ClientConnection = s;
- }
-
- /*****************************************************************************
- Function: WndProc
-
- Abstract: Override of the WndProc for this Edit control. Used to
- process the WM_CHAR event.
-
- Input Parameters: m(Message)
-
- Returns: void
- ******************************************************************************/
- protected override void WndProc(ref Message m)
- {
- switch (m.msg) {
- case Microsoft.Win32.Interop.win.WM_CHAR:
- if (OnChar(m.wParam,m.lParam&0xFFFF,(m.lParam>>16)&0xFFFF))
- return;
- }
- base.WndProc(ref m);
- }
-
- /*****************************************************************************
- Function: OnChar
-
- Abstract: Called by the WndProc whenever a character is typed. The
- character is sent to the Socket hosted by this control.
-
- Input Parameters: nChar(int), nRepCnt(int), nFlags(int)
-
- Returns: bool
- ******************************************************************************/
- protected bool OnChar(int nChar, int nRepCnt, int nFlags)
- {
- try {
- if (ClientConnection == null) {
- MessageBox.Show("No connection present. Please click "+
- "\"Connect\" button to make a connection","WinTalk");
- return false;
- }
-
- char c = (char)nChar;
- byte[] b = new byte[] { (byte)nChar };
-
- // create a String for appending to edit control
- String str = null;
- if (c >= ' ' || c == '\t') {
- char[] wt = new char[] { c };
- str = new String(wt);
- } else if (c == '\r') {
- str = "\r\n";
- } else if (c != '\b') { // throw out control characters
- return false;
- }
-
- for (int i = 0; i < nRepCnt; i++) {
- ClientConnection.Send(b, b.Length, 0);
- if (c == '\b') {
- TruncateOneCharFromEnd();
- } else {
- Append(str);
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("Error Occured!!!");
- Console.WriteLine(e.Message);
- return false;
- }
-
- return false; // we want the default handler to be called
- }
-
- }
-