vb.net2008半于控件透明或半透明(不是模拟透明,希望真实透明)的求助与讨论,有样板请老师们指导,多谢~~~
最近研究关于控件透明的问题,在网上找了很多方法,都不能达到目标要求,肯请老师们指导~~~
1、设定控件(如:Lable)背景图为Transparent,产生模拟透明效果,可与窗体背景产生透明,但控件之间相互不能产生透明。
2、WPF做的控件,可以实现真实透明效果,但相对的鼠标事件又不能达到像winform控件这样的效果
3、用gdi+绘制了图形,可以实现真实透明效果,但不知道如何实现鼠标事件。图形画出来是最能达到预期效果的,看好多贴子都说这样可以实现真实透明。但没有介绍如何实现事件,肯请赐教。
希望达到的效果如下:
不知道人家这是用什么方法做出来的。控件是绝对透明的,而且还可以响应鼠标事件。拖动,拉伸~~~~
多谢赐教~~~
以下是绘制的两个矩形,不知道如何响应事件。
Dim p As Graphics = Me.CreateGraphics
Dim mp As New Pen(Color.Black, 2)
Dim pt As New SolidBrush(Color.FromArgb(150, Color.Blue))
Dim p1 As New Rectangle(100, 100, 100, 100)
Dim p2 As New Rectangle(120, 120, 100, 100)
p.DrawRectangle(mp, p1)
p.DrawRectangle(mp, p2)
p.FillRectangle(pt, p1)
p.FillRectangle(pt, p2)
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Monitor
{
public partial class TransparentInfo : System.Windows.Forms.UserControl
{
private string m_strInfo;
public void ShowInfo(string strInfo)
{
m_strInfo = strInfo;
this.Invalidate();
}
private void TransparentInfo_MouseEnter(object sender, EventArgs e)
{
this.Visible = true;
}
public bool drag = false;
public bool enab = false;
private Color fillColor = Color.White;
private int opacity = 75;
private int alpha;
public TransparentInfo()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
this.BackColor = Color.Transparent;
}
public Color FillColor
{
get
{
return this.fillColor;
}
set
{
this.fillColor = value;
if (this.Parent != null) Parent.Invalidate(this.Bounds, true);
}
}
public int Opacity
{
get
{
if (opacity > 100) { opacity = 100; }
else if (opacity < 1) { opacity = 1; }
return this.opacity;
}
set
{
this.opacity = value;
if (this.Parent != null) Parent.Invalidate(this.Bounds, true);
}
}
//protected override CreateParams CreateParams
//{
// get
// {
// CreateParams cp = base.CreateParams;
// cp.ExStyle
[解决办法]
= 0x20;
// return cp;
// }
//}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle bounds = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
Color frmColor = this.Parent.BackColor;
Brush brushColor;
Brush bckColor;
alpha = (opacity * 255) / 100;
if (drag)
{
Color dragFillColor;
Color dragBckColor;
if (BackColor != Color.Transparent)
{
int Rb = BackColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255;
int Gb = BackColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255;
int Bb = BackColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255;
dragBckColor = Color.FromArgb(Rb, Gb, Bb);
}
else dragBckColor = frmColor;
if (fillColor != Color.Transparent)
{
int Rf = fillColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255;
int Gf = fillColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255;
int Bf = fillColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255;
dragFillColor = Color.FromArgb(Rf, Gf, Bf);
}
else dragFillColor = dragBckColor;
alpha = 255;
brushColor = new SolidBrush(Color.FromArgb(alpha, dragFillColor));
bckColor = new SolidBrush(Color.FromArgb(alpha, dragBckColor));
}
else
{
Color color = fillColor;
brushColor = new SolidBrush(Color.FromArgb(alpha, color));
bckColor = new SolidBrush(Color.FromArgb(alpha, this.BackColor));
}
if (this.BackColor != Color.Transparent
[解决办法]
drag)
{
g.FillRectangle(bckColor, bounds);
}
if(m_strInfo!=null && m_strInfo.Length>0)
{
g.DrawString(m_strInfo, this.Font, new SolidBrush(this.ForeColor), 10, 10);
}
brushColor.Dispose();
bckColor.Dispose();
g.Dispose();
base.OnPaint(e);
}
protected override void OnBackColorChanged(EventArgs e)
{
if (this.Parent != null) Parent.Invalidate(this.Bounds, true);
base.OnBackColorChanged(e);
}
protected override void OnParentBackColorChanged(EventArgs e)
{
this.Invalidate();
base.OnParentBackColorChanged(e);
}
}
}
简单地处理一个静态图片文件,可以用用GDI+之类的。如果是很现实地开发大一点的应用,请把精力放在WPF上。