⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listbasedform.html

📁 基于WINDOWS mobile 的用于创建一个窗体和自定义试图的工程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
            <td>
                General purpose string editor.
            </td>
        </tr>
    </tbody>
</table>
</p>
<p>
The <code>CFormListCtrl</code> class derives from MFC's <code>CListCtrl</code> via <code>CBaseListCtrl</code>. Each item in the 
form is a <code>CFormItem</code>-derived class. The base class implements the common custom draw logic leaving to the derived
classes the specialized custom drawing implementations as well as managing the input control.
<h3>Creating a specialized form</h3>
<p>
The first step to create a new form is to derive a new class from <code>CFormListCtrl</code>.
        This will be your specialized form and all the data items will be declared within it:</p>
        
<pre>class CDemoForm : public CFormListCtrl
{
public:
    CDemoForm(void);
    virtual ~CDemoForm(void);

    CFormItemGroup    m_groupUser,
                      m_groupData;
    CFormItemString   m_itemFirstName,
                      m_itemLastName;
    CFormItemDateTime m_itemBirthDate;
    CFormItemNumber   m_itemInteger,
                      m_itemNumber,
                      m_itemCurrency;
    CFormItemCombo    m_itemCombo;
    CFormItemCheck    m_itemCheck;

    virtual BOOL Initialize();
    virtual void ItemOptions(CFormItem* pItem);

private:
    CMenu m_menuCombo; // Menu for combo box item
};
</pre>

<h3>Initializing the items</h3>
<p>
Each item is static so you don't have to worry about memory management. Declaring these objects is the first step, the next
step in creating the form is to initialize these on the form constructor:<pre>m_groupUser.Init(_T("User"));
m_groupData.Init(_T("Data"));
	
m_itemLastName .Init(_T("Last name:"), FIF_NORMAL | FIF_MANDATORY);
m_itemFirstName.Init(_T("First name:"));
m_itemBirthDate.Init(_T("Birth date:"));
m_itemCheck    .Init(_T("Check:"));

m_itemInteger .Init(_T("An integer:"), fmtInteger);
m_itemNumber  .Init(_T("A number:"),   fmtNumber);
m_itemCurrency.Init(_T("Currency:"),   fmtCurrency);

m_itemCombo.Init(_T("Selection:"), FIF_NORMAL | FIF_HASOPTIONS);
m_itemCombo.AddString(_T("Apples"),  1);
m_itemCombo.AddString(_T("Oranges"), 2);
m_itemCombo.AddString(_T("Grapes"),  3);
m_itemCombo.SetSel(0);
</pre>

<p>
Item initialization entails setting a caption string and a few other optional parameters. 
Depending on the item type, the <code>Init</code> 
parameter list will vary, but one of the most important parameters that is common to all is the flag bitmask:

<pre>#define FIF_LINE        0x00000001    // Draw a line at the bottom
#define FIF_VISIBLE     0x00000002    // Item is visible
#define FIF_ENABLED     0x00000004    // Item is enabled
#define FIF_MANDATORY   0x00000008    // Data for the item must be filled in by the user
#define FIF_UNDERLINE   0x00000010    // Underline the caption text
#define FIF_HASOPTIONS  0x00000020    // The item has a context menu (on the caption)
#define FIF_DRAWOPTIONS 0x00000040    // Internal flag
#define FIF_SELECTED    0x00000080    // The item is selected

#define FIF_NORMAL      (FIF_LINE|FIF_VISIBLE|FIF_ENABLED)
</pre>

<p>
By default all items are created with the <code>FIF_NORMAL</code> flags set. In this
    sample I am using two special flags on the first and last data items: <code>FIF_MANDATORY</code>
    and <code>FIF_HASOPTIONS</code>. The first flag tells the item that data must be
    filled in before the form is dismissed. Graphically the item will be displayed with
    a red caption and a yellow background on the data. The <code>FIF_HASOPTIONS</code> flag marks the
    item as displaying custom options that are handles by the <code>ItemOptions</code> virtual method.<p>
Before the form is displayed you can set the items initial values. Each item has a set of <code>Get</code> and <code>Set</code> 
functions to transfer the data to and from the item. Please note that the item data is cached so setting the data after the form
is displayed requires that the item be repainted. You can do this with a regular call to <code>CFormListCtrl::RedrawItems</code>, 
inherited from MFC's <code>CListCtrl</code>.
<h3>Form initialization</h3>
    <p>
        Form initialization is handled by the virtual <code>Initialize</code> method. This
        method is inherited from the base class where some initialization does occur (like
        ceating the default fonts and adding the two invisible header columns). Your code
        must call the base class initialization method before doing its own stuff:</p>
