Let's talk about my problem first!
When using listctrl, I need to add a checkbox in the list header to implement the full selection function.
After listing the online materials, I found a demo and used the re-painting method. During the usage process, I found that the checkbox cannot be drawn in the list header, so I started to find the problem, the final result is that the function in the reloaded class cheadctlcl cannot respond,
So the question is approaching step by step. Why can't I call a heavy-duty function? All functions of the cheadctlcl class cannot respond. So I found the problem:
Void clistctrlex: presubclasswindow ()
{
// Todo: Add dedicated code and/or call the base class here
// _ Super: presubclasswindow ();
Modifystyle (0, lvs_ownerdrawfixed );
Clistctrl: presubclasswindow ();
Modifystyle (0, lvs_report | lvs_showselalways | lvs_sortdescending );
Cheaderctrl * pheader = This-> getheaderctrl ();
M_header.subclasswindow (pheader-> getsafehwnd ());
}
Debugging found that the getheaderctrl () call always returns NULL.
So I went to the Internet to find information and found the following Article !!!
//////////////////////////////////////// Reference: http://wangqingyun84.blog.163.com/blog/static/7908361720119744913240 ////////////////////////////////////////////////
If you want to customize the header of the clistctrl control, for example, the most common two-line display text, or to display some images, the most common method is to customize a cheaderctrl class and then subclass it. the procedure is as follows:
1. First define a subclass of the cheaderctrl class, for example, cheaderctrlex. There are two methods available next.
First, the most violent method is to rewrite the onpaint () function, that is, to add the on_wm_paint () message processing function to cheaderctrlex, but at this time, all things should be drawn by themselves, that is to say, the whole header should be drawn by itself, and even the middle separator line should be included. obviously too radical...
Second, a slightly mild approach. use the style hdi_format. in fact, this style can be specified when you call insertcolumn () to insert a column. When this column is drawn, the ondrawitem () function is called. Of course, this function must use the on_wm_drawitem () Macro, you can customize various items in the message response function. of course, each painting is the header of a column. this is not like the lvs_ownerdrawfixed of clistctrl. It is a line that draws the entire item.
2. the second step is to subclass the header control of the clistctrl class. Of course, I define a clistctrl subclass. Suppose the name is clistctrlex. so what we need to do is define a cheaderctrex _ headerctrl in this class, and then select an appropriate opportunity _ headerctrl. subclasswindow (getheaderctrl ()-> getsafehwnd ()
Our problem is: When to subclass, that is, to call this code...
1. The presubclasswindow () function must be used in many places on the Internet...
2. But the getheaderctrl () of a Baidu has an eternal problem: the returned value of getheaderctrl () is null...
3. Some people say that null is returned in presubclasswindow (), but it is OK in the oncreate () function...
But in fact many of them solve this problem in different ways:
1. The best way to escape is to use the resource editor to add a clistctrl and subclass it into clistctrlex. Then there is no problem in both presubclasswindow () and oncreate...
2. Some people say it won't work in presubclasswindow (). It should be in oncreate...
3. however, a problem occurs during dynamic creation, that is, _ listctrl. create (...), the controls created using this method still have problems in presubclasswindow () and oncreate ().
4. Some people are very creative. For example, the reference is as follows:
I noticed this too. If the list control is created as part of a dialog
Template (for example) then the hwnd of the header control exists inside
The presubclasswindow function, whereas if the list control is created
Dynamically (with a call to clistctrl: Create () then it does not.
I solved the problem by postmessage 'ing a user? Defined message to myself
From inside of the presubclasswindow function. In the handler for
User? Defined message, the hwnd for the header always exists, regardless
How the list control was created, and I subclass the header from inside this
Handler (and not from inside the presubclasswindow function ).
I think it works because the user? Defined message is posted at the end
The message queue and is therefore handled at a later time, when everything
Has "settled down" and both the list control and its header both have been
Created.
That is to say, this is not directly calling getheaderctrl () in the presubclasswindow () function. Instead, it sends a custom message to itself and then adds the message processing function, call getheaderctrl () when you receive the message. In this case, "general" is normal.
So what is the key ???
It is very simple: getheaderctrl () will return the header object only when lvs_report is made. other styles will certainly return null.
The reason for the static drag-in control in the resource editor is not a problem, probably because the style is set to lvs_report when the drag-in control is dragged. experiment shows that if it is not lvs_report, in the presubclasswindow () function, getheaderctrl () still returns NULL.
The only confusing thing is that, if you do not specify its style as lvs_report during static addition, it is impossible in presubclasswindow (), but it is always getheaderctrl () in oncreate () it may work normally...
Therefore, the solution is:
1. Add controls using a static resource editor. If lvs_report is specified, no problem is found in presubclasswindow () or oncreate (), and getheaderctrl () is helpful.
2. Add controls using the static resource editor. If lvs_report is not specified, getheaderctrl () can only be used in oncreate (), but not in presubclasswindow.
3. the best way is in _ listctrl. when lvs_report is specified in create (), getheaderctrl () will certainly work after the clistctrl: oncreate () of the base class is called...
Or better, before getheaderctrl (), first modify: modifystyle (0, lvs_report), so you are most confident ..., however, this method is not particularly useful in presubclasswindow (), because presubclasswindow () is used before oncreate () is called, and the control does not exist yet, so it is not easy to use.
//////////////////////////////////////// //////////////////////////////////////// ////////////////
The final solution is to reload the oncreate function in the clistctrlex class, and implement subclass in the oncreate function.
Getheaderctrl () returns NULL when the listctrl control is reloaded.