Files
2026-07-14 13:55:17 +08:00

83 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JYControl
{
public static class ControlHelper
{
/// <summary>
/// The m LST freeze control
/// </summary>
static Dictionary<Control, bool> m_lstFreezeControl = new Dictionary<Control, bool>();
public static void FreezeControl(Control control, bool blnToFreeze)
{
if (blnToFreeze && control.IsHandleCreated && control.Visible && !control.IsDisposed && (!m_lstFreezeControl.ContainsKey(control) || (m_lstFreezeControl.ContainsKey(control) && m_lstFreezeControl[control] == false)))
{
m_lstFreezeControl[control] = true;
control.Disposed += control_Disposed;
NativeMethods.SendMessage(control.Handle, 11, 0, 0);
}
else if (!blnToFreeze && !control.IsDisposed && m_lstFreezeControl.ContainsKey(control) && m_lstFreezeControl[control] == true)
{
m_lstFreezeControl.Remove(control);
NativeMethods.SendMessage(control.Handle, 11, 1, 0);
control.Invalidate(true);
}
}
/// <summary>
/// Handles the Disposed event of the control control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
static void control_Disposed(object sender, EventArgs e)
{
try
{
if (m_lstFreezeControl.ContainsKey((Control)sender))
m_lstFreezeControl.Remove((Control)sender);
}
catch { }
}
/// <summary>
/// 设置GDI高质量模式抗锯齿
/// </summary>
/// <param name="g">The g.</param>
public static void SetGDIHigh(this Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
}
/// <summary>
/// 根据矩形和圆得到一个圆角矩形Path
/// </summary>
/// <param name="rect">The rect.</param>
/// <param name="cornerRadius">The corner radius.</param>
/// <returns>GraphicsPath.</returns>
public static GraphicsPath CreateRoundedRectanglePath(this Rectangle rect, int cornerRadius)
{
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;
}
}
}