<p>
</p>
<pre>BOOL CDemoForm::Initialize()
{
    if(CFormListCtrl::Initialize())
    {
        EnableEdit(TRUE);

        m_groupUser.AddItem(&amp;m_itemLastName);
        m_groupUser.AddItem(&amp;m_itemFirstName);
        m_groupUser.AddItem(&amp;m_itemBirthDate);
        m_groupUser.AddItem(&amp;m_itemCheck);

        m_groupData.AddItem(&amp;m_itemInteger);
        m_groupData.AddItem(&amp;m_itemNumber);
        m_groupData.AddItem(&amp;m_itemCurrency);
        m_groupData.AddItem(&amp;m_itemCombo);

        AddItem(&amp;m_groupUser);
        AddItem(&amp;m_groupData);

        return TRUE;
    }
    return FALSE;
}
</pre>

<p>
    The first line marks this form as editable while the others create the structure
    for the form by setting the data items in groups. Note that only the group items
    are added to the form. This is not mandatory - you can add data items directly to
    the form, not only to group items. What you cannot do is nest groups.</p>
<h3>Item options</h3>
<p>
Item options allow you to add extra functionality to an item. As I showed above,
    you must mark an item with options with a flag and when the user clicks its caption,
    your form's ItemOptions virtual method will be called with a pointer to the item.
    Here is how this sample implements this method:
    
<pre>void CDemoForm::ItemOptions(CFormItem *pItem)
{
    // Check if this is the combo box item
    if(pItem == &amp;m_itemCombo)
    {
        int iItem;

        // Get the item index
        iItem = FindFormItem(pItem);
        if(iItem != -1)
        {
            CRect    rc;

            // Now get the item bounding rect
            if(GetItemRect(iItem, &rc, LVIR_BOUNDS))
            {
                CPoint pt(rc.left, rc.bottom);
                int    nCmd;

                // Display a popup menu below the arrow
                ClientToScreen(&amp;pt);
                nCmd = m_menuCombo.TrackPopupMenu(TPM_LEFTALIGN | TPM_RETURNCMD, pt.x, pt.y, this);

                switch(nCmd)
                {
                case ID_COMBO_ADD:
                    MessageBox(_T("Add new item"), _T("Option"));
                    break;
                case ID_COMBO_EDIT:
                    MessageBox(_T("Edit item"), _T("Option"));
                    break;
                case ID_COMBO_DELETE:
                    MessageBox(_T("Delete item"), _T("Option"));
                    break;
                }
            }
        }
    }
}
</pre>
<p>
Here I am just displaying a context menu right below the options arrow, but you could implement whatever feature you want, like displaying another dialog. If you
    want to display a context menu like this sample, please reuse the above code as
    it correctly positions the context menu below the item caption.&nbsp;<h2>Points of interest</h2>

<p>
    The code I present here is a bit old (2003-2004 time frame) and it required some
    adaptations for the modern Windows Mobile devices. Back when this code was written,
    there was only one display resolution and no keyboards on Windows Mobile devices.
    Now you have all sorts of devices with and without keyboards or thumb pads, and
    the display resolutions and screen orientations will vary.</p>
    <p>
        Interestingly the code was easily adapted using Microsoft's own "device resolution
        aware" header file (<code>DeviceResolutionAware.h</code>). This file has some very
        interesting programming techniques that are worth looking at.</p>

<h2>Credits</h2>

<p>
Some of the code in this article was adapted or inspired from the following sources:<br />
    <br />
<a href="http://www.codeproject.com/listctrl/listeditor.asp">Easy Navigation Through an Editable List View</a> by <a href="http://www.codeproject.com/script/Articles/list_articles.asp?userid=131">Lee Nowotny</a><br />
Flipcode's <a href="http://www.flipcode.org/cgi-bin/fcarticles.cgi?show=64064">Dialog Template</a> by Max McGuire</p>
    <p>
I have also used adapted versions of code I previously posted:<br />
    <br />
<a href="http://www.codeproject.com/ce/cdatetimeformat.asp">A Date and Time formatter</a><br />
<a href="http://www.codeproject.com/ce/tapandhold.asp">Handling tap-and-hold</a><br />
<a href="http://www.codeproject.com/ce/ppc_numpad.asp">Pocket PC Numeric Key Pad</a></p>

<h2>History</h2>

<p>
    2007-10-18: Published&nbsp;</p>
    <p>
        &nbsp;</p>


<!-------------------------------    That's it!   --------------------------->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -