Step 1: Create a column
Note: The development platform is vs2008.net.
1,CreateMFCApplicationProgramIn the left-side navigation pane.
The procedure is as follows:ÀNewÀProjectÀVisual c ++ÀMFCÀMFCApplication, enter the name of the project you want to create in the name below, here isListctrl.
InMFCSelect the reference program type in the Application Wizard, select the application type based on the dialog box on the right of the window, and then select complete.
2,Go to the project, and you will see a dialog box that shows the"Todo: Delete in the place dialog box ".
3,DragList contrrolIn the dialog box, adjust the size to the desired state.
4,SetIDIsIdc_list1)ViewChangedReportGenerally, the default value isIcon.
5, Use CTRL + Double-click the member variable to open the Add member variable wizard. Select control variable and Control ID For List Control Of ID (Default value: Idc_list1 ), Set the access permission (generally Public ), Variable type ( Clistctrl ) , You can set the variable name by yourself. M_listctrl, Click OK. It can be found that a variable is added to the class corresponding to your dialog box. The class name corresponding to the dialog box is Cxxxxdlg , Xxx Name of your project. Clistctrldlg .
6,Class corresponding to the dialog boxClistctrldlgInOninitdialog (void)InCdialog: oninitdialog ();Then you can addCodeAs follows:
M_listctrl.insertcolumn (0, _ T ("Name"), Lvcfmt_left, 60,-1 );
7,PressF5To debug the program, you can find the following window:
8,This is becauseWindows 7The results on the system.XPThe following visual effects are a little different. You can also find that the width of the name column can be adjusted.
9,Next let's take a look at the line of code that we just added:
M_listctrl.insertcolumn (0, _ T ("Name"), Lvcfmt_left, 60,-1 );
M_listctrlIs the member variable we just added, that isClistctrlClass variables can also be understoodClistctrlAn object of the class;
insertcolumn (); actually clistctrl class member functions. The specific content is as follows:
Clistctrl: insertcolumn
Int insertcolumn (INT ncol, const lvcolumn * pcolumn)
Int insertcolumn (INT ncol,
Lpctstr lpszcolumnheading,
Int nformat = lvcfmt_left,
Int nwidth =-1,
Int nsubitem =-1
)
Return Value: If successful, the index value of the new column is returned. Otherwise-1.
Parameters:
Ncol |
Index value of the new column. |
pcolumn |
lvcolumn structure address. |
lpszcolumnheading |
address of the string containing the column title. |
nformat |
specifies an integer for column alignment. It is one of the following values: lvcfmt_left , lvcfmt_right or lvcfmt_center . |
nwidth |
column width in pixels. If the parameter is -1 , the column width is not set. |
Nsubitem |
The index of the subitem associated with the column. If this parameter is-1, So no subitem is related to the column. |
Note: insert a new column in the List View control.
10. In fact,
Int insertcolumn (INT ncol,
Lpctstr lpszcolumnheading,
Int nformat = lvcfmt_left,
Int nwidth =-1,
Int nsubitem =-1
)
Can be passed directly to the four values of the function. The last value is not required, and the same effect can be achieved.
from: http://blog.csdn.net/snsn1984/article/details/5387901