如何让窗体能半透明且有透明渐变

hainang1234 2007-08-01 04:50:52
比如让窗体从右到左透明渐变,最右边是完全透明,再过来一点透明度就低一点,到最左边的时候就完全不透明。

如何实现?
...全文
868 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
zolo1981 2007-08-08
  • 打赏
  • 举报
回复
有一个重绘的思路,但可能会影响性能,就是在重绘时把重绘区分割成多个小区间,每个区间用递减的颜色来填充,只要区间充分小,应该会有比较柔和的颜色过渡,但性能就……这个是我的一个思路,具体行不行还不知道,望大侠们指点一下!
Qim 2007-08-08
  • 打赏
  • 举报
回复
可以实现.
已在blog中给你回复.
hainang1234 2007-08-08
  • 打赏
  • 举报
回复
我是想实现从左到右的静态的透明渐变。不知是否能实现?

                     ----楼主
Qim 2007-08-08
  • 打赏
  • 举报
回复
太客气了,把分全给了我.
不好意思.
hainang1234 2007-08-08
  • 打赏
  • 举报
回复
Qim(莫名-从星做起(blog.csdn.net/qim/))已帮我解决,若需要代码,可在他blog中去看。

多谢!
vainnetwork 2007-08-07
  • 打赏
  • 举报
回复
关注,这个
yucong 2007-08-07
  • 打赏
  • 举报
回复
友情帮顶!
andy888666 2007-08-07
  • 打赏
  • 举报
回复
timer 定时重绘窗体达到渐变 不停改变Opacity的透明度 理论上是这样的
DavidNoWay 2007-08-07
  • 打赏
  • 举报
回复
顶!!!
Qim 2007-08-07
  • 打赏
  • 举报
回复
晚上回家整理一下,发我blog里。
Qim 2007-08-07
  • 打赏
  • 举报
回复
通过用背景图的渐变透明是可以实现的。
例如用png的图片,直接绘到窗体上。
核心代码如下:
public void SetBitmap(Bitmap bitmap, byte opacity)
{
if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");

// The ideia of this is very simple,
// 1. Create a compatible DC with screen;
// 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
// 3. Call the UpdateLayeredWindow.

IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
IntPtr hBitmap = IntPtr.Zero;
IntPtr oldBitmap = IntPtr.Zero;

try
{
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap
oldBitmap = Win32.SelectObject(memDc, hBitmap);// select the newbitmap,return the oldbitmap

Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);//create the size of LayeredWindow
Win32.Point pointSource = new Win32.Point(0, 0);
Win32.Point topPos = new Win32.Point(Left, Top);//LayeredWindow start position
Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();

/////????
blend.BlendOp = Win32.AC_SRC_OVER;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = opacity;
blend.AlphaFormat = Win32.AC_SRC_ALPHA;

//the most important function
Win32.UpdateLayeredWindow(this.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
}
finally
{
Win32.ReleaseDC(IntPtr.Zero, screenDc);
if (hBitmap != IntPtr.Zero)
{
Win32.SelectObject(memDc, oldBitmap);
//Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
Win32.DeleteObject(hBitmap);
}
Win32.DeleteDC(memDc);
}
}
Karual 2007-08-07
  • 打赏
  • 举报
回复
渐变透明不知道
整体FORM透明的很简单
guojh021 2007-08-07
  • 打赏
  • 举报
回复
顶一下,搞出来记得放代码哦~
hainang1234 2007-08-07
  • 打赏
  • 举报
回复
d
hainang1234 2007-08-06
  • 打赏
  • 举报
回复
我当时也想用透明渐变的png图片来实现,却发现不行。

看来有点难。

其实现在关注的是能否实现,怎么实现,致于性能,在能实现后仍然会权衡。

                         ----楼主
leixueqiyi 2007-08-02
  • 打赏
  • 举报
回复
透明渐变,太恐怖了,如果真的实现了,记得共享一下
Macosx 2007-08-02
  • 打赏
  • 举报
回复
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Opacity = 0.6;
this.FormBorderStyle = FormBorderStyle.None;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
LinearGradientBrush lgbrush = new LinearGradientBrush(new Point(0, 0), new Point(ClientRectangle.Right,ClientRectangle.Bottom), Color.White, Color.Black);
e.Graphics.FillRectangle(lgbrush, ClientRectangle);
}
}
}
yaoshuwen 2007-08-02
  • 打赏
  • 举报
回复
Macosx(咋就不结贴呢) ( ) 信誉:106 2007-08-02 11:30:16 得分: 0


看这个效果能满足不


===========================
楼主肯定不满足=。=
天开之想 2007-08-02
  • 打赏
  • 举报
回复
GDI
Macosx 2007-08-02
  • 打赏
  • 举报
回复
看这个效果能满足不
加载更多回复(18)

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