java 添加年份组合框_为列表控件添加组合框和编辑框

为列表控件添加组合框和编辑框

在列表控件的指定单元格内,添加组合框和编辑框,改造自网上的代码

一必要条件 1.

给对话框加入如下的控件,并为其添加变量

CComboBox m_combox1;

CComboBox m_combox2;

CEdit m_Edit;

CListCtrl m_listctrl;

2. 为上面的控件添加失去焦点消息,和单击消息

ON_CBN_KILLFOCUS(IDC_COMBO1,

OnKillfocusCombo1)

ON_CBN_KILLFOCUS(IDC_COMBO2,

OnKillfocusCombo2)

ON_EN_KILLFOCUS(IDC_EDIT1,

OnKillfocusEdit1)

ON_NOTIFY(NM_CLICK, IDC_LIST1,

OnClickList1)

3. 记录单击列表控件时的行列号

int m_row,m_col;

二。初始化时,为它们创建数据

BOOL CListCtrlComboxDlg::OnInitDialog()

{

CDialog::OnInitDialog();

//....

// TODO: Add extra initialization here

//1 m_combox1.SetParent(&m_listctrl);

//确保CComboBox的坐标是相对于列表控件而言的。

m_combox1.AddString("111aa");

m_combox1.AddString("111bb");

//2

m_combox2.SetParent(&m_listctrl);

//确保CComboBox的坐标是相对于列表控件而言的。

m_combox2.AddString("222aa");

m_combox2.AddString("222bb");

//3 m_Edit.SetParent(&m_listctrl);//确保Edit控件的坐标是相对于列表控件而言的。

//4 列表控件要足够宽,以便容纳5列

//m_listctrl.SetHeadings(_T("序号,50;颜色说明,100;实际颜色,200"));

//标题

m_listctrl.InsertColumn(0,"item1",LVCFMT_CENTER,100);

m_listctrl.InsertColumn(1,"item2",LVCFMT_CENTER,100);

m_listctrl.InsertColumn(2,"item3",LVCFMT_CENTER,100);

m_listctrl.InsertColumn(3,"item4",LVCFMT_CENTER,100);

m_listctrl.InsertColumn(4,"item4",LVCFMT_CENTER,100);

m_listctrl.InsertItem(0,"sub11");

m_listctrl.SetItemText(0,1,"sub12");

m_listctrl.InsertItem(1,"sub21");

m_listctrl.SetItemText(1,3,"sub24");

//5 修改常规风格,或者从控件的属性中

LONG lStyle;

lStyle

=GetWindowLong(m_listctrl.m_hWnd,GWL_STYLE);

// lStyle &= ~LVS_TYPEMASK;//清除显示方式位

lStyle |= LVS_REPORT;//设置为报告风格

SetWindowLong(m_listctrl.m_hWnd,GWL_STYLE,lStyle);

//6 设置扩展风格,整行选择,网格线,前面有检查框

//

m_listctrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_CHECKBOXES);

DWORD dwStyle

=m_listctrl.GetExtendedStyle();

dwStyle |=

LVS_EX_FULLROWSELECT;//选中某行使整行高亮(只适用与report风格的listctrl)

dwStyle |=

LVS_EX_GRIDLINES;//网格线(只适用与report风格的listctrl)

dwStyle |=

LVS_EX_CHECKBOXES;//item前生成checkbox控件

m_listctrl.SetExtendedStyle(dwStyle);

//设置扩展

return TRUE; // return

TRUE unless you set the focus to a control

}

三。消息对应的相关函数//1.

组合框1失去焦点的函数

void CListCtrlComboxDlg::OnKillfocusCombo1()

{

// TODO: Add your control notification handler

code here

POINT pt;

GetCursorPos(&pt);

m_listctrl.ScreenToClient(&pt);

CRect rect;

m_listctrl.GetSubItemRect(m_row,m_col,LVIR_BOUNDS,rect);

if(!rect.PtInRect(pt))//如果单击在一个节点文本区域内

{

CString text;

m_combox1.GetWindowText(text);

m_listctrl.SetItemText(m_row,m_col,text);

m_combox1.ShowWindow(SW_HIDE);//将组合框隐藏

} }

//2. 组合框2失去焦点的函数

void CListCtrlComboxDlg::OnKillfocusCombo2()

