ソースを参照

发布修复新版本,优化MD5,IP地址信息,字节格式转换等方法

yuzhengyang 7 年 前
コミット
05d3e919b1

BIN
Azylee.Utils/.vs/Azylee.Utils/v15/Server/sqlite3/storage.ide


+ 4 - 0
Azylee.Utils/Azylee.Core/Azylee.Core.csproj

@@ -20,6 +20,7 @@
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
+    <DocumentationFile>bin\Debug\Azylee.Core.xml</DocumentationFile>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>none</DebugType>
@@ -75,6 +76,7 @@
     <Compile Include="DelegateUtils\ProcessDelegateUtils\ProgressEventArgs.cs" />
     <Compile Include="DllUtils\DllInvokeTool.cs" />
     <Compile Include="DrawingUtils\ColorUtils\ColorStyle.cs" />
+    <Compile Include="FormUtils\FormManTool.cs" />
     <Compile Include="IOUtils\BinaryUtils\BinaryFileTool.cs" />
     <Compile Include="IOUtils\FileManUtils\FileWatcher.cs" />
     <Compile Include="IOUtils\FileManUtils\FileWatcherEventArgs.cs" />
@@ -144,6 +146,8 @@
     <Compile Include="WindowsUtils\ClipboardUtils\ClipboardTool.cs" />
     <Compile Include="WindowsUtils\CMDUtils\CMDNetstatTool.cs" />
     <Compile Include="WindowsUtils\CMDUtils\CMDProcessTool.cs" />
+    <Compile Include="WindowsUtils\HookUtils\KeyboardHook.cs" />
+    <Compile Include="WindowsUtils\HookUtils\KeyboardHookHelper.cs" />
     <Compile Include="WindowsUtils\HookUtils\UserActivityHook.cs" />
     <Compile Include="WindowsUtils\InfoUtils\ComputerInfoTool.cs" />
     <Compile Include="WindowsUtils\InfoUtils\ComputerStatusTool.cs" />

+ 21 - 6
Azylee.Utils/Azylee.Core/DataUtils/EncryptUtils/MD5Tool.cs

@@ -20,16 +20,31 @@ namespace Azylee.Core.DataUtils.EncryptUtils
         /// <param name="s">待加密字符串</param>
         /// <returns>加密后的字符串</returns>
         public static string Encrypt(string s)
+        { 
+            if (s != null)
+            {
+                try
+                {
+                    byte[] buffer = Encoding.UTF8.GetBytes(s);
+                    return Encrypt(buffer);
+                }
+                catch { }
+            }
+            return "";
+        }
+        public static string Encrypt(byte[] data)
         {
             string result = "";
-            try
+            if (data != null)
             {
-                byte[] buffer = Encoding.UTF8.GetBytes(s);
-                HashAlgorithm algorithm = MD5.Create();
-                byte[] hashBytes = algorithm.ComputeHash(buffer);
-                result = BitConverter.ToString(hashBytes).Replace("-", "");
+                try
+                {
+                    HashAlgorithm algorithm = MD5.Create();
+                    byte[] hashBytes = algorithm.ComputeHash(data);
+                    result = BitConverter.ToString(hashBytes).Replace("-", "");
+                }
+                catch { }
             }
-            catch { }
             return result;
         }
     }

+ 6 - 6
Azylee.Utils/Azylee.Core/DataUtils/UnitConvertUtils/ByteConvertTool.cs

