How to make the label background transparent (TIPS)

Source: Internet
Author: User

The problem is simple, right? Set the backcolor of the label to color. Transparent, and then it becomes transparent! It looks like this on the surface, but it actually makes the background the same as the background of its parent control, so it looks transparent, actually in its onpaintbackground, still finished the same drawing work.

When the label is on an image, you will find that it becomes opaque again, and its background color is the same as that of picturebox.

I only want a label that can be transparent on the image. I used Google to find out the results. Most of them modified the backcolor. Of course, there are also some advanced methods:

This. setstyle (controlstyles. supportstransparentbackcolor, true );

The result is the same as setting the background color. The image cannot be transparent.

So I want to re-write a transparent label and decide to derive from the original label. When the background is transparent, I will not draw the background, but only the text. The onpaint and onpaintbackground functions are directly reloaded, and the background turns black. Depressed! When the onpaintbackground function does not do anything, it actually draws a background with the default black. In fact, what I want is to let the main window ignore the label background and draw the background of the control using the image in the original area. But the problem is: I cannot get the background of the given area of the main window. If you want to use graphics. fromhwnd, the result is a running error when graphic is obtained, let alone the image area. I don't want to think about other methods.

In another way, if you just want to draw some text on the image, why not start with picturebox directly? Reload its onpaint method to draw our text. Isn't that enough? The background remains unchanged. This idea is good, andCodeIt is also simple. The final result is also very good.

Using System;
Using System. collections;
Using System. componentmodel;
Using System. drawing;
Using System. Data;
Using System. Windows. forms;

Namespace Webb. instantreplay. publiccontrols. uicontrols
{
/**/ /// <Summary>
///Summary Description for transparentlabel2.
/// </Summary>
Public   Class Transparentlabel2: system. Windows. Forms. picturebox
{
// Fields
Private Point _ location =   New Point ( 0 , 0 );
// Properties
Public Point textlocation
{
Get
{
Return This. _ Location;
}
Set
{
This. _ Location=Value;
}
}
[Browsable ( True )]
New   Public   String Text
{
Get {Return Base. Text ;}
Set {Base. Text=Value ;}
}
[Browsable ( True )]
New   Public Font font
{
Get {Return Base. Font ;}
Set {Base. Font=Value ;}
}
//
Public Transparentlabel2 ()
{
}

Protected   Override   Void Onpaint (painteventargs E)
{
Base . Onpaint (E );
Sizef m_size = E. Graphics. measurestring ( This . Text, This . Font );
E. Graphics. drawstring ( This . Text, This . Font, brushes. Black, New Rectanglef ( This . _ Location, m_size ));
}
}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.