mfc控件 --动态生成与事件CButton CEdit CTreeCtrl

    
    CTreeCtrl* pTree= new CTreeCtrl;
    pTree->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(100, 100, 300, 400), this, 5000);    
    pTree->InsertItem("CTreeCtrl 窗框",TVI_ROOT,TVI_LAST);    
    CString str;
    HTREEITEM hItem;      
    for (int i = 0; i < 5; i++)
    {
        str.Format(TEXT("item %d"), i);
        hItem = pTree->InsertItem(str);        
        CPoint *ptOrg = new CPoint(50*i,200);        
        if (hItem != NULL)
        {pTree->SetItemData(hItem, (DWORD_PTR)ptOrg); //(DWORD_PTR)}
        ptOrg = NULL;
    }
CListCtrl* pCtrlList= new CListCtrl;
pCtrlList->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(1000, 100, 1200, 400), this, 5001);//为表格控件设置表头
DWORD dwExtStyles1 = pCtrlList->GetExtendedStyle();
pCtrlList->SetExtendedStyle(dwExtStyles1 | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES );
LONG lStyle;
lStyle = GetWindowLong(pCtrlList->m_hWnd, GWL_STYLE);//获取当前窗口style
lStyle &= ~LVS_TYPEMASK; //清除显示方式位
lStyle |= LVS_REPORT; //设置style
SetWindowLong(pCtrlList->m_hWnd, GWL_STYLE, lStyle);//设置style

for(i = 0 ; i < 2 ; i++)
{  
    if(i ==0)
    pCtrlList->InsertColumn(i,"名称",LVCFMT_LEFT,100,i);    
    else
    pCtrlList->InsertColumn(i,"数值 "+ itos(i),LVCFMT_LEFT,50,i);      
 }

pCtrlList->InsertItem(0,"CListCtrl 窗框");

void CTestView::OnMENUTest4() 
{

    int i =1;

    HTREEITEM Root = pTree->GetRootItem();                 //获取根节点
    HTREEITEM BrothorItem= pTree->GetNextSiblingItem(Root);//获取跟结点的下一个兄弟结点;
    do
    {    
        CString strText = pTree->GetItemText(BrothorItem);      //获取兄弟结点的文本
        //AfxMessageBox(strText);

            //pCtrlList->SetItemText(0,i,strText);//设置该行的不同列的显示字符

            pCtrlList->InsertItem(i,strText);
            CPoint * ptOrg = (CPoint*)pTree->GetItemData(BrothorItem);    
            if(ptOrg)
            {
                CString sMsg = itos(ptOrg->x);

                pCtrlList->SetItemText(i,1,sMsg);//设置该行的不同列的显示字符
            }        
        //pCtrlList->SetItemText(i,0,strText);
        i++;
    } while ((BrothorItem = pTree->GetNextSiblingItem(BrothorItem)) != NULL); //主要就是这里.循环遍历父节点的兄弟结点.
}

#define IDC_MYBUTTON                    32795
#define IDC_MYEDIT                      32796

    afx_msg void OnMybtn1();

ON_BN_CLICKED(IDC_MYBUTTON, OnMybtn1)

    CButton *p_Button = new CButton();
    ASSERT_VALID(p_Button);
    p_Button->Create( "123", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(20,10,80,40), this, IDC_MYBUTTON  );

void CTestView::OnMybtn1() 
{
    // TODO: Add your command handler code here
    
    AfxMessageBox("button");
    
}

class DynCEdit : public CEdit

    DynCEdit *pEdit = new DynCEdit;
    pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
              CRect(400, 50, 450, 75), this, IDC_MYEDIT);
    SetDlgItemText(IDC_MYEDIT,_T("abc"));


void DynCEdit::OnKillfocus() 
{
    // TODO: Add your control notification handler code here
    AfxMessageBox("edit");
}
BOOL CTestView::PreTranslateMessage(MSG* pMsg) 
{
    // TODO: Add your specialized code here and/or call the base class
     if (pMsg->message == WM_CHAR && pMsg->wParam == VK_RETURN)
     {
        HWND hwnd1 = ((CEdit*)GetDlgItem(40000))->m_hWnd;     //Edit框ID号
        if (pMsg->hwnd == hwnd1)
        {
            AfxMessageBox("000");         
        }    
     }
    
    return CZoomView::PreTranslateMessage(pMsg);
}
 

///

CTreeCtrl *pTree ;

    pTree= new CTreeCtrl;
    pTree->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(400, 100, 600, 400), this, 5000);

    pTree->InsertItem("窗框",TVI_ROOT,TVI_LAST);

    CString str;
    HTREEITEM hItem;   
    
    for (int i = 0; i < 5; i++)
    {
        str.Format(TEXT("item %d"), i);
        hItem = pTree->InsertItem(str);

        CPoint *ptOrg = new CPoint(50*i,200); 

        if (hItem != NULL)
        {
            pTree->SetItemData(hItem, (DWORD_PTR)ptOrg); //(DWORD_PTR)
        }
        ptOrg = NULL;
    }

    


BOOL CTestView::PreTranslateMessage(MSG* pMsg) 
{
    // TODO: Add your specialized code here and/or call the base class

     if (pMsg->message == WM_LBUTTONDBLCLK )
     {
         HWND hwnd2 = ((CTreeCtrl*)GetDlgItem(5000))->m_hWnd;     //ID号
         if (pMsg->hwnd == hwnd2)
         {          
        
            //HTREEITEM hItem = pTree->GetSelectedItem();   //获得的项并不是我点击的项
            CPoint pt = GetCurrentMessage()->pt;
            pTree->ScreenToClient(&pt);
            UINT uFlag = 0;
            HTREEITEM hitem =  pTree->HitTest(pt,&uFlag);

            if (NULL != hitem && (TVHT_ONITEM & uFlag))
            {            
                CString   sMsg;   
                sMsg.Format(_T("%s"),pTree->GetItemText(hitem)); 


                CPoint * ptOrg = (CPoint*)pTree->GetItemData(hitem);    
                if(ptOrg)
                sMsg = sMsg + "---" + itos(ptOrg->x);

                AfxMessageBox(sMsg);        

           }
         }    
     }    
    return CZoomView::PreTranslateMessage(pMsg);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值