Wince下自绘文本控件

最近,因为工作比较紧张一个月都没有写文章了。由于工作的需要我自己封装几个基本控件,有按钮、进度条、列表框、静态文本域、动态文本域、时钟、对话框等基本控件。

下面我们来解释一下文本域(静态文本)的封装实现,文本域包括以下几个部分:

1、  字体的大小

2、  字体颜色

3、  字体显示的区域

4、  文本域的背景

现在我们搞清楚了文本包括以上部分,就可以开始封装文本控件。

首先,在代码定义一个文本属性结构体:

namespace Text

{

    struct Property

    {

        RECT rcWndPos;  //文字显示的位置

        TSTRING strText;//显示的文本

        UINT uFormat;   //绘制文本的格式

        int iPointSize; //文字的高度

        int iBkMode;    //文字背景格式

        COLORREF crTextColor; //文字颜色

        COLORREF crBkColor;   //背景颜色

        int iWeight;          //文字的宽度

        BOOL bItalic;         //是否为斜体

        BOOL bUnderline;     //是否要下划线

        BOOL bStrikeOut;     //是否要删除线

        TSTRING strFaceName; //字体名称

    };

}

再定义一个文本窗口属性结构体:

namespace TextWnd

{

    struct Property

    {

        TSTRING strBkImage; //文本窗口背景图片路径

        RECT rcDrawPosition;//文本窗口位置

        RECT rcImagePosition;//背景图片的显示区域

        BOOL bTransparent;   //是否过滤透明色

        COLORREF crTransparentColor;//透明颜色

        Text::Property proText;      //为位置属性

    };

}

有了上面两个结构体我们就可以通过我编写的文本框来显示我们想要的字体。在这里先教大家怎么使用我封装的文本类,之后我再把代码列出来。

 

 

    //TxtPro

    Text::Property TxtPro;//定义文本属性对象

    TxtPro.rcWndPos    = CreateRect(0,0,215,15);

    TxtPro.crBkColor   = RGB(0,255,0);

    TxtPro.crTextColor = RGB(255,255,255);

    TxtPro.iPointSize  = 16;

    TxtPro.iWeight     = 600;

    TxtPro.strText     = TEXT("Text");;

    TxtPro.strFaceName = TEXT("新宋体");

    TxtPro.iBkMode     = TRANSPARENT;

    TxtPro.uFormat     = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS ;

    TxtPro.bStrikeOut  = FALSE;

    TxtPro.bUnderline  = FALSE;

    TxtPro.bItalic     = FALSE;

 

    //TxtWndPro

    TextWnd::Property TxtWndPro; //定义文本窗口属性对象

    TxtWndPro.rcDrawPosition = CreateRect(250,100,550,200);

    TxtWndPro.proText = TxtPro;

    TxtWndPro.proText.rcWndPos = CreateRect(0,0,300,100);

    TxtWndPro.proText.iPointSize = 40;

    TxtWndPro.proText.iWeight = 800;

 

    CTextWnd txtWnd; //定义文本窗口对象

    txtWnd.SetProperty(TxtWndPro);  //设置属性

    //完成了以上步骤下面只要通过下面一步就可以看到一个文本框了

    txtWnd.Create(父窗口指针,TEXT("文本框"));//创建文本窗口

   

    //要改变文本内容可以调用:

    txtWnd. SetText(TEXT("填写你要显示的文本"));

 

    //返回文本

    TSTRING strText = txtWnd.GetText();

 

   //改变显示的位置

    RECT rcPos ={0,0,100,100};

   txtWnd. SetTextPosition(rcPos);

 

实现源代码如下:

//TextWnd.h