@@ -23,15 +23,15 @@ namespace Azylee.Core.DataUtils.UnitConvertUtils
         public static string Fmt(long size, int digits = 2)
         {
             string rs = "";
-            if (size > 1024 * 1024 * 1024)
+            if (size >= 1024 * 1024 * 1024)
             {
                 rs = Math.Round((double)size / 1024 / 1024 / 1024, digits) + " GB";
             }
-            else if (size > 1024 * 1024)
+            else if (size >= 1024 * 1024)
             {
                 rs = Math.Round((double)size / 1024 / 1024, digits) + " MB";
             }
-            else if (size > 1024)
+            else if (size >= 1024)
             {
                 rs = Math.Round((double)size / 1024, digits) + " KB";
             }
@@ -50,15 +50,15 @@ namespace Azylee.Core.DataUtils.UnitConvertUtils
         public static string Fmt(double size, int digits = 2)
         {
             string rs = "";
-            if (size > 1024 * 1024 * 1024)
+            if (size >= 1024 * 1024 * 1024)
             {
                 rs = Math.Round(size / 1024 / 1024 / 1024, digits) + " GB";
             }
-            else if (size > 1024 * 1024)
+            else if (size >= 1024 * 1024)
             {
                 rs = Math.Round(size / 1024 / 1024, digits) + " MB";
             }
-            else if (size > 1024)
+            else if (size >= 1024)
             {
                 rs = Math.Round(size / 1024, digits) + " KB";
             }

+ 96 - 0
Azylee.Utils/Azylee.Core/FormUtils/FormManTool.cs

@@ -0,0 +1,96 @@
+//************************************************************************
+//      https://github.com/yuzhengyang
+//      author:     yuzhengyang
+//      date:       2017.7.31 - 2017.7.31
+//      desc:       窗体管理器
+//      Copyright (c) yuzhengyang. All rights reserved.
+//************************************************************************
+using Azylee.Core.DataUtils.CollectionUtils;
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Windows.Forms;
+
+namespace Azylee.Core.FormUtils
+{
+    /// <summary>
+    /// 窗体管理器
+    /// </summary>
+    public class FormManTool
+    {
+        public Color? BackColor = null;
+
+        protected ConcurrentDictionary<Type, Form> UniqueForms = new ConcurrentDictionary<Type, Form>();
+
+        public List<Form> AllForms { get { return _AllForms; } }
+        private List<Form> _AllForms = new List<Form>();
+
+
+        public bool Add<T>(T form) where T : Form
+        {
+            if (BackColor != null) form.BackColor = (Color)BackColor;
+            _AllForms.Add(form);
+            return true;
+        }
+        /// <summary>
+        /// 获取唯一窗体对象
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <returns></returns>
+        public T GetUnique<T>() where T : Form, new()
+        {
+            if (UniqueForms.ContainsKey(typeof(T)))
+            {
+                // 字典中有该窗体,则读取窗体对象
+                Form value;
+                if (UniqueForms.TryGetValue(typeof(T), out value))
+                {
+                    if (value != null && !value.IsDisposed)
+                    {
+                        // 窗体对象可用(不为空、没释放),反馈窗体对象
+                        return (T)value;
+                    }
+                    else
+                    {
+                        // 窗体对象不可用,从字典中移除窗体对象
+                        Form temp;
+                        UniqueForms.TryRemove(typeof(T), out temp);
+                    }
+                }
+            }
+
+            // 未能返回正确的窗体,则创建新窗体(使用默认new方法)
+            T form = new T();
+            if (BackColor != null) form.BackColor = (Color)BackColor;
+            if (AddUnique(form)) return form;
+            return null;
+        }
+        private bool AddUnique<T>(T value) where T : Form, new()
+        {
+            if (!UniqueForms.ContainsKey(typeof(T)))
+            {
+                if (UniqueForms.TryAdd(typeof(T), value))
+                {
+                    return true;
+                }
+            }
+            return false;
+        }
+        /// <summary>
+        /// 设置所有窗体的背景色
+        /// </summary>
+        /// <param name="c"></param>
+        public void SetAllBackColor(Color c)
+        {
+            if (ListTool.HasElements(AllForms))
+            {
+                foreach (var form in AllForms)
+                {
+                    if (form != null && !form.IsDisposed)
+                        form.BackColor = c;
+                }
+            }
+        }
+    }
+}

+ 2 - 1
Azylee.Utils/Azylee.Core/LogUtils/StatusLogUtils/StatusLog.cs

@@ -159,7 +159,8 @@ namespace Azylee.Core.LogUtils.StatusLogUtils
                 long afktemp = WindowsAPI.GetLastInputTime() - afk;
                 if (afktemp > 0) status.AFK = status.AFK + afktemp;
                 //计算平均值数据
-                int cpu = (int)ComputerProcessor.NextValue();//CPU占用
+                int cpu = 0;
+                try { cpu = (int)ComputerProcessor.NextValue(); } catch { }//CPU占用
                 long ram = (long)ComputerInfoTool.AvailablePhysicalMemory();//系统可用内存
                 int appcpu = (int)AppInfoTool.CalcCpuRate(AppProcess, pin, Interval);//程序CPU占用
                 long appram = AppInfoTool.RAM();//程序内存占用

+ 1 - 2
Azylee.Utils/Azylee.Core/Properties/AssemblyInfo.cs

@@ -32,5 +32,4 @@ using System.Runtime.InteropServices;
 // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
 //通过使用 "*",如下所示:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyVersion("1.0.0.1")]

+ 11 - 7
Azylee.Utils/Azylee.Core/WindowsUtils/APIUtils/WindowsAPI.cs

@@ -34,13 +34,17 @@ namespace Azylee.Core.WindowsUtils.APIUtils
         /// <returns></returns>
         public static long GetLastInputTime()
         {
-            LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
-            vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
-            // 捕获时间  
-            if (!GetLastInputInfo(ref vLastInputInfo))
-                return 0;
-            else
-                return Environment.TickCount - (long)vLastInputInfo.dwTime;
+            long time = 0;
+            try
+            {
+                LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
+                vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
+                // 捕获时间  
+                if (!GetLastInputInfo(ref vLastInputInfo)) time = 0;
+                else time = Environment.TickCount - (long)vLastInputInfo.dwTime;
+            }
+            catch { }
+            return time > 0 ? time : 0;
         }
         #endregion
 

+ 169 - 0
Azylee.Utils/Azylee.Core/WindowsUtils/HookUtils/KeyboardHook.cs

@@ -0,0 +1,169 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Windows.Forms;
+
+namespace Azylee.Core.WindowsUtils.HookUtils
+{
+    /// <summary>
+    /// 键盘钩子
+    /// [以下代码来自某网友,并非本人原创]
+    /// 测试发现有问题,暂不使用
+    /// </summary>
+    class KeyboardHook
+    {
+        public event System.Windows.Forms.KeyEventHandler KeyDownEvent;
+        public event KeyPressEventHandler KeyPressEvent;
+        public event System.Windows.Forms.KeyEventHandler KeyUpEvent;
+
+        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
+        static int hKeyboardHook = 0; //声明键盘钩子处理的初始值
+        //值在Microsoft SDK的Winuser.h里查询
+        // http://www.bianceng.cn/Programming/csharp/201410/45484.htm
+        public const int WH_KEYBOARD_LL = 13;   //线程键盘钩子监听鼠标消息设为2,全局键盘监听鼠标消息设为13
+        HookProc KeyboardHookProcedure; //声明KeyboardHookProcedure作为HookProc类型
+        //键盘结构
+        [StructLayout(LayoutKind.Sequential)]
+        public class KeyboardHookStruct
+        {
+            public int vkCode;  //定一个虚拟键码。该代码必须有一个价值的范围1至254
+            public int scanCode; // 指定的硬件扫描码的关键
+            public int flags;  // 键标志
+            public int time; // 指定的时间戳记的这个讯息
+            public int dwExtraInfo; // 指定额外信息相关的信息
+        }
+        //使用此功能,安装了一个钩子
+        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
+        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
+
+
+        //调用此函数卸载钩子
+        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
+        public static extern bool UnhookWindowsHookEx(int idHook);
+
+
+        //使用此功能,通过信息钩子继续下一个钩子
+        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
+        public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
+
+        // 取得当前线程编号(线程钩子需要用到)
+        [DllImport("kernel32.dll")]
+        static extern int GetCurrentThreadId();
+
+        //使用WINDOWS API函数代替获取当前实例的函数,防止钩子失效
+        [DllImport("kernel32.dll")]
+        public static extern IntPtr GetModuleHandle(string name);
+
+        public void Start()
+        {
+            // 安装键盘钩子
+            if (hKeyboardHook == 0)
+            {
+                KeyboardHookProcedure = new HookProc(KeyboardHookProc);
+                hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0);
+                //hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
+                //************************************
+                //键盘线程钩子
+                //SetWindowsHookEx( 2,KeyboardHookProcedure, IntPtr.Zero, GetCurrentThreadId());//指定要监听的线程idGetCurrentThreadId(),
+                //键盘全局钩子,需要引用空间(using System.Reflection;)
+                //SetWindowsHookEx( 13,MouseHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
+                //
+                //关于SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId)函数将钩子加入到钩子链表中,说明一下四个参数:
+                //idHook 钩子类型,即确定钩子监听何种消息,上面的代码中设为2,即监听键盘消息并且是线程钩子,如果是全局钩子监听键盘消息应设为13,
+                //线程钩子监听鼠标消息设为7,全局钩子监听鼠标消息设为14。lpfn 钩子子程的地址指针。如果dwThreadId参数为0 或是一个由别的进程创建的
+                //线程的标识,lpfn必须指向DLL中的钩子子程。 除此以外,lpfn可以指向当前进程的一段钩子子程代码。钩子函数的入口地址,当钩子钩到任何
+                //消息后便调用这个函数。hInstance应用程序实例的句柄。标识包含lpfn所指的子程的DLL。如果threadId 标识当前进程创建的一个线程,而且子
+                //程代码位于当前进程,hInstance必须为NULL。可以很简单的设定其为本应用程序的实例句柄。threaded 与安装的钩子子程相关联的线程的标识符
+                //如果为0,钩子子程与所有的线程关联,即为全局钩子
+                //************************************
+                //如果SetWindowsHookEx失败
+                if (hKeyboardHook == 0)
+                {
+                    Stop();
+                    throw new Exception("安装键盘钩子失败");
+                }
+            }
+        }
+        public void Stop()
+        {
+            bool retKeyboard = true;
+
+
+            if (hKeyboardHook != 0)
+            {
+                retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
+                hKeyboardHook = 0;
+            }
+
+            if (!(retKeyboard)) throw new Exception("卸载钩子失败!");
+        }
+        //ToAscii职能的转换指定的虚拟键码和键盘状态的相应字符或字符
+        [DllImport("user32")]
+        public static extern int ToAscii(int uVirtKey, //[in] 指定虚拟关键代码进行翻译。
+                                         int uScanCode, // [in] 指定的硬件扫描码的关键须翻译成英文。高阶位的这个值设定的关键,如果是(不压)
+                                         byte[] lpbKeyState, // [in] 指针,以256字节数组,包含当前键盘的状态。每个元素(字节)的数组包含状态的一个关键。如果高阶位的字节是一套,关键是下跌(按下)。在低比特,如果设置表明,关键是对切换。在此功能,只有肘位的CAPS LOCK键是相关的。在切换状态的NUM个锁和滚动锁定键被忽略。
+                                         byte[] lpwTransKey, // [out] 指针的缓冲区收到翻译字符或字符。
+                                         int fuState); // [in] Specifies whether a menu is active. This parameter must be 1 if a menu is active, or 0 otherwise.
+
+        //获取按键的状态
+        [DllImport("user32")]
+        public static extern int GetKeyboardState(byte[] pbKeyState);
+
+
+        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
+        private static extern short GetKeyState(int vKey);
+
+        private const int WM_KEYDOWN = 0x100;//KEYDOWN
+        private const int WM_KEYUP = 0x101;//KEYUP
+        private const int WM_SYSKEYDOWN = 0x104;//SYSKEYDOWN
+        private const int WM_SYSKEYUP = 0x105;//SYSKEYUP
+
+        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
+        {
+            // 侦听键盘事件
+            if ((nCode >= 0) && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null))
+            {
+                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
+                // raise KeyDown
+                if (KeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
+                {
+                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
+                    System.Windows.Forms.KeyEventArgs e = new System.Windows.Forms.KeyEventArgs(keyData);
+                    KeyDownEvent(this, e);
+                }
+
+                //键盘按下
+                if (KeyPressEvent != null && wParam == WM_KEYDOWN)
+                {
+                    byte[] keyState = new byte[256];
+                    GetKeyboardState(keyState);
+
+                    byte[] inBuffer = new byte[2];
+                    if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1)
+                    {
+                        KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);
+                        KeyPressEvent(this, e);
+                    }
+                }
+
+                // 键盘抬起
+                if (KeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
+                {
+                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
+                    System.Windows.Forms.KeyEventArgs e = new System.Windows.Forms.KeyEventArgs(keyData);
+                    KeyUpEvent(this, e);
+                }
+
+            }
+            //如果返回1,则结束消息,这个消息到此为止,不再传递。
+            //如果返回0或调用CallNextHookEx函数则消息出了这个钩子继续往下传递,也就是传给消息真正的接受者
+            return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
+        }
+        ~KeyboardHook()
+        {
+            Stop();
+        }
+    }
+}

