C# WindowsMediaPlayer顶部的PictureBox透明面板
我在我的C#(桌面)应用程序中嵌入了WindowsMediaPlayer,希望在其上放置一个透明的C# WindowsMediaPlayer顶部的PictureBox透明面板,c#,winforms,panel,transparency,windows-media-player,C#,Winforms,Panel,Transparency,Windows Media Player,我在我的C#(桌面)应用程序中嵌入了WindowsMediaPlayer,希望在其上放置一个透明的面板或图片盒 由于Panel或PictureBox都没有TransparencyKey属性,因此我无法使用,BackColor=TransparencyKey=Color.Magenta方法 我已尝试将面板的背景色设置为Color.Transparent。它似乎不起作用。[也可能是这样,但WMP视频根本没有通过(透明)面板显示。] (最终)目标是防止用户直接与WMP交互,理想情况下,建立一个覆盖层,
面板
或图片盒
由于Panel
或PictureBox
都没有TransparencyKey
属性,因此我无法使用,BackColor=TransparencyKey=Color.Magenta
方法
我已尝试将面板的背景色设置为Color.Transparent
。它似乎不起作用。[也可能是这样,但WMP视频根本没有通过(透明)面板显示。]
(最终)目标是防止用户直接与WMP交互,理想情况下,建立一个覆盖层,将“有用”的信息传达给用户
有人成功地做到了这一点吗?我对如何解决这个问题的大多数想法持开放态度
编辑:
使用“”中的示例,我创建了“我自己的”面板,如下所示:
class TransarentPanel : Panel {
public bool drag = false;
public bool enab = false;
private int m_opacity = 100;
private int alpha;
System.Drawing.Font drawFont;
System.Drawing.SolidBrush drawBrush;
System.Drawing.StringFormat drawFormat;
public TransarentPanel() {
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
this.BackColor = Color.Transparent;
}
public int Opacity {
get {
if (m_opacity > 100) {
m_opacity = 100;
} else if (m_opacity < 1) {
m_opacity = 1;
}
return this.m_opacity;
}
set {
this.m_opacity = value;
if (this.Parent != null) {
Parent.Invalidate(this.Bounds, true);
}
}
}
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle = 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 bckColor = default(Brush);
alpha = (m_opacity * 255) / 100;
if (drag) {
Color dragBckColor = default(Color);
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;
}
alpha = 255;
bckColor = new SolidBrush(Color.FromArgb(alpha, dragBckColor));
} else {
bckColor = new SolidBrush(Color.FromArgb(alpha, this.BackColor));
}
if (this.BackColor != Color.Transparent | drag) {
g.FillRectangle(bckColor, bounds);
}
// Display overlay info
{
drawFont = new System.Drawing.Font("Arial", 24);
drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
drawFormat = new System.Drawing.StringFormat();
g.DrawString("Overlay", drawFont, drawBrush, this.Location.X + 50, this.Location.Y + 30, drawFormat);
drawFont.Dispose();
drawBrush.Dispose();
drawFormat.Dispose();
}
bckColor.Dispose();
g.Dispose(); // Should this object be disposed of here since it was not created here?
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);
}
} // end class TransparentPanel
class TransarentPanel:面板{
公共布尔阻力=假;
公共bool enab=false;
私有int m_不透明度=100;
私有int-alpha;
System.Drawing.Font drawFont;
System.Drawing.SolidBrush drawBrush;
System.Drawing.StringFormat drawFormat;
公共事务委员会(){
设置样式(ControlStyles.SupportsTransparentBackColor,true);
SetStyle(ControlStyles.不透明,true);
this.BackColor=Color.Transparent;
}
公共整数不透明度{
得到{
如果(m_不透明度>100){
m_不透明度=100;
}否则如果(m_不透明度<1){
m_不透明度=1;
}
返回此.m_不透明度;
}
设置{
此.m_不透明度=值;
如果(this.Parent!=null){
Parent.Invalidate(this.Bounds,true);
}
}
}
受保护的重写CreateParams CreateParams{
得到{
CreateParams cp=base.CreateParams;
cp.ExStyle=cp.ExStyle | 0x20;
返回cp;
}
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
图形g=e.图形;
矩形边界=新矩形(0,0,this.Width-1,this.Height-1);
颜色frmColor=this.Parent.BackColor;
笔刷bckColor=默认值(笔刷);
α=(m_不透明度*255)/100;
如果(拖动){
颜色DragBackColor=默认值(颜色);
if(背景色!=颜色.透明){
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);
}否则{
dragBckColor=frmColor;
}
α=255;
bckColor=新的SolidBrush(Color.FromArgb(alpha,DragBackColor));
}否则{
bckColor=newsolidbrush(Color.FromArgb(alpha,this.BackColor));
}
if(this.BackColor!=Color.Transparent |拖动){
g、 FillRectangle(bckColor,边界);
}
//显示覆盖信息
{
drawFont=新系统.Drawing.Font(“Arial”,24);
drawBrush=新的System.Drawing.SolidBrush(System.Drawing.Color.Black);
drawFormat=新的System.Drawing.StringFormat();
g、 DrawString(“叠加”,drawFont,drawBrush,this.Location.X+50,this.Location.Y+30,drawFormat);
Dispose();
Dispose();
drawFormat.Dispose();
}
bckColor.Dispose();
g、 Dispose();//由于此对象不是在此处创建的,是否应在此处处置?
基础漆(e);
}
BackColorChanged上的受保护覆盖无效(EventArgs e)
{
如果(this.Parent!=null)
{
Parent.Invalidate(this.Bounds,true);
}
基色变化(e);
}
受保护的覆盖无效OnParentBackColorChanged(EventArgs e)
{
这个。使无效();
基。基于ParentBackColorChanged(e);
}
}//结束类TransparentPanel
这实现了一个“不可见”面板的目标,该面板覆盖在WMP上,同时允许WMP显示。但是,我仍然需要在WMP窗口的顶部(前面)显示一些信息
我曾尝试直接在这个透明面板上“绘制”[请参见OnPaint()中“//显示覆盖信息”处的代码块],但没有显示。但是,如果我“Hide()”WMP窗口,则会显示写入内容(当然,WMP不会)。如果我让我的透明面板只覆盖半个屏幕,并添加一个“常规”面板(带有背景图像),它会显示,除非它的任何部分与透明面板重叠,在这种情况下它不会显示
我已经注意到有关面板背景的父图形的评论。我不明白这一点,但将透明面板的父级设置为WMP(TP.Parent=WMP;)没有任何区别
我准备相信我在这里遗漏了一些基本的东西,因为我对表单/控件的知识充其量只是初步的。尽管如此,我还是很想了解我遗漏了什么,这样我就可以把它添加到我的知识库中,在这里解决我的问题,然后继续前进
[祝福乔治·比比利斯,如果没有他,我仍然会站在第一位!:-)]
大宗报价
以加载的形式,将PictureBox1的背景指定为透明,然后将PictureBox1指定为PictureBox2容器的父容器,可以在PictureBox2中实现透明,并与PictureBox1的背景透明。VB2010环境。
代码如下:左边是附在屏幕上的运行效果,右边是编程环境,希望这个效果你会满意
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.BackColor = Color.Transparent
PictureBox2.Parent = PictureBox1
Dim g As Graphics = Me.CreateGraphics