Xom9ik

Lab_4/15var (IV semester) OS&SP

Jun 1st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using System.Diagnostics;
  11. using System.Runtime.InteropServices;
  12. using System.Windows.Forms;
  13.  
  14. namespace Lab4
  15. {
  16.     public partial class Lab_4 : Form
  17.     {
  18.         public static Lab_4 Instance { get; private set; }
  19.  
  20.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  21.         private static extern IntPtr FindWindow(string className, string windowName);
  22.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  23.         private static extern int SendMessage(IntPtr hwnd, int wMessage, int wParam, int lParam);
  24.         [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  25.         private static extern uint RegisterWindowMessage(string lpString);
  26.  
  27.         private uint messageId = RegisterWindowMessage("777");
  28.  
  29.         public Lab_4(){
  30.             if (Instance == null)
  31.                 Instance = this;
  32.             InitializeComponent();
  33.  
  34.         }
  35.         protected override void WndProc(ref Message m) {
  36.             if (m.Msg == messageId)
  37.                 chatHistory.AppendText(((char)m.WParam).ToString());
  38.             base.WndProc(ref m);
  39.         }
  40.         private void SendButton_Click(object sender, EventArgs e)
  41.         {
  42.             if (!(string.IsNullOrEmpty(message.Text) ||
  43.                 string.IsNullOrEmpty(to.Text) ||
  44.                 string.IsNullOrEmpty(username.Text)))
  45.             {
  46.                 string result = "Message from " + username.Text + " in " + DateTime.Now.ToShortTimeString() + ": " + message.Text + "\r\n";
  47.                 IntPtr windowToFind = FindWindow(null, to.Text);
  48.                 for (int i = 0; i < result.Length; i++)
  49.                     SendMessage(windowToFind, (int)messageId, result[i], 0);
  50.                 chatHistory.AppendText(result);
  51.                 message.Text = "";
  52.             }
  53.         }
  54.         private void nameText_TextChanged(object sender, EventArgs e) {
  55.             Text = Name = username.Text;
  56.         }
  57.  
  58.         private void messageText_KeyPress(object sender, KeyPressEventArgs e){
  59.             if (e.KeyChar == (char)Keys.Enter){
  60.                 send.PerformClick();
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment