You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

292 lines
9.7 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection.Emit;
using System.Windows.Forms.VisualStyles;
namespace Control2
{
public partial class AdvancedLabel : UserControl
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoEllipsis = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Left;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 16);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.label1.Click += new System.EventHandler(this.label1_Click);
this.label1.Resize += new System.EventHandler(this.label1_Resize);
//
// label2
//
this.label2.AutoEllipsis = true;
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label2.Dock = System.Windows.Forms.DockStyle.Right;
this.label2.Location = new System.Drawing.Point(56, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// AdvancedLabel
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.MinimumSize = new System.Drawing.Size(70, 16);
this.Name = "AdvancedLabel";
this.Size = new System.Drawing.Size(112, 16);
this.Load += new System.EventHandler(this.AdvancedLabel_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
public AdvancedLabel()
{
InitializeComponent();
}
public String Txt
{
get => label1.Text;
set
{
if (InvokeRequired)
label1.BeginInvoke((MethodInvoker)(() => label1.Text = value));
else
label1.Text = value;
}
}
public String Value
{
get => label2.Text;
set
{
if (InvokeRequired)
label2.BeginInvoke((MethodInvoker)(() => label2.Text = value));
else
label2.Text = value;
}
}
public void Val<T>(T value)
{
label2.Text = value.ToString();
}
public Int32 Width1
{
get => label1.Width;
set
{
Width = value + label2.Width + Space;
label1.Width = value;
}
}
public Int32 Width2
{
get => label2.Width;
set
{
label2.Width = value;
Width = value + label1.Width + Space;
}
}
public Int32 Space
{
get
{
return Width - label1.Width - label2.Width;
}
set
{
Width = value + label1.Width + label2.Width;
// label2.Left = value + label1.Width;
}
}
public Int32 LabelWidth
{
get
{
return label1.Width + Space + label2.Width;
}
set
{
Width = value;
}
}
public System.Drawing.ContentAlignment ValAlign
{
get
{
return label2.TextAlign;
}
set
{
label2.TextAlign = value;
}
}
public BorderStyle Border
{
get
{
return label2.BorderStyle;
}
set
{
label2.BorderStyle = value;
}
}
private void label1_Resize(object sender, EventArgs e)
{
// label2.Top = label1.Top;
// label2.Left = label1.Left + label1.Width + 6;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void AdvancedLabel_Load(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
}
public class AdvancedRadioButton : CheckBox
{
public enum Level { Parent, Form };
[Category("AdvancedRadioButton"),
Description("Gets or sets the level that specifies which RadioButton controls are affected."),
DefaultValue(Level.Parent)]
public Level GroupNameLevel { get; set; }
[Category("AdvancedRadioButton"),
Description("Gets or sets the name that specifies which RadioButton controls are mutually exclusive.")]
public string GroupName { get; set; }
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
if (Checked)
{
var arbControls = (dynamic)null;
switch (GroupNameLevel)
{
case Level.Parent:
if (this.Parent != null)
arbControls = GetAll(this.Parent, typeof(AdvancedRadioButton));
break;
case Level.Form:
Form form = this.FindForm();
if (form != null)
arbControls = GetAll(this.FindForm(), typeof(AdvancedRadioButton));
break;
}
if (arbControls != null)
foreach (Control control in arbControls)
if (control != this &&
(control as AdvancedRadioButton).GroupName == this.GroupName)
(control as AdvancedRadioButton).Checked = false;
}
}
protected override void OnClick(EventArgs e)
{
if (!Checked)
base.OnClick(e);
}
protected override void OnPaint(PaintEventArgs pevent)
{
CheckBoxRenderer.DrawParentBackground(pevent.Graphics, pevent.ClipRectangle, this);
RadioButtonState radioButtonState;
if (Checked)
{
radioButtonState = RadioButtonState.CheckedNormal;
if (Focused)
radioButtonState = RadioButtonState.CheckedHot;
if (!Enabled)
radioButtonState = RadioButtonState.CheckedDisabled;
}
else
{
radioButtonState = RadioButtonState.UncheckedNormal;
if (Focused)
radioButtonState = RadioButtonState.UncheckedHot;
if (!Enabled)
radioButtonState = RadioButtonState.UncheckedDisabled;
}
Size glyphSize = RadioButtonRenderer.GetGlyphSize(pevent.Graphics, radioButtonState);
Rectangle rect = pevent.ClipRectangle;
rect.Width -= glyphSize.Width;
rect.Location = new Point(rect.Left + glyphSize.Width, rect.Top);
RadioButtonRenderer.DrawRadioButton(pevent.Graphics, new System.Drawing.Point(0, rect.Height / 2 - glyphSize.Height / 2), rect, this.Text, this.Font, this.Focused, radioButtonState);
}
private IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
}
}