LoginSignup
0
0

VC++ MFCでの(List Control)作成方法メモ

Last updated at Posted at 2024-02-01

すぐに忘れてしまうので、VC++ MFCでの(List Control)作成方法メモ

環境

  • Windows 10
  • Visual Studio 2022 C++

準備

  1. 最新のvXXXビルドツール用 C++ MFC(x86およびx64) のインストール

作成

  1. 新しいプロジェクト で、MFCアプリを作成

    • アプリケーションの種類を ダイアログベース で作成する
      (リソース上でコントロールを追加できる)

  2. リソース上のダイアログに、「List Control」を追加

    • プロパティの「ビュー」は、「レポート」を設定、その他属性設定
    • リソース「List Control」で右クリック
      → 変数の追加
        → コントロール変数の追加 に「m_list」で、完了
    • OnInitDialog に初期設定を追記
        // スタイル
        m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
    

    link:【C++/MFC】リストビューの基本的な使い方

  3. まず、最初の列を設定する

    • 今回は、カラム(項目)数は、初期固定とする
    • Dialog側のClass(OnInitDialog、OnPaint、等のClass)に、最初の列用のstructをprivateで定義する
    class CxxxDlg : public CDialogEx
    {
    ...
    private:
    	struct FirstCol
    	{
    		int cx;				// 幅
    		std::tstring text;	// タイトルテキスト
    
    	};
    
    • ついでに、カラムIndexと個数の為、同じ位置にenumも定義する
    
    ...
    
    enum class ItemRow : int
    {
    	en = 0,
    	ja,
    
    	max
    };
    
    • OnInitDialog の初期設定記入箇所に戻り、最初の列(カラム)を挿入する
    	const FirstCol init_FirstCol[] =
    	{
    		{200, TEXT("English")},
    		{100, TEXT("日本語") },
    	};
    
    	LVCOLUMN col;
    
    	col.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
    	col.fmt = LVCFMT_LEFT;
    
    	for (int i = 0; i < _countof(init_FirstCol); i++)
    	{
    		col.cx = init_FirstCol[i].cx;
    		col.pszText = (LPTSTR)init_FirstCol[i].text.c_str();
    		col.iSubItem = i;
    		ListView_InsertColumn(m_list, i, &col);
    	}
    

操作

  • 後は追加したいタイミングで、ListView_InsertItemで追加して、ListView_SetItemで残りの項目を変えていく
    	// リスト追加
    	LVITEM item = { 0 };
    	int nCount = ListView_GetItemCount(m_list);
    	std::tstring msg1 = TEXT("English") ;
    
    	item.mask = LVIF_TEXT;
    	item.pszText = (LPTSTR)msg1.c_str();
    	item.iItem = nCount;
    	item.iSubItem = static_cast<int>(ItemRow::en);
    	ListView_InsertItem(m_list, &item);
    
    
    	item = { 0 };
    	std::tstring msg2 = std::format(TEXT("英語 {}"), nCount);
    
    	item.mask = LVIF_TEXT;
    	item.pszText = (LPTSTR)msg2.c_str();
    	item.iItem = nCount;
    	item.iSubItem = static_cast<int>(ItemRow::ja);
    	ListView_SetItem(m_list, &item);
    
  • 以下の通りになります
    2024-02-01_122731.png
    link:リストビュー
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0