CListCtrl的一些用法问答(FQA)

来源:互联网 发布:windows xp停止支持 编辑:程序博客网 时间:2024/06/10 12:34

Q:怎样将 CListCtrl 中的一行( one item)的背景或 Text Color 设为其它 color ?
A:
Using Custom Draw
The following code fragment is a portion of a WM_NOTIFY handler that illustrates how to handle custom draw notifications sent to a list-view control:

Hide Example

  
LPNMLISTVIEW  pnm    = (LPNMLISTVIEW)lParam;

switch (pnm->hdr.code){
...
case NM_CUSTOMDRAW:

    LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)lParam;

    switch(lplvcd->nmcd.dwDrawStage) {

    case CDDS_PREPAINT :
        return CDRF_NOTIFYITEMDRAW;

    case CDDS_ITEMPREPAINT:
        SelectObject(lplvcd->nmcd.hdc,
                     GetFontForItem(lplvcd->nmcd.dwItemSpec,
                                    lplvcd->nmcd.lItemlParam) );
        lplvcd->clrText = GetColorForItem(lplvcd->nmcd.dwItemSpec,
                                          lplvcd->nmcd.lItemlParam);
        lplvcd->clrTextBk = GetBkColorForItem(lplvcd->nmcd.dwItemSpec,
                                              lplvcd->nmcd.lItemlParam);

/* At this point, you can change the background colors for the item
and any subitems and return CDRF_NEWFONT. If the list-view control
is in report mode, you can simply return CDRF_NOTIFYSUBITEMREDRAW
to customize the item's subitems individually */
        ...

        return CDRF_NEWFONT;
//  or return CDRF_NOTIFYSUBITEMREDRAW;

    case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
        SelectObject(lplvcd->nmcd.hdc,
                     GetFontForSubItem(lplvcd->nmcd.dwItemSpec,
                                       lplvcd->nmcd.lItemlParam,
                                       lplvcd->iSubItem));
        lplvcd->clrText = GetColorForSubItem(lplvcd->nmcd.dwItemSpec,
                                             lplvcd->nmcd.lItemlParam,
                                             lplvcd->iSubItem));
        lplvcd->clrTextBk = GetBkColorForSubItem(lplvcd->nmcd.dwItemSpec,
                                                 lplvcd->nmcd.lItemlParam,
                                                 lplvcd->iSubItem));

/* This notification is received only if you are in report mode and
returned CDRF_NOTIFYSUBITEMREDRAW in the previous step. At
this point, you can change the background colors for the
subitem and return CDRF_NEWFONT.*/
        ...
        return CDRF_NEWFONT;    
    }
...
}

Q:请问怎样在clistctrl里显示jpeg图片的缩略图呢
A:BEGIN_MESSAGE_MAP(CImagePage, CPropertyPage)
//{{AFX_MSG_MAP(CImagePage)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_PREVIEW, OnCustomDraw)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CImagePage::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
LPNMLVCUSTOMDRAW pnmCustDraw = (LPNMLVCUSTOMDRAW)pNMHDR;
switch(pnmCustDraw->nmcd.dwDrawStage){
case CDDS_PREPAINT:
  *pResult = CDRF_NOTIFYITEMDRAW ;break;
case CDDS_ITEMPREPAINT:
  *pResult = CDRF_NOTIFYPOSTPAINT ;break;
case CDDS_ITEMPOSTPAINT:{
  int iItem=pnmCustDraw->nmcd.dwItemSpec;
  CDC dc;
  dc.Attach(pnmCustDraw->nmcd.hdc);
  HICON hi=GetIconFromFile(m_strImageFile,iItem);
  m_pic.CreateFromIcon(hi);DestroyIcon(hi);
  CRect rectDest;
//call CListCtrl::GetItemRect to get target rect
  m_wndImgPrvwList.GetItemRect(iItem,rectDest,LVIR_ICON);
  dc.DPtoLP(rectDest);
  m_pic.Render(&dc,&rectDest);
  dc.Detach();
  *pResult = CDRF_DODEFAULT;
  break;
  }
default:
  *pResult = CDRF_DODEFAULT;
  break;
}
}
显示图标的……改改就行……

