using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
namespace Mesnac.Controls.ChemicalWeighing
{
public static class ControlHelper
{
#region 设置控件Enabled,切不改变控件颜色
///
/// 功能描述:设置控件Enabled,切不改变控件颜色
/// 作 者:HZH
/// 创建日期:2019-03-04 13:43:32
/// 任务编号:POS
///
/// c
/// enabled
public static void SetControlEnabled(this Control c, bool enabled)
{
if (!c.IsDisposed)
{
if (enabled)
{
ControlHelper.SetWindowLong(c.Handle, -16, -134217729 & ControlHelper.GetWindowLong(c.Handle, -16));
}
else
{
ControlHelper.SetWindowLong(c.Handle, -16, 134217728 + ControlHelper.GetWindowLong(c.Handle, -16));
}
}
}
///
/// 功能描述:设置控件Enabled,切不改变控件颜色
/// 作 者:HZH
/// 创建日期:2019-03-04 13:43:32
/// 任务编号:POS
///
/// cs
/// enabled
public static void SetControlEnableds(Control[] cs, bool enabled)
{
for (int i = 0; i < cs.Length; i++)
{
Control c = cs[i];
SetControlEnabled(c, enabled);
}
}
#endregion
///
/// Sets the window long.
///
/// The h WND.
/// Index of the n.
/// The wndproc.
/// System.Int32.
[DllImport("user32.dll ")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int wndproc);
///
/// Gets the window long.
///
/// The h WND.
/// Index of the n.
/// System.Int32.
[DllImport("user32.dll ")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
///
/// Gets the foreground window.
///
/// IntPtr.
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
///
/// Threads the base call back.
///
/// The parent.
/// The object.
private static void ThreadBaseCallBack(Control parent, object obj)
{
if (obj is Exception)
{
if (parent != null)
{
ThreadInvokerControl(parent, delegate
{
Exception ex = obj as Exception;
});
}
}
}
///
/// 委托调用主线程控件
///
/// 主线程控件
/// 修改控件方法
public static void ThreadInvokerControl(Control parent, System.Action action)
{
if (parent != null)
{
if (parent.InvokeRequired)
{
parent.BeginInvoke(action);
}
else
{
action();
SetForegroundWindow(parent.Handle);
}
}
}
///
/// 使用一个线程执行一个操作
///
/// 父控件
/// 执行内容
/// 执行后回调
/// 执行期间禁用控件列表
/// 执行期间是否显示等待提示
/// 执行期间等待提示内容,默认为“正在处理,请稍候...”
/// 延迟显示等待提示时间
//public static void ThreadRunExt(
// Control parent,
// System.Action func,
// Action