Win32自定义绘制树视图控件 [英] Win32 Custom Draw Treeview Control

查看:129
本文介绍了Win32自定义绘制树视图控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NM_CUSTOMDRAW消息来自定义树视图控件.我只想用灰色绘制所有其他可见项目.这是绘制的代码:

I'm trying to customize a treeview control using NM_CUSTOMDRAW messages. I simply want to draw every other visible item with a grey color. Here is code to draw:

INT CResourceOutliner::On_WM_NOTIFY( HWND hDlg, WPARAM wParam, LPARAM lParam )
{
    HWND hTree = GetDlgItem( hDlg, IDC_TREE1 ); 
    switch( ( ( LPNMHDR )lParam )->code )
    {
    ...
    case NM_CUSTOMDRAW:
            {           
                LPNMTVCUSTOMDRAW pCustomDraw = ( LPNMTVCUSTOMDRAW )lParam;
                switch( pCustomDraw->nmcd.dwDrawStage )
                {               
                case CDDS_PREPAINT:
                    return CDRF_NOTIFYITEMDRAW;
                case CDDS_ITEMPREPAINT:
                    {
                        switch ( pCustomDraw->iLevel )                  
                        {
                            // painting all 0-level items blue,                     
                            // and all 1-level items red (GGH)25.                       
                        case 0:
                            {
                            if( pCustomDraw->nmcd.uItemState == ( CDIS_FOCUS | CDIS_SELECTED ) )                            
                                pCustomDraw->clrTextBk = RGB( 255, 255, 255 );                          
                            else                                    
                                pCustomDraw->clrTextBk = RGB( 128, 128, 128 );                          
                                break;
                            }
                        case 1:                         
                            {
                                if( pCustomDraw->nmcd.uItemState == ( CDIS_FOCUS | CDIS_SELECTED ) )                            
                                    pCustomDraw->clrTextBk = RGB( 255, 255, 255 );                          
                                else    
                                    pCustomDraw->clrTextBk = RGB( 128, 128, 128 );
                                break;
                            }
                        default:
                            break;
                        }
                        return CDRF_SKIPDEFAULT;
                    }
                default:
                    break;
                }
            }
    ...
    }
}

此代码来自问题在于,在CDDS_PREPAINT通知消息上返回CDRF_NOTIFYITEMDRAW之后,CDDS_ITEMPREPAINT消息再也不会出现...是否有设置选项以启用自定义绘图.我想不是因为CDDS_PREPAINT消息是由控件发送的...

The problem is that after returning CDRF_NOTIFYITEMDRAW on CDDS_PREPAINT notification message, the CDDS_ITEMPREPAINT message never comes...Is there an option to set in order to enable custom drawing..? I imagine that there isn't because the CDDS_PREPAINT message is sent by the control...

...上面的代码也不是要绘制其他所有项目...仅是来自codeguru.com的演示

...also the code above is not meant to draw every other item...its just a demo from codeguru.com

这是消息处理实现...

here is message handling implementation...

int CResourceOutliner::DoModal( int resID, RECT rct, HWND hParent )
{   
    // Set properties
    m_dwpSaveThis = ( DWORD_PTR )this; /// store this pointer
    m_nResId = resID;
    m_hParent = hParent;

    m_hWindow = CreateDialog( GetModuleHandle( NULL ), MAKEINTRESOURCE( m_nResId ), m_hParent, ( DLGPROC )MsgProcStatic );

    // Set window position
    SetWindowPos( m_hWindow, 0, rct.left, rct.top, rct.right, rct.bottom, 0 );

    ShowWindow( m_hWindow, SW_HIDE );

    if( m_hWindow )
        return 1;

    return 0; 
}
INT CALLBACK CResourceOutliner::MsgProcStatic( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    if( !m_hWindow )
        m_hWindow = hWnd;

    CResourceOutliner *pDlg = ( CResourceOutliner* )m_dwpSaveThis;
    if( pDlg )
        return pDlg->MsgProc( hWnd, uMsg, wParam, lParam );
    else
        return 0;
}
INT CALLBACK CResourceOutliner::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    switch( uMsg )
    {
    case WM_INITDIALOG:
        On_WM_INITDIALOG( hWnd, wParam, lParam );
        break;
    case WM_COMMAND:
        On_WM_COMMAND( hWnd, wParam, lParam );
        break;
    case WM_NOTIFY:
        {           
            return On_WM_NOTIFY( hWnd, wParam, lParam );
        }
    case WM_LBUTTONDOWN:
        On_WM_LBUTTONDOWN( hWnd, wParam, lParam );
        break;
    case WM_LBUTTONUP:
        On_WM_LBUTTONUP( hWnd, wParam, lParam );
        break;
    case WM_MOUSEMOVE:
        On_WM_MOUSEMOVE( hWnd, wParam, lParam );
        break;
    case WM_PAINT:
        On_WM_PAINT( hWnd, wParam, lParam );
        break;
    case WM_CLOSE:
        On_WM_CLOSE( hWnd, wParam, lParam );
        break;
    default:
        return 0;
    }
    return 0;
}

推荐答案

对话过程中的大多数返回码都需要通过 DWLP_MSGRESULT 进行设置,例如:

Most return codes from the dialog procedure need to be set via DWLP_MSGRESULT, for example:

SetWindowLongPtr(hWnd, DWLP_MSGRESULT, CDRF_NOTIFYITEMDRAW);

该规则几乎没有例外( WM_CTLCOLORSTATIC 是直接返回的一个示例)作为

There are very few exceptions to this rule (WM_CTLCOLORSTATIC is one example that returns directly) as a dialog procedure is generally defined as returning TRUE or FALSE depending on whether the message was handled or not.

这篇关于Win32自定义绘制树视图控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