C# winform 自定义透明控件,可以重叠

         

直接上代码:

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.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;

namespace WFControl
{
public partial class ArrowLine : UserControl
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020;
return cp;
}
}
public ArrowLine()
{
InitializeComponent();
[subscribe]this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);[/subscribe]
this.SetStyle(ControlStyles.ResizeRedraw, false);
}
private int mlineHeight = 5;
private byte mdirection = 0;

///

/// 线条宽度.
///

[Description(“宽度”)]

public int LineHeight
{
get
{
return mlineHeight;
}
set
{
mlineHeight = value;
}
}
private int mIsArrow = 0;
///

/// 是否显示箭头.
///

[Description(“箭头”)]
public int IsArrow
{
get { return mIsArrow; }
set
{
mIsArrow = value;
}
}
///

/// 线条方向
///0:从右至左;1:从下至上;2:从左至右;3:从上至下.
///

[Description(“类型”)]

public byte Direction
{
get
{
return mdirection;
}
set
{
mdirection = value;
}
}
[Description(“颜色”)]
public Color color
{
get;
set;
}
protected override void OnPaint(PaintEventArgs e)
{
Color drawColor = Color.FromArgb(0, Color.Purple);
Brush brush = new SolidBrush(drawColor);

Pen pen = new Pen(this.color);
pen.Width = LineHeight;
AdjustableArrowCap aac = null;
AdjustableArrowCap aac1 = null;
if (mIsArrow == 1)
{
aac = new AdjustableArrowCap(4, 2);
pen.CustomStartCap = aac;
}
if (mIsArrow == 2)
{
aac = new AdjustableArrowCap(4, 2);
pen.CustomEndCap = aac;
}
if (mIsArrow == 3)
{
aac = new AdjustableArrowCap(4, 2);
aac1 = new AdjustableArrowCap(4, 2);
pen.CustomStartCap = aac;
pen.CustomEndCap = aac1;
}

int lenWidth = mlineHeight;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
if (Direction == 0)
{
// 0:从右至左; 1:从下至上;2:从左至右; 3:从上至下.
x1 = this.Size.Width;
y1 = this.Size.Height / 2;
x2 = 0;
y2 = y1;
}
else if (Direction == 1)
{
x1 = this.Size.Width / 2;
y1 = this.Size.Height;
x2 = x1;
y2 = 0;
}
else if (Direction == 2)
{
//2:从左至右;
x1 = 0;
y1 = this.Size.Height / 2;
x2 = this.Size.Width;
y2 = y1;
}
else if (Direction == 3)
{
//3:从上至下.
x1 = this.Size.Width / 2;
y1 = 0;
x2 = x1;
y2 = this.Size.Height;
}

//e.Graphics.FillRectangle(brush, 0, 0, this.Size.Width, this.Size.Height);
e.Graphics.DrawLine(pen, x1, y1, x2, y2);
pen.Dispose();
if (mIsArrow == 1 || mIsArrow == 2)
{
aac.Dispose();
}
if (mIsArrow == 3)
{
aac.Dispose();
aac1.Dispose();
}
brush.Dispose();
}
}
}

打赏
         

flying

   

码农

发表评论

电子邮件地址不会被公开。 必填项已用*标注