(C#)透明功能??? (C#)我想实现这样一个功能:在子窗体弹开之后,透过子窗体的一张半透明图片(通常是PNG或GIF),可以看到父窗体的界面,这种透明效果能否做出来(就好像子窗体的中间放了一块玻璃,通过这块玻璃可以看到外面的父窗体)?我不是见到问题就上论坛的人,实在是什么方法都过试过啦(比如PAICTY,TRANSPARENTCYKEY),都不行!分不够可以再加!??????? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 又提这个问题啦? 我说的这种方法不行么?将Form的TransprencyKey和BackColor设置成同一种鲜明的颜色,比如说黑色(或白色)就可以了。设置后, 我在From上加了张图片图片, 觉得你说的这个要求可以实现。 你试试。 private void Form2_Load(object sender, EventArgs e) { //_Graphcis.FillRegion(new SolidBrush(Color.Yellow), _Region); // this.BackgroundImage = _Bitmap; this.TransparencyKey = Color.Yellow; this.BackColor = Color.Yellow; } private void Form2_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(this.BackColor); System.Drawing.Drawing2D.GraphicsPath _NotGraphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); _NotGraphicsPath.AddEllipse(new Rectangle(0, 0, this.Width - 32, this.Height - 32)); Region _Region = new Region(new Rectangle(0, 0, this.Width, this.Height)); _Region.Xor(_NotGraphicsPath); e.Graphics.FillRegion(new SolidBrush(Color.Red), _Region); }这样? 3楼此言差矣,你说的可能我都做了!1000%的不行!要不你就试着放一张半透明(不是透明)的图片到子窗体里,看能不能看到父窗体!我现在快发疯了,现在我已经认为这是微软的BUG了! Form2中只有一幅背景图, 其它的控件都是From1中的。 这样也不行么? 问题是你现在整个FORM2差不多都透明了,我只需要一部分半透明,你这种方法不是我要的!你能不能做这样的一个效果:子窗体本身不透明,但中间可以放一块图片,此图片是半透明的,透过它可以看到父窗体即可! 可以通过重绘的方法,将Form中的内容截取出来,绘制到弹出控件的表面,会看起来有透明的效果 中间一个Panel,它的背景色跟From2的TRANSPARENTCYKEY颜色是一致的。 至于你所说的半透明图片, 我不知道是否可行。 你自已试试呀。 this.Opacity = 0.8;不行么? 就是一张简单的半透明PNG图片,你自己都可以做啊!总之一句话:只要能通过子窗体图片看到父窗体即可! 18楼能否告之QQ?我的QQ是:443111466 刚加你的QQ没加上, 我的是:308251570, 再加一个from3做透明窗体。这样是否可行? 以前写的一个半透明信息提示窗体,构造函数的PNG图片的位置你需要调整下。注意:此窗体不能使Mdi的子窗体不然this.Opacity = 0.8; 就没有效果VS2003测试通过,好像2005也可以。 你试试效果,不知道是不是你的需求using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.IO;namespace LJCM{ /// <summary> /// FloatForm 的摘要说明。 /// </summary> public class FloatForm : System.Windows.Forms.Form { private Image im_BackColor; private string s_Caption; private Font font; private string s_Text; private bool b_IsMouseDown; private Point p_MouseDownPosition; private Point p_FormLocation; private System.Windows.Forms.Panel panel_Close; private System.Windows.Forms.Panel panel_WorkPlace; private System.Windows.Forms.ToolTip toolTip_Msg; private System.ComponentModel.IContainer components; public FloatForm() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // this.p_FormLocation = new Point(); this.p_MouseDownPosition = new Point(); this.b_IsMouseDown = false; this.panel_Close.Parent = this.panel_WorkPlace; this.panel_Close.BackColor = Color.Transparent; this.s_Caption = "标题"; this.s_Text = "文本"+ System.Environment.NewLine+"TODO: 在 InitializeComponent 调用后添加任何构造函数代码"; this.s_Text += System.Environment.NewLine + "长度"; font = new Font(FontFamily.GenericMonospace,8,FontStyle.Regular); string filePath = System.AppDomain.CurrentDomain.BaseDirectory+ @"\Image\提示窗体背景.png"; if(File.Exists(filePath)) { this.im_BackColor = Image.FromFile(filePath); } else { this.im_BackColor = null; } } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FloatForm)); this.panel_Close = new System.Windows.Forms.Panel(); this.panel_WorkPlace = new System.Windows.Forms.Panel(); this.toolTip_Msg = new System.Windows.Forms.ToolTip(this.components); this.panel_WorkPlace.SuspendLayout(); this.SuspendLayout(); // // panel_Close // this.panel_Close.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.panel_Close.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_Close.BackgroundImage"))); this.panel_Close.Location = new System.Drawing.Point(232, 0); this.panel_Close.Name = "panel_Close"; this.panel_Close.Size = new System.Drawing.Size(42, 16); this.panel_Close.TabIndex = 1; this.toolTip_Msg.SetToolTip(this.panel_Close, "关闭/隐藏"); this.panel_Close.Click += new System.EventHandler(this.panel_Close_Click); // // panel_WorkPlace // this.panel_WorkPlace.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.panel_WorkPlace.Controls.Add(this.panel_Close); this.panel_WorkPlace.Location = new System.Drawing.Point(0, 0); this.panel_WorkPlace.Name = "panel_WorkPlace"; this.panel_WorkPlace.Size = new System.Drawing.Size(280, 72); this.panel_WorkPlace.TabIndex = 2; this.panel_WorkPlace.SizeChanged += new System.EventHandler(this.panel_WorkPlace_SizeChanged); this.panel_WorkPlace.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel_WorkPlace_MouseUp); this.panel_WorkPlace.Paint += new System.Windows.Forms.PaintEventHandler(this.panel_WorkPlace_Paint); this.panel_WorkPlace.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_WorkPlace_MouseMove); this.panel_WorkPlace.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_WorkPlace_MouseDown); // // FloatForm // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.BackColor = System.Drawing.SystemColors.Control; this.ClientSize = new System.Drawing.Size(280, 72); this.Controls.Add(this.panel_WorkPlace); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "FloatForm"; this.Opacity = 0.8; this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; this.Text = "FloatForm"; this.TopMost = true; this.TransparencyKey = System.Drawing.SystemColors.Control; this.panel_WorkPlace.ResumeLayout(false); this.ResumeLayout(false); } #endregion private void panel_Close_Click(object sender, System.EventArgs e) { this.Close(); } private void panel_WorkPlace_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { if(this.im_BackColor != null) { e.Graphics.DrawImage(this.im_BackColor,this.panel_WorkPlace.Bounds); } e.Graphics.DrawRectangle(Pens.Gray,0,0,this.panel_WorkPlace.Width - 1, this.panel_WorkPlace.Height - 1); e.Graphics.DrawString(this.s_Caption, new Font(FontFamily.GenericSansSerif,9,FontStyle.Bold), Brushes.Black, new RectangleF(8,4,this.panel_WorkPlace.Width - 60,24), new StringFormat()); e.Graphics.DrawString(this.s_Text, font, Brushes.Black, new RectangleF(12,16,this.panel_WorkPlace.Width - 20 ,this.panel_WorkPlace.Height - 20), new StringFormat()); } public void SetCaption(string caption) { this.s_Caption = caption; this.panel_WorkPlace.Refresh(); } public void SetText(string text) { this.s_Text = text; Graphics g = this.panel_WorkPlace.CreateGraphics(); this.Height = (int) g.MeasureString(this.s_Text,font,this.panel_WorkPlace.Width - 32, new StringFormat()).Height + 26; this.panel_WorkPlace.Refresh(); } private void panel_WorkPlace_SizeChanged(object sender, System.EventArgs e) { this.panel_WorkPlace.Refresh(); } private void panel_WorkPlace_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Left) { this.b_IsMouseDown = true; this.p_MouseDownPosition.X = Cursor.Position.X; this.p_MouseDownPosition.Y = Cursor.Position.Y; this.p_FormLocation.X = this.Left; this.p_FormLocation.Y = this.Top; } } private void panel_WorkPlace_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Left) { this.b_IsMouseDown = false; } } private void panel_WorkPlace_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if(this.b_IsMouseDown) { this.Left =this.p_FormLocation.X + Cursor.Position.X - this.p_MouseDownPosition.X; this.Top =this.p_FormLocation.Y + Cursor.Position.Y - this.p_MouseDownPosition.Y; } } }} 不太明白LZ的意思.LZ是想实现窗体的局部透明(不是镂空)? 如果是png实现局部的透明话以前我用wtl做过用于显示的png在FW里用一个椭圆做了渐变的填充,中间的透明度为30%,边缘为不透明.不知道是不是LZ想要的效果. 用WPF非常容易……反正既然用了C#了,那就怎么方便怎么来呗…… 为什么主页中的iframe所连接的页面报$未定义!!! 求高手,backgroundworker引用类(类中有WEBBROWSER)出错 C# socket连接问题 问个checkboxlist的问题 市面上有没有支持.net进行开发的抄表机? 高手帮忙 [图文并茂]再说类和接口的选择? 求一查所有表的SQL语句 讨论:在桌面开发(windows)方面,C#会不会有那么一天超过dephil(用户数量) 怎样与SQL SERVER 数据库中取数据? C# winform 窗体属性HelpButton设置为True,运行时窗体标题栏怎么没显示帮助按钮,晕? 手机软件课题,一起来
{
//_Graphcis.FillRegion(new SolidBrush(Color.Yellow), _Region); // this.BackgroundImage = _Bitmap; this.TransparencyKey = Color.Yellow;
this.BackColor = Color.Yellow;
} private void Form2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(this.BackColor); System.Drawing.Drawing2D.GraphicsPath _NotGraphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); _NotGraphicsPath.AddEllipse(new Rectangle(0, 0, this.Width - 32, this.Height - 32)); Region _Region = new Region(new Rectangle(0, 0, this.Width, this.Height));
_Region.Xor(_NotGraphicsPath); e.Graphics.FillRegion(new SolidBrush(Color.Red), _Region);
}这样?
中间一个Panel,它的背景色跟From2的TRANSPARENTCYKEY颜色是一致的。 至于你所说的半透明图片, 我不知道是否可行。 你自已试试呀。
不行么?
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;namespace LJCM
{
/// <summary>
/// FloatForm 的摘要说明。
/// </summary>
public class FloatForm : System.Windows.Forms.Form
{
private Image im_BackColor;
private string s_Caption;
private Font font;
private string s_Text;
private bool b_IsMouseDown;
private Point p_MouseDownPosition;
private Point p_FormLocation; private System.Windows.Forms.Panel panel_Close;
private System.Windows.Forms.Panel panel_WorkPlace;
private System.Windows.Forms.ToolTip toolTip_Msg;
private System.ComponentModel.IContainer components; public FloatForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
// this.p_FormLocation = new Point();
this.p_MouseDownPosition = new Point();
this.b_IsMouseDown = false; this.panel_Close.Parent = this.panel_WorkPlace;
this.panel_Close.BackColor = Color.Transparent; this.s_Caption = "标题";
this.s_Text = "文本"+ System.Environment.NewLine+"TODO: 在 InitializeComponent 调用后添加任何构造函数代码";
this.s_Text += System.Environment.NewLine + "长度";
font = new Font(FontFamily.GenericMonospace,8,FontStyle.Regular);
string filePath = System.AppDomain.CurrentDomain.BaseDirectory+
@"\Image\提示窗体背景.png";
if(File.Exists(filePath))
{
this.im_BackColor = Image.FromFile(filePath);
}
else
{
this.im_BackColor = null;
}
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FloatForm));
this.panel_Close = new System.Windows.Forms.Panel();
this.panel_WorkPlace = new System.Windows.Forms.Panel();
this.toolTip_Msg = new System.Windows.Forms.ToolTip(this.components);
this.panel_WorkPlace.SuspendLayout();
this.SuspendLayout();
//
// panel_Close
//
this.panel_Close.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.panel_Close.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_Close.BackgroundImage")));
this.panel_Close.Location = new System.Drawing.Point(232, 0);
this.panel_Close.Name = "panel_Close";
this.panel_Close.Size = new System.Drawing.Size(42, 16);
this.panel_Close.TabIndex = 1;
this.toolTip_Msg.SetToolTip(this.panel_Close, "关闭/隐藏");
this.panel_Close.Click += new System.EventHandler(this.panel_Close_Click);
//
// panel_WorkPlace
//
this.panel_WorkPlace.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel_WorkPlace.Controls.Add(this.panel_Close);
this.panel_WorkPlace.Location = new System.Drawing.Point(0, 0);
this.panel_WorkPlace.Name = "panel_WorkPlace";
this.panel_WorkPlace.Size = new System.Drawing.Size(280, 72);
this.panel_WorkPlace.TabIndex = 2;
this.panel_WorkPlace.SizeChanged += new System.EventHandler(this.panel_WorkPlace_SizeChanged);
this.panel_WorkPlace.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel_WorkPlace_MouseUp);
this.panel_WorkPlace.Paint += new System.Windows.Forms.PaintEventHandler(this.panel_WorkPlace_Paint);
this.panel_WorkPlace.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_WorkPlace_MouseMove);
this.panel_WorkPlace.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_WorkPlace_MouseDown);
//
// FloatForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(280, 72);
this.Controls.Add(this.panel_WorkPlace);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "FloatForm";
this.Opacity = 0.8;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "FloatForm";
this.TopMost = true;
this.TransparencyKey = System.Drawing.SystemColors.Control;
this.panel_WorkPlace.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion private void panel_Close_Click(object sender, System.EventArgs e)
{
this.Close();
} private void panel_WorkPlace_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if(this.im_BackColor != null)
{
e.Graphics.DrawImage(this.im_BackColor,this.panel_WorkPlace.Bounds);
}
e.Graphics.DrawRectangle(Pens.Gray,0,0,this.panel_WorkPlace.Width - 1,
this.panel_WorkPlace.Height - 1); e.Graphics.DrawString(this.s_Caption,
new Font(FontFamily.GenericSansSerif,9,FontStyle.Bold),
Brushes.Black,
new RectangleF(8,4,this.panel_WorkPlace.Width - 60,24),
new StringFormat());
e.Graphics.DrawString(this.s_Text,
font,
Brushes.Black,
new RectangleF(12,16,this.panel_WorkPlace.Width - 20 ,this.panel_WorkPlace.Height - 20),
new StringFormat());
} public void SetCaption(string caption)
{
this.s_Caption = caption;
this.panel_WorkPlace.Refresh();
} public void SetText(string text)
{
this.s_Text = text; Graphics g = this.panel_WorkPlace.CreateGraphics();
this.Height = (int) g.MeasureString(this.s_Text,font,this.panel_WorkPlace.Width - 32,
new StringFormat()).Height + 26;
this.panel_WorkPlace.Refresh();
}
private void panel_WorkPlace_SizeChanged(object sender, System.EventArgs e)
{
this.panel_WorkPlace.Refresh();
} private void panel_WorkPlace_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
this.b_IsMouseDown = true;
this.p_MouseDownPosition.X = Cursor.Position.X;
this.p_MouseDownPosition.Y = Cursor.Position.Y;
this.p_FormLocation.X = this.Left;
this.p_FormLocation.Y = this.Top;
}
} private void panel_WorkPlace_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
this.b_IsMouseDown = false;
}
} private void panel_WorkPlace_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(this.b_IsMouseDown)
{
this.Left =this.p_FormLocation.X + Cursor.Position.X - this.p_MouseDownPosition.X;
this.Top =this.p_FormLocation.Y + Cursor.Position.Y - this.p_MouseDownPosition.Y;
}
} }
}
LZ是想实现窗体的局部透明(不是镂空)?
用于显示的png在FW里用一个椭圆做了渐变的填充,中间的透明度为30%,边缘为不透明.
不知道是不是LZ想要的效果.
反正既然用了C#了,那就怎么方便怎么来呗……