+ 39 - 0
Azylee.Utils/Azylee.Core/WindowsUtils/HookUtils/KeyboardHookHelper.cs

@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.WindowsUtils.HookUtils
+{
+    class KeyboardHookHelper
+    {
+        //KeyboardHook k_hook;
+        //public hook()
+        //{
+        //    InitializeComponent();
+        //    k_hook = new KeyboardHook();
+        //    //k_hook.KeyDownEvent += new System.Windows.Forms.KeyEventHandler(hook_KeyDown);//钩住键按下 
+        //    k_hook.KeyPressEvent += K_hook_KeyPressEvent;
+        //    k_hook.Start();//安装键盘钩子
+        //}
+
+        //private void K_hook_KeyPressEvent(object sender, KeyPressEventArgs e)
+        //{
+        //    //tb1.Text += e.KeyChar;
+        //    int i = (int)e.KeyChar;
+        //    System.Windows.Forms.MessageBox.Show(i.ToString());
+        //}
+
+        //private void hook_KeyDown(object sender, KeyEventArgs e)
+        //{
+        //    tb1.Text += (char)e.KeyData;
+
+
+        //    //判断按下的键(Alt + A) 
+        //    //if (e.KeyValue == (int)Keys.A && (int)System.Windows.Forms.Control.ModifierKeys == (int)Keys.Alt)
+        //    //{
+        //    //    System.Windows.Forms.MessageBox.Show("ddd");
+        //    //}
+        //}
+    }
+}

+ 7 - 0
Azylee.Utils/Azylee.YeahWeb/BaiDuWebAPI/IPLocationAPI/IPLocationModel.cs

@@ -45,6 +45,13 @@ namespace Azylee.YeahWeb.BaiDuWebAPI.IPLocationAPI
             catch { }
             return false;
         }
+        public override string ToString()
+        {
+            StringBuilder sb = new StringBuilder();
+            sb.Append(FirstPOI?.Address + ",");
+            sb.Append(SematicDescription ?? "");
+            return sb.ToString();
+        }
     }
     /// <summary>
     /// 地址组成

BIN
nuget/Azylee.Core/Azylee.Core.1.0.0.nupkg


BIN
nuget/Azylee.Core/dlls/Azylee.Core.dll


ファイルの差分が大きいため隠しています
+ 2543 - 0
nuget/Azylee.Core/dlls/Azylee.Core.xml