質問

私はWin32 / MFCプロジェクトに取り組んでいます。 私は、時々、文字の文字列を追加する必要があるカスタムCLISTCTRLコントロールがあります。 私のCLISTCTRLに動的に追加されたアイテムに対して何らかの操作を実行する必要があります。

超基本的に、私は以下の必要があります:

  1. 検出の追加単一要素の追加。
  2. _single items_を再検査してください(理想的には、indertItem()呼び出しの直後)。
  3. store マップ内の単一項目の値を維持します。

    この方法drawItem()を上書きすることを考えていました。しかし、OnDraw Event は私のclistctrlに利用できるようにしていないようです。

    イベントは生成されません。

    重要: myCustomClistctrlには " true に設定されているが" view "プロパティを設定していることに注意してください。 > ""プロパティはではありませんレポート

    だから、私は説明されているように、私のCustomDrawハンドラを書くことを決定しました。 .85%29.aspx "rel=" nofollow ">ここ http://msdn.microsoft.com/en-us/library/ms364048%28v=vs.80%29。 ASPX "REL=" NOFOLLOW ">こちら

    ここ別のコード例を見ることができます。

    それでは、私のclistctrlから単一のItemIDを取得する方法が必要です。
    より正確には、NMHDR Struct から単一の項目IDを取得する方法が必要です。

    どうすればいいですか? 私が追加した last 項目のIDを取得できるだけです。 私はそれが私が見つけることができない単純な間違いだと確信しています。

    下記のサンプルコード:

    CLIST CTRLを含むダイアログのソース:

    /* file MyDlg.cpp */
    
    #include "stdafx.h"
    #include "MyDlg.h"
    
    // MyDlg dialog
    
    IMPLEMENT_DYNAMIC(MyDlg, CDialog)
    
    MyDlg::MyDlg(CWnd* pParent)
        : CDialog(MyDlg::IDD, pParent)
    {
    }
    
    MyDlg::~MyDlg()
    {
    }
    
    void MyDlg::DoDataExchange(CDataExchange* pDX)
    {
        CDialog::DoDataExchange(pDX);
        DDX_Control(pDX, IDC_LIST1, listView); /* listView is a MyCustomCListCtrl object */
    }
    
    BEGIN_MESSAGE_MAP(MyDlg, CDialog)
        ON_BN_CLICKED(IDC_BUTTON1, &MyDlg::OnBnClickedButton1) 
    END_MESSAGE_MAP()
    
    BOOL MyDlg::OnInitDialog()
    {
        CDialog::OnInitDialog();
        return TRUE;
    }
    
    /* OnBnClickedButton1 handler add new strings to MyCustomCListCtrl object */
    
    void MyDlg::OnBnClickedButton1()
    {
        listView.InsertItem(0, "Hello,");
        listView.InsertItem(1, "World!");
    }
    
    .

    マイカスタムCLIST CTRLソース:

    /* file MyCustomCListCtrl.cpp */
    
    #include "stdafx.h"
    #include "MyCustomCListCtrl.h"
    
    MyCustomCListCtrl::MyCustomCListCtrl()
    {
    }
    
    MyCustomCListCtrl::~MyCustomCListCtrl()
    {
    }
    
    BEGIN_MESSAGE_MAP(MyCustomCListCtrl, CListCtrl)
        //{{AFX_MSG_MAP(MyCustomCListCtrl)
        //}}AFX_MSG_MAP
        // ON_WM_DRAWITEM()                             /* WM_DRAWITEM NON-AVAILABLE */
        ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
    END_MESSAGE_MAP()
    
    // 'Owner Draw Fixed' property is already TRUE
    /*  void CTranslatedCListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
        bool inside = true; /* Member function removed: I never enter here... */
    }  */
    
    void MyCustomCListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
    {
        /* Here, I must retrieve single strings added to my MyCustomCListCtrl object */
    
        LPNMLISTVIEW plvInfo = (LPNMLISTVIEW)pNMHDR;
        LVITEM lvItem;
    
        lvItem.iItem = plvInfo->iItem;          /* Here I always get _the same_ ID: ID of last element...*/
        lvItem.iSubItem = plvInfo->iSubItem;    // subItem not used, for now...
    
        int MyID = 0;
    
        this->GetItem(&lvItem); // There mai be something error here?
        MyID = lvItem.iItem;
    
        CString str = this->GetItemText(MyID, 0); /* ...due to previous error, here I will always get the last string I have added("World!") */
    
        // Immediately after obtaining ALL IDS, I can Do My Work
    
        *pResult = 0;
    }
    
    .

    任意の助けが高く評価されています!

    p.S。 のようなヒントを教えてください。

    1. あなたの「独自の描画固定」プロパティをtrueに設定します。
    2. 行 "on_wmdrawItem()" を挿入したことを確認しました
    3. CLISTCTRLをレポートとして変換します。

      私はすでに試してみました...: - )

      すべてのおかげで!

      それはです

役に立ちましたか?

解決 2

first of all... Thank you wasted your precious time with this stupid question. I never found anything about LVN_INSERT event. I write scientific software(most on Linux platform); I am not a long-time Win32 developer, so I don't know Win32 APIs in depth. I have modified source file of MyCustomCListCtrl class, as you have suggested. Code below seems to be the best( and faster )way to achieve what I want:

    /* file MyCustomCListCtrl.cpp */

    #include "stdafx.h"
    #include "MyCustomCListCtrl.h"

    MyCustomCListCtrl::MyCustomCListCtrl()
    {
    }

    MyCustomCListCtrl::~MyCustomCListCtrl()
    {
    }

    BEGIN_MESSAGE_MAP(MyCustomCListCtrl, CListCtrl)
        //{{AFX_MSG_MAP(MyCustomCListCtrl)
        //}}AFX_MSG_MAP
        ON_NOTIFY_REFLECT(LVN_INSERTITEM, OnLvnInsertItem)
    END_MESSAGE_MAP()

    ...

    afx_msg void CTranslatedListCtrl::OnLvnInsertItem(NMHDR* pNMHDR, LRESULT* pResult)
    {
        LPNMLISTVIEW plvInfo = (LPNMLISTVIEW)pNMHDR;
        CString str = this->GetItemText(plvInfo->iItem, 0);

        // Add Some Logic

        *pResult = 0;
    }

Can You confirm? From what I can see, it seems to work. :-) Thanks again!

IT

他のヒント

First, if you need to detect adding of single items, why don't you handle the LVN_INSERTITEM message? I mean, that's what that message is for. Handling NM_CUSTOMDRAW instead is the wrong way, since you won't necessarily get that notification if the control is hidden, your window minimized, ...

In your OnCustomDraw() you always get the same ID: that's because the list control always draws all visible items, so you get the ID of the first visible item. If you set a breakpoint there, then on the next run the control gets refreshed and the drawing starts again from the first visible item.

Note: since you're handling NM_CUSTOMDRAW, you won't get any notification of added items that are not inserted into the visible part of the control! So as I mentioned, you should handle LVN_INSERTITEM instead.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top