Brief Introduction
After using wtl for a while, I found that there were very few related documents. In my opinion, wtl is the best solution to Win32 programming, and the more documents we can get on the network, the more helpful we are.
This article describes how to use wtl to create a custom Drawing Control.
I chose to use a listview to associate with the article neat stuff to do with List controls using customdraw. This article demonstrates the same concept through MFC. I chose a simple method and will lead you to more examples in the Document. Through these examples, you can improve the visual effect of your listview.
Mylistcontrol
Wtl makes the custom draw control very simple. The essentials are that you have to understand every line of code that implements the function in your project. We start with clistviewctrl to define our own class, so that we can place custom painting in the same place.
Class mylistview: Public cwindowimpl <mylistview, clistviewctrl>, <br/> Public ccustomdraw <mylistview> <br/>{< br/> Public: <br/> begin_msg_map (mylistview) <br/> chain_msg_map (ccustomdraw <mylistview>) <br/> end_msg_map () <br/>}
We must inherit from csf-wimpl, otherwise we will not be able to get any window message. More importantly, we need to inherit from ccustomdraw. This class starts with the chain_msg_map (ccustomdraw <mylistview>) Macro, allowing us to easily reload the clistviewcontrol painting process.
The macro chain_msg_map forwards messages to the default message ing in the base class.
If you take a look at ccustomdraw <t> In the atlctrls. H code, you will see that it defines the macro notify_code_handler (nm_customdraw, oncustomdraw), which processes custom drawn messages. Notify_code_handler (nm_customdraw, oncustomdraw) distributes messages to various components and calls corresponding functions. The following functions can be reloaded to draw your controls.
// Overrideables <br/> DWORD onprepaint (INT/* idctrl */, lpnmcustomdraw/* lpnmcustomdraw */); <br/> DWORD onpostpaint (INT/* idctrl */, lpnmcustomdraw/* lpnmcustomdraw */); <br/> DWORD onpreerase (INT/* idctrl */, lpnmcustomdraw/* lpnmcustomdraw */); <br/> DWORD onposterase (INT/* idctrl */, lpnmcustomdraw/* lpnmcustomdraw */); <br/> DWORD onitemprepaint (INT/* idctrl */, lpnmcustomdraw/* lpnmcustomdraw */); <br/> DWORD onitempostpaint (INT/* idctrl */, lpnmcustomdraw/* lpnmcustomdraw */); <br/> DWORD onitempreerase (INT/* idctrl */, lpnmcustomdraw/* lpnmcustomdraw */); <br/> DWORD onitemposterase (INT/* idctrl */, lpnmcustomdraw/* lpnmcustomdraw */);
We will reload several functions and change the background fill color of each line to make it layered and intuitive, making it easier to read each line.
So in our mylistviewctrl class, we will define the following two reloads.
Class mylistview: Public cwindowimpl <mylistview, clistviewctrl>, <br/> Public ccustomdraw <mylistview> <br/>{< br/> Public: <br/> begin_msg_map (mylistview) <br/> chain_msg_map (ccustomdraw <mylistview>) <br/> end_msg_map () <br/> DWORD onprepaint (INT/* idctrl */, lpnmcustomdraw/* lpnmcustomdraw */) <br/>{< br/> return cdrf_policyitemdraw; <br/>}< br/> DWORD onitemprepaint (INT/* idctrl */, lpnmcustomdraw) <br/>{< br/> nmlvcustomdraw * plvcd = reinterpret_cast <nmlvcustomdraw *> (lpnmcustomdraw); <br/> // This is the prepaint stage for an item. here's where we set the <br/> // item's text color. our return value will tell windows to draw the <br/> // item itself, but it will use the new color we set here for the background <br/> colorref crtext; <br/> If (plvcd-> nmcd. dwitemspec % 2) = 0) <br/> crtext = RGB (200,200,255); <br/> else <br/> crtext = RGB (255,255,255 ); <br/> // store the color back in the nmlvcustomdraw struct. <br/> plvcd-> clrtextbk = crtext; <br/> // tell windows to paint the control itself. <br/> return cdrf_dodefault; <br/>}< br/> };
Create controls
Most of the examples we see in msdn and other articles assume that the controls are created as resources and then linked to our code. To be different from the previous one, we will directly create a list box on the main interface.
Below are all the code we will add to the cmainframe created by the Wizard to create our custom Drawing Control. Find macro chain_msg
_ Map_member (m_listview ). If you lose this macro, your class will not be able to get the message nm_customdraw. This macro forwards messages in the data packet to the default message ing. This is exactly what we want to do.
In addition, we may have processed the nm_customdraw message at this level. Check to see what the Control ID is? Is the rendering process reasonable?
In some cases, this is very useful, and it is much simpler than what we are currently doing. Note: keep our code concise and clear.
Class cmainframe: Public cframeworkwimpl <cmainframe>, <br/> Public cupdateui <cmainframe>, <br/> Public cmessagefilter, public cidlehandler <br/>{< br/> public: <br/> //... <br/> // code left out for readability <br/> //... <br/> begin_msg_map (cmainframe) <br/> //... <br/> // code left out for readability <br/> //... <br/> chain_msg_map_member (m_listview) <br/> end_msg_map () </P> <p> lresult oncreate (uint/* umsg */, wparam/* wparam */, <br/> lparam/* lparam */, bool &/* bhandled */) <br/>{< br/> // code left out for readability <br/> // create a list box <br/> rect r = }; <br/> m_listview.create (m_hwnd, R, clistviewctrl: getwndclassname (), <br/> ws_child | ws_visible | visible | <br/> ws_clipchildren | lvs_report, ws_ex_clientedge ); </P> <p> // fill in the headers <br/> m_listview.addcolumn (_ T ("symbol"), 0 ); <br/> m_listview.addcolumn (_ T ("company"), 1); <br/> m_listview.addcolumn (_ T ("price"), 2 ); <br/> m_listview.additem (252.25, "VOD"); <br/> m_listview.additem (, "Vodaphone airtouch"); <br/> m_listview.additem "); <br/> m_listview.additem (149.9, "Lat"); <br/> m_listview.additem (, "lattice group"); <br/> m_listview.additem "); <br/> m_listview.additem (132.50, "linoleic"); <br/> m_listview.additem (, "claims direct"); <br/> m_listview.additem "); <br/> return 0; <br/>}</P> <p> //... <br/> // code left out for readability <br/> //... </P> <p> Public: <br/> mylistview m_listview; <br/> };
In oncreate (), we are only adding several example values. Success!
License)
This article does not have a clear license.
By BILLY leverington
Translation: sky
Http://www.codeproject.com/KB/wtl/customdrawlist_wtl.aspx.