Http://www.freedelphitips.com/listview-from-another-painting-from-the-paint-effect.html
Previously, the self-painting Effect of listview was written.
And a self-painted Effect of ListBox.
Now we have made another self-painting effect for listview.
Generally, listview displays a row of data in the vsreport type. In this example, the data is displayed in the vsicon type. All data is automatically painted in the advancedcustomdrawitem event.
Technical points involved
1. Adjust the spacing between items
2. Change the color and background of an item with the mouse over it
3. You can control the background of each item.
Download demo
Code:
Unit launcherunit; <br/> interface </P> <p> uses <br/> Windows, messages, sysutils, variants, classes, graphics, controls, forms, <br/> dialogs, buttons, extctrls, comctrls, commctrl, shellapi, imglist, stdctrls; </P> <p> type <br/> plistdata = ^ tlistdata; <br/> tlistdata = record <br/> sname: string; <br/> Desc: string; <br/> end; </P> <p> type <br/> tlauncherform = Class (tform) <br/> listview1: tlistview; <br/> image_icon: timagelist; <br/> imagelist1: timagelist; <br/> pnl_tip: tpanel; <br/> img_t_l: timage; <br/> img_t_m: timage; <br/> img_t_r: timage; <br/> pnl_bot: tpanel; <br/> img1: timage; <br/> img2: timage; <br/> img3: timage; <br/> speedbutton1: tspeedbutton; <br/> pnl_client: tpanel; <br/> img_c_l: timage; <br/> img_c_r: timage; <br/> pnl_line_l: tpanel; <br/> pnl_line_r: tpanel; </P> <p> procedure speedbutton1click (Sender: tobject); <br/> procedure listview1click (Sender: tobject); <br/> procedure formshow (Sender: tobject ); </P> <p> procedure formresize (Sender: tobject); <br/> procedure formcreate (Sender: tobject); <br/> procedure listview1mousemove (Sender: tobject; shift: tshiftstate; X, <br/> Y: integer); <br/> procedure listview1advancedcustomdrawitem (Sender: tcustomlistview; <br/> item: tlistitem; State: tcustomdrawstate; Stage: tcustomdrawstage; <br/> var defadradraw: Boolean); </P> <p> private <br/> procedure showdata (); </P> <p> Public <br/> {public declarations} <br/> end; </P> <p> var <br/> launcherform: tlauncherform; <br/> listshortcut: tlist; <br/> Implementation </P> <p> {$ R *. DFM} </P> <p> procedure tlauncherform. speedbutton1click (Sender: tobject); <br/> begin <br/> close; <br/> end; </P> <p> procedure tlauncherform. listview1click (Sender: tobject); <br/> var <br/> S: string; <br/> listdata: plistdata; <br/> begin <br/> If listview1.itemindex <0 Then exit; <br/> listdata: = listmediacut. items [listview1.itemindex]; <br/> S: = listdata. DESC; // obtain the address before the corresponding data <br/> showmessage (s); <br/> end; </P> <p> procedure tlauncherform. showdata (); <br/> var <br/> listdata: plistdata; <br/> I: integer; <br/> begin <br/> listview1.items. clear; <br/> listmediacut. clear; <br/> // create demo data <br/> for I: = 0 to 15 DO <br/> begin <br/> New (listdata ); <br/> listdata. sname: = 'name' + inttostr (I) + 'name'; <br/> listdata. desc: = 'specifies the '+ inttostr (I) + 'name'; <br/> listshortcut. add (listdata); <br/> end; </P> <p> for I: = 0 to listmediacut. count-1 do // Add null item <br/> listview1.items. add; <br/> end; </P> <p> procedure tlauncherform. formshow (Sender: tobject); <br/> var <br/> HR: thandle; <br/> begin <br/> // define the form corner <br/> HR: = createroundrectrgn (0, 0, width + 1, height + 1, 5, 5); // defines the garden corner rectangle (win api function) <br/> setjavaswrgn (handle, HR, true); // sets the garden corner window <br/> showdata; <br/> end; </P> <p> procedure tlauncherform. formresize (Sender: tobject); <br/> begin <br/> // showdata; <br/> self. repaint; <br/> end; </P> <p> procedure tlauncherform. formcreate (Sender: tobject); <br/> var <br/> imglist: timagelist; <br/> begin </P> <p> listview1.doublebuffered: = true; // enable dual-cache to prevent screen flash. <br/> listview1.tag: =-1; // remember the ID of the item where the last mouse was located. This tag generally does not need to be used as a global variable <br/> listshortcut: = tlist. create; </P> <p> // ---------------- set the largeimages of listview here, and enlarge its item with imglist <br/> imglist: = timagelist. create (NiL); <br/> imglist. width: = 49; <br/> imglist. height: = 49; <br/> listview1.largeimages: = imglist; // <br/> sendmessage (listview1.handle, lvm_seticonspacing, 0, makelong (70, 70 )); // set the icon spacing </P> <p> end; </P> <p> procedure tlauncherform. listview1mousemove (Sender: tobject; <br/> shift: tshiftstate; X, Y: integer); <br/> var <br/> P: tpoint; <br/> Li: tlistitem; <br/> listindex: integer; <br/> r: trect; // R indicates the text coverage range. <br/> imglistviewbk: tbitmap; // rfill indicates the text filling range. <br/> softicon: tbitmap; <br/> listdata: plistdata; <br/> begin <br/> P. x: = x; <br/> P. y: = y; <br/> Li: = listview1.getitemat (P. x, p. y); <br/> listindex: = (sender as tlistview ). items. indexof (LI); <br/> If listindex =-1 then exit; <br/> If listview1.items. count = 0 Then exit; </P> <p> If listview1.tag =-1 then // enter an item for the first time to draw the current item <br/> begin <br/> listview1.tag: = listindex; </P> <p> r: = listview1.items [listindex]. displayrect (drbounds); <br/> listdata: = listshortcut. items [listindex]; <br/> with listview1.canvas DO <br/> begin <br/> imglistviewbk: = tbitmap. create; <br/> imglistviewbk. width: = imagelist1.width; <br/> imglistviewbk. height: = imagelist1.height; <br/> imagelist1.draw (imglistviewbk. canvas, 0, 0, 2, true); <br/> stretchdraw (R, imglistviewbk ); // draw the background image </P> <p> // draw the icon <br/> softicon: = tbitmap. create; <br/> softicon. width: = 32; <br/> softicon. height: = 32; <br/> image_icon.draw (softicon. canvas, 0, 0, listindex, true); <br/> draw (R. left + (R. right-R. left-32) Div 2, R. top + 5, softicon); <br/> softicon. free; <br/> // draw the software name <br/> r. top: = R. top + 40; <br/> r. bottom: = R. bottom-5; <br/> r. left: = R. left + 3; <br/> r. right: = R. right-3; <br/> setbkmode (handle, transparent); <br/> drawtext (handle, <br/> pchar (listdata. sname), <br/> length (listdata. sname), <br/> r, <br/> dt_wordbreak or dt_center); <br/> end; <br/> with listview1.canvas DO <br/> if assigned (font. onchange) Then font. onchange (font); <br/> end <br/> else if (listview1.tag <>-1) and (listview1.tag <> listindex) then <br/> // if the location ID of the previous mouse is already present and the ID of this mouse is different from that of the previous mouse, the ID of this mouse is first drawn, at the same time, draw the item of the last Id as the default <br/> begin <br/> // draw the current item <br/> r: = listview1.items [listindex]. displayrect (drbounds); <br/> listdata: = listshortcut. items [listindex]; <br/> with listview1.canvas DO <br/> begin <br/> imglistviewbk: = tbitmap. create; <br/> imglistviewbk. width: = imagelist1.width; <br/> imglistviewbk. height: = imagelist1.height; <br/> imagelist1.draw (imglistviewbk. canvas, 0, 0, 2, true); <br/> stretchdraw (R, imglistviewbk ); // draw the background image </P> <p> // draw the icon <br/> softicon: = tbitmap. create; <br/> softicon. width: = 32; <br/> softicon. height: = 32; <br/> image_icon.draw (softicon. canvas, 0, 0, listindex, true); <br/> draw (R. left + (R. right-R. left-32) Div 2, R. top + 5, softicon); <br/> softicon. free; <br/> // draw the software name <br/> r. top: = R. top + 40; <br/> r. bottom: = R. bottom-5; <br/> r. left: = R. left + 3; <br/> r. right: = R. right-3; <br/> setbkmode (handle, transparent); <br/> drawtext (handle, <br/> pchar (listdata. sname), <br/> length (listdata. sname), <br/> r, <br/> dt_wordbreak or dt_center); <br/> end; <br/> // restore the previous color <br/> r: = listview1.items [listview1.tag]. displayrect (drbounds); <br/> listdata: = listshortcut. items [listview1.tag]; <br/> with listview1.canvas DO <br/> begin <br/> brush. color: = clwhite; <br/> fillrect (r); // fill color </P> <p> // draw an icon <br/> softicon: = tbitmap. create; <br/> softicon. width: = 32; <br/> softicon. height: = 32; <br/> image_icon.draw (softicon. canvas, 0, 0, listview1.tag, true); <br/> draw (R. left + (R. right-R. left-32) Div 2, R. top + 5, softicon); <br/> softicon. free; <br/> // draw the software name <br/> r. top: = R. top + 40; <br/> r. bottom: = R. bottom-5; <br/> r. left: = R. left + 3; <br/> r. right: = R. right-3; <br/> setbkmode (handle, transparent); <br/> drawtext (handle, <br/> pchar (listdata. sname), <br/> length (listdata. sname), <br/> r, <br/> dt_wordbreak or dt_center); <br/> end; <br/> with listview1.canvas DO <br/> if assigned (font. onchange) Then font. onchange (font); <br/> listview1.tag: = listindex; <br/> end; </P> <p> procedure tlauncherform. listview1advancedcustomdrawitem (<br/> Sender: tcustomlistview; item: tlistitem; State: tcustomdrawstate; <br/> stage: tcustomdrawstage; var defaultdraw: Boolean ); <br/> var <br/> r: trect; // R indicates the text coverage range. <br/> imglistviewbk: tbitmap; // background <br/> softicon: tbitmap; // icon <br/> listdata: plistdata; <br/> begin <br/> r: = item. displayrect (drbounds); <br/> listdata: = listshortcut. items [item. index]; <br/> with listview1.canvas DO <br/> begin <br/> If cdsselected in State then <br/> begin <br/> imglistviewbk: = tbitmap. create; <br/> imglistviewbk. width: = imagelist1.width; <br/> imglistviewbk. height: = imagelist1.height; <br/> imagelist1.draw (imglistviewbk. canvas, 0, 0, 0, true); <br/> stretchdraw (R, imglistviewbk); // draws the background image <br/> imglistviewbk. free; <br/> end; </P> <p> // draw an icon <br/> softicon: = tbitmap. create; <br/> softicon. width: = 32; <br/> softicon. height: = 32; </P> <p> image_icon.draw (softicon. canvas, 0, 0, item. index, true); <br/> draw (R. left + (R. right-R. left-32) Div 2, R. top + 5, softicon); <br/> softicon. free; </P> <p> // draw the software name <br/> r. top: = R. top + 40; <br/> r. bottom: = R. bottom-5; <br/> r. left: = R. left + 3; <br/> r. right: = R. right-3; <br/> setbkmode (handle, transparent); // set the text to transparent <br/> drawtext (handle, <br/> pchar (listdata. sname), <br/> length (listdata. sname), <br/> r, <br/> dt_wordbreak or dt_center); <br/> end; <br/> with sender. canvas DO <br/> if assigned (font. onchange) Then font. onchange (font); <br/> end.