Share via


목록 뷰 열을 추가하는 방법

이 항목에서는 목록 뷰 컨트롤에 열을 추가하는 방법을 보여 줍니다. 열은 목록 뷰 컨트롤이 보고서(세부 사항) 보기에 있을 때 항목 및 하위 항목을 표시하는 데 사용됩니다. 선택한 열의 텍스트도 타일 보기에 표시할 수 있습니다.

알아야 하는 작업

기술

필수 구성 요소

  • C/C++
  • Windows 사용자 인터페이스 프로그래밍

지침

목록 뷰 컨트롤에 열을 추가하려면 LVM_INSERTCOLUMN 메시지를 보내거나 ListView_InsertColumn 매크로를 사용합니다. 열을 삭제하려면 LVM_DELETECOLUMN 메시지를 사용합니다.

다음 C++ 코드 예는 ListView_InsertColumn 매크로를 호출하여 목록 뷰 컨트롤에 열을 추가합니다. 열 머리글은 애플리케이션의 헤더 파일에서 문자열 리소스로 정의되며 IDS_FIRSTCOLUMN부터 시작하여 연속적으로 번호가 매겨집니다. 열 수는 헤더 파일에서 C_COLUMNS로 정의됩니다.

// InitListViewColumns: Adds columns to a list-view control.
// hWndListView:        Handle to the list-view control. 
// Returns TRUE if successful, and FALSE otherwise. 
BOOL InitListViewColumns(HWND hWndListView) 
{ 
    WCHAR szText[256];     // Temporary buffer.
    LVCOLUMN lvc;
    int iCol;

    // Initialize the LVCOLUMN structure.
    // The mask specifies that the format, width, text,
    // and subitem members of the structure are valid.
    lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

    // Add the columns.
    for (iCol = 0; iCol < C_COLUMNS; iCol++)
    {
        lvc.iSubItem = iCol;
        lvc.pszText = szText;
        lvc.cx = 100;               // Width of column in pixels.

        if ( iCol < 2 )
            lvc.fmt = LVCFMT_LEFT;  // Left-aligned column.
        else
            lvc.fmt = LVCFMT_RIGHT; // Right-aligned column.

        // Load the names of the column headings from the string resources.
        LoadString(g_hInst,
                   IDS_FIRSTCOLUMN + iCol,
                   szText,
                   sizeof(szText)/sizeof(szText[0]));

        // Insert the columns into the list view.
        if (ListView_InsertColumn(hWndListView, iCol, &lvc) == -1)
            return FALSE;
    }
    
    return TRUE;
} 

목록 뷰 컨트롤 참조

목록 뷰 컨트롤 정보

목록 뷰 컨트롤 사용