【转载】MiniUtilityFramework(十):CButton和BUTTON

转载自:http://blog.csdn.net/norains/article/details/3957135

 

如果说别的控件可能不一定用上,但对于CButton来说,基本上每个程序都可能存在其身影。想想看,程序你想点击后退出,通用做法是啥?加个按钮,然后在该按钮按下的时候处理退出信息。由此可见,CButton可是控件中必不可少的一个成员。只是,重要归重要,但实际上却一点都不复杂。
  
我们先来看看头文件声明:

// Button.h: interface for the CButton class.  
//  
//////////////////////////////////////////////////////////////////////  
  
#pragma once  
  
#include "..//Table//imageTabBase.h"  
#include "Text.h"  
  
  
namespace Button  
{  
    enum DisplayStyle  
    {  
        STYLE_AUTO,  
        STYLE_PUSH,  
        STYLE_ENABLE,  
        STYLE_DISABLE  
    };  
}  
  
class CButton    
{  
public:  
    //Before using you must call the function  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set image table object  
    //  
    //-------------------------------------------------------------------  
    void SetImgTab(CImageTabBase &imgTab);  
  
public:       
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the text face name  
    //  
    //-------------------------------------------------------------------  
    void SetTextFaceName(const TSTRING &strFaceName);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the button position  
    //  
    //-------------------------------------------------------------------  
    void SetPosition(const RECT &rcWnd);  
  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the strikeOut  
    //  
    //---------------------------------------------------------------------  
    void SetTextStrikeOut(BOOL bStrikeOut);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the underline  
    //  
    //---------------------------------------------------------------------  
    void SetTextUnderline(BOOL bUnderline);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the italic  
    //  
    //---------------------------------------------------------------------  
    void SetTextItalic(BOOL bItalic);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Specifies the weight of the font in the range 0 through 1000. For example,   
    //400 is normal and 700 is bold. If this value is zero, a default weight is used.   
    //  
    //---------------------------------------------------------------------  
    BOOL SetTextWeight(int iWeight);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the point size of text  
    //  
    //---------------------------------------------------------------------  
    void SetTextPointSize(int iPointSize);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set Format.  
    //  
    //Parameters:  
    //  The value you should see the uFormat of DrawText()  
    //--------------------------------------------------------------------  
    void SetTextFormat(UINT uFormat);  
  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the text. If you want to display the text ,you should call the Display()  
    //  
    //--------------------------------------------------------------------  
    BOOL SetText(const TSTRING &strText);  
  
      
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the visible  
    //  
    //Parameters:  
    //  bVisible:[in]  
    //      TRUE - visible  
    //      FALSE - invisible  
    //  
    //--------------------------------------------------------------------  
    void SetVisible(BOOL bVisible);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Draw the button  
    //  
    //-------------------------------------------------------------------  
    BOOL Draw(HDC hdc, Button::DisplayStyle btnDraw);     
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the button status   
    //  
    //-------------------------------------------------------------------  
    void SetEnable(BOOL bEnable);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  The button is enable or not  
    //  
    //-------------------------------------------------------------------  
    BOOL GetEnable();  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Check tapped position in the area.If the button is disable,  
    //it would return FALSE.  
    //  
    //-------------------------------------------------------------------  
    BOOL CheckTap(const POINT &pt);  
  
      
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the text color of enable  
    //  
    //-------------------------------------------------------------------  
    void SetTextColorEnable(COLORREF crVal);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the text color of disable  
    //  
    //-------------------------------------------------------------------  
    void SetTextColorDisable(COLORREF crVal);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the text color of push  
    //  
    //-------------------------------------------------------------------  
    void SetTextColorPush(COLORREF crVal);  
  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the image index of enable  
    //  
    //-------------------------------------------------------------------  
    void SetImgIndexEnable(TSTRING strIndex);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the image index of disable  
    //  
    //-------------------------------------------------------------------  
    void SetImgIndexDisable(TSTRING strIndex);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the image index of push  
    //  
    //-------------------------------------------------------------------  
    void SetImgIndexPush(TSTRING strIndex);  
      
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the transparent mode  
    //  
    //Parameters:  
    //  bTran:[in]  
    //      TRUE  - Don't draw the transparent color  
    //      FALSE - Draw all the color  
    //  
    //-------------------------------------------------------------------  
    void SetTransparent(BOOL bTran);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Get the image position  
    //  
    //-------------------------------------------------------------------  
    RECT GetImgPosition();  
  
public:  
    CButton();    
    virtual ~CButton();  
  
  
protected:  
    //--------------------------------------------------------------------  
    //Description:  
    //  Draw the button  
    //  
    //-------------------------------------------------------------------  
    BOOL Draw(HDC hdc, Button::DisplayStyle btnDraw,const RECT &rcWndImg,const RECT &rcText);  
  
  
private:  
    BOOL m_bEnable;  
    RECT m_rcWnd;  
    BOOL m_bTran;  
    BOOL m_bVisible;  
    CText m_Text;  
    TSTRING m_ImgIndexEnable;  
    TSTRING m_ImgIndexDisable;  
    TSTRING m_ImgIndexPush;  
    CImageTabBase *m_pImgTab;  
  
