subclass那个GetHeaderCtrl的Hwnd,然后自己画。

解决方案 »

  1.   

    我这里有一个简单的代码可以实现CListCtrl头部CHeaderCtrl的自绘制.
    class CListCtrlEx : public CListCtrl
    {
    DECLARE_DYNAMIC(CListCtrlEx)
    // Construction
    public:
    CListCtrlEx();// Attributes
    public:// Operations
    public:
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CListCtrlEx)
    protected:
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CListCtrlEx(); // Generated message map functions
    protected:
    void DrawListCtrl(LPDRAWITEMSTRUCT lpDIS);
    void DrawHeader(LPDRAWITEMSTRUCT lpDIS);
    //{{AFX_MSG(CListCtrlEx)
    afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };
    IMPLEMENT_DYNAMIC(CListCtrlEx,CListCtrl)
    CListCtrlEx::CListCtrlEx()
    {
    }CListCtrlEx::~CListCtrlEx()
    {
    }
    BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
    //{{AFX_MSG_MAP(CListCtrlEx)
    ON_WM_DRAWITEM()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CListCtrlEx message handlersvoid CListCtrlEx::DrawHeader(LPDRAWITEMSTRUCT lpDIS)
    {
    CDC* pDC = CDC::FromHandle(lpDIS->hDC);
    CRect rect(lpDIS->rcItem); CHeaderCtrl* pCtrl = GetHeaderCtrl();
    ASSERT(pCtrl);
    ASSERT_VALID(pCtrl); int nItem = (int)lpDIS->itemID;
    pDC->SetBkColor(RGB(255,0,0));
    HD_ITEM item;
    ZeroMemory(&item,sizeof(item));
    item.mask = HDI_FORMAT | HDI_HEIGHT | HDI_TEXT;
    TCHAR  lpBuffer[256];
    item.pszText = lpBuffer;
    item.cchTextMax = 256; VERIFY(pCtrl->GetItem(nItem,&item)); CString str(lpBuffer);

    UINT nFormat = DT_SINGLELINE | DT_VCENTER;
    if(item.fmt & HDF_CENTER)
    nFormat |= DT_CENTER;
    else if(item.fmt & HDF_LEFT)
    nFormat |= DT_LEFT;
    else if(item.fmt & HDF_RIGHT)
    nFormat |= DT_RIGHT; pDC->DrawText(str,&rect, nFormat);}void CListCtrlEx::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS) 
    {
    // TODO: Add your message handler code here and/or call default
    // 绘制标题
    if(lpDIS->CtlType == ODT_HEADER)
    {
    DrawHeader(lpDIS);
    }

    CListCtrl::OnDrawItem(nIDCtl, lpDIS);
    }
    需要指出的是如果你为CListCtrl设置了LVS_TYPEMASK似乎就不起作用了.相反,你的CListCtrl本身如果是自绘制的时候,此时你的CListCtrl中的内容会显示出来,而标题控件则消失了.
      

  2.   

    就上面兩位的答案﹐似乎只有利用subclass來實現header control自繪才可以。
    謝謝各位的幫忙﹐ 待我在codeguru上找一找﹐ 再來給各位加分。
      

  3.   

    在codeguru上走了一圈﹐ 結果還是和smallfool(smallfool)說的差不多。 雖然沒有找到直接的方法﹐但在codeguru上也得益不少。 如果smallfool(smallfool)不介意的﹐我想我會引用你的code。 Thanks All..
      

  4.   

    oh...god, I forget my password.
    but don't worry, I will give the points right after I get my password.
    Anyway, it will be finished in a few days.and anyone knows how to get the password from the cookie?
    it will help me to give the points faster..thanks.
      

  5.   

    謝謝你smallfool(smallfool)﹐對于列表头自画方面﹐畫出來的結果並不如想象中那麼好﹐文字歪了可以慢慢調教﹐但對于如果列表头是有3D look的話那就無法做到了﹐旁邊的那些border是無法自己畫出來的。 對這個老兄是否其他的見解了﹖