How to customize custom icons for custom controls in the toolbox

Source: Internet
Author: User
Tags bmp image

Many friends write their own controls for their own use, but all the controls by default in the toolbox picture is a single icon-gear. How to customize custom icons for custom controls in the toolbox

Background: When I wrote a control with a progress bar with digits in the middle, I suddenly found that the charts added to the toolbox were a bit simple, as a result, how to use a custom graph to create a toolbox graph is generated, and a large amount of information is queried. this article is a little different from the original article. I modify some of the bugs and use my own instance to describe them below.

You can do this in different ways. In the following example, bitmap or icon images must follow the following rules:

1. The Bitmap Or icon size cannot be 16 colors 16X16

2. the background color must be transparent.

 

Technical solution 1: Use a bitmap image (not an icon image, embedded in resources) File

No special ToolboxBitmapAttribute class is required.

For example, if you have a namespace CarryNoProgramBar and the custom control is Bar.

1. Create an image named "bar.bmp" based on your image rules and add the image to your project,

2. Set the bar.bmp image setting attribute generation operation to a embedded resource.

3. The namespace of the image must be CarryNoProgramBar.

4. If the namespace of the control does not match the default namespace of the project, you must move the bitmap image to the appropriate subdirectory for matching. If this method is invalid, you obviously cannot use this technology to implement your custom images. You can use the ToolboxBitmap attribute technology below to implement it.

5. Indicate the icons in the toolbox that I used directly in the root directory.

The above simple technology is used to meet your needs, and you do not need to use ToolboxBitmapAttribute to generate your type.

Technical solution 2:

Use ToolboxBitmap attributes

Use a bitmap image with the same name as the type instead of an icon to embed resources. The default namespace is CarryNoProgramBar.

Namespace CarryNoProgramBar {

[ToolboxBitmap (typeof (Bar)]

Public class Bar: UserControl {...}

}

When running the example, the root directory of your project contains a resource image named "bar.bmp". Note that your images are consistent with the namespace of the control.

Example 2 if you need a sub-directory in the project to put your image, you can change it

Namespace CarryNoProgramBar {

[ToolboxBitmap (typeof (Bar), "sub.Bar.bmp")]

Public class Bar: UserControl

{......}

}

Or

[ToolboxBitmap (typeof (Bar), "sub. Bar. ico")]

Using sub-directories allows you to use special resources, including ico files.

Example 3

Sometimes your controls and images are not in the same namespace. In the following case, you must use images of the same type of embedded resources in a uniform namespace.

Default namespace

Namespace MyAssemblyNamespace {

Public class SomeType

{...}

}

Namespace DifferentNamespace

{

[ToolboxBitmap (typeof (SomeType), "Bar. ico")]

Public class Bar: UserControl

{...}

}

We recommend that you use the same namespace directly to call the icons in the toolbox,
Author's Blog:Http://blog.csdn.net/designonline/

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.