norains的专栏

只专注于WINCE开发

用户操作
[即时聊天] [发私信] [加为好友]
norainsID:norains
142878次访问,排名598,好友0人,关注者53人。
代码其实是一种乐趣
norains的文章
原创 189 篇
翻译 0 篇
转载 10 篇
评论 274 篇
norains的公告
联系方式请看置顶文章
最近评论
dfdf:讨厌MFC!我觉得MFC就是太乱了!看似无用的代码不要不行,MD微软啥都给我们做完了,原理性的东西我们却永远没法搞懂了!
ironox:有个地方 我觉得很别扭,不知道怎么办好

比如说 CReg reg(HKEY_CURRENT_USER,TEXT("ControlPanel\Volume"));
ControlPanel\Volume 有可能不存在呀,这个该怎么处理哦?对象虽然创建了,出错了也没提示
szterry:呵呵,果然工作狂技术狂,同感,一样的感觉……不过我才刚毕业一年……搞IT就是玩……
jinlking:这个botton的实现只是在主窗口画了一块区域,对于事件的处理还要放在主窗口的窗口处理函数之中,在对应的消息处理上调用CheckTap来判断是否是此“按钮”,问一下,这种方法与把按钮封装在子窗口中有什么区别,二者使用那个更好?
KUODY:博主真是好人
文章分类
收藏
    相册
    动漫
    文章图片
    程序交流
    xumercury的BLOG
    狗友们的博客
    清蒸石斑鱼
    美女如刀锋
    茁茁的BLOG
    魅力老姐的窝
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 CText类使用例程收藏

    新一篇: CProgress类使用例程 | 旧一篇: WinCE圆圈消息

    //========================================================================
    //TITLE:
    //    CText类使用例程
    //AUTHOR:
    //    norains
    //DATE:
    //    Tuesday  17-April-2007
    //Environment:
    //        EVC4.0 + Standard SDK 4.2
    //        EVC4.0 + Standard SDK 5.0
    //========================================================================

            CText是为了方便在屏幕中输出文本而封装的类.该类将复杂的设置操作封装成简单的函数,便于代码书写的简便性.

            该类使用简单,示例如下:

               //设置显示范围
     txtInfo.SetPosition(&rcWnd);

        //设置字体颜色  
     txtInfo.SetTextColor(RGB(255,0,0));

       
        //设置字体磅值,0为默认的字体大小   
     txtInfo.SetTextHeight(0);
       
        //设置显示的文本
     txtInfo.SetText(TEXT("测试"));
          
        //设置格式,该参数和DrawText()相同
     txtInfo.SetFormat(DT_RIGHT);

        //绘制文本
     txtInfo.Draw(hdc);
       
        默认的是透明显示,如果需要显示背景颜色,可以如下操作:
           
        //设置背景色
     txtInfo.SetBkColor(RGB(0,0,233));

        //OPAQUE为绘制背景色
     txtInfo.SetBkMode(OPAQUE);
       
        然后再调用Draw进行绘制即可.


        
    ///////////////////////////////////////////////////////////////////////
    // Text.h: interface for the CText class.
    //
    //Version: 
    //    1.0
    //Data:
    //    2007.03.27   
    //////////////////////////////////////////////////////////////////////

    #ifndef TEXT_H
    #define TEXT_H


    //------------------------------------------------------------------
    class CText  
    {
    public:
        
    void GetPosition(RECT * prcOut);
        
    void SetTextHeight(int iHeight);
        
    void SetFormat(UINT uFormat);
        
    void SetBkColor(COLORREF crColor);
        
    void SetTextColor(COLORREF crColor);
        BOOL SetBkMode(
    int iMode);
        
    void Draw(HDC hdc);
        BOOL SetText(
    const TCHAR *pszText);
        
    void SetPosition(const RECT *prc);
        CText();
        
    virtual ~CText();
    protected:
        RECT m_rcWndPos;    
        TCHAR 
    *m_pszText;
        ULONG m_ulSizeText;
        UINT m_uFormat;
        
    int m_iTextHeight;
        
    int m_iBkMode;
        COLORREF m_crTextColor;
        COLORREF m_crBkColor;

    }
    ;

    #endif // #ifndef TEXT_H


    //////////////////////////////////////////////////////////////////////
    // Text.cpp: implementation of the CText class.
    //
    //////////////////////////////////////////////////////////////////////

    #include 
    "stdafx.h"
    #include 
    "Text.h"


    //--------------------------------------------------------------------
    //Macro define
    #define DEFAULT_BKMODE            TRANSPARENT
    #define DEFAULT_TEXT_COLOR        RGB(0,0,0)
    #define DEFAULT_BK_COLOR        RGB(255,255,255)
    #define DEFAULT_FORMAT            (DT_LEFT | DT_SINGLELINE)
    #define DEFAULT_TEXT_HEIGHT        0


    //--------------------------------------------------------------------
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    CText::CText()
    {
        memset(
    &m_rcWndPos,0,sizeof(m_rcWndPos));
        m_pszText 
    = NULL;
        m_ulSizeText 
    = 0;
        m_iBkMode 
    = DEFAULT_BKMODE;
        m_crTextColor 
    = DEFAULT_TEXT_COLOR;
        m_crBkColor 
    = DEFAULT_BK_COLOR;
        m_uFormat 
    = DEFAULT_FORMAT;
        m_iTextHeight 
    = DEFAULT_TEXT_HEIGHT;
    }


    CText::
    ~CText()
    {
        
    if(m_pszText != NULL)
        
    {
            delete [] m_pszText;
            m_pszText 
    = NULL;
        }

    }


    //--------------------------------------------------------------------
    //Description:
    //    Set the control position
    //
    //--------------------------------------------------------------------
    void CText::SetPosition(const RECT *prc)
    {
        m_rcWndPos 
    = *prc;
    }



    //--------------------------------------------------------------------
    //Description:
    //    Set the text. If you want to display the text ,you should call the Display()
    //
    //--------------------------------------------------------------------
    BOOL CText::SetText(const TCHAR *pszText)
    {
        ULONG ulLen 
    = _tcslen(pszText);

        
    if(m_pszText == NULL)
        
    {        
            m_pszText 
    = new TCHAR [ulLen + 1];
            
            
    if(m_pszText == NULL)
            
    {
                
    return FALSE;
            }


            m_ulSizeText 
    = ulLen + 1;
        }

        
    else if(ulLen + 1 > m_ulSizeText)
        
    {
            delete [] m_pszText;

            m_pszText 
    = new TCHAR [ulLen + 1];
            
            
    if(m_pszText == NULL)
            
    {
                
    return FALSE;
            }


            m_ulSizeText 
    = ulLen + 1;
        }


        _tcscpy(m_pszText,pszText);

        
    return TRUE;
    }



    //--------------------------------------------------------------------
    //Description:
    //    Display the text stored.
    //
    //--------------------------------------------------------------------
    void CText::Draw(HDC hdc)
    {
        COLORREF crOldTextColor 
    = ::SetTextColor(hdc,m_crTextColor);
        COLORREF crOldBkColor 
    = ::SetBkColor(hdc,m_crBkColor);
        
    int iOldMode = ::SetBkMode(hdc,m_iBkMode);

        LOGFONT lf 
    = {0};
        HFONT hFontNew, hFontOld;
        lf.lfQuality 
    = CLEARTYPE_QUALITY;
        lf.lfHeight 
    = -1 * (m_iTextHeight * GetDeviceCaps(hdc,LOGPIXELSY) / 72); //pound
        hFontNew = CreateFontIndirect(&lf);
        hFontOld 
    = (HFONT) SelectObject(hdc, hFontNew);

        DrawText(hdc,m_pszText,
    -1,&m_rcWndPos,m_uFormat);

        SelectObject(hdc, hFontOld);
        DeleteObject(hFontNew);

        ::SetTextColor(hdc,crOldTextColor);
        ::SetBkColor(hdc,crOldBkColor);
        ::SetBkMode(hdc,iOldMode);
    }



    //--------------------------------------------------------------------
    //Description:
    //    Set the background mode.
    //
    //Parameters:
    //    iMode: [in] The value is just like as follow:
    //        OPAQUE      -- Background is filled with the current background color before the text, 
    //                        hatched brush, or pen is drawn. 
    //        TRANSPARENT -- Background remains untouched. 

    //--------------------------------------------------------------------
    BOOL CText::SetBkMode(int iMode)
    {
        
    if(iMode == OPAQUE || iMode == TRANSPARENT)
        
    {
            m_iBkMode 
    = iMode;
            
    return TRUE;
        }

        
    else
        
    {
            
    return FALSE;
        }

    }



    //--------------------------------------------------------------------
    //Description:
    //    Set the text color
    //
    //--------------------------------------------------------------------
    void CText::SetTextColor(COLORREF crColor)
    {
        m_crTextColor 
    = crColor;
    }



    //--------------------------------------------------------------------
    //Description:
    //    Set the background color
    //
    //--------------------------------------------------------------------
    void CText::SetBkColor(COLORREF crColor)
    {
        m_crBkColor 
    = crColor;
    }


    //--------------------------------------------------------------------
    //Description:
    //    Set Format.
    //
    //Parameters:
    //    The value you should see the uFormat of DrawText()
    //--------------------------------------------------------------------
    void CText::SetFormat(UINT uFormat)
    {
        m_uFormat 
    = uFormat;
    }



    //--------------------------------------------------------------------
    //Description:
    //    Set the height of text as pound
    //
    //---------------------------------------------------------------------
    void CText::SetTextHeight(int iHeight)
    {
        m_iTextHeight 
    = iHeight;
    }



    //--------------------------------------------------------------------
    //Description:
    //    Get the position as rect
    //
    //---------------------------------------------------------------------
    void CText::GetPosition(RECT *prcOut)
    {
        
    *prcOut = m_rcWndPos;
    }

    发表于 @ 2007年04月17日 23:30:00|评论(loading...)|编辑

    新一篇: CProgress类使用例程 | 旧一篇: WinCE圆圈消息

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © norains