#pragma once #include "ChildWndInterface.h" #include "Text.h" #include "MemDC.h" namespace TextWnd { struct Property { TSTRING strBkImage; RECT rcDrawPosition; RECT rcImagePosition; BOOL bTransparent; COLORREF crTransparentColor; Text::Property proText; }; } class CTextWnd: public CChildWndInterface { public: //---------------------------------------------------------------------- //Description: // Create the CButtonWnd window // //Parameters: // hWndParent : [in] The parent window // strWndName : [in] The name of the windows //---------------------------------------------------------------------- BOOL Create(HWND hWndParent,const TSTRING &strWndName); //---------------------------------------------------------------------- //Description: // Set the property for window // //Parameters: // Property : [in] The window's property. // //---------------------------------------------------------------------- void SetProperty( const TextWnd::Property &Property); //---------------------------------------------------------------------- //Description: // Get the property for window // //Parameters: // Property : [out] Store the window's property. // //---------------------------------------------------------------------- void GetProperty(TextWnd::Property &Property); //-------------------------------------------------------------------- //Description: // Set the text. If you want to display the text ,you should call the Display() // //-------------------------------------------------------------------- void SetText(const TSTRING &strText); //---------------------------------------------------------------------- //Description: // Get the text. //---------------------------------------------------------------------- TSTRING GetText(); //-------------------------------------------------------------------- //Description: // Set the draw text position // //-------------------------------------------------------------------- void SetTextPosition(const RECT &rcWndPos); protected: //---------------------------------------------------------------------- //Description: // Actual WndProc //---------------------------------------------------------------------- virtual LRESULT WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam); //---------------------------------------------------------------------- //Description: // Draw window //---------------------------------------------------------------------- void DrawWnd(HDC hdc,BOOL bParent = FALSE); //---------------------------------------------------------------------- //Description: // On WM_PAINT message //---------------------------------------------------------------------- void OnPaint(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam); //---------------------------------------------------------------------- //Description: // Initialize the necessary work when creating windows. //---------------------------------------------------------------------- virtual void CompleteInitialize(); //---------------------------------------------------------------------- //Description: // Get text window background image. //---------------------------------------------------------------------- void GetBkGndMem(CMemDC &dcMem); public: CTextWnd(); virtual ~CTextWnd(); private: struct TxtWndProperty { TSTRING strBkImage; RECT rcDrawPosition; RECT rcImagePosition; BOOL bTransparent; COLORREF crTransparentColor; }; TxtWndProperty m_Property; CText m_CText; CMemDC m_dcBkGndMem; };

//TextWnd.cpp

