C#,重写 OnPaint:带双缓冲区的 alpha 透明度
我正在使用C#使用.Net Compact Framework 2.0 SP2开发Windows Mobile 5.0及更高版本应用程序。
我正在重写自定义消息框上的 OnPaint 方法,该方法绘制一个位图,该位图用 alpha 透明度填充整个表单,以及一个带有按钮的渐变框和半透明背景上的消息。
我正在测试它,但它太慢了,所以我将使用双缓冲区。我可以使用双缓冲区来绘制渐变框和测试,但如果我将双缓冲区与具有 alpha 透明度的背景位图一起使用,则不会绘制 alpha 透明度。所以我只用渐变框和消息和按钮做双缓冲区。空背景透明位图直接绘制在 e.Graphics 上。
我想知道是否可以将 e.Graphics 保存在位图上以完成所有工作并结束 OnPaint 方法绘制到 e.Graphics 我之前保存的位图。
这是我的代码:
protected override void OnPaint(PaintEventArgs e)
{
Graphics gxOff;
gxOff = Graphics.FromImage(bmpOffscreen);
if (!isOuterBackgroundPainted)
{
isOuterBackgroundPainted = true;
DrawingHelper.DrawAlpha(e.Graphics, outerBackground, 180, 0, 0);
// Here I don't use double buffer because Alpha Blend doesn't work with double buffer.
//DrawingHelper.DrawAlpha(gxOff, outerBackground, 180, 0, 0);
}
// Draw the gradient box
GradientFill.Fill(gxOff, rectangle, startColor, endColor, FillDirection.TopToBottom);
gxOff.DrawString(message, font, brush, textLayoutRectangle);
e.Graphics.DrawImage(bmpOffscreen, 10, 10);
base.OnPaint(e);
}
bmpOffscreen:双缓冲区的位图。
也许我可以将表单的快照商店放入 bmpOffscreen 中,然后在其上绘制半透明背景、渐变框和文本。
总结: 我想使用带双缓冲区的 alpha 混合。
有什么建议吗?
I'm developing a Windows Mobile 5.0 and above application with .Net Compact Framework 2.0 SP2 with C#.
I'm overriding OnPaint method on a custom messagebox that draw a bitmap that fills the entire form with alpha transparency, and a gradient box with a button and a message over the semi-transparent background.
I'm testing it but it is so slow, so I'm going to use double buffer. I can use double buffer to draw the gradient box and the test, but if I use double buffer with the background bitmap with alpha transparency it doen't paint the alpha transparency. So I only do double buffer with gradient box and message and button. The bacground transparent bitmap is painted directly on e.Graphics.
I wondering if I can save e.Graphics on a bitmap to make all the work and end the OnPaint method drawing to e.Graphics this bitmap that I save it before.
This is my code:
protected override void OnPaint(PaintEventArgs e)
{
Graphics gxOff;
gxOff = Graphics.FromImage(bmpOffscreen);
if (!isOuterBackgroundPainted)
{
isOuterBackgroundPainted = true;
DrawingHelper.DrawAlpha(e.Graphics, outerBackground, 180, 0, 0);
// Here I don't use double buffer because Alpha Blend doesn't work with double buffer.
//DrawingHelper.DrawAlpha(gxOff, outerBackground, 180, 0, 0);
}
// Draw the gradient box
GradientFill.Fill(gxOff, rectangle, startColor, endColor, FillDirection.TopToBottom);
gxOff.DrawString(message, font, brush, textLayoutRectangle);
e.Graphics.DrawImage(bmpOffscreen, 10, 10);
base.OnPaint(e);
}
bmpOffscreen: double buffer's bitmap.
Maybe I can get a snapshop of form into bmpOffscreen and then draw semi-transparent background over it, gradient box and text.
Summarizing: I want to use alpha blend with double buffer.
Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
究竟如何做到这一点实际上相当复杂——比这里可以提供的简单答案还要复杂。查看Project Resistance 的来源。我们有一个双缓冲表单,我们在背景和带有透明度 Alpha 通道的控件中进行绘制。
Exactly how to do this is actually pretty complicated - more complicated than a simple answer here can provide. Take a look at the source for Project Resistance. We have a double-buffered Form and we're painting in a background and controls with a transparency alpha channel.
这里是一种为 Windows Mobile 上运行的应用程序拍摄快照的方法,无需拍摄标题栏和菜单。
这是我正在寻找开始双缓冲的图片。
Here is a way to take a snapshot of an application running on Windows Mobile, without taking the title bar and menu.
This is the picture that I was looking for to start double buffering.