Q:哪位大侠知道:在程序中如何改变CListCtrl或其它控件的滚动条的颜色(前景、背景)?
A:按理来说在窗口过程中处理WM_CTLCOLORSCROLLBAR就可以了……滚动条会往父窗口发这个消息……
Subclass滚动条,在派生类里面自画/处理WM_CTLCOLORSCROLLBAR也应该可以……
_AfxGrayBackgroundWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
// handle standard gray backgrounds if enabled
_AFX_WIN_STATE* pWinState = _afxWinState;
if (pWinState->m_hDlgBkBrush != NULL &&
  (nMsg == WM_CTLCOLORBTN || nMsg == WM_CTLCOLORDLG ||
   nMsg == WM_CTLCOLORSTATIC || nMsg == WM_CTLCOLORSCROLLBAR ||
   nMsg == WM_CTLCOLORLISTBOX) &&
  CWnd::GrayCtlColor((HDC)wParam, (HWND)lParam,
   (UINT)(nMsg - WM_CTLCOLORMSGBOX),
   pWinState->m_hDlgBkBrush, pWinState->m_crDlgTextClr))
{
  return (LRESULT)pWinState->m_hDlgBkBrush;
}

// do standard activation related things as well
return _AfxActivationWndProc(hWnd, nMsg, wParam, lParam);
}

Q:如何设置ListCtrl高亮(选中)时的颜色?
A:捕获NM_CUSTOMDRAW,在自画的时候判断ItemState
http://www.codeproject.com/listctrl/lvcustomdraw.asp

