效果图:
1.透明TextBox效果和水印效果
2.背影半透明效果
新建一个FormLogins窗体
项目添加BaseTextBox 类库
在此类库下建立一个TextBox1类,实现背景透明效果和水印效果
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BaseTextBox
{
[ToolboxBitmap(typeof(TextBox))]
public class TextBox1 : TextBox
{
private string _emptyTextTip;
private Color _emptyTextTipColor = Color.DarkGray;
private const int WM_PAINT = 0xF;
public TextBox1() : base()
{
}
[DefaultValue("")]
public string EmptyTextTip
{
get { return _emptyTextTip; }
set
{
_emptyTextTip = value;
base.Invalidate();
}
}
[DefaultValue(typeof(Color), "DarkGray")]
public Color EmptyTextTipColor
{
get { return _emptyTextTipColor; }
set
{
_emptyTextTipColor = value;
base.Invalidate();
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
WmPaint(ref m);
}
}
private void WmPaint(ref Message m)
{
using (Graphics graphics = Graphics.FromHwnd(base.Handle))
{
if (Text.Length == 0
&& !string.IsNullOrEmpty(_emptyTextTip)
&& !Focused)
{
TextFormatFlags format =
TextFormatFlags.EndEllipsis |
TextFormatFlags.VerticalCenter;
if (RightToLeft == RightToLeft.Yes)
{
format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
}
TextRenderer.DrawText(
graphics,
_emptyTextTip,
Font,
base.ClientRectangle,
_emptyTextTipColor,
format);
}
}
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr LoadLibrary(string lpFileName);
protected override CreateParams CreateParams
{
get
{
CreateParams prams = base.CreateParams;
if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
{
prams.ExStyle |= 0x020; // transparent
prams.ClassName = "RICHEDIT50W";
}
return prams;
}
}
}
}
出自:https://blog.csdn.net/x894802545/article/details/48736087
再添加一个类库 BaseContro
里面新建一个 TransparentRect 类,实现背影效果
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 BaseContro
{
public class TransparentRect : UserControl
{
public enum ShapeBorderStyles
{
ShapeBSNone,
ShapeBSFixedSingle,
};
private ShapeBorderStyles _borderStyle = ShapeBorderStyles.ShapeBSNone;
private Color _backColor = Color.Black;
private Color _borderColor = Color.White;
private int _radius = 20;
private int _opacity = 125;
protected int pointX = 0;
protected int pointY = 0;
protected Rectangle iRect = new Rectangle();
public TransparentRect()
{
base.BackColor = Color.Transparent;
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
SetStyle(ControlStyles.Opaque, false);
UpdateStyles();
}
public new Color BackColor
{
get { return _backColor; }
set { _backColor = value; Invalidate(); }
}
public ShapeBorderStyles ShapeBorderStyle
{
get { return _borderStyle; }
set { _borderStyle = value; this.Invalidate(); }
}
public Color BorderColor
{
get { return _borderColor; }
set { _borderColor = value; Invalidate(); }
}
public int Opacity
{
get { return _opacity; }
set { _opacity = value; this.Invalidate(); }
}
public int Radius
{
get { return _radius; }
set { _radius = value; this.Invalidate(); }
}
public override Color ForeColor
{
get { return base.ForeColor; }
set { base.ForeColor = value; this.Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
SmoothingMode sm = e.Graphics.SmoothingMode;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if (_borderStyle == ShapeBorderStyles.ShapeBSFixedSingle) DrawBorder(e.Graphics);
DrawLabelBackground(e.Graphics);
e.Graphics.SmoothingMode = sm;
}
private void DrawBorder(Graphics g)
{
Rectangle rect = this.ClientRectangle;
rect.Width--;
rect.Height--;
using (GraphicsPath bp = GetPath(rect, _radius))
{
using (Pen p = new Pen(_borderColor))
{
g.DrawPath(p, bp);
}
}
}
private void DrawLabelBackground(Graphics g)
{
Rectangle rect = this.ClientRectangle;
iRect = rect;
rect.X++;
rect.Y++;
rect.Width -= 2;
rect.Height -= 2;
using (GraphicsPath bb = GetPath(rect, _radius))
{
using (Brush br = new SolidBrush(Color.FromArgb(_opacity, _backColor)))
{
g.FillPath(br, bb);
}
}
}
protected GraphicsPath GetPath(Rectangle rc, int r)
{
int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
r = r << 1;
GraphicsPath path = new GraphicsPath();
if (r > 0)
{
if (r > h) { r = h; };
if (r > w) { r = w; };
path.AddArc(x, y, r, r, 180, 90);
path.AddArc(x + w - r, y, r, r, 270, 90);
path.AddArc(x + w - r, y + h - r, r, r, 0, 90);
path.AddArc(x, y + h - r, r, r, 90, 90);
path.CloseFigure();
}
else
{
path.AddRectangle(rc);
}
return path;
}
}
}
项目结构:
最后,重新编译解决方案。打开FormLogins窗体界面,再打开工具栏。
工具栏会出现重写后的两个组件,TextBox1,TransparentRect
然后就可以像平常使用winform组件一样,拉进窗体使劲造了。
出自:https://bbs.csdn.net/topics/392587176
代码:https://download.csdn.net/download/weixin_39237340/15107248
参考文献:https://bbs.csdn.net/topics/392587176
参考文献:https://www.cnblogs.com/ZCoding/p/4269637.html
参考文献:https://blog.csdn.net/x894802545/article/details/48736087