Commit d56ce959 HZH

添加GraphicalOverlay组件

1 个父辈 2ce7c3bc
......@@ -185,15 +185,16 @@ namespace HZH_Controls.Controls
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
g.SetGDIHigh();
Color c1 = lampColor[intColorIndex];
g.FillEllipse(new SolidBrush(c1), this.ClientRectangle);
g.FillEllipse(new SolidBrush(c1), new Rectangle(this.ClientRectangle.Location, this.ClientRectangle.Size - new Size(1, 1)));
if (isHighlight)
{
GraphicsPath gp = new GraphicsPath();
Rectangle rec = new Rectangle(5, 5, this.Width - 10, this.Height - 10);
Rectangle rec = new Rectangle(5, 5, this.Width - 10 - 1, this.Height - 10 - 1);
gp.AddEllipse(rec);
Color[] surroundColor = new Color[] { c1 };
......@@ -205,7 +206,7 @@ namespace HZH_Controls.Controls
if (isShowBorder)
{
g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), 2), new Rectangle(4, 4, this.Width - 8, this.Height - 8));
g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), 2), new Rectangle(4, 4, this.Width - 1 - 8, this.Height - 1 - 8));
}
}
}
......
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace HZH_Controls.Controls
{
[DefaultEvent("Paint")]
public partial class GraphicalOverlay : Component
{
public event EventHandler<PaintEventArgs> Paint;
public GraphicalOverlay()
{
InitializeComponent();
}
public GraphicalOverlay(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private Control owner;
public Control Owner
{
get { return owner; }
set
{
// The owner form cannot be set to null.
if (value == null)
throw new ArgumentNullException();
// The owner form can only be set once.
if (owner != null)
throw new InvalidOperationException();
// Save the form for future reference.
owner = value;
// Handle the form's Resize event.
owner.Resize += new EventHandler(Form_Resize);
// Handle the Paint event for each of the controls in the form's hierarchy.
ConnectPaintEventHandlers(owner);
}
}
private void Form_Resize(object sender, EventArgs e)
{
owner.Invalidate(true);
}
private void ConnectPaintEventHandlers(Control control)
{
// Connect the paint event handler for this control.
// Remove the existing handler first (if one exists) and replace it.
control.Paint -= new PaintEventHandler(Control_Paint);
control.Paint += new PaintEventHandler(Control_Paint);
control.ControlAdded -= new ControlEventHandler(Control_ControlAdded);
control.ControlAdded += new ControlEventHandler(Control_ControlAdded);
// Recurse the hierarchy.
foreach (Control child in control.Controls)
ConnectPaintEventHandlers(child);
}
private void Control_ControlAdded(object sender, ControlEventArgs e)
{
// Connect the paint event handler for the new control.
ConnectPaintEventHandlers(e.Control);
}
private void Control_Paint(object sender, PaintEventArgs e)
{
// As each control on the form is repainted, this handler is called.
Control control = sender as Control;
Point location;
// Determine the location of the control's client area relative to the form's client area.
if (control == owner)
// The form's client area is already form-relative.
location = control.Location;
else
{
// The control may be in a hierarchy, so convert to screen coordinates and then back to form coordinates.
location = owner.PointToClient(control.Parent.PointToScreen(control.Location));
// If the control has a border shift the location of the control's client area.
location += new Size((control.Width - control.ClientSize.Width) / 2, (control.Height - control.ClientSize.Height) / 2);
}
// Translate the location so that we can use form-relative coordinates to draw on the control.
if (control != owner)
e.Graphics.TranslateTransform(-location.X, -location.Y);
// Fire a paint event.
OnPaint(sender, e);
}
private void OnPaint(object sender, PaintEventArgs e)
{
// Fire a paint event.
// The paint event will be handled in Form1.graphicalOverlay1_Paint().
if (Paint != null)
Paint(sender, e);
}
}
}
namespace System.Windows.Forms
{
using System.Drawing;
public static class Extensions
{
public static Rectangle Coordinates(this Control control)
{
// Extend System.Windows.Forms.Control to have a Coordinates property.
// The Coordinates property contains the control's form-relative location.
Rectangle coordinates;
Form form = (Form)control.TopLevelControl;
if (control == form)
coordinates = form.ClientRectangle;
else
coordinates = form.RectangleToClient(control.Parent.RectangleToScreen(control.Bounds));
return coordinates;
}
}
}
namespace HZH_Controls.Controls
{
partial class GraphicalOverlay
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
......@@ -70,6 +70,12 @@
<Compile Include="Controls\Charts\RadarChart\UCRadarChart.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="Controls\GraphicalOverlay\GraphicalOverlay.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Controls\GraphicalOverlay\GraphicalOverlay.designer.cs">
<DependentUpon>GraphicalOverlay.cs</DependentUpon>
</Compile>
<Compile Include="Controls\Navigation\CrumbNavigationClickEventArgs.cs" />
<Compile Include="Controls\Navigation\CrumbNavigationItem.cs" />
<Compile Include="Controls\ScrollBar\ScrollbarComponent.cs">
......
......@@ -59,6 +59,7 @@ namespace Test
tnControl.Nodes.Add("有标题的面板");
tnControl.Nodes.Add("图标");
tnControl.Nodes.Add("滚动条");
tnControl.Nodes.Add("控件水印");
this.tvMenu.Nodes.Add(tnControl);
TreeNode tnCharts = new TreeNode(" 图表");
......@@ -242,7 +243,9 @@ namespace Test
case "滚动条":
AddControl(new UC.UCTestScrollbar() { Dock = DockStyle.Fill });
break;
case "控件水印":
AddControl(new UC.UCTestGraphicalOverlay() { Dock = DockStyle.Fill });
break;
#endregion
#region 图表 English:Chart
......
......@@ -128,6 +128,12 @@
<Compile Include="UC\UCTestFunnelChart.Designer.cs">
<DependentUpon>UCTestFunnelChart.cs</DependentUpon>
</Compile>
<Compile Include="UC\UCTestGraphicalOverlay.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="UC\UCTestGraphicalOverlay.Designer.cs">
<DependentUpon>UCTestGraphicalOverlay.cs</DependentUpon>
</Compile>
<Compile Include="UC\UCTestIcon.cs">
<SubType>UserControl</SubType>
</Compile>
......@@ -362,6 +368,9 @@
<EmbeddedResource Include="UC\UCTestFunnelChart.resx">
<DependentUpon>UCTestFunnelChart.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="UC\UCTestGraphicalOverlay.resx">
<DependentUpon>UCTestGraphicalOverlay.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="UC\UCTestGridTable.resx">
<DependentUpon>UCTestGridTable.cs</DependentUpon>
</EmbeddedResource>
......
namespace Test.UC
{
partial class UCTestGraphicalOverlay
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.graphicalOverlay1 = new HZH_Controls.Controls.GraphicalOverlay(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// graphicalOverlay1
//
this.graphicalOverlay1.Owner = this;
this.graphicalOverlay1.Paint += new System.EventHandler<System.Windows.Forms.PaintEventArgs>(this.graphicalOverlay1_Paint);
//
// button1
//
this.button1.Location = new System.Drawing.Point(272, 23);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 23);
this.button1.TabIndex = 0;
this.button1.Text = "显示/隐藏颜色";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(38, 23);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "添加按钮";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(160, 23);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 1;
this.button3.Text = "添加Label";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(36, 66);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(407, 12);
this.label1.TabIndex = 2;
this.label1.Text = "graphicalOverlay的paint事件可以做更多的事情,这里已覆盖控件颜色演示";
//
// UCTestGraphicalOverlay
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "UCTestGraphicalOverlay";
this.Size = new System.Drawing.Size(681, 528);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private HZH_Controls.Controls.GraphicalOverlay graphicalOverlay1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HZH_Controls;
namespace Test.UC
{
public partial class UCTestGraphicalOverlay : UserControl
{
Random r = new Random();
bool blnColor = true;
public UCTestGraphicalOverlay()
{
InitializeComponent();
}
private void graphicalOverlay1_Paint(object sender, PaintEventArgs e)
{
if (blnColor)
{
e.Graphics.SetGDIHigh();
foreach (Control item in this.Controls)
{
if (item is Button)
{
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, r.Next(0, 255), r.Next(0, 255), r.Next(0, 255))), item.Bounds);
}
else if (item is Label)
{
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, r.Next(0, 255), r.Next(0, 255), r.Next(0, 255))), item.Bounds);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "随机按钮";
btn.Location = new Point(r.Next(0, this.Width - btn.Width), r.Next(0, this.Height - btn.Height));
this.Controls.Add(btn);
}
private void button1_Click(object sender, EventArgs e)
{
blnColor = !blnColor;
this.Invalidate(true);
}
private void button3_Click(object sender, EventArgs e)
{
Label lbl = new Label();
lbl.Text = "随机标签";
lbl.Location = new Point(r.Next(0, this.Width - lbl.Width), r.Next(0, this.Height - lbl.Height));
this.Controls.Add(lbl);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="graphicalOverlay1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>
\ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!