在状态栏创建合适的列表框
这段代码可以在状态行中的任意地方建立一个列表框
1.从view菜单中选择resource symbols,加入一个新的id(在本例中假设为
idc_list),让计算机为该id赋值也许是较好的选择.
2. 在mainfrm.cpp中寻找indicators数组,然后在该数组中加入上面新增加的id,
请将它放在其它id的后面,这将使以后的列表框出现在状态行的最右边,如果你
不想 让列表框出现在最右边,你也可以将id放置在其它你想出现的地方
.在本例子中如下:
static uint indicators[] =
{
id_separator, // status line indicator
idc_list file://自己定义的那段区域,本例子中用于存放listctrl控件
};
3. 打开资源文件中的字符串表,然后插入一个新字符串.你可以通过insert菜单
来加入一个新字符表,或者在字符串表上单击右键也可以完成该工作.
本例子中为空字符。
4. 该字符串的字符为新增的id,后面的消息,请加入适当的空格.(加入的空格
至少要比建立的列表框要大)
现在我们已经建立了一个空的长方形,接下来我们要在这里面放入一个列表框.
1. 在mainfrm.h中声明一个公共变量clistctrl m_list;
2. 在mainfrm.h中声明一个保护变量 bool m_blistcreate;
在
3. 在mainfrm.cpp中置
cmainframe::cmainframe()
{
m_blistcreate=false;
}
4.在mainfrm.h声明 void setlist(void); 函数,该函数用于初始化列表框
在mainfrm.cpp中实现setlist()函数,具体如下:
void cmainframe::setlist(void)
{
file://create the listview
rect myrect;
// substitute 4 with the zero-based index of your status bar pane.
// for example, if you put your pane first in the indicators array,
// you抎 put 0, second you抎 put 1, etc.
m_wndstatusbar.getitemrect(1, &myrect);
if(m_blistcreate==false)
{
lv_column lvc;
lvc.mask = lvcf_text | lvcf_subitem | lvcf_width |lvcf_fmt;
lvc.fmt=lvcfmt_center;
afxmessagebox("ok");
m_list.create(ws_child|ws_visible|lvs_alignleft|lvs_report|ws_border ,myrect, &m_wndstatusbar, 1);
m_list.setbkcolor(::getsyscolor(color_infobk));
m_list.settextbkcolor(::getsyscolor(color_3dface));
lvc.isubitem = 0;
lvc.psztext = _t("am 点到");
lvc.cx = 60;
if(m_list.insertcolumn(1,&lvc)==0)
afxmessagebox("false");
lvc.isubitem = 1;
lvc.psztext = _t("午饭");
lvc.cx = 60;
m_list.insertcolumn(2,&lvc);
file://afxmessagebox("ok");
m_blistcreate=true;
}
else
afxmessagebox("has been created!");
}
这个函数可以根据自己的需要来实现。
5. 在本例子里列表框在程序初始化的同时也被初始化,所以将列表框的初始代码
放在主框架初始生成的oncreate()函数中。具体如下:
int cmainframe::oncreate(lpcreatestruct lpcreatestruct)
{
if (cframewnd::oncreate(lpcreatestruct) == -1)
return -1;
if (!m_wndtoolbar.createex(this, tbstyle_flat, ws_child | ws_visible | cbrs_top
| cbrs_gripper | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic) ||
!m_wndtoolbar.loadtoolbar(idr_mainframe))
{
trace0("failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndstatusbar.createex(this,sbars_sizegrip) ||
!m_wndstatusbar.setindicators(indicators,
sizeof(indicators)/sizeof(uint)))
{
trace0("failed to create status bar\n");
return -1; // fail to create
}
m_wndstatusbar.getstatusbarctrl().setminheight(40);//该语句用于设置状态栏的高度。
setlist();//用于列表框的初始化,将列表框放置在状态栏的合适位置。
// todo: delete these three lines if you don't want the toolbar to
// be dockable
m_wndtoolbar.enabledocking(cbrs_align_any);
enabledocking(cbrs_align_any);
dockcontrolbar(&m_wndtoolbar);
return 0;
}
6。如果窗口在建立列表框之后,改变了大小,那么列表框并不会被重新调整位置,所
以 我们必须在wm_size事件中加入自己的代码来调整该列表框的位置:
void cmainframe::onsize(uint ntype, int cx, int cy)
{
cframewnd::onsize(ntype, cx, cy);
if(m_blistcreate==true)
{
rect rc;
m_wndstatusbar.getitemrect(1, &rc);
// reposition the progress control correctly!
m_list.setwindowpos(&wndtop, rc.left, rc.top, rc.right - rc.left,rc.bottom - rc.top, 0);
}
}