Foxtable(狐表)用户栏目专家坐堂 → 控件功能意见


  共有22206人关注过本帖树形打印复制链接

主题:控件功能意见

帅哥哟,离线,有人找我吗?
狐狸爸爸
  11楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:47448 积分:251054 威望:0 精华:91 注册:2008/6/17 17:14:00
  发帖心情 Post By:2009/6/17 12:04:00 [只看该作者]

以下是引用qtcks在2009-6-17 12:03:00的发言:
贺老师,开发工具你是使用什么的?


vb.net

其实都可以,但是没有必要。

[此贴子已经被作者于2009-6-17 12:04:15编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
blackzhu
  12楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 一级勋章
等级:狐仙 帖子:9875 积分:57584 威望:0 精华:15 注册:2008/9/1 9:45:00
  发帖心情 Post By:2009/6/17 12:04:00 [只看该作者]

以下是引用qtcks在2009-6-17 12:03:00的发言:
贺老师,开发工具你是使用什么的?

 vb


 回到顶部
帅哥哟,离线,有人找我吗?
qtcks
  13楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:485 积分:3926 威望:0 精华:6 注册:2009/3/29 13:25:00
  发帖心情 Post By:2009/6/17 12:05:00 [只看该作者]

图片点击可在新窗口打开查看我现在碰到的问题就有这样的需求,怎么办啊````

 回到顶部
帅哥哟,离线,有人找我吗?
qtcks
  14楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:485 积分:3926 威望:0 精华:6 注册:2009/3/29 13:25:00
  发帖心情 Post By:2009/6/17 12:45:00 [只看该作者]

Introduction - No Transparency in Windows Forms!

If you've tried to work with more complex forms that include images and labels, you probably found out that Windows Forms doesn't support true transparency. You're probably tearing your hair out - but don't sweat!

Even if you use the Transparent value for a control's BackColor you won't get transparency. What really happens is that the control will actually render its parent's background. This behaviour is visible on the following picture.

No_Transparency.gif

In this article, we will show you a simple way to get labels with pictures as background and also how you can use images and text with correct transparency.

How To Make Transparent Labels

Using a picture for background and labels or text in the foreground with real transparency can actually be achieved quite easily. In this chapter, we will show how to make a label's background transparent.

There are two ways which you can use to get a label to handle transparency correctly with an image as background (there are actually more ways to do this, but we're only going to talk about the more straightforward ones):

  1. By setting a Panel's BackgroundImage property and putting the Label(s) inside it
  2. By parenting the PictureBox to the Label (label.Parent = pictureBox;)

We will approach the first solution, which doesn't require any code and we can see the result right away in the designer.

Setting_Transparent_Label.gif

First, start by dragging a Panel to your form. Now set the BackgroundImage property to the image that you would like to see as the background (you can use the BackgroundImageLayout to control its behaviour).

Finally, add a Label and set the BackColor property to Transparent (the first option in the Web tab). The final result should be similar to the following image.

Transparent_Label.gif

This allows us to use labels with transparency, but the images are still broken (no transparency between them)! Don't worry, in the next chapter we will discuss a solution to use images with real transparency.

Using GDI+ for Drawing Images with Transparency

Drawing images with correct transparency is a little more tricky, because we can't use the default controls that come with Windows Forms and .NET.

For more complex image and graphics manipulation, we can use GDI+, which stands for Graphics Device Interface (it can be found in the System.Drawing namespace).

What we'll do is create a generic control that we can then arbitrarily inherit to draw images and text. This can be found in the project's source code, but if you want to understand how it works, then read on.

Generic Control for Drawing Images

Start by creating a new class which inherits from Panel. Lets call it DrawingArea. This class will have an abstract OnDraw method which will be overridden by its subclasses, so we also need to declare the class as abstract.

Also, we'll add a Graphics object where all the drawing will take place. You should have something like this:

Collapse Copy Code
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

/// <summary>
/// Use this for drawing custom graphics and text with transparency.
/// Inherit from DrawingArea and override the OnDraw method.
/// </summary>
abstract public class DrawingArea : Panel
{
    /// <summary>
    /// Drawing surface where graphics should be drawn.
    /// Use this member in the OnDraw method.
    /// </summary>
    protected Graphics graphics;

    /// <summary>
    /// Override this method in subclasses for drawing purposes.
    /// </summary>
    abstract protected void OnDraw();
} 

We need to make sure our control's background transparency will be correctly handled. For this, we override the CreateParams property to make sure the correct style is included when the control is instantiated (thanks to Bob Powell for this tip).

Collapse Copy Code
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT

            return cp;
        }
    } 