{

// TODO: Add your control notification handler

code here

POINT pt;

GetCursorPos(&pt);

m_listctrl.ScreenToClient(&pt);

CRect rect;

m_listctrl.GetSubItemRect(m_row,m_col,LVIR_BOUNDS,rect);

if(!rect.PtInRect(pt))//如果单击在一个节点文本区域内

{

CString text;

m_combox2.GetWindowText(text);

m_listctrl.SetItemText(m_row,m_col,text);

m_combox2.ShowWindow(SW_HIDE);//将组合框隐藏

} }

//3. 编辑框失去焦点的函数

void CListCtrlComboxDlg::OnKillfocusEdit1()

{

POINT pt;

GetCursorPos(&pt);

m_listctrl.ScreenToClient(&pt);

CRect rect;

m_listctrl.GetSubItemRect(m_row,m_col,LVIR_BOUNDS,rect);

if(!rect.PtInRect(pt))//如果单击在一个节点文本区域内

{

CString text;

m_Edit.GetWindowText(text);

m_listctrl.SetItemText(m_row,m_col,text);

m_Edit.ShowWindow(SW_HIDE);//将编辑框隐藏 }

}

//4. 单击列表控件的函数

void CListCtrlComboxDlg::OnClickList1(NMHDR* pNMHDR, LRESULT*

pResult)

{

//因博客的限制,使用时,应将泛型参数<LPNMITEMACTIVATE>,改回英文的左右尖括号

LPNMITEMACTIVATE pNMItemActivate =

reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);

// TODO: Add your control notification handler

code here

POINT PT;

GetCursorPos(&PT);

m_listctrl.ScreenToClient(&PT);

LVHITTESTINFO hitInfo;

hitInfo.pt=PT;

//hitem=m_Tree.GetSelectedItem();

m_listctrl.SubItemHitTest(&hitInfo);

if(hitInfo.flags &

LVHT_ONITEMLABEL)//判断是否单击在文本上

{

CRect rect;

m_listctrl.GetSubItemRect(hitInfo.iItem,hitInfo.iSubItem,LVIR_BOUNDS,rect);

if (hitInfo.iSubItem==0)

{

rect.right=rect.left+m_listctrl.GetColumnWidth(0);//每列宽度不相同的,请写在每列中

}

else if(hitInfo.iSubItem==1 ||

hitInfo.iSubItem==2)//对列进行判断在这里1,2为编辑框edit,3,4为组合框combo box

{

CString

mes=m_listctrl.GetItemText(hitInfo.iItem,hitInfo.iSubItem);

m_col=hitInfo.iSubItem;

m_row=hitInfo.iItem;

m_Edit.MoveWindow(&rect,TRUE);

m_Edit.ShowWindow(SW_NORMAL);

m_Edit.SetWindowText(mes);

m_Edit.BringWindowToTop();

m_Edit.SetFocus();//使编辑框聚焦

}

else if(hitInfo.iSubItem==3)

{

CString

mes=m_listctrl.GetItemText(hitInfo.iItem,hitInfo.iSubItem);

//

rect.InflateRect(0,0,0,0);//增大组合框的高度使其可以容纳整行文本。

m_col=hitInfo.iSubItem;

m_row=hitInfo.iItem;

m_combox1.MoveWindow(&rect,TRUE);

m_combox1.ShowWindow(SW_NORMAL);

m_combox1.SetWindowText(mes);

m_combox1.BringWindowToTop();

m_combox1.SetFocus();//使组合框聚焦

}

else if(hitInfo.iSubItem==4 )

{

CString

mes=m_listctrl.GetItemText(hitInfo.iItem,hitInfo.iSubItem);

m_col=hitInfo.iSubItem;

m_row=hitInfo.iItem;

m_combox2.MoveWindow(&rect,TRUE);

m_combox2.ShowWindow(SW_NORMAL);

m_combox2.SetWindowText(mes);

m_combox2.BringWindowToTop();

m_combox2.SetFocus();//使组合框聚焦

}

} *pResult = 0;

}

//5. 按回车键时的功能,可以不要

BOOL CListCtrlComboxDlg::PreTranslateMessage(MSG* pMsg)

{

if(pMsg->wParam==VK_RETURN)

{

//int i,j;

CString text;

if (m_col==3)

{

m_combox1.GetWindowText(text);

m_listctrl.SetItemText(m_row,m_col,text);

m_combox1.ShowWindow(SW_HIDE);//隐藏组合框

}

if (m_col==4)

{

m_combox2.GetWindowText(text);

m_listctrl.SetItemText(m_row,m_col,text);

m_combox2.ShowWindow(SW_HIDE);//隐藏组合框

}

return TRUE;//阻止了父类对消息的处理,使得按下回车键时,不会关闭对话框

} return CDialog::PreTranslateMessage(pMsg);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值