代码改变世界

WTL学习笔记(4)控件加强

2011-08-10 20:39  Clingingboy  阅读(2656)  评论(0编辑  收藏  举报

 

1.CHyperLink

扩展了Static Text控件

URL超链接

2.CCheckListViewCtrlImpl

一个以checkbox为元素的ListView控件

3.CWaitCursor && CCustomWaitCursor

构造函数中载入等待的图标,析构函数中还原,省去了手动还原的步骤,CCustomWaitCursor继承CWaitCursor,允许采用自定义的图标

4.CSortListViewCtrl

提供SortItems方法对列进行排序

5.关于OwnerDraw和CustomDraw

OwnerDraw将会重绘控件整体UI呈现,CustomDraw只不过更改控件其中的一个属性而已.

好比WPF重写Render方法就是OwnerDraw,更改一个属性则是CustomDraw

6.CBitmapButton

在一个位图中加载多个按钮资源状态,比normal,pushed,hover,disable

CImageList iml;
    iml.CreateFromImage ( IDB_ALYSON_IMGLIST, 81, 1, CLR_NONE, IMAGE_BITMAP, 
                          LR_CREATEDIBSECTION );
    m_wndBmpBtn.SubclassWindow ( GetDlgItem(IDC_ALYSON_BMPBTN) );
    m_wndBmpBtn.SetToolTipText ( _T("Alyson") );
    m_wndBmpBtn.SetImageList ( iml );
    
    m_wndBmpBtn.SetImages ( 0, 1, 2, 3 );

7.ReBar

ReBar:可以说的ToolBar的加强版,可以更改布局

image

image

ToolBar:

image

使用方法

// create command bar window
HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
// attach menu
m_CmdBar.AttachMenu(GetMenu());
// load command bar images
m_CmdBar.LoadImages(IDR_MAINFRAME);
// remove old menu
SetMenu(NULL);

HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);

CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
AddSimpleReBarBand(hWndCmdBar);
AddSimpleReBarBand(hWndToolBar, NULL, TRUE);

CreateSimpleStatusBar();

m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);

UIAddToolBar(hWndToolBar);
UISetCheck(ID_VIEW_TOOLBAR, 1);
UISetCheck(ID_VIEW_STATUS_BAR, 1);

(1)使用ReBar之前必须先调用CreateSimpleReBar方法,然后再调用AddSimpleReBarBand方法

HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);
CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
AddSimpleReBarBand(hWndToolBar, NULL, TRUE);

(2)对Menu添加ReBar功能,先将Menu添加到CCommandBarCtrl里面,然后调用AddSimpleReBarBand方法

// create command bar window
HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
// attach menu
m_CmdBar.AttachMenu(GetMenu());
// load command bar images
m_CmdBar.LoadImages(IDR_MAINFRAME);
// remove old menu
SetMenu(NULL);

8.GDI双缓冲机制

WTL提供了CDoubleBufferImpl双缓冲实现,及默认的CDoubleBufferWindowImpl,原理是利用CMemoryDC创建了一个bitmap,继承CDoubleBufferImpl的类只需要重写DoPaint方法即可

CMemoryDC的实现

class CMemoryDC : public CDC
{
public:
// Data members
    HDC m_hDCOriginal;
    RECT m_rcPaint;
    CBitmap m_bmp;
    HBITMAP m_hBmpOld;

// Constructor/destructor
    CMemoryDC(HDC hDC, RECT& rcPaint) : m_hDCOriginal(hDC), m_hBmpOld(NULL)
    {
        m_rcPaint = rcPaint;
        CreateCompatibleDC(m_hDCOriginal);
        ATLASSERT(m_hDC != NULL);
        m_bmp.CreateCompatibleBitmap(m_hDCOriginal, m_rcPaint.right - m_rcPaint.left, m_rcPaint.bottom - m_rcPaint.top);
        ATLASSERT(m_bmp.m_hBitmap != NULL);
        m_hBmpOld = SelectBitmap(m_bmp);
        SetViewportOrg(-m_rcPaint.left, -m_rcPaint.top);
    }

    ~CMemoryDC()
    {
        ::BitBlt(m_hDCOriginal, m_rcPaint.left, m_rcPaint.top, m_rcPaint.right - m_rcPaint.left, m_rcPaint.bottom - m_rcPaint.top, m_hDC, m_rcPaint.left, m_rcPaint.top, SRCCOPY);
        SelectBitmap(m_hBmpOld);
    }
};
参考:http://blog.csdn.net/blz_wowar/article/details/2046886
http://msdn2.microsoft.com/zh-cn/library/ms364048(VS.80).aspx
http://www.codeproject.com/KB/wtl/wtl4mfc5.aspx