Now, only two more things are needed. First we must make sure that the background doesn't get drawn. We do this by overriding the OnPaintBackground method with nothing in it.

The second thing that's needed is to override the OnPaint method. This allows us to define the procedure that will be called when it's time for the control to paint itself.

Collapse Copy Code
    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        // Don't paint background
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // Update the private member so we can use it in the OnDraw method
        this.graphics = e.Graphics;

        // Set the best settings possible (quality-wise)
        this.graphics.TextRenderingHint = 
            System.Drawing.Text.TextRenderingHint.AntiAlias;
        this.graphics.InterpolationMode = 
            System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
        this.graphics.PixelOffsetMode = 
            System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        this.graphics.SmoothingMode = 
            System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        // Calls the OnDraw subclass method
        OnDraw();
    } 

I also defined a DrawText method and some variations so it would be easy to write text. It's a little lengthy so I'll leave it off the tutorial, but you can find it in the project's source code.

Using the Control to Draw Images and Text with Transparency

Now, how do we use this control? We need to make a new class and inherit from DrawingArea. This is very simple and easy to do. Here I provide an example:

Collapse Copy Code
class BroculosDrawing : DrawingArea
{
    protected override void OnDraw()
    {
        // Gets the image from the global resources
        Image broculoImage = global::WindowsApplication1.Properties.Resources.broculo;

        // Sets the images' sizes and positions
        int width = broculoImage.Size.Width;
        int height = broculoImage.Size.Height;
        Rectangle big = new Rectangle(0, 0, width, height);
        Rectangle small = new Rectangle(50, 50, (int)(0.75 * width), 
                (int)(0.75 * height));

        // Draws the two images
        this.graphics.DrawImage(broculoImage, big);
        this.graphics.DrawImage(broculoImage, small);

        // Sets the text's font and style and draws it
        float fontSize = 8.25f;
        Point textPosition = new Point(50, 100);
        DrawText("http://www.broculos.net", "Microsoft Sans Serif", fontSize
            , FontStyle.Underline, Brushes.Blue, textPosition);
    }
} 

This will draw two images and text (similar to the previous ones), but now with true transparency!

We can use this control like a normal one. Compile the solution. Create a new form. The new control should appear in the toolbox. Drag it to the form and voila! You can see the outcome in the following image:

Images_Text_Full_Transparency.gif

Conclusion

Now you know how to draw images with transparency. The big drawback is that it isn't as easy to use as .NET built-in Windows Forms controls. The default controls are very limited for more advanced image usage and manipulation, so we used GDI+ to overcome this.

With this knowledge and a little more work, it should be possible to make a TransparentPictureBox. Hope you found it useful.

Resources

History

  • 8th April, 2008: Initial post
<!-- Main Page Contents End -->

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nuno Freitas


Member
Find more of my tutorials at Broculos.net.
Occupation: Web Developer
Location: Portugal Portugal
 下载信息  [文件大小:   下载次数: ]
点击浏览该文件:transparentimages.zip


 回到顶部
帅哥哟,离线,有人找我吗?
狐狸爸爸
  15楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:47448 积分:251054 威望:0 精华:91 注册:2008/6/17 17:14:00
  发帖心情 Post By:2009/6/17 15:28:00 [只看该作者]

呵呵,用foxtable是一样的做,我前面不是说了吗,用Painter:

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:管理项目105.rar


 回到顶部
美女呀,离线,留言给我吧!
yangming
  16楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 一级勋章
