home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Wintalk / VC / SendEdit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.8 KB  |  132 lines

  1. /*+==========================================================================
  2.   File:      SendEdit.cpp
  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-1999 Microsoft Corporation.  All rights reserved.
  15. ==========================================================================+*/
  16.  
  17. #using <mscorlib.dll>
  18. #using <System.DLL>
  19. #using <System.WinForms.DLL>
  20. #using <System.Net.DLL>
  21. #using <System.Drawing.DLL>
  22. #using <Microsoft.Win32.Interop.DLL>
  23.  
  24. using namespace System;
  25. using namespace System::Core;
  26. using namespace System::WinForms;
  27. using namespace System::Net;
  28. using namespace System::Net::Sockets;
  29. using namespace Microsoft::Win32::Interop;
  30.  
  31. #define NULL 0
  32.  
  33. #include "SendEdit.h"
  34.  
  35. /*****************************************************************************
  36.  Constructor : SendEdit
  37.  
  38.  Abstract:     Constructs an instance of SendEdit.
  39.             
  40.  Input Parameters: (none)
  41.  
  42. ******************************************************************************/
  43.     SendEdit::SendEdit() : ClientConnection(NULL)
  44.     {
  45.         Console::WriteLine("SendEdit Box created");
  46.     }
  47.  
  48. /*****************************************************************************
  49.  Function:     SetClientConnection
  50.  
  51.  Abstract:     Sets the socket used for sending keystroke information to.
  52.             
  53.  Input Parameters: s(Socket)
  54.  
  55.  Returns: void
  56. ******************************************************************************/
  57.     void SendEdit::SetClientConnection(Socket *s)
  58.     {
  59.         ClientConnection = s;
  60.     }
  61.     
  62. /*****************************************************************************
  63.  Function:     WndProc
  64.  
  65.  Abstract:     Override of the WndProc for this Edit control.  Used to 
  66.                process the WM_CHAR event.
  67.             
  68.  Input Parameters: m(Message)
  69.  
  70.  Returns: void
  71. ******************************************************************************/
  72.     void SendEdit::WndProc(Message *m)
  73.     {
  74.         switch (m->msg) {
  75.         case Microsoft::Win32::Interop::win::WM_CHAR:
  76.             if (OnChar(m->wParam,m->lParam&0xFFFF,(m->lParam>>16)&0xFFFF))
  77.                 return;
  78.         }
  79.         DataEdit::WndProc(m);
  80.     }
  81.  
  82. /*****************************************************************************
  83.  Function:     OnChar
  84.  
  85.  Abstract:     Called by the WndProc whenever a character is typed.  The
  86.                character is sent to the Socket hosted by this control.
  87.             
  88.  Input Parameters: nChar(int), nRepCnt(int), nFlags(int)
  89.  
  90.  Returns: bool
  91. ******************************************************************************/
  92.     bool SendEdit::OnChar(int nChar, int nRepCnt, int nFlags)
  93.     {
  94.         try {
  95.             if (ClientConnection == NULL) {
  96.                 MessageBox::Show(String::Concat(S"No connection present.  Please click ",
  97.                         S"\"Connect\" button to make a connection"),S"WinTalk");
  98.                 return false;
  99.             }
  100.             
  101.             wchar_t c = (wchar_t)nChar;
  102.             Char b[] = new Char[1];
  103.             b[0] =  (Char)nChar;
  104.             Console::WriteLine(b);
  105.  
  106.             // create a String for appending to edit control
  107.             String *str = NULL;
  108.             if (c >= L' ' || c == L'\t') {
  109.                 wchar_t *wt = new wchar_t[1];
  110.                 wt[0] = c;
  111.                 str = new String(wt);
  112.             } else if (c == L'\r') {
  113.                 str = S"\r\n";
  114.             } else if (c != L'\b') { // throw out control characters
  115.                 return false;
  116.             }
  117.  
  118.             for (int i = 0; i < nRepCnt; i++) {
  119.                 ClientConnection->Send(b, b->Length, 0);
  120.                 if (c == L'\b') {
  121.                     TruncateOneCharFromEnd();
  122.                 } else {
  123.                     Append(str);
  124.                 }
  125.             }    
  126.         }
  127.         catch (Exception *e) {}
  128.         
  129.         return false; // we want the default handler to be called
  130.     }
  131.             
  132.