LRESULT DoNotify (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  LPNMLISTVIEW pnm = (LPNMLISTVIEW)lParam;

  switch (pnm->hdr.code)
  {
    case NM_CUSTOMDRAW:
    {
      LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)lParam;

      if (lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
        return CDRF_NOTIFYITEMDRAW;

      if (lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
      {
        if (!(lplvcd->nmcd.dwItemSpec % 3))
          SelectObject (lplvcd->nmcd.hdc, g_hNewFont);
        else
          return CDRF_DODEFAULT;

        lplvcd->clrText = RGB(150, 75, 150);
        lplvcd->clrTextBk = RGB(255,255,255);

        return CDRF_NEWFONT;
      }
    }

    default:
      break;
  }

  return 0;
}

Q:怎么做不同行用不同文字颜色的clistctrl(REPORT型),请赐教!
A:Custom Draw With List View and Tree View Controls
Most common controls can be handled in essentially the same way. However, the list view and tree view controls have some features that require a somewhat different approach to custom draw.

For Version 5.0 of the common controls, these two controls may display clipped text if you change the font by returning CDRF_NEWFONT. This behavior is necessary for backward compatibility with earlier versions of the common controls. If you want to change the font of a list view or tree view control, you will get better results if you send a CCM_SETVERSION message with the wParam value set to 5 before adding any items to the control.

Custom Draw With List View Controls
Because list view controls have subitems and multiple display modes, you will need to handle the NM_CUSTOMDRAW notification somewhat differently than for the other common controls.

For report mode:

The first NM_CUSTOMDRAW notification will have the dwDrawStage member of the associated NMCUSTOMDRAW structure set to CDDS_PREPAINT. Return CDRF_NOTIFYITEMDRAW.
You will then receive an NM_CUSTOMDRAW notification with dwDrawStage set to CDDS_ITEMPREPAINT. If you specify new fonts or colors and return CDRF_NEWFONT, all subitems of the item will be changed. If you want instead to handle each subitem separately, return CDRF_NOTIFYSUBITEMDRAW.
If you returned CDRF_NOTIFYITEMDRAW in the previous step, you will then receive an NM_CUSTOMDRAW notification for each subitem with dwDrawStage set to CDDS_SUBITEM | CDDS_PREPAINT. To change the font or color for that subitem, specify a new font or color and return CDRF_NEWFONT.
For the large icon, small icon, and list modes:

The first NM_CUSTOMDRAW notification will have the dwDrawStage member of the associated NMCUSTOMDRAW structure set to CDDS_PREPAINT. Return CDRF_NOTIFYITEMDRAW.
You will then receive an NM_CUSTOMDRAW notification with dwDrawStage set to CDDS_ITEMPREPAINT. You can change the fonts or colors of an item by specifying new fonts and colors and returning CDRF_NEWFONT. Because these modes do not have subitems, you will not receive any additional NM_CUSTOMDRAW notifications.
An example of a list view NM_CUSTOMDRAW notification handler is given in the next section.

Using Custom Draw
The following code fragment is a portion of a WM_NOTIFY handler that illustrates how to handle custom draw notifications sent to a list view control:

LPNMLISTVIEW  pnm    = (LPNMLISTVIEW)lParam;

switch (pnm->hdr.code){
...
case NM_CUSTOMDRAW:

    LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)lParam;

    switch(lplvcd->nmcd.dwDrawStage) {
    case CDDS_PREPAINT :
        return CDRF_NOTIFYITEMDRAW;
  
    case CDDS_ITEMPREPAINT:
        SelectObject(lplvcd->nmcd.hdc,
                     GetFontForItem(lplvcd->nmcd.dwItemSpec,
                                    lplvcd->nmcd.lItemlParam) );
        lplvcd->clrText = GetColorForItem(lplvcd->nmcd.dwItemSpec,
                                          lplvcd->nmcd.lItemlParam);
        lplvcd->clrTextBk = GetBkColorForItem(lplvcd->nmcd.dwItemSpec,
                                              lplvcd->nmcd.lItemlParam);


/* At this point, you can change the background colors for the item
and any subitems and return CDRF_NEWFONT. If the list view control
is in report mode, you can simply return CDRF_NOTIFYSUBITEMREDRAW
to customize the item's subitems individually */

        ...
        return CDRF_NEWFONT;
//  or return CDRF_NOTIFYSUBITEMREDRAW;

    case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
        SelectObject(lplvcd->nmcd.hdc,
                     GetFontForSubItem(lplvcd->nmcd.dwItemSpec,
                                       lplvcd->nmcd.lItemlParam,
                                       lplvcd->iSubItem));
        lplvcd->clrText = GetColorForSubItem(lplvcd->nmcd.dwItemSpec,
                                             lplvcd->nmcd.lItemlParam,
                                             lplvcd->iSubItem));
        lplvcd->clrTextBk = GetBkColorForSubItem(lplvcd->nmcd.dwItemSpec,
                                                 lplvcd->nmcd.lItemlParam,
                                                 lplvcd->iSubItem));

/* This notification is received only if you are in report mode and
returned CDRF_NOTIFYSUBITEMREDRAW in the previous step. At
this point, you can change the background colors for the
subitem and return CDRF_NEWFONT.*/

        ...
        return CDRF_NEWFONT;    
    }
...
}

The first NM_CUSTOMDRAW notification has the dwDrawStage member of the NMCUSTOMDRAW structure set to CDDS_PREPAINT. The handler returns CDRF_NOTIFYITEMDRAW to indicate that it wishes to modify one or more items individually. The control then sends an NM_CUSTOMDRAW notification with dwDrawStage set to CDDS_PREPAINT for each item. The handler returns CDRF_NOTIFYITEMDRAW to indicate that it wishes to modify the item.

If CDRF_NOTIFYITEMDRAW was returned in the previous step, the next NM_CUSTOMDRAW notification has dwDrawStage set to CDDS_ITEMPREPAINT. The handler gets the current color and font values. At this point, you can specify new values for small icon, large icon, and list modes. If the control is in report mode, you can also specify new values that will apply to all subitems of the item. If you have changed anything, return CDRF_NEWFONT. If the control is in report mode and you want to handle the subitems individually, return CDRF_NOTIFYSUBITEMREDRAW.

The final notification is only sent if the control is in report mode and you returned CDRF_NOTIFYSUBITEMREDRAW in the previous step. The procedure for changing fonts and colors is the same as that step, but it only applies to a single subitem. Return CDRF_NEWFONT to notify the control if the color or font was changed.