等级:超级版主 帖子:4109 积分:23338 威望:0 精华:21 注册:2008/9/1 20:07:00
  发帖心情 Post By:2009/6/17 15:47:00 [只看该作者]

贺老师,楼主是想动态增加控件,而Painter是不能动态增加的吧?

 回到顶部
帅哥哟,离线,有人找我吗?
狐狸爸爸
  17楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:47448 积分:251054 威望:0 精华:91 注册:2008/6/17 17:14:00
  发帖心情 Post By:2009/6/17 15:54:00 [只看该作者]

以下是引用yangming在2009-6-17 15:47:00的发言:
贺老师,楼主是想动态增加控件,而Painter是不能动态增加的吧?


Painter也可以动态增加的。


 回到顶部
美女呀,离线,留言给我吧!
yangming
  18楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 一级勋章
等级:超级版主 帖子:4109 积分:23338 威望:0 精华:21 注册:2008/9/1 20:07:00
  发帖心情 Post By:2009/6/17 15:56:00 [只看该作者]

哦,帮助中没有列出来,呵呵

 回到顶部
帅哥哟,离线,有人找我吗?
smileboy
  19楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 好心情 一级勋章
等级:超级版主 帖子:715 积分:5564 威望:0 精华:1 注册:2008/8/31 20:45:00
  发帖心情 Post By:2009/6/17 15:59:00 [只看该作者]

以下是引用yangming在2009-6-17 15:56:00的发言:
哦,帮助中没有列出来,呵呵

Painter

Painter表示绘图板,我们可以通过代码在其中绘制图形。
Painter采用GDI+绘制图形,GDI+的内容很多,可以写成一本厚厚的书,我们不可能在这里详细地介绍GDI+,但是会介绍其中最基础的部分。

属性

  • Graphics

    返回一个Graphics对象,我们所有的图形绘都是通过这个对象来完成的。
     
  • Image

    返回绘制好的图形。

方法

  • Repaint

    显示绘制好的图形。
     
  • Save

    保存绘制好的图形,语法:

    Save(FileName)

    FielName: 字符型,指定文件名。

示例

在窗口中插入一个绘图板和一个按钮,将按钮的Click事件设为:

Dim p As WinForm.Painter = e.Form.Controls("Painter1")
Dim
g As Graphics = p.Graphics
g.DrawArc(Pens.Red,
0, 0, 30, 40, 0, 135)
g.DrawEllipse(Pens.Blue,
20, 20, 90, 45)
p.Repaint()
'显示绘制好的图片

以后单击该按钮,即可绘制出下图所示的图片:


 回到顶部
美女呀,离线,留言给我吧!
yangming
  20楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 一级勋章
等级:超级版主 帖子:4109 积分:23338 威望:0 精华:21 注册:2008/9/1 20:07:00
  发帖心情 Post By:2009/6/17 16:05:00 [只看该作者]

以下是引用smileboy在2009-6-17 15:59:00的发言:

Painter

Painter表示绘图板,我们可以通过代码在其中绘制图形。
Painter采用GDI+绘制图形,GDI+的内容很多,可以写成一本厚厚的书,我们不可能在这里详细地介绍GDI+,但是会介绍其中最基础的部分。

属性

  • Graphics

    返回一个Graphics对象,我们所有的图形绘都是通过这个对象来完成的。
     
  • Image

    返回绘制好的图形。

方法

  • Repaint

    显示绘制好的图形。
     
  • Save

    保存绘制好的图形,语法:

    Save(FileName)

    FielName: 字符型,指定文件名。

示例

在窗口中插入一个绘图板和一个按钮,将按钮的Click事件设为:

Dim p As WinForm.Painter = e.Form.Controls("Painter1")
Dim
g As Graphics = p.Graphics
g.DrawArc(Pens.Red,
0, 0, 30, 40, 0, 135)
g.DrawEllipse(Pens.Blue,
20, 20, 90, 45)
p.Repaint()
'显示绘制好的图片

以后单击该按钮,即可绘制出下图所示的图片:

好版,我说的是动态增加,呵呵,不是事先在窗口中加入的


 回到顶部
总数 21 上一页 1 2 3 下一页