#include "TextWnd.h" CTextWnd::CTextWnd() { RECT rc ={0}; Text::Property txtProperty; txtProperty.bItalic = FALSE; txtProperty.bStrikeOut = FALSE; txtProperty.bUnderline = FALSE; txtProperty.crBkColor = RGB(0,0,0); txtProperty.crTextColor = RGB(255,255,255); txtProperty.iBkMode = TRANSPARENT; txtProperty.iPointSize = 12; txtProperty.iWeight = FW_NORMAL; txtProperty.strFaceName = TEXT("Font"); txtProperty.strText = TEXT(""); txtProperty.uFormat = DT_LEFT; m_Property.strBkImage = TEXT(""); m_Property.rcImagePosition = rc; m_Property.rcDrawPosition = rc; m_Property.bTransparent = FALSE; m_Property.crTransparentColor =RGB(0,0,0); m_CText.SetProperty(txtProperty); } CTextWnd::~CTextWnd() { } BOOL CTextWnd::Create(HWND hWndParent, const TSTRING &strWndName) { BOOL bReturn = FALSE; std::vector<TCHAR> vtClass(MAX_PATH,0); _stprintf(&vtClass[0],TEXT("CTextWnd_CLS_%d"),GetModuleHandle(NULL)); bReturn = CChildWndInterface::Create(hWndParent,&vtClass[0],strWndName); if(bReturn) { ::MoveWindow(GetWindow(), m_Property.rcDrawPosition.left, m_Property.rcDrawPosition.top, m_Property.rcDrawPosition.right - m_Property.rcDrawPosition.left, m_Property.rcDrawPosition.bottom- m_Property.rcDrawPosition.top, TRUE); CompleteInitialize(); SetVisible(TRUE); } return bReturn; } void CTextWnd::CompleteInitialize() { if(m_Property.strBkImage.empty() == TRUE) { GetBkGndMem(m_dcBkGndMem); } } void CTextWnd::GetBkGndMem(CMemDC &dcMem) { if(dcMem.IsOK()) { dcMem.Delete(); } SIZE size ={m_Property.rcDrawPosition.right - m_Property.rcDrawPosition.left, m_Property.rcDrawPosition.bottom - m_Property.rcDrawPosition.top}; HDC hdc = GetDC(GetParentWindow()); dcMem.Create(hdc,&size); BitBlt(dcMem.GetDC(),0,0,size.cx,size.cy,hdc,m_Property.rcDrawPosition.left,m_Property.rcDrawPosition.top,SRCCOPY); ReleaseDC(GetParentWindow(),hdc); } void CTextWnd::SetProperty(const TextWnd::Property &Property) { m_Property.bTransparent = Property.bTransparent; m_Property.crTransparentColor = Property.crTransparentColor; m_Property.rcDrawPosition = Property.rcDrawPosition; m_Property.rcImagePosition = Property.rcImagePosition; m_Property.strBkImage = Property.strBkImage; m_CText.SetProperty(Property.proText); } void CTextWnd::GetProperty(TextWnd::Property &Property) { Property.bTransparent = m_Property.bTransparent; Property.crTransparentColor = m_Property.crTransparentColor; Property.rcDrawPosition = m_Property.rcDrawPosition; Property.rcImagePosition = m_Property.rcImagePosition; Property.strBkImage = m_Property.strBkImage; Property.proText = m_CText.GetProperty(); } void CTextWnd::SetText(const TSTRING &strText) { m_CText.SetText(strText); HDC hdc = GetDC(GetWindow()); DrawWnd(hdc); ReleaseDC(GetWindow(),hdc); } TSTRING CTextWnd::GetText() { return m_CText.GetText(); } void CTextWnd::SetTextPosition(const RECT &rcWndPos) { m_CText.SetPosition(rcWndPos); HDC hdc = GetDC(GetWindow()); DrawWnd(hdc); ReleaseDC(GetWindow(),hdc); } LRESULT CTextWnd::WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { switch(wMsg) { case WM_PAINT: OnPaint(hWnd,wMsg,wParam,lParam); return 0; } return CChildWndInterface::WndProc(hWnd,wMsg,wParam,lParam); } void CTextWnd::OnPaint(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hWnd,&ps); DrawWnd(hdc); EndPaint(hWnd,&ps); } void CTextWnd::DrawWnd(HDC hdc,BOOL bParent) { CMemDC dcMem; SIZE size ={m_Property.rcDrawPosition.right - m_Property.rcDrawPosition.left, m_Property.rcDrawPosition.bottom - m_Property.rcDrawPosition.top}; dcMem.Create(hdc,&size); if(m_dcBkGndMem.IsOK()) { BitBlt( dcMem.GetDC(), 0, 0, size.cx, size.cy, m_dcBkGndMem.GetDC(), 0, 0, SRCCOPY); } else { #ifdef _WIN32_WCE HBITMAP hBmp = SHLoadDIBitmap(m_Property.strBkImage.c_str()); #else HBITMAP hBmp = reinterpret_cast<HBITMAP>(LoadImage(NULL,m_Property.strBkImage.c_str(),IMAGE_BITMAP,0,0,LR_LOADFROMFILE)); #endif if(hBmp == NULL) { return; } //Crete the bitmap DC HDC hdcBmp = CreateCompatibleDC(hdc); if(hdcBmp == NULL) { DeleteObject(hBmp); return; } HGDIOBJ hOldSel = SelectObject(hdcBmp,hBmp); StretchBlt( dcMem.GetDC(), 0, 0, size.cx, size.cy, hdcBmp, m_Property.rcImagePosition.left, m_Property.rcImagePosition.top, m_Property.rcImagePosition.right - m_Property.rcImagePosition.left, m_Property.rcImagePosition.bottom - m_Property.rcImagePosition.top, SRCCOPY); SelectObject(hdcBmp,hOldSel); //Delete the bitmap DC DeleteDC(hdcBmp); //Delete the bitmap DeleteObject(hBmp); } //Draw text to dcMem m_CText.Draw(dcMem.GetDC()); if(m_Property.bTransparent == TRUE) { TransparentBlt( hdc, bParent ? m_Property.rcDrawPosition.left : 0, bParent ? m_Property.rcDrawPosition.top : 0, size.cx, size.cy, dcMem.GetDC(), 0, 0, size.cx, size.cy, m_Property.crTransparentColor); } else { BitBlt( hdc, bParent ? m_Property.rcDrawPosition.left : 0, bParent ? m_Property.rcDrawPosition.top : 0, size.cx, size.cy, dcMem.GetDC(), 0, 0, SRCCOPY); } }

你可能感兴趣的:(Wince下自绘文本控件)