mirror of
https://github.com/eggplantlwj/VisionEdit.git
synced 2026-03-25 09:16:34 +08:00
1、优化LOG显示与引用
2、添加PMA工具,工具内容待完善 3、修复流程树显示 4、添加开源项目,优化UI空间 5、其他BUG更改
This commit is contained in:
1446
UsingControl/HZHControls/Helpers/ControlHelper.cs
Normal file
1446
UsingControl/HZHControls/Helpers/ControlHelper.cs
Normal file
File diff suppressed because it is too large
Load Diff
476
UsingControl/HZHControls/Helpers/Ext.cs
Normal file
476
UsingControl/HZHControls/Helpers/Ext.cs
Normal file
@@ -0,0 +1,476 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : HZH_Controls
|
||||
// Created : 08-08-2019
|
||||
//
|
||||
// ***********************************************************************
|
||||
// <copyright file="Ext.cs">
|
||||
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
|
||||
// </copyright>
|
||||
//
|
||||
// Blog: https://www.cnblogs.com/bfyx
|
||||
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
|
||||
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
|
||||
//
|
||||
// If you use this code, please keep this note.
|
||||
// ***********************************************************************
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace HZH_Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// Class Ext.
|
||||
/// </summary>
|
||||
public static partial class Ext
|
||||
{
|
||||
/// <summary>
|
||||
/// Clones the model.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="classObject">The class object.</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T CloneModel<T>(this T classObject) where T : class
|
||||
{
|
||||
T result;
|
||||
if (classObject == null)
|
||||
{
|
||||
result = default(T);
|
||||
}
|
||||
else
|
||||
{
|
||||
object obj = Activator.CreateInstance(typeof(T));
|
||||
PropertyInfo[] properties = typeof(T).GetProperties();
|
||||
PropertyInfo[] array = properties;
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
PropertyInfo propertyInfo = array[i];
|
||||
if (propertyInfo.CanWrite)
|
||||
propertyInfo.SetValue(obj, propertyInfo.GetValue(classObject, null), null);
|
||||
}
|
||||
result = (obj as T);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// ASCII编码的数组转换为英文字符串
|
||||
/// </summary>
|
||||
/// <param name="s">字符串</param>
|
||||
/// <returns>结果</returns>
|
||||
public static string ToEnString(this byte[] s)
|
||||
{
|
||||
return ToEncodeString(s, Encoding.ASCII).Trim('\0').Trim();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数组按指定编码转换为字符串
|
||||
/// </summary>
|
||||
/// <param name="dealBytes">数组</param>
|
||||
/// <param name="encode">编码</param>
|
||||
/// <returns>结果</returns>
|
||||
public static string ToEncodeString(this byte[] dealBytes, Encoding encode)
|
||||
{
|
||||
return encode.GetString(dealBytes);
|
||||
}
|
||||
#region 转换为base64字符串
|
||||
/// <summary>
|
||||
/// 功能描述:转换为base64字符串
|
||||
/// 作 者:HZH
|
||||
/// 创建日期:2019-03-29 10:12:38
|
||||
/// 任务编号:POS
|
||||
/// </summary>
|
||||
/// <param name="data">data</param>
|
||||
/// <returns>返回值</returns>
|
||||
public static string ToBase64Str(this string data)
|
||||
{
|
||||
if (data.IsEmpty())
|
||||
return string.Empty;
|
||||
byte[] buffer = Encoding.Default.GetBytes(data);
|
||||
return Convert.ToBase64String(buffer);
|
||||
}
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// 转换为坐标
|
||||
/// </summary>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <returns>System.Drawing.Point.</returns>
|
||||
public static System.Drawing.Point ToPoint(this string data)
|
||||
{
|
||||
if (!System.Text.RegularExpressions.Regex.IsMatch(data, @"^\s*\d+(\.\d+)?\s*\,\s*\d+(\.\d+)?\s*$"))
|
||||
{
|
||||
return System.Drawing.Point.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
string[] strs = data.Split(',');
|
||||
return new System.Drawing.Point(strs[0].ToInt(), strs[1].ToInt());
|
||||
}
|
||||
}
|
||||
|
||||
#region 数值转换
|
||||
/// <summary>
|
||||
/// 转换为整型
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns>System.Int32.</returns>
|
||||
public static int ToInt(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return 0;
|
||||
if (data is bool)
|
||||
{
|
||||
return (bool)data ? 1 : 0;
|
||||
}
|
||||
int result;
|
||||
var success = int.TryParse(data.ToString(), out result);
|
||||
if (success)
|
||||
return result;
|
||||
try
|
||||
{
|
||||
return Convert.ToInt32(ToDouble(data, 0));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空整型
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns>System.Nullable<System.Int32>.</returns>
|
||||
public static int? ToIntOrNull(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return null;
|
||||
int result;
|
||||
bool isValid = int.TryParse(data.ToString(), out result);
|
||||
if (isValid)
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为双精度浮点数
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns>System.Double.</returns>
|
||||
public static double ToDouble(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return 0;
|
||||
double result;
|
||||
return double.TryParse(data.ToString(), out result) ? result : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为双精度浮点数,并按指定的小数位4舍5入
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="digits">小数位数</param>
|
||||
/// <returns>System.Double.</returns>
|
||||
public static double ToDouble(this object data, int digits)
|
||||
{
|
||||
return Math.Round(ToDouble(data), digits, System.MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空双精度浮点数
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns>System.Nullable<System.Double>.</returns>
|
||||
public static double? ToDoubleOrNull(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return null;
|
||||
double result;
|
||||
bool isValid = double.TryParse(data.ToString(), out result);
|
||||
if (isValid)
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为高精度浮点数
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns>System.Decimal.</returns>
|
||||
public static decimal ToDecimal(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return 0;
|
||||
decimal result;
|
||||
return decimal.TryParse(data.ToString(), out result) ? result : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为高精度浮点数,并按指定的小数位4舍5入
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="digits">小数位数</param>
|
||||
/// <returns>System.Decimal.</returns>
|
||||
public static decimal ToDecimal(this object data, int digits)
|
||||
{
|
||||
return Math.Round(ToDecimal(data), digits, System.MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空高精度浮点数
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns>System.Nullable<System.Decimal>.</returns>
|
||||
public static decimal? ToDecimalOrNull(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return null;
|
||||
decimal result;
|
||||
bool isValid = decimal.TryParse(data.ToString(), out result);
|
||||
if (isValid)
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空高精度浮点数,并按指定的小数位4舍5入
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="digits">小数位数</param>
|
||||
/// <returns>System.Nullable<System.Decimal>.</returns>
|
||||
public static decimal? ToDecimalOrNull(this object data, int digits)
|
||||
{
|
||||
var result = ToDecimalOrNull(data);
|
||||
if (result == null)
|
||||
return null;
|
||||
return Math.Round(result.Value, digits, System.MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 日期转换
|
||||
/// <summary>
|
||||
/// 转换为日期
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns>DateTime.</returns>
|
||||
public static DateTime ToDate(this object data)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (data == null)
|
||||
return DateTime.MinValue;
|
||||
if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{8}$"))
|
||||
{
|
||||
string strValue = data.ToStringExt();
|
||||
return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt());
|
||||
}
|
||||
DateTime result;
|
||||
return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空日期
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns>System.Nullable<DateTime>.</returns>
|
||||
public static DateTime? ToDateOrNull(this object data)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (data == null)
|
||||
return null;
|
||||
if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{8}$"))
|
||||
{
|
||||
string strValue = data.ToStringExt();
|
||||
return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt());
|
||||
}
|
||||
DateTime result;
|
||||
bool isValid = DateTime.TryParse(data.ToString(), out result);
|
||||
if (isValid)
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 布尔转换
|
||||
/// <summary>
|
||||
/// 转换为布尔值
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
public static bool ToBool(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return false;
|
||||
bool? value = GetBool(data);
|
||||
if (value != null)
|
||||
return value.Value;
|
||||
bool result;
|
||||
return bool.TryParse(data.ToString(), out result) && result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取布尔值
|
||||
/// </summary>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
private static bool? GetBool(this object data)
|
||||
{
|
||||
switch (data.ToString().Trim().ToLower())
|
||||
{
|
||||
case "0":
|
||||
return false;
|
||||
case "1":
|
||||
return true;
|
||||
case "是":
|
||||
return true;
|
||||
case "否":
|
||||
return false;
|
||||
case "yes":
|
||||
return true;
|
||||
case "no":
|
||||
return false;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空布尔值
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
public static bool? ToBoolOrNull(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return null;
|
||||
bool? value = GetBool(data);
|
||||
if (value != null)
|
||||
return value.Value;
|
||||
bool result;
|
||||
bool isValid = bool.TryParse(data.ToString(), out result);
|
||||
if (isValid)
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 字符串转换
|
||||
/// <summary>
|
||||
/// 字符串转换为byte[]
|
||||
/// </summary>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <returns>System.Byte[].</returns>
|
||||
public static byte[] ToBytes(this string data)
|
||||
{
|
||||
return System.Text.Encoding.GetEncoding("GBK").GetBytes(data);
|
||||
}
|
||||
/// <summary>
|
||||
/// Converts to bytesdefault.
|
||||
/// </summary>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <returns>System.Byte[].</returns>
|
||||
public static byte[] ToBytesDefault(this string data)
|
||||
{
|
||||
return System.Text.Encoding.Default.GetBytes(data);
|
||||
}
|
||||
/// <summary>
|
||||
/// 转换为字符串
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns>System.String.</returns>
|
||||
public static string ToStringExt(this object data)
|
||||
{
|
||||
return data == null ? string.Empty : data.ToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 安全返回值
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="value">可空值</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T SafeValue<T>(this T? value) where T : struct
|
||||
{
|
||||
return value ?? default(T);
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否为空
|
||||
/// </summary>
|
||||
/// <param name="value">值</param>
|
||||
/// <returns><c>true</c> if the specified value is empty; otherwise, <c>false</c>.</returns>
|
||||
public static bool IsEmpty(this string value)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(value);
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否为空
|
||||
/// </summary>
|
||||
/// <param name="value">值</param>
|
||||
/// <returns><c>true</c> if the specified value is empty; otherwise, <c>false</c>.</returns>
|
||||
public static bool IsEmpty(this Guid? value)
|
||||
{
|
||||
if (value == null)
|
||||
return true;
|
||||
return IsEmpty(value.Value);
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否为空
|
||||
/// </summary>
|
||||
/// <param name="value">值</param>
|
||||
/// <returns><c>true</c> if the specified value is empty; otherwise, <c>false</c>.</returns>
|
||||
public static bool IsEmpty(this Guid value)
|
||||
{
|
||||
if (value == Guid.Empty)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否为空
|
||||
/// </summary>
|
||||
/// <param name="value">值</param>
|
||||
/// <returns><c>true</c> if the specified value is empty; otherwise, <c>false</c>.</returns>
|
||||
public static bool IsEmpty(this object value)
|
||||
{
|
||||
if (value != null && !string.IsNullOrEmpty(value.ToString()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
#region 是否数字
|
||||
/// <summary>
|
||||
/// 功能描述:是否数字
|
||||
/// 作 者:HZH
|
||||
/// 创建日期:2019-03-06 09:03:05
|
||||
/// 任务编号:POS
|
||||
/// </summary>
|
||||
/// <param name="value">value</param>
|
||||
/// <returns>返回值</returns>
|
||||
public static bool IsNum(this string value)
|
||||
{
|
||||
return System.Text.RegularExpressions.Regex.IsMatch(value, @"^\d+(\.\d*)?$");
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
45
UsingControl/HZHControls/Helpers/GraphDirection.cs
Normal file
45
UsingControl/HZHControls/Helpers/GraphDirection.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : HZH_Controls
|
||||
// Created : 2019-09-17
|
||||
//
|
||||
// ***********************************************************************
|
||||
// <copyright file="GraphDirection.cs">
|
||||
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
|
||||
// </copyright>
|
||||
//
|
||||
// Blog: https://www.cnblogs.com/bfyx
|
||||
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
|
||||
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
|
||||
//
|
||||
// If you use this code, please keep this note.
|
||||
// ***********************************************************************
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace HZH_Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// Enum GraphDirection
|
||||
/// </summary>
|
||||
public enum GraphDirection
|
||||
{
|
||||
/// <summary>
|
||||
/// The upward
|
||||
/// </summary>
|
||||
Upward = 1,
|
||||
/// <summary>
|
||||
/// The downward
|
||||
/// </summary>
|
||||
Downward,
|
||||
/// <summary>
|
||||
/// The leftward
|
||||
/// </summary>
|
||||
Leftward,
|
||||
/// <summary>
|
||||
/// The rightward
|
||||
/// </summary>
|
||||
Rightward
|
||||
}
|
||||
}
|
||||
53
UsingControl/HZHControls/Helpers/ITheme.cs
Normal file
53
UsingControl/HZHControls/Helpers/ITheme.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Drawing;
|
||||
|
||||
namespace HZH_Controls
|
||||
{
|
||||
public interface ITheme
|
||||
{
|
||||
/// <summary>
|
||||
/// 重置主题
|
||||
/// </summary>
|
||||
/// <param name="theme"></param>
|
||||
void ResetTheme(ThemeEntity theme);
|
||||
/// <summary>
|
||||
/// 是否禁用一键换肤,默认禁用
|
||||
/// </summary>
|
||||
bool EnabledTheme { get; set; }
|
||||
}
|
||||
|
||||
public class ThemeEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 边框颜色
|
||||
/// </summary>
|
||||
public Color? BorderColor { get; set; }
|
||||
/// <summary>
|
||||
/// 填充颜色
|
||||
/// </summary>
|
||||
public Color? FillColor { get; set; }
|
||||
/// <summary>
|
||||
/// 背景色
|
||||
/// </summary>
|
||||
public Color? BackColor { get; set; }
|
||||
/// <summary>
|
||||
/// 前景色
|
||||
/// </summary>
|
||||
public Color? ForeColor { get; set; }
|
||||
/// <summary>
|
||||
/// 字体颜色
|
||||
/// </summary>
|
||||
public Color? FontColor { get; set; }
|
||||
/// <summary>
|
||||
/// 选中颜色
|
||||
/// </summary>
|
||||
public Color? SelectedColor { get; set; }
|
||||
/// <summary>
|
||||
/// 选中字体颜色
|
||||
/// </summary>
|
||||
public Color? SelectedFontColor { get; set; }
|
||||
}
|
||||
}
|
||||
213
UsingControl/HZHControls/Helpers/MouseHook.cs
Normal file
213
UsingControl/HZHControls/Helpers/MouseHook.cs
Normal file
@@ -0,0 +1,213 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : HZH_Controls
|
||||
// Created : 08-08-2019
|
||||
//
|
||||
// ***********************************************************************
|
||||
// <copyright file="MouseHook.cs">
|
||||
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
|
||||
// </copyright>
|
||||
//
|
||||
// Blog: https://www.cnblogs.com/bfyx
|
||||
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
|
||||
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
|
||||
//
|
||||
// If you use this code, please keep this note.
|
||||
// ***********************************************************************
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace HZH_Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// 鼠标全局钩子
|
||||
/// </summary>
|
||||
public static class MouseHook
|
||||
{
|
||||
/// <summary>
|
||||
/// The wm mousemove
|
||||
/// </summary>
|
||||
private const int WM_MOUSEMOVE = 0x200;
|
||||
/// <summary>
|
||||
/// The wm lbuttondown
|
||||
/// </summary>
|
||||
private const int WM_LBUTTONDOWN = 0x201;
|
||||
/// <summary>
|
||||
/// The wm rbuttondown
|
||||
/// </summary>
|
||||
private const int WM_RBUTTONDOWN = 0x204;
|
||||
/// <summary>
|
||||
/// The wm mbuttondown
|
||||
/// </summary>
|
||||
private const int WM_MBUTTONDOWN = 0x207;
|
||||
/// <summary>
|
||||
/// The wm lbuttonup
|
||||
/// </summary>
|
||||
private const int WM_LBUTTONUP = 0x202;
|
||||
/// <summary>
|
||||
/// The wm rbuttonup
|
||||
/// </summary>
|
||||
private const int WM_RBUTTONUP = 0x205;
|
||||
/// <summary>
|
||||
/// The wm mbuttonup
|
||||
/// </summary>
|
||||
private const int WM_MBUTTONUP = 0x208;
|
||||
/// <summary>
|
||||
/// The wm lbuttondblclk
|
||||
/// </summary>
|
||||
private const int WM_LBUTTONDBLCLK = 0x203;
|
||||
/// <summary>
|
||||
/// The wm rbuttondblclk
|
||||
/// </summary>
|
||||
private const int WM_RBUTTONDBLCLK = 0x206;
|
||||
/// <summary>
|
||||
/// The wm mbuttondblclk
|
||||
/// </summary>
|
||||
private const int WM_MBUTTONDBLCLK = 0x209;
|
||||
|
||||
/// <summary>
|
||||
/// 点
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public class POINT
|
||||
{
|
||||
/// <summary>
|
||||
/// The x
|
||||
/// </summary>
|
||||
public int x;
|
||||
/// <summary>
|
||||
/// The y
|
||||
/// </summary>
|
||||
public int y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 钩子结构体
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public class MouseHookStruct
|
||||
{
|
||||
/// <summary>
|
||||
/// The pt
|
||||
/// </summary>
|
||||
public POINT pt;
|
||||
/// <summary>
|
||||
/// The h WND
|
||||
/// </summary>
|
||||
public int hWnd;
|
||||
/// <summary>
|
||||
/// The w hit test code
|
||||
/// </summary>
|
||||
public int wHitTestCode;
|
||||
/// <summary>
|
||||
/// The dw extra information
|
||||
/// </summary>
|
||||
public int dwExtraInfo;
|
||||
}
|
||||
|
||||
|
||||
// 全局的鼠标事件
|
||||
/// <summary>
|
||||
/// Occurs when [on mouse activity].
|
||||
/// </summary>
|
||||
public static event MouseEventHandler OnMouseActivity;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The h mouse hook
|
||||
/// </summary>
|
||||
private static int _hMouseHook = 0; // 鼠标钩子句柄
|
||||
/// <summary>
|
||||
/// 启动全局钩子
|
||||
/// </summary>
|
||||
/// <exception cref="System.Exception">SetWindowsHookEx failed.</exception>
|
||||
/// <exception cref="Exception">SetWindowsHookEx failed.</exception>
|
||||
public static void Start()
|
||||
{
|
||||
// 安装鼠标钩子
|
||||
if (_hMouseHook != 0)
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
// 生成一个HookProc的实例.
|
||||
WindowsHook.HookMsgChanged += WindowsHook_HookMsgChanged;
|
||||
_hMouseHook = WindowsHook.StartHook(HookType.WH_MOUSE_LL);
|
||||
|
||||
//假设装置失败停止钩子
|
||||
if (_hMouseHook == 0)
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
|
||||
static void WindowsHook_HookMsgChanged(string strHookName, int nCode, IntPtr msg, IntPtr lParam)
|
||||
{
|
||||
// 假设正常执行而且用户要监听鼠标的消息
|
||||
if (nCode >= 0 && OnMouseActivity != null)
|
||||
{
|
||||
MouseButtons button = MouseButtons.None;
|
||||
int clickCount = 0;
|
||||
|
||||
switch ((int)msg)
|
||||
{
|
||||
case WM_LBUTTONDOWN:
|
||||
button = MouseButtons.Left;
|
||||
clickCount = 1;
|
||||
break;
|
||||
case WM_LBUTTONUP:
|
||||
button = MouseButtons.Left;
|
||||
clickCount = 1;
|
||||
break;
|
||||
case WM_LBUTTONDBLCLK:
|
||||
button = MouseButtons.Left;
|
||||
clickCount = 2;
|
||||
break;
|
||||
case WM_RBUTTONDOWN:
|
||||
button = MouseButtons.Right;
|
||||
clickCount = 1;
|
||||
break;
|
||||
case WM_RBUTTONUP:
|
||||
button = MouseButtons.Right;
|
||||
clickCount = 1;
|
||||
break;
|
||||
case WM_RBUTTONDBLCLK:
|
||||
button = MouseButtons.Right;
|
||||
clickCount = 2;
|
||||
break;
|
||||
}
|
||||
if (button != MouseButtons.None && clickCount > 0)
|
||||
{
|
||||
// 从回调函数中得到鼠标的信息
|
||||
MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
|
||||
MouseEventArgs e = new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0);
|
||||
OnMouseActivity(null, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止全局钩子
|
||||
/// </summary>
|
||||
/// <exception cref="System.Exception">UnhookWindowsHookEx failed.</exception>
|
||||
/// <exception cref="Exception">UnhookWindowsHookEx failed.</exception>
|
||||
public static void Stop()
|
||||
{
|
||||
bool retMouse = true;
|
||||
|
||||
if (_hMouseHook != 0)
|
||||
{
|
||||
retMouse = WindowsHook.StopHook(_hMouseHook);
|
||||
_hMouseHook = 0;
|
||||
}
|
||||
|
||||
// 假设卸下钩子失败
|
||||
if (!(retMouse))
|
||||
throw new Exception("UnhookWindowsHookEx failed.");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
327
UsingControl/HZHControls/Helpers/NativeMethods.cs
Normal file
327
UsingControl/HZHControls/Helpers/NativeMethods.cs
Normal file
@@ -0,0 +1,327 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : HZH_Controls
|
||||
// Created : 08-08-2019
|
||||
//
|
||||
// ***********************************************************************
|
||||
// <copyright file="NativeMethods.cs">
|
||||
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
|
||||
// </copyright>
|
||||
//
|
||||
// Blog: https://www.cnblogs.com/bfyx
|
||||
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
|
||||
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
|
||||
//
|
||||
// If you use this code, please keep this note.
|
||||
// ***********************************************************************
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace HZH_Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// Class NativeMethods.
|
||||
/// </summary>
|
||||
internal class NativeMethods
|
||||
{
|
||||
/// <summary>
|
||||
/// Enum ComboBoxButtonState
|
||||
/// </summary>
|
||||
public enum ComboBoxButtonState
|
||||
{
|
||||
/// <summary>
|
||||
/// The state system none
|
||||
/// </summary>
|
||||
STATE_SYSTEM_NONE,
|
||||
/// <summary>
|
||||
/// The state system invisible
|
||||
/// </summary>
|
||||
STATE_SYSTEM_INVISIBLE = 32768,
|
||||
/// <summary>
|
||||
/// The state system pressed
|
||||
/// </summary>
|
||||
STATE_SYSTEM_PRESSED = 8
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Struct RECT
|
||||
/// </summary>
|
||||
public struct RECT
|
||||
{
|
||||
/// <summary>
|
||||
/// The left
|
||||
/// </summary>
|
||||
public int Left;
|
||||
|
||||
/// <summary>
|
||||
/// The top
|
||||
/// </summary>
|
||||
public int Top;
|
||||
|
||||
/// <summary>
|
||||
/// The right
|
||||
/// </summary>
|
||||
public int Right;
|
||||
|
||||
/// <summary>
|
||||
/// The bottom
|
||||
/// </summary>
|
||||
public int Bottom;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the rect.
|
||||
/// </summary>
|
||||
/// <value>The rect.</value>
|
||||
public Rectangle Rect
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Rectangle(this.Left, this.Top, this.Right - this.Left, this.Bottom - this.Top);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size.
|
||||
/// </summary>
|
||||
/// <value>The size.</value>
|
||||
public Size Size
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Size(this.Right - this.Left, this.Bottom - this.Top);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RECT" /> struct.
|
||||
/// </summary>
|
||||
/// <param name="left">The left.</param>
|
||||
/// <param name="top">The top.</param>
|
||||
/// <param name="right">The right.</param>
|
||||
/// <param name="bottom">The bottom.</param>
|
||||
public RECT(int left, int top, int right, int bottom)
|
||||
{
|
||||
this.Left = left;
|
||||
this.Top = top;
|
||||
this.Right = right;
|
||||
this.Bottom = bottom;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RECT" /> struct.
|
||||
/// </summary>
|
||||
/// <param name="rect">The rect.</param>
|
||||
public RECT(Rectangle rect)
|
||||
{
|
||||
this.Left = rect.Left;
|
||||
this.Top = rect.Top;
|
||||
this.Right = rect.Right;
|
||||
this.Bottom = rect.Bottom;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Froms the xywh.
|
||||
/// </summary>
|
||||
/// <param name="x">The x.</param>
|
||||
/// <param name="y">The y.</param>
|
||||
/// <param name="width">The width.</param>
|
||||
/// <param name="height">The height.</param>
|
||||
/// <returns>NativeMethods.RECT.</returns>
|
||||
public static NativeMethods.RECT FromXYWH(int x, int y, int width, int height)
|
||||
{
|
||||
return new NativeMethods.RECT(x, y, x + width, y + height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Froms the rectangle.
|
||||
/// </summary>
|
||||
/// <param name="rect">The rect.</param>
|
||||
/// <returns>NativeMethods.RECT.</returns>
|
||||
public static NativeMethods.RECT FromRectangle(Rectangle rect)
|
||||
{
|
||||
return new NativeMethods.RECT(rect.Left, rect.Top, rect.Right, rect.Bottom);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Struct PAINTSTRUCT
|
||||
/// </summary>
|
||||
public struct PAINTSTRUCT
|
||||
{
|
||||
/// <summary>
|
||||
/// The HDC
|
||||
/// </summary>
|
||||
public IntPtr hdc;
|
||||
|
||||
/// <summary>
|
||||
/// The f erase
|
||||
/// </summary>
|
||||
public int fErase;
|
||||
|
||||
/// <summary>
|
||||
/// The rc paint
|
||||
/// </summary>
|
||||
public NativeMethods.RECT rcPaint;
|
||||
|
||||
/// <summary>
|
||||
/// The f restore
|
||||
/// </summary>
|
||||
public int fRestore;
|
||||
|
||||
/// <summary>
|
||||
/// The f inc update
|
||||
/// </summary>
|
||||
public int fIncUpdate;
|
||||
|
||||
/// <summary>
|
||||
/// The reserved1
|
||||
/// </summary>
|
||||
public int Reserved1;
|
||||
|
||||
/// <summary>
|
||||
/// The reserved2
|
||||
/// </summary>
|
||||
public int Reserved2;
|
||||
|
||||
/// <summary>
|
||||
/// The reserved3
|
||||
/// </summary>
|
||||
public int Reserved3;
|
||||
|
||||
/// <summary>
|
||||
/// The reserved4
|
||||
/// </summary>
|
||||
public int Reserved4;
|
||||
|
||||
/// <summary>
|
||||
/// The reserved5
|
||||
/// </summary>
|
||||
public int Reserved5;
|
||||
|
||||
/// <summary>
|
||||
/// The reserved6
|
||||
/// </summary>
|
||||
public int Reserved6;
|
||||
|
||||
/// <summary>
|
||||
/// The reserved7
|
||||
/// </summary>
|
||||
public int Reserved7;
|
||||
|
||||
/// <summary>
|
||||
/// The reserved8
|
||||
/// </summary>
|
||||
public int Reserved8;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Struct ComboBoxInfo
|
||||
/// </summary>
|
||||
public struct ComboBoxInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// The cb size
|
||||
/// </summary>
|
||||
public int cbSize;
|
||||
|
||||
/// <summary>
|
||||
/// The rc item
|
||||
/// </summary>
|
||||
public NativeMethods.RECT rcItem;
|
||||
|
||||
/// <summary>
|
||||
/// The rc button
|
||||
/// </summary>
|
||||
public NativeMethods.RECT rcButton;
|
||||
|
||||
/// <summary>
|
||||
/// The state button
|
||||
/// </summary>
|
||||
public NativeMethods.ComboBoxButtonState stateButton;
|
||||
|
||||
/// <summary>
|
||||
/// The HWND combo
|
||||
/// </summary>
|
||||
public IntPtr hwndCombo;
|
||||
|
||||
/// <summary>
|
||||
/// The HWND edit
|
||||
/// </summary>
|
||||
public IntPtr hwndEdit;
|
||||
|
||||
/// <summary>
|
||||
/// The HWND list
|
||||
/// </summary>
|
||||
public IntPtr hwndList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The wm paint
|
||||
/// </summary>
|
||||
public const int WM_PAINT = 15;
|
||||
|
||||
/// <summary>
|
||||
/// The wm setredraw
|
||||
/// </summary>
|
||||
public const int WM_SETREDRAW = 11;
|
||||
|
||||
/// <summary>
|
||||
/// The false
|
||||
/// </summary>
|
||||
public static readonly IntPtr FALSE = IntPtr.Zero;
|
||||
|
||||
/// <summary>
|
||||
/// The true
|
||||
/// </summary>
|
||||
public static readonly IntPtr TRUE = new IntPtr(1);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ComboBox information.
|
||||
/// </summary>
|
||||
/// <param name="hwndCombo">The HWND combo.</param>
|
||||
/// <param name="info">The information.</param>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
[DllImport("user32.dll")]
|
||||
public static extern bool GetComboBoxInfo(IntPtr hwndCombo, ref NativeMethods.ComboBoxInfo info);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the window rect.
|
||||
/// </summary>
|
||||
/// <param name="hwnd">The HWND.</param>
|
||||
/// <param name="lpRect">The lp rect.</param>
|
||||
/// <returns>System.Int32.</returns>
|
||||
[DllImport("user32.dll")]
|
||||
public static extern int GetWindowRect(IntPtr hwnd, ref NativeMethods.RECT lpRect);
|
||||
|
||||
/// <summary>
|
||||
/// Begins the paint.
|
||||
/// </summary>
|
||||
/// <param name="hWnd">The h WND.</param>
|
||||
/// <param name="ps">The ps.</param>
|
||||
/// <returns>IntPtr.</returns>
|
||||
[DllImport("user32.dll")]
|
||||
public static extern IntPtr BeginPaint(IntPtr hWnd, ref NativeMethods.PAINTSTRUCT ps);
|
||||
|
||||
/// <summary>
|
||||
/// Ends the paint.
|
||||
/// </summary>
|
||||
/// <param name="hWnd">The h WND.</param>
|
||||
/// <param name="ps">The ps.</param>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
[DllImport("user32.dll")]
|
||||
public static extern bool EndPaint(IntPtr hWnd, ref NativeMethods.PAINTSTRUCT ps);
|
||||
|
||||
/// <summary>
|
||||
/// Sends the message.
|
||||
/// </summary>
|
||||
/// <param name="hWnd">The h WND.</param>
|
||||
/// <param name="msg">The MSG.</param>
|
||||
/// <param name="wParam">The w parameter.</param>
|
||||
/// <param name="lParam">The l parameter.</param>
|
||||
[DllImport("user32.dll")]
|
||||
public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
|
||||
}
|
||||
}
|
||||
67
UsingControl/HZHControls/Helpers/TextInputType.cs
Normal file
67
UsingControl/HZHControls/Helpers/TextInputType.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : HZH_Controls
|
||||
// Created : 08-08-2019
|
||||
//
|
||||
// ***********************************************************************
|
||||
// <copyright file="TextInputType.cs">
|
||||
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
|
||||
// </copyright>
|
||||
//
|
||||
// Blog: https://www.cnblogs.com/bfyx
|
||||
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
|
||||
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
|
||||
//
|
||||
// If you use this code, please keep this note.
|
||||
// ***********************************************************************
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace HZH_Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// 功能描述:文本控件输入类型
|
||||
/// 作 者:HZH
|
||||
/// 创建日期:2019-02-28 10:09:00
|
||||
/// </summary>
|
||||
public enum TextInputType
|
||||
{
|
||||
/// <summary>
|
||||
/// 不控制输入
|
||||
/// </summary>
|
||||
[Description("不控制输入")]
|
||||
NotControl = 1,
|
||||
/// <summary>
|
||||
/// 任意数字
|
||||
/// </summary>
|
||||
[Description("任意数字")]
|
||||
Number = 2,
|
||||
/// <summary>
|
||||
/// 非负数
|
||||
/// </summary>
|
||||
[Description("非负数")]
|
||||
UnsignNumber = 4,
|
||||
/// <summary>
|
||||
/// 正数
|
||||
/// </summary>
|
||||
[Description("正数")]
|
||||
PositiveNumber = 8,
|
||||
/// <summary>
|
||||
/// 整数
|
||||
/// </summary>
|
||||
[Description("整数")]
|
||||
Integer = 16,
|
||||
/// <summary>
|
||||
/// 非负整数
|
||||
/// </summary>
|
||||
[Description("非负整数")]
|
||||
PositiveInteger = 32,
|
||||
/// <summary>
|
||||
/// 正则验证
|
||||
/// </summary>
|
||||
[Description("正则验证")]
|
||||
Regex = 64
|
||||
}
|
||||
}
|
||||
178
UsingControl/HZHControls/Helpers/WindowsHook.cs
Normal file
178
UsingControl/HZHControls/Helpers/WindowsHook.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace HZH_Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// 钩子类型
|
||||
/// </summary>
|
||||
public enum HookType : int
|
||||
{
|
||||
/// <summary>
|
||||
/// 安装一个钩子过程,该过程监视由于对话框,消息框,菜单或滚动条中的输入事件而生成的消息。
|
||||
/// 有关更多信息,请参阅MessageProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644987(v=vs.85))挂接过程。
|
||||
/// </summary>
|
||||
WH_MSGFILTER = -1,
|
||||
/// <summary>
|
||||
/// 安装一个钩子过程,记录发布到系统消息队列的输入消息。 此挂钩对于录制宏非常有用。
|
||||
/// 有关更多信息,请参阅JournalRecordProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644983(v=vs.85))挂钩过程。
|
||||
/// </summary>
|
||||
WH_JOURNALRECORD = 0,
|
||||
/// <summary>
|
||||
/// 安装一个挂钩过程,该过程发布先前由WH_JOURNALRECORD挂钩过程记录的消息。
|
||||
/// 有关更多信息,请参阅JournalPlaybackProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644982(v=vs.85))挂钩过程。
|
||||
/// </summary>
|
||||
WH_JOURNALPLAYBACK = 1,
|
||||
/// <summary>
|
||||
/// 安装一个监视击键消息的钩子程序。
|
||||
/// 有关更多信息,请参阅KeyboardProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644984(v=vs.85))挂钩过程。
|
||||
/// </summary>
|
||||
WH_KEYBOARD = 2,
|
||||
/// <summary>
|
||||
/// 安装一个钩子过程来监视发布到消息队列的消息。
|
||||
/// 有关更多信息,请参阅GetMsgProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644981(v=vs.85))挂接过程。
|
||||
/// </summary>
|
||||
WH_GETMESSAGE = 3,
|
||||
/// <summary>
|
||||
/// 安装一个钩子过程,在系统将消息发送到目标窗口过程之前监视消息。
|
||||
/// 有关更多信息,请参阅CallWndProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644975(v=vs.85))挂接过程。
|
||||
/// </summary>
|
||||
WH_CALLWNDPROC = 4,
|
||||
/// <summary>
|
||||
/// 安装一个钩子程序,接收对CBT应用程序有用的通知。
|
||||
/// 有关更多信息,请参阅CBTProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644977(v=vs.85))挂钩过程。
|
||||
/// </summary>
|
||||
WH_CBT = 5,
|
||||
/// <summary>
|
||||
/// 安装一个钩子过程,该过程监视由于对话框,消息框,菜单或滚动条中的输入事件而生成的消息。
|
||||
/// 钩子过程监视与调用线程在同一桌面中的所有应用程序的这些消息。
|
||||
/// 有关更多信息,请参阅SysMsgProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644992(v=vs.85))挂接过程。
|
||||
/// </summary>
|
||||
WH_SYSMSGFILTER = 6,
|
||||
/// <summary>
|
||||
/// 安装监视鼠标消息的钩子过程。
|
||||
/// 有关更多信息,请参阅MouseProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644988(v=vs.85))挂钩过程。
|
||||
/// </summary>
|
||||
WH_MOUSE = 7,
|
||||
/// <summary>
|
||||
/// 安装一个用于调试其他钩子过程的钩子过程。
|
||||
/// 有关更多信息,请参阅DebugProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644978(v=vs.85))挂接过程。
|
||||
/// </summary>
|
||||
WH_DEBUG = 9,
|
||||
/// <summary>
|
||||
/// 安装一个钩子过程,接收对shell应用程序有用的通知。
|
||||
/// 有关更多信息,请参阅ShellProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644991(v=vs.85))挂钩过程。
|
||||
/// </summary>
|
||||
WH_SHELL = 10,
|
||||
/// <summary>
|
||||
/// 安装一个钩子过程,当应用程序的前台线程即将变为空闲时将调用该过程。
|
||||
/// 此挂钩对于在空闲时执行低优先级任务非常有用。
|
||||
/// 有关更多信息,请参阅ForegroundIdleProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644980(v=vs.85))挂钩过程。
|
||||
/// </summary>
|
||||
WH_FOREGROUNDIDLE = 11,
|
||||
/// <summary>
|
||||
/// 安装一个钩子过程,该过程在目标窗口过程处理完消息后对其进行监视。
|
||||
/// 有关更多信息,请参阅CallWndRetProc(https://docs.microsoft.com/windows/desktop/api/winuser/nc-winuser-hookproc)挂接过程。
|
||||
/// </summary>
|
||||
WH_CALLWNDPROCRET = 12,
|
||||
/// <summary>
|
||||
/// 安装一个监视低级键盘输入事件的钩子过程。 有关更多信息,
|
||||
/// 请参阅LowLevelKeyboardProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644985(v=vs.85))挂接过程。
|
||||
/// </summary>
|
||||
WH_KEYBOARD_LL = 13,
|
||||
/// <summary>
|
||||
/// 安装一个监视低级鼠标输入事件的钩子过程。 有关更多信息,
|
||||
/// 请参阅LowLevelMouseProc(https://docs.microsoft.com/previous-versions/windows/desktop/legacy/ms644986(v=vs.85))挂接过程。
|
||||
/// </summary>
|
||||
WH_MOUSE_LL = 14,
|
||||
}
|
||||
public class WindowsHook
|
||||
{
|
||||
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
|
||||
// 装置钩子的函数
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
||||
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, int 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, IntPtr wParam, IntPtr lParam);
|
||||
[DllImport("User32.dll", CharSet = CharSet.Auto)]
|
||||
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
|
||||
/// <summary>
|
||||
/// Delegate HookMsgHandler
|
||||
/// </summary>
|
||||
/// <param name="strHookName">钩子名称</param>
|
||||
/// <param name="msg">消息值</param>
|
||||
public delegate void HookMsgHandler(string strHookName, int nCode, IntPtr msg, IntPtr lParam);
|
||||
/// <summary>
|
||||
/// 钩子消息事件
|
||||
/// </summary>
|
||||
public static event HookMsgHandler HookMsgChanged;
|
||||
/// <summary>
|
||||
/// 启动一个钩子
|
||||
/// </summary>
|
||||
/// <param name="hookType">钩子类型</param>
|
||||
/// <param name="wParam">模块句柄,为空则为当前模块</param>
|
||||
/// <param name="pid">进程句柄,默认为0则表示当前进程</param>
|
||||
/// <param name="strHookName">钩子名称</param>
|
||||
/// <returns>钩子句柄(消耗钩子时需要使用)</returns>
|
||||
/// <exception cref="Exception">SetWindowsHookEx failed.</exception>
|
||||
public static int StartHook(HookType hookType, int wParam = 0, int pid = 0, string strHookName = "")
|
||||
{
|
||||
int _hHook = 0;
|
||||
// 生成一个HookProc的实例.
|
||||
var _hookProcedure = new HookProc((nCode, msg, lParam) =>
|
||||
{
|
||||
|
||||
if (HookMsgChanged != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
HookMsgChanged(strHookName, nCode, msg, lParam);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
int inext = CallNextHookEx(_hHook, nCode, msg, lParam);
|
||||
return inext;
|
||||
});
|
||||
if (pid ==0)
|
||||
pid = AppDomain.GetCurrentThreadId();
|
||||
_hHook = SetWindowsHookEx((int)hookType, _hookProcedure, wParam, pid);
|
||||
|
||||
//假设装置失败停止钩子
|
||||
if (_hHook == 0)
|
||||
{
|
||||
StopHook(_hHook);
|
||||
}
|
||||
return _hHook;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止钩子
|
||||
/// </summary>
|
||||
/// <param name="_hHook">StartHook函数返回的钩子句柄</param>
|
||||
/// <returns><c>true</c> if 停止成功, <c>false</c> 否则.</returns>
|
||||
public static bool StopHook(int _hHook)
|
||||
{
|
||||
bool ret = true;
|
||||
|
||||
if (_hHook != 0)
|
||||
{
|
||||
ret = UnhookWindowsHookEx(_hHook);
|
||||
}
|
||||
|
||||
// 假设卸下钩子失败
|
||||
if (!ret)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user