Development of custom drawing controls in Visual c++2005

Source: Internet
Author: User
This article source code download: CustomDraw.exe.

After you decide to develop a control that is outside the scope of the general free custom controls provided by Windows, you must determine how much of your control will be unique-both in terms of functionality and appearance. For example, we assume that you are creating a control that is similar to a meter-speed table. Because there are no similar controls in the Common Control Library (ComCtrl32.dll), you need to write the code needed for all control functions, draw, default end-user interaction, and any message processing that the control needs between its parent window.

On the other hand, you'll also include situations where you just want to adjust the functionality of a common control. For example, we assume that you want to create a masked edit control that only allows you to accept the specified characters. If you use MFC, it typically involves deriving a class from the classes provided by MFC that encapsulates a common control (in a masked edit control, typically CEdit), overrides a required virtual function (or processes the specified message), and then joins the custom code.

The focus of this article is somewhere between the two-the common controls give you most of the functionality you want, but the control doesn't look what you want. For example, a list view control provides a way to display a list of data in many view styles-small icons, large icons, lists, and detailed lists (reports). However, if you want a grid control, what's the result? Although the common control library does not specifically contain a grid, the list view control is closer to it, displaying data in rows and columns and having a related header control. As a result, many people create their own grid controls with a standard list view control, and then override how the control and its subkeys are rendered or plotted.

Dominate drawing operations

Even if you are drawing only, you still have at least four options available, all of which have distinct advantages and disadvantages:

• Handling WM_PAINT

• Owner Drawing

• Custom Drawing

• Handling Wm_ctlcolor

Handling WM_PAINT

The most extreme choice is to execute a WM_PAINT handler and do all the drawing yourself. This means that your code will need to do some trivial things related to rendering the control-create the appropriate device context (one or more), determine the size and position of the control, draw the control, and so on. There is little need for this level of control during the drawing process.

Owner Drawing

Another way to control the drawing of controls is to draw with the owner. In fact, you may have heard the developer mention the owner Drawing Control because it is the most common technique for developing custom controls. The main reason this technology is commonly used is that Windows can provide you with a lot of help. At the moment that the control is rendered, Windows has created and filled out the device context, determines the size and location of the control, and passes you information to make you aware of the requirements that are being drawn at the moment. For list controls (for example, list boxes and list views), Windows draws code for each item in the list, which means that you only need to draw the items, regardless of other aspects of the control. Note that owner drawing is available for most controls. However, it cannot be used for editing controls, and it can be used only for report view styles, taking into account the list control.

Custom Drawing

This may be the least known technique for drawing your own controls. In fact, many highly skilled developers also confuse the term owner drawing (owner-draw) and custom drawing (Custom-draw). For a custom control, you first need to understand that it is intended only for the specified common controls: headers, list views, rebar, toolbars, ToolTips, trace bars, and tree views. In addition, although owner drawing only allows you to draw a list view control that Reports view style, custom drawing enables you to work with drawing of all view styles for a list view control. Another obvious advantage of using custom drawing is that you can carefully select what you want to draw. The implementation is that Windows sends a message to the code at each stage of the control drawing. This allows you to decide whether you want to do all of the drawing work yourself at each stage, increase the default drawing, or allow Windows to perform all the drawing for that stage. (Since custom drawing is a topic in this article, you'll soon see how it works.) )

Handling Wm_ctlcolor

This may be the easiest way to help decide how to render the control. As the message name means, when you want to draw a control and it allows your code to decide which brush to use, send a WM_CTLCOLOR message. Typically, you use this technique if you only want to change the color of the control and do not provide more functionality than the control itself. In addition, the message is not sent for common controls introduced by Internet Explorer (list view, tree view, rebar, and so on), and it is only used in conjunction with standard controls (Edit, list box, and so on).

Implementing a custom drawn three-step curve

Now that you understand the various options available for drawing controls (including the benefits of using custom drawing), let's take a look at the three main steps needed to implement a custom drawing control.

• Execute a NM_CUSTOMDRAW message handler.

• Specify the drawing phase required for processing.

• Filter specific drawing stages (in these phases, you need to add your own control-specific drawing code).

Execute a NM_CUSTOMDRAW message handler

When you need to draw a common control, MFC feeds the control's custom drawing notification message (originally sent to the control's parent window) to the control in the form of a NM_CUSTOMDRAW message. The following is an example of a nm_customdraw handler.

void Cmycustomdrawcontrol::oncustomdraw (nmhdr* pnmhdr, lresult* pResult)
{
Lpnmcustomdraw PNMCD = reinterpret_cast (PNMHDR);
...
}

As you can see, the Nm_customdraw handler passes a pointer to the structure of the NMHDR type. However, this value is not sufficient for structures such as NMHDR that contain only three members (Hwndfrom, Idfrom, and code).

Therefore, you typically need to convert the structure pointer to a more informative structure-lpnmcustomdraw. Lpnmcustomdraw points to Nmcustomdraw, which contains members such as Dwdrawstage, Dwitemspec, and uitemstate-they determine the current drawing phase and the exact drawing (for example, the control itself, or a project or subkey for a control.

It's worth noting that you can also point a NMHDR pointer to a structure that is specific to the type of the control you are drawing. Table 1 shows a list of controls and their associated custom drawing structure type names.

Table 1: Controls and their associated custom drawing structures

Control Structure (defined in commctrl.h)
Rebar, Trackbar, AuthTicket, My.Resources, My.Settings, My.User and My.WebServices. Nmcustomdraw
List-view Nmlvcustomdraw
Toolbar Nmtbcustomdraw
Tooltip Nmttcustomdraw
Tree-view Nmtvcustomdraw
Related Article

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.