代码改变世界

金山卫士UI原理解析(2)CBkWindow

2011-08-15 22:05  Clingingboy  阅读(1580)  评论(0编辑  收藏  举报

 

一.CBkWindow

CBkWindow相当于传统win32的窗体类,但并非每个窗体有一句柄,而是把把一个HWND当作一个容器

同时其还包含一个BkWnds,BkWnds是一个单例类,其包含多个CBkWindow

CBkWindow继承自CBkObject

1.共有属性

BKWIN_DECLARE_ATTRIBUTES_BEGIN()
    BKWIN_STYLE_ATTRIBUTE("class", m_style, TRUE)
    BKWIN_INT_ATTRIBUTE("id", m_uCmdID, FALSE)//, 0)
    BKWIN_COLOR_ATTRIBUTE("crbg", m_style.m_crBg, FALSE)
    BKWIN_COLOR_ATTRIBUTE("crtext", m_style.m_crText, FALSE)
    BKWIN_FONT_ATTRIBUTE("font", m_style.m_ftText, FALSE)
    BKWIN_TSTRING_ATTRIBUTE("href", m_strLinkUrl, FALSE)
    BKWIN_TSTRING_ATTRIBUTE("tip", m_strToolTipText, FALSE)
    BKWIN_ENUM_ATTRIBUTE("valign", UINT, TRUE)
        BKWIN_ENUM_VALUE("top", VAlign_Top)
        BKWIN_ENUM_VALUE("middle", VAlign_Middle)
        BKWIN_ENUM_VALUE("bottom", VAlign_Bottom)
    BKWIN_ENUM_END(m_uVAlign)
    BKWIN_ENUM_ATTRIBUTE("align", UINT, TRUE)
        BKWIN_ENUM_VALUE("left", Align_Left)
        BKWIN_ENUM_VALUE("center", Align_Center)
        BKWIN_ENUM_VALUE("right", Align_Right)
    BKWIN_ENUM_END(m_uHAlign)
    BKWIN_CUSTOM_ATTRIBUTE("pos", OnAttributePosChange)
    BKWIN_INT_ATTRIBUTE("transparent", m_bTransparent, FALSE)//, 0)
BKWIN_DECLARE_ATTRIBUTES_END()

2.必要的消息

为重绘和控件定位等做准备

BKWIN_BEGIN_MSG_MAP()
    MSG_WM_CREATE(OnCreate)
    MSG_WM_PAINT(OnPaint)
    MSG_WM_DESTROY(OnDestroy)
    MSG_WM_WINDOWPOSCHANGED(OnWindowPosChanged)
    MSG_WM_NCCALCSIZE(OnNcCalcSize)
    MSG_WM_SHOWWINDOW(OnShowWindow)
BKWIN_END_MSG_MAP_BASE()

3.Load方法

其会解析每个xml的节点,并初始化CBkWindow的数据

4.OnPaint

BeforePaint用于获取style的样式

AfterPaint用于恢复原先样式信息.

BkStyle类定义了样式信息

void BeforePaint(CDCHandle &dc, BkDCPaint &BkDC)
{
    HFONT /*hftOld = NULL, */hftDraw = NULL;
    COLORREF /*crOld = CLR_INVALID, crOldBg = CLR_INVALID, */crDraw = CLR_INVALID;
    /*int nOldBkMode = OPAQUE;*/

    BkDC.rcClient = m_rcWindow;
    BkDC.rcClient.DeflateRect(m_style.m_nMarginX, m_style.m_nMarginY);

    if (!m_style.m_strSkinName.IsEmpty())
    {
        BkDC.bBkModeChanged = TRUE;
        BkDC.nOldBkMode = dc.SetBkMode(TRANSPARENT);
    }
    if (CLR_INVALID != m_style.m_crBg)
    {
        BkDC.bBgColorChanged = TRUE;
        BkDC.crOldBg = dc.SetBkColor(m_style.m_crBg);
    }

    if (m_style.m_ftText)
        hftDraw = m_style.m_ftText;

    if (m_style.m_crText != CLR_INVALID)
        crDraw = m_style.m_crText;

    if (IsDisabled(TRUE)/*BkWndState_Disable == (GetState() & BkWndState_Disable)*/)
    {
        if (m_style.m_crDisabledText != CLR_INVALID)
            crDraw = m_style.m_crDisabledText;
    }
    else if (BkWndState_Hover == (GetState() & BkWndState_Hover))
    {
        if (m_style.m_ftHover)
            hftDraw = m_style.m_ftHover;

        if (m_style.m_crHoverText != CLR_INVALID)
            crDraw = m_style.m_crHoverText;
    }

    if (hftDraw)
    {
        BkDC.bFontChanged = TRUE;
        BkDC.hftOld = dc.SelectFont(hftDraw);
    }

    if (crDraw != CLR_INVALID)
    {
        BkDC.bTextColorChanged = TRUE;
        BkDC.crOld = dc.SetTextColor(crDraw);
    }
}

void AfterPaint(CDCHandle &dc, BkDCPaint &BkDC)
{
    if (BkDC.bFontChanged)
        dc.SelectFont(BkDC.hftOld);

    if (BkDC.bTextColorChanged)
        dc.SetTextColor(BkDC.crOld);

    if (BkDC.bBkModeChanged)
        dc.SetBkMode(BkDC.nOldBkMode);

    if (BkDC.bBgColorChanged)
        dc.SetBkColor(BkDC.crOldBg);
}

// Draw inner text default
void OnPaint(CDCHandle dc)
{
    BkDCPaint BkDC;

    BeforePaint(dc, BkDC);
    dc.DrawText(m_strInnerText, m_strInnerText.GetLength(), BkDC.rcClient, m_style.m_nTextAlign);
    AfterPaint(dc, BkDC);
}

二.BkStyle

同skin一样,也是从xml载入资源

<style>
<class name=mainhead skin=mainbghead font=0000 crtext=000000 crbg=FBFCFD/>
<class name=mainbody skin=mainbgbody font=0000 crtext=FFFFFF crbg=FBFCFD x-margin=7/>
<class name=mainfoot skin=mainbgfoot font=0000 crtext=000000 crbg=FBFCFD x-margin=10 y-margin=1/>
<class name=msgboxhead skin=msgboxhead font=0000 crtext=000000 crbg=FBFCFD/>
<class name=msgboxbody skin=msgboxbody font=0000 crtext=000000 crbg=FBFCFD/>
<class name=msgboxfoot skin=msgboxfoot font=0000 crtext=000000 crbg=FBFCFD/>
<class name=dlgbody skin=dlgbody font=0000 crtext=000000 crbg=FBFCFD/>
<class name=dlgfoot skin=dlgfoot font=0000 crtext=000000 crbg=FBFCFD/>
<class name=linktext font=0002 hoverfont=0002 crtext=3064A5 crhover=FF0000 cursor=hand/>
<class name=linktext2 crtext=3064A5 crhover=FF0000 cursor=hand/>
<class name=linktext3 font=0002 hoverfont=0002 crtext=0051E5 crhover=FF0000 cursor=hand/>
…
</style>