Custom controls using subclass Methods

Source: Internet
Author: User

I always wanted to write something about subclass, because subclass is a basic and practical technology for interface programming. However, a good application instance has never been found, because this example cannot be too complex, otherwise it is not easy to understand, and it is too simple and there is no write header. Recently, I suddenly thought that the operation path dialog box that I have been using is a good example. So I wrote this article in a small example.
For those who are familiar with interface programming, the concept of subclass is certainly not unfamiliar. Many composite controls themselves have parent-child relationships. For example, the header of listctrl is actually a headctrl, And the edit and list types in ComboBox are also of this type. I have also written an article titled ComboBox advanced processing. The methods described in this article are implemented by subclass technology, it should be said that subclass applications are typical when such controls are overloaded. However, there is another case, that is, the type described in this article. Let's take a look at it.

Figure 1 Figure 2. Let's take a look at the above two figures. This is the Windows path selection dialog box. If we set the default path (for example, D: \ ntddk \ bin) when calling the dialog box, if the specified location has a sub-directory, the directory will be expanded, 1. I personally think this processing method is not reasonable. I think that since the path dialog box is selected, the path or folder should be the target object, so there is no need to expand the default location, that is, it should look like Figure 2. Well, what should I do if I have an idea?
There are actually many methods, but the method described in this article is customized by subclass. Let's take a look at the code for calling the select path dialog box:
Void metadata: onclickbuttonbrowse () {browseinfobrowseinfo; plaintext; tcharszpath [max_path]; // get the current path memset (szpath, 0, sizeof (szpath); getdlgitemtext (idc_edit_path, szpath, max_path); shilcreatefrompath (_ T ("d :\\"), & pitemid, null); // configure the path dialog box memset (& browseinfo, 0, sizeof (browseinfo )); browseinfo. hwndowner = m_hwnd; browseinfo. pidlroot = pitemid; browseinfo. lpsztitle = _ T ("select path"); browseinfo. lpfn = browseforfolderproc; browseinfo. lparam = (lparam) szpath; pitemid = shbrowseforfolder (& browseinfo); If (pitemid) {If (pitemid, szpath) {intnpahtlen = _ tcslen (szpath ); szpath [npahtlen] = _ T ('\'); setdlgitemtext (idc_edit_path, szpath );}}}

Everyone should be familiar with this Code. Anyone who has used this dialog box should be familiar with this Code. Here I specially added a function to set the root directory, I hope it will be helpful to anyone who needs similar functions. It should be noted that the default path is set through the browseinfo. the lparam parameter is set in browseinfo. the function set in the lpfn parameter is called. That is to say, if the two parameters are not set at the same time, the default parameter setting function cannot be implemented. At the same time, we will also think of a question: can we also do something in the function specified by browseinfo. lpfn? Let's take a look at what this function is used:The address of the callback function in the browser dialog box defined by the application. when an event occurs in the dialog box, this dialog box calls the callback function.Naturally, this function can respond to messages in the select path dialog box. As long as we find the right time to subclass the treectrl in the dialog box, we can control it. The implementation code is as follows:

int CALLBACK CPathSelDlg::BrowseForFolderProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData){if(uMsg == BFFM_INITIALIZED){CTreeCtrltreePath;HTREEITEMhItemSel;::SendMessage(hWnd, BFFM_SETSELECTION, TRUE, lpData);treePath.SubclassWindow(::GetDlgItem(hWnd, 0x3741));hItemSel= treePath.GetSelectedItem();treePath.Expand(hItemSel, TVE_COLLAPSE);treePath.UnsubclassWindow();}return   1;}

You can use umsg to determine the message. At a glance, bffm_initialized is equivalent to the oninitdialog in the dialog box. Subclass is the best time. 0x3741 is the treectrl ID observed through spy ++, so you can get the control handle through getdlgitem and then subclass it through subclasswindow. Once this operation succeeds, you can do whatever you want. Here I just want to collapse the lower-Layer Nodes of the selected node, but I can do anything else. After the operation, unsubclasswindow is used to release the control. If you want to keep control of the control, you can choose to release it when the window is destroyed. However, treepath should be a static variable, otherwise, problems may occur.
Now, all the things I want to introduce are finished. To sum up, subclass is a good method if you want to control a control in a third-party window, what you need to do is to get the handle of the window and the ID of the target control, and call subclasswindow at an appropriate time, so that you can operate freely like a general control, after the operation, you can find a suitable time to release the control. If you are interested, you can download this example and see it. I hope everyone can make personalized applications.

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.