zoukankan      html  css  js  c++  java
  • c#事件处理

      1 /*
      2 using System;
      3 using System.Text;
      4 using System.Runtime.InteropServices;
      5 using System.Reflection;
      6 using System.Windows.Forms;
      7 
      8 namespace MouseKeyboardLibrary
      9 { 
     10 /// <summary>
     11 /// Abstract base class for Mouse and Keyboard hooks
     12 /// </summary>
     13     public abstract class GlobalHook
     14     {
     15         #region Windows API Code
     16 
     17         [StructLayout(LayoutKind.Sequential)]
     18         protected class POINT
     19         {
     20             public int x;
     21             public int y;
     22         }
     23 
     24         [StructLayout(LayoutKind.Sequential)]
     25         protected class MouseHookStruct
     26         {
     27             public POINT pt;
     28             public int hwnd;
     29             public int wHitTestCode;
     30             public int dwExtraInfo;
     31         }
     32 
     33         [StructLayout(LayoutKind.Sequential)]
     34         protected class MouseLLHookStruct
     35         {
     36             public POINT pt;
     37             public int mouseData;
     38             public int flags;
     39             public int time;
     40             public int dwExtraInfo;
     41         }
     42 
     43         [StructLayout(LayoutKind.Sequential)]
     44         protected class KeyboardHookStruct
     45         {
     46             public int vkCode;
     47             public int scanCode;
     48             public int flags;
     49             public int time;
     50             public int dwExtraInfo;
     51         }
     52 
     53         [DllImport("user32.dll", CharSet = CharSet.Auto,
     54             CallingConvention = CallingConvention.StdCall,
     55             SetLastError = true)]
     56         protected static extern int setWindowsHookEx(
     57             int idHook, 
     58             HookProc lpfn,
     59             IntPtr hMod,
     60             int dwThreadId);
     61 
     62         [DllImport("user32", CharSet = CharSet.Auto,
     63             CallingConvention = CallingConvention.StdCall,
     64             SetLastError = true)]
     65         protected static extern int UnhookWindowsHookEx(int idHook);
     66 
     67         [DllImport("user32", CharSet = CharSet.Auto,
     68             CallingConvention = CallingConvention.StdCall)]
     69         protected static extern int CallNextHookEx(
     70             int idHook,
     71             int nCode,
     72             int wParam,
     73             IntPtr lParam);
     74 
     75         [DllImport("user32")]
     76         protected static extern int ToAscii(
     77             int uVirtKey,
     78             int uScanCode,
     79             byte[] lpbKeyState,
     80             byte[] lpwTransKey,
     81             int fuState);
     82 
     83         [DllImport("user32")]
     84         protected static extern int GetKeyboardState(byte[] pbKeyState);
     85 
     86         [DllImport("user32",CharSet=CharSet.Auto,CallingConvention =CallingConvention.StdCall)]
     87         protected static extern short GetKeyState(int vKey);
     88 
     89         protected delegate int HookProc(int nCode, int wParam, IntPtr lParam);
     90 
     91         protected const int WH_MOUSE_LL = 14;
     92         protected const int WH_KEYBOARD_LL = 13;
     93 
     94         protected const int WH_MOUSE = 7;
     95         protected const int WH_KEYBOARD = 2;
     96         protected const int WH_MOUSEMOVE = 0x200;
     97         protected const int WH_LBUTTONDOWN = 0x201;
     98         protected const int WH_RBUTTONDOWN = 0x204;
     99         protected const int WH_MBUTTONDOWN = 0x207;
    100         protected const int WH_LBUTTONUP = 0x202;
    101         protected const int WH_RBUTTONUP = 0x205;
    102         protected const int WH_MBUTTONUP = 0x208;
    103         protected const int WH_LBUTTONCLICK = 0x203;
    104         protected const int WH_RBUTTONCLICK = 0x206;
    105         protected const int WH_MBUTTONCLICK = 0x209;
    106         protected const int WH_MOUSEWHEEL = 0x020a;
    107         protected const int WH_KEYDOWN = 0x100;
    108         protected const int WH_KEYUP = 0x101;
    109         protected const int WH_SYSKEYDOWN = 0x104;
    110         protected const int WH_SYSKEYUP = 0x105;
    111 
    112         protected const byte VK_SHIFTT = 0x10;
    113         protected const byte VK_CAPITAL = 0x14;
    114         protected const byte VK_NUMLOCK = 0x90;
    115 
    116         protected const byte VK_LSHIFT = 0xa0;
    117         protected const byte VK_RSHIFT = 0xa1;
    118         protected const byte VK_LCONTROL = 0xa2;
    119         protected const byte VK_RCONTROL = 0xa3;
    120         protected const byte VK_LALT = 0xa4;
    121         protected const byte VK_RALT = 0xa5;
    122 
    123         protected const byte LLKHF_ALTDOWN = 0x20;
    124 
    125         #endregion
    126 
    127         #region Private Variables
    128         protected int _hookType;
    129         protected int _handleToHook;
    130         protected bool _isStarted;
    131         protected HookProc _hookCallback;
    132         #endregion
    133 
    134 
    135 
    136 
    137     }
    138 }
    139 */
    140 
    141 
    142 
    143 
    144 
    145 //写log文件
    146 /*
    147 using System;
    148 using System.Collections.Generic;
    149 using System.Linq;
    150 using System.Text;
    151 using System.Threading.Tasks;
    152 //using System.Diagnostics;
    153 using System.IO;
    154 
    155 
    156 namespace ConsoleApplication1
    157 {
    158     class Program
    159     {
    160         //
    161         static void Main(string[] args)
    162         {
    163 
    164             //可以作为修改的内容
    165             int i;
    166             int j;
    167 
    168             //作除法的设置
    169             try
    170             {
    171                 i = 0;
    172                 j = 2 / i;
    173             }
    174             catch (Exception ex)
    175             {
    176                 WriteLog(ex);//set ex
    177             }
    178             finally
    179             {
    180                 j = 2 / 1;
    181                 Console.Write(j);
    182                 Console.ReadLine();
    183             }
    184         }
    185 
    186         ///<summary>
    187         ///打印异常
    188         ///</summary>
    189         ///<param name="ex">异常</param>
    190         ///<param name="LogAddress">日志文件地址</param>
    191         public static void WriteLog(Exception ex, string LogAddress = "")
    192         {
    193             //if日志文件为空,默认在Debug目录下建立
    194             //建立日志文件的标题
    195             if (LogAddress == "")
    196             {
    197                 LogAddress = Environment.CurrentDirectory + '\' +
    198                     DateTime.Now.Year + '-' +
    199                     DateTime.Now.Month + '-' +
    200                     DateTime.Now.Day + "_Log.log";
    201             }
    202 
    203             //将异常信息输出到文件
    204             StreamWriter fs = new StreamWriter(LogAddress, true);
    205             fs.WriteLine("当前信息:" + DateTime.Now.ToString());
    206             fs.WriteLine("异常信息:" + ex.Message);
    207             fs.WriteLine("异常信息:" + ex.Source);
    208             fs.WriteLine("调用堆栈:
    " + ex.StackTrace.Trim());
    209             fs.WriteLine("触发方法:" + ex.TargetSite);
    210             fs.WriteLine();
    211             fs.Close();//释放WriteLine内存
    212         }
    213     }
    214 }
    215 */
    216 
    217 //2018-10-10
    218 //using events
    219 //创建一个委托
    220 //将创建委托与事件关联
    221 //编写c#事件处理程序
    222 //生成委托实例
    223 //委托实例添加到产生事件对象的事件列表中,为订阅事件
    224 //
    225 /*
    226 using System;
    227 using System.Collections.Generic;
    228 using System.Linq;
    229 using System.Text;
    230 
    231 namespace Example_EventTest
    232 {
    233     class Judgment
    234     { 
    235         //define 委托
    236         public delegate void delegateRun();
    237         //define event
    238         public event delegateRun eventRun;
    239         //引发事件的方法
    240         public void Begin()
    241         {
    242             eventRun();//被引发的事件
    243         }
    244 
    245     }
    246     class RunSports
    247     { 
    248         //define deal with events ways
    249         public void Run()
    250         {
    251             Console.WriteLine("start and later end");
    252         }
    253     }
    254     class Program
    255     {
    256         static void Main(string[] args)
    257         {
    258             RunSports runsport = new RunSports();//实例化事件发布者
    259             Judgment judgment = new Judgment();//实例化事件订阅者
    260             //订阅事件
    261             judgment.eventRun +=new Judgment.delegateRun(runsport.Run);
    262             //引发事件
    263             judgment.Begin();
    264             Console.ReadKey();
    265 
    266         }
    267     }
    268 }
    269 */
    270 
    271 
    272 using System;
    273 using System.IO;
    274 
    275 namespace BoilerEventApp1
    276 { 
    277     //boiler类
    278     class Boiler
    279     {
    280         private int temp;
    281         private int pressure;
    282         public Boiler(int t, int p)
    283         {
    284             temp = t;
    285             pressure = p;
    286         }
    287         //temp,pressure
    288         public int getTemp()
    289         {
    290             return temp;
    291         }
    292         public int getPressure()
    293         {
    294             return pressure;
    295         }
    296     }
    297 
    298     //事件发布器
    299     class DelegateBoilerEvent
    300     {
    301         public delegate void BoilerLogHandler(string status);
    302         //委托定义事件
    303         public event BoilerLogHandler BoilerEventLog;
    304         public void LogProgress()
    305         { 
    306             string remarks="o.k";
    307             Boiler b = new Boiler(100, 12);
    308             int t = b.getTemp();
    309             int p = b.getPressure();
    310             if (t > 150 || t < 80 || p < 12 || p > 15)
    311             {
    312                 remarks = "start one access";
    313             }
    314             //output message
    315             OnBoilerEventLog("Logging Info:
    ");
    316             OnBoilerEventLog("Temparature"+t+"
    Pressure:"+p);
    317             OnBoilerEventLog("Message:"+remarks);
    318         }
    319         protected void OnBoilerEventLog(string message)
    320         {
    321             if (BoilerEventLog != null)
    322             {
    323                 BoilerEventLog(message);
    324             }
    325         }
    326     }
    327 
    328     //保留该类写入日志文件
    329     class BoilerInfoLogger
    330     {
    331         FileStream fs;
    332         StreamWriter sw;
    333         public BoilerInfoLogger(string filename)
    334         {
    335             fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
    336             sw = new StreamWriter(fs);
    337         }
    338         public void Logger(string info)
    339         {
    340             sw.WriteLine(info);
    341         }
    342         public void Close()
    343         {
    344             sw.Close();
    345             fs.Close();
    346         }
    347     }
    348 
    349     //事件订阅器
    350     public class RecordBoilerInfo
    351     {
    352         static void Logger(string info)
    353         {
    354             Console.WriteLine(info);
    355         }
    356         //Main code
    357         static void Main(string[] args)
    358         {
    359             //location
    360             BoilerInfoLogger filelog = new BoilerInfoLogger("e:\boiler.txt");
    361             DelegateBoilerEvent boilerEvent = new DelegateBoilerEvent();
    362             boilerEvent.BoilerEventLog+=new DelegateBoilerEvent.BoilerLogHandler(Logger);
    363             boilerEvent.BoilerEventLog+=new DelegateBoilerEvent.BoilerLogHandler(filelog.Logger);
    364             boilerEvent.LogProgress();
    365             Console.ReadLine();
    366             filelog.Close();
    367         }
    368     }
    369 }
  • 相关阅读:
    Rabbitmq整合SpringBoot
    linux下转pdf乱码
    invalid authentication data connection reset
    谷粒商城异步编排(三十一)
    KSA运行
    安装k8s缺少模块
    ansible安装
    Rabbitmq常见的模式
    Rabbitmq入门
    谷粒商城检索服务(三十)
  • 原文地址:https://www.cnblogs.com/Blue-Moniter4/p/9767263.html
Copyright © 2011-2022 走看看