您的位置:首页 > 编程语言 > C语言/C++

VC++ MFC类学习的一些问题和技巧

2014-11-04 10:09 302 查看
1 组合框的字符串显示顺序问题

     组合框不论addstring()的顺序如何,显示的时候都是按照string的字母顺序的。例如添加的value是1到31的数字,代表日期,而显示的时候会变成1、12、13...2、21、22...这样的形式排列,而我所希望是的安照1、2、3...顺序排列的,那我怎么改变这种默认情况呢?

      解决方法:去掉它的sort属性

 

2 列表框各行显示不同颜色操作实例

(声明:这个代码记不得是从哪里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类,它是继承一个基于对话框的类。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: