(声明:这个代码记不得是从哪里copy过来的,很通用。)
代码:
void CNetstateAqui::OnCustomDraw(NMHDR *pnotify, LRESULT *result)
{
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pnotify;
if(lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
// Request prepaint notifications for each item.
*result = CDRF_NOTIFYITEMDRAW;
if(lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
{
// Below giving different colors to alternate items.
if(!(lplvcd->nmcd.dwItemSpec%2))
{
lplvcd->clrText = RGB(255,0,0);
lplvcd->clrTextBk = RGB(255,255,0);
}
else
{
lplvcd->clrText = RGB(255, 255 , 0);
lplvcd->clrTextBk = RGB(100,100,100);
}
*result = CDRF_DODEFAULT;
}
} //功能实现列表框单双行不同色
除了这个函数,还需要手动添加消息映射:ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST, OnCustomDraw)
并在头文件中声明:afx_msg void OnCustomDraw(NMHDR*, LRESULT*);
3 窗口分割与布局操作实例
操作步骤:
1、在MainFrame.h中添加定义:
函数:
protected:
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);//在AFX_VIRTUAL之间。
变量:
public:
CSplitterWnd m_cDataView;
CSplitterWnd m_cEventView;
2、在MainFrame.cpp中添加函数:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
if (!m_cEventView.CreateStatic (this, 2, 1))
{
TRACE (_T("OTC: CKMainWnd::OnCreateClient () -> CreateStatic () failed/n"));
return (FALSE);
}
if (!m_cDataView.CreateStatic (&m_cEventView, 1, 2, WS_CHILD |WS_VISIBLE,m_cEventView.IdFromRowCol(0, 0)))
{
TRACE (_T("OTC: CKMainWnd::OnCreateClient () -> CreateStatic () failed/n"));
return (FALSE);
}
RECT rc;
GetClientRect (&rc);
// Create "group view" in top left pane:
if (!m_cDataView.CreateView (
0, 0, RUNTIME_CLASS (CGroupView), CSize (180, 3 * rc.bottom / 4), pContext) )
{
ASSERT (FALSE);
return (FALSE);
}
// Create "item view" in top right pane:
if (!m_cDataView.CreateView (
0, 1,RUNTIME_CLASS (COpcItemView),CSize ( 0, rc.bottom / 4), pContext) )
{
ASSERT (FALSE);
return (FALSE);
}
// Create "event view" in bottom pane:
if (!m_cEventView.CreateView (
1, 0,RUNTIME_CLASS (CEventView),CSize (rc.right, 2 * rc.bottom / 5), pContext) )
{
ASSERT (FALSE);
return (FALSE);
}
m_cEventView.SetRowInfo(0, 5 * rc.bottom / 8, 0);
m_cEventView.RecalcLayout();
return(TRUE);
}
说明:这段函数代码将窗口先划分成左右两个,再将右边的分成上下两个。左边的窗口是CGroupView它是一个CTreeView的继承类。右上是一个继承于CListView的类COpcItemView,右下是CEventView类,它是继承一个基于对话框的类。