    COLORREF m_crTxtEnable;  
    COLORREF m_crTxtDisable;  
    COLORREF m_crTxtPush;  

 和其它控件一样,很多函数我们完全可以无视。和别的控件不同,CButton有一个专门处理点击消息的回调函数:
    virtual BOOL HandleCommand(const TSTRING &strName,const TSTRING &strCmd,const TSTRING &strParam);
    
    我们只要在CUserWnd的派生类中重载该函数,就能获取按钮的点击信息,如:
    
    BOOL CWnd::HandleCommand(const TSTRING &strName,const TSTRING &strCmd,const TSTRING &strParam)
  {
   if(strCmd == Command::CMD_USER)
   {
    //To do sometingh
   }
   
   return CUserWnd::HandleCommand(strName,strCmd,strParam);
  }

    函数中各形参的意义如下:
    
    strName:控件的名字。即在配置文件中的SECTION段名
    
    strCmd:命令的类别。目前有如下命令可以判断:CMD_USER,CMD_WND,CMD_APP,CMD_EXIT,CMD_VOL_INC,CMD_VOL_DEC,CMD_BKLIGHT_INC,CMD_BKLIGHT_DEC,CMD_LANGUAGE_PRE,CMD_LANGUAGE_NEXT。这些命令的具体含义请参考本文的配置文件这部份。
    
    strParam:命令的参数。
    
    以上三个形参,都是字符串形式。
    
    
    接下来我们来看看配置文件的可配置段:
  
  TYPE:类型,该VALUE必须为BUTTON
  
  RECT_POS:按钮的位置
  
  IMG_ENABLE:正常状态的图片
  
  IMG_DISABLE:无效状态的图片
  
  IMG_PUSH:按下状态的图片
  
  COMMAND:按钮的命令类型。可选的命令请见本文其后说明。
  
  PARAMETER:命令的参数
  
  TRANSPARENT_MODE:TRUE--不绘制图片的透明色。 FALSE--绘制图片的透明色
  
  除此以外,还有如下字段:POINT_SIZE,WEIGHT,FORMAT,TXT_COLOR_ENABLE,TXT_COLOR_DISABLE,TXT_COLOR_PUSH,STRING,FONT_FACE_NAME。这些字段所代表的意义和CText的配置文件一致。如果需要在按钮显示相应的文字,请参考CText配置文件的用法。

    在这里必须要重点说一下COMMAND这个KEY。COMMOND的VALUE是固定的,可选的数值只能为如下其一:
    
    CMD_USER:用户自定义命令。
    
    CMD_WND:窗口切换命令。该命令的PARAMETER必须为配置文件中某一个窗口名。
    
    CMD_APP:执行外部程序。该命令的PARAMETER必须为有效的应用程序的绝对路径。
    
    CMD_EXIT:退出应用程序。
    
    CMD_VOL_INC,CMD_VOL_DEC:音量的增加和减少。
    
    CMD_BKLIGHT_INC,CMD_BKLIGHT_DEC:背光亮度的增加很减少。该命令和底层有关,并不具备移植性。
    
    CMD_LANGUAGE_PRE,CMD_LANGUAGE_NEXT:前一个和后一个语言设置。该命令的有效性和[LANGUAGE]有关。
    
    
    如果我们想退出程序,借助已经封装好的命令,则配置文件可以简单如此:
    
    [BTN_EXIT]
  TYPE=BUTTON
  RECT_POS=0,0,10,10
  COMMAND=CMD_EXIT
  TRANSPARENT_MODE=TRUE
  
  调用外部程序,则可以如下书写:
  
  [BTN_NAVI]
  TYPE=BUTTON
  RECT_POS=0,0,10,10
  COMMAND=CMD_APP
  TRANSPARENT_MODE="/NAND/navigation.exe"
  
  类似,切换窗口仅仅是如下定义:
  
  [BTN_NAVI]
  TYPE=BUTTON
  RECT_POS=0,0,10,10
  COMMAND=CMD_WND
  TRANSPARENT_MODE=WND_LANGUAGE
    
    该配置文件意思是按下BTN_NAVI这个按钮,则会切换到WND_LANGUAGE窗口。
    
    如上的三个配置样例,主要是为了说明相应的功能,所以没有将相应的图片序号配置。现在给出一个比较完整的,但不带文字的按钮配置文件样例:
    
    [BTN_SECOND_DEC_WND_TIME_SETTING]
  TYPE=BUTTON
  RECT_POS=352,77,374,99
  COMMAND=CMD_USER
  PARAMETER=SECOND_DEC
  IMG_ENABLE=IMG_FILE_DEC_ENABLE
  IMG_PUSH=IMG_FILE_DEC_PUSH
  TRANSPARENT_MODE=TRUE

posted @ 2011-12-05 10:47  风雨雪夜  阅读(167)  评论(0编辑  收藏  举报