none
一段代码求注释 RRS feed

  • 问题

  • void DDX_CBData(CDataExchange* pDX, int nIDC, DWORD& dwItemData)
    {
        HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
        if (pDX->m_bSaveAndValidate)
        {
            dwItemData = 0L;
            int nCurSel = (int) ::SendMessage(hWndCtrl, CB_GETCURSEL, 0, 0L);
            if (nCurSel != CB_ERR)
                dwItemData = ::SendMessage(hWndCtrl, CB_GETITEMDATA, nCurSel, 0L);
        }
        else
        {
            int nIndex;
            for ( nIndex = (int) ::SendMessage(hWndCtrl, CB_GETCOUNT, 0, 0L) - 1;
            nIndex >= 0; nIndex--)
            {
                DWORD dwData = ::SendMessage(hWndCtrl, CB_GETITEMDATA, nIndex, 0L);
                if (dwData == dwItemData)
                {
                    ::SendMessage(hWndCtrl, CB_SETCURSEL, nIndex, 0L);
                    break;
                }
            }
            if (nIndex < 0) // item wasn't found
            SendMessage(hWndCtrl, WM_SETTEXT, 0, (LPARAM) (LPCSTR) "???");
        }
    }这段代码看了半天找而来半天资料,还是只能看个大概,能能帮忙仔细的解说下,帮忙注释下,感激不尽。本人初学者
    2011年6月20日 13:47

答案

  • 您上面的这个应该是做类似DDX_Control()的操作,简单的可以参考对话框工程中的DoDataExchange()函数中的相关代码。可以参考下面CDataExchange类的定义

    class CDataExchange
    {
    // Attributes
    public:
    	BOOL m_bSaveAndValidate;  // TRUE => save and validate data
    	CWnd* m_pDlgWnd;      // container usually a dialog
    
    // Operations (for implementors of DDX and DDV procs)
    	HWND PrepareCtrl(int nIDC);   // return HWND of control
    	HWND PrepareEditCtrl(int nIDC); // return HWND of control
    	void Fail();          // will throw exception
    
    #ifndef _AFX_NO_OCC_SUPPORT
    	CWnd* PrepareOleCtrl(int nIDC); // for OLE controls in dialog
    #endif
    
    // Implementation
    	CDataExchange(CWnd* pDlgWnd, BOOL bSaveAndValidate);
    
    	HWND m_hWndLastControl;  // last control used (for validation)
    	BOOL m_bEditLastControl;  // last control was an edit item
    };
    

    关于您上面的SendMessage()的发送消息的部分您可以针对具体的消息,参考MSDN文档,理解各个消息的含义。



    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    • 已标记为答案 Rob Pan 2011年6月27日 8:57
    2011年6月23日 0:33
    版主