home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Wintalk / CS / SendEdit.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  3.2 KB  |  114 lines

  1. /*+==========================================================================
  2.   File:      SendEdit.cs
  3.  
  4.   Summary:   Extends the default functionality of the DataEdit control
  5.              by forwarding characters typed by the user to a Socket.
  6.  
  7.   Classes:   SendEdit
  8.  
  9.   Functions: SetClientConnection, WndProc, OnChar
  10.              
  11. ----------------------------------------------------------------------------
  12.   This file is part of the Microsoft NGWS Samples.
  13.  
  14.   Copyright (C) 1998-2000 Microsoft Corporation.  All rights reserved.
  15. ==========================================================================+*/
  16.  
  17. using System;
  18. using System.Core;
  19. using System.WinForms;
  20. using System.Net;
  21. using System.Net.Sockets;
  22.  
  23. public class SendEdit : DataEdit
  24. {
  25.     protected Socket ClientConnection = null;
  26.     
  27. /*****************************************************************************
  28.  Function:     SetClientConnection
  29.  
  30.  Abstract:     Sets the socket used for sending keystroke information to.
  31.             
  32.  Input Parameters: s(Socket)
  33.  
  34.  Returns: void
  35. ******************************************************************************/
  36.     public void SetClientConnection(Socket s)
  37.     {
  38.         ClientConnection = s;
  39.     }
  40.     
  41. /*****************************************************************************
  42.  Function:     WndProc
  43.  
  44.  Abstract:     Override of the WndProc for this Edit control.  Used to 
  45.                process the WM_CHAR event.
  46.             
  47.  Input Parameters: m(Message)
  48.  
  49.  Returns: void
  50. ******************************************************************************/
  51.     protected override void WndProc(ref Message m)
  52.     {
  53.         switch (m.msg) {
  54.         case Microsoft.Win32.Interop.win.WM_CHAR:
  55.             if (OnChar(m.wParam,m.lParam&0xFFFF,(m.lParam>>16)&0xFFFF))
  56.                 return;
  57.         }
  58.         base.WndProc(ref m);
  59.     }
  60.  
  61. /*****************************************************************************
  62.  Function:     OnChar
  63.  
  64.  Abstract:     Called by the WndProc whenever a character is typed.  The
  65.                character is sent to the Socket hosted by this control.
  66.             
  67.  Input Parameters: nChar(int), nRepCnt(int), nFlags(int)
  68.  
  69.  Returns: bool
  70. ******************************************************************************/
  71.     protected bool OnChar(int nChar, int nRepCnt, int nFlags)
  72.     {
  73.         try {
  74.             if (ClientConnection == null) {
  75.                 MessageBox.Show("No connection present.  Please click "+
  76.                         "\"Connect\" button to make a connection","WinTalk");
  77.                 return false;
  78.             }
  79.             
  80.             char c = (char)nChar;
  81.             byte[] b = new byte[] { (byte)nChar };
  82.  
  83.             // create a String for appending to edit control
  84.             String str = null;
  85.             if (c >= ' ' || c == '\t') {
  86.                 char[] wt = new char[] { c };
  87.                 str = new String(wt);
  88.             } else if (c == '\r') {
  89.                 str = "\r\n";
  90.             } else if (c != '\b') { // throw out control characters
  91.                 return false;
  92.             }
  93.  
  94.             for (int i = 0; i < nRepCnt; i++) {
  95.                 ClientConnection.Send(b, b.Length, 0);
  96.                 if (c == '\b') {
  97.                     TruncateOneCharFromEnd();
  98.                 } else {
  99.                     Append(str);
  100.                 }
  101.             }    
  102.         }
  103.         catch (Exception e) 
  104.         {
  105.             Console.WriteLine("Error Occured!!!");
  106.             Console.WriteLine(e.Message);
  107.             return false;
  108.         }
  109.         
  110.         return false; // we want the default handler to be called
  111.     }
  112.             
  113. }
  114.