zoukankan      html  css  js  c++  java
  • 我写的C++控件基类 荣

    1. 属性类:存储控件属性的类:

    控件的属性从文本文件中读取。
    文本文件的案例为:
    ID_BTN_NAVI_SCALE=rect=10,80,50,80;type=button;text=30M;img_src=IDB_MAIN_CHS.BMP;img_num=14,1;forcolor=255,255,255;isshow=0;bgcolor=255,255,255;

    关键字=rect=左,右,上,80;下=button;text=控件类型;img_src=北背景图片位置;img_num=图片中分图总数,背景图片的索引书;forcolor=字体颜色(255,255,255);isshow=是否显示bgcolor=背景颜色;

    头文件代码:

    // Property.h: interface for the CProperty class.
    //
    //////////////////////////////////////////////////////////////////////

    #if !defined(AFX_PROPERTY_H__2ACF12EB_5996_4F05_B412_9E2FE471B270__INCLUDED_)
    #define AFX_PROPERTY_H__2ACF12EB_5996_4F05_B412_9E2FE471B270__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    #include 
    "ToolKit.h"

    class CProperty  
    {
    private:
        
    static void SetDefaultContrlStruct(CreateWndStruct &controlStruct);
        
    static void SetDefaultFont(LOGFONT &font);
    public:
        CProperty();
        
    virtual ~CProperty();
    public:
        
    /**------------------------------------------------------------------------------------
        * 名    称:    <SetValue>
        * 说    明:     <根据字符串指针,取得控件的配置信息>
        * 参    数:        <char *chValue>:<字符串指针>    
        *                <char* keye>:<控件主键值>
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */
        
    void SetValue(char* chValue, char* key);
    private:
        
    /**------------------------------------------------------------------------------------
        * 名    称:    <GetSpeCharPtr>
        * 说    明:     <取得根字符串中,某子字符串后面的字符串>
        * 参    数:        <char *chValue>:<根字符串>    
        *                <char* &chSpe>:<处理后的字符串>
        *                <char* key>:<子字符串>    
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */
        
    void GetSpeCharPtr(char *chValue, char* &chSpe, char* key);

        
    /**------------------------------------------------------------------------------------
        * 名    称:    <GetColor>
        * 说    明:     <根据某主键,取得根字符串中该主键对应的颜色>
        * 参    数:        <char *chValue>:<根字符串>    
        *                <COLORREF &color>:<对应的颜色>
        *                <char* key>:<主键>    
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */
        
    void GetColor(char* chValue, COLORREF &color, char* key);

        
    /**------------------------------------------------------------------------------------
        * 名    称:    <GetInt>
        * 说    明:     <根据某主键,取得根字符串中该主键对应的整数>
        * 参    数:        <char *chValue>:<根字符串>    
        *                <int &nInt>:<整数>
        *                <char* key>:<主键>    
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */
        
    void GetInt(char* chValue, int &nInt, char* key);
        
        
    /**------------------------------------------------------------------------------------
        * 名    称:    <GetCString>
        * 说    明:     <根据某主键,取得根字符串中该主键对应的字符串值>
        * 参    数:        <char *chValue>:<根字符串>    
        *                <CString &strSrc>:<字符串值>
        *                <char* key>:<主键>    
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */
        
    void GetCString(char *chValue, CString &strSrc, char* key);

        
    /**------------------------------------------------------------------------------------
        * 名    称:    <GetImgInfo>
        * 说    明:     <取得根字符串中该主键对应的图片信息>
        * 参    数:        <char *chValue>:<根字符串>    
        *                <int &nCount>:<图片总数>
        *                <int &nIndex>:<图片部分的索引>    
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */
        
    void GetImgInfo(char *chValue, int &nCount, int &nIndex);

        
    /**------------------------------------------------------------------------------------
        * 名    称:    <GetOtherInfo>
        * 说    明:     <取得控件的位置>
        * 参    数:        <char *chValue>:<根字符串>    
        *                <RECT &rect>:<控件的位置>
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */
        
    void GetRect(char *chValue, RECT &rect, char* key);
    public:
        CString            m_strKey;            
    // 配置文件中的主键
        CString            m_strType;            // 控件类型
        CString            m_strText;            // 控件文本
        COLORREF        m_FocColor;            // 获取焦点后的背景颜色
        COLORREF        m_EnbColor;            // 控件不可用时的颜色
        COLORREF        m_ForColor;            // 前台颜色
        COLORREF        m_BgColor;            // 背景颜色
        CString            m_strImgSrc;        // 背景图路径
        int                m_nImgIndex;        // 背景图索引
        int                m_nImgCount;        // 背景图图数
        RECT            m_Rect;                // 控件位置
        bool            m_bIsShow;            // 控件是否显示
        
    // char*            m_chValue;            // 控件描述字符串
        CreateWndStruct m_WndStruct;        // 创建控件的样式
        LOGFONT            m_logfont;            // 控件文本的字体
        UINT            m_TextFormat;        // 控件文本的样式:靠上,靠下等
        bool            m_bIsBold;            // 字体是否粗体
        bool            m_bIsEnable;        // 是否不可用
    };

    #endif // !defined(AFX_PROPERTY_H__2ACF12EB_5996_4F05_B412_9E2FE471B270__INCLUDED_)

    实现代码:

    // Property.cpp: implementation of the CProperty class.
    //
    //////////////////////////////////////////////////////////////////////

    #include 
    "stdafx.h"
    #include 
    "Property.h"

    #include 
    "ItemKey.h"

    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////


    void CProperty::SetDefaultFont(LOGFONT &font)
    {
        font.lfHeight        
    = 0;        // 以逻辑单位指定字体字符元(character cell)或字符的高度
        font.lfWidth        = 0;        // 以逻辑单位指定字体字符的平均宽度
        font.lfEscapement    = 0;        // 以十分之一度为单位指定每一行文本输出时相对于页面底端的角度
        font.lfOrientation    = 0;        // 以十分之一度为单位指定字符基线相对于页面底端的角度
        font.lfWeight        = 700;        // 指定字体重量。
        font.lfItalic        = 0;        // 当lfItalic为TRUE时使用斜体
        font.lfUnderline    = 0;        // 当lfUnderline为TRUE时给字体添加下划线
        font.lfStrikeOut    = 0;        // 当lfStrikeOut为TRUE时给字体添加删除线
        font.lfCharSet        = 0;        // 指定字符集
        font.lfOutPrecision    = 0;        // 指定输出精度
        font.lfQuality        = 0;        // 定义输出质量 DEFAULT_QUALITY 
                                        
    // (默认质量)DRAFT_QUALITY (草稿质量)PROOF_QUALITY (正稿质量)

        font.lfClipPrecision    
    = 0;    // 指定剪辑精度
        font.lfPitchAndFamily    = 0;        // 指定字体的字符间距和族
        strcpy(font.lfFaceName, TEXT(""));

    }

    void CProperty::SetDefaultContrlStruct(CreateWndStruct &controlStruct)
    {
        controlStruct.dwExStyle            
    = 0;
        controlStruct.lpszName            
    = TEXT("");
        controlStruct.style                
    = WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_OWNERDRAW;
        controlStruct.hInstance            
    = GetModuleHandle(NULL);//CBaseWnd::g_hInst;
        controlStruct.lpCreateParams    = NULL;
    }

    CProperty::CProperty()
    {
        
    // 设置属性的默认值
        
    //this->m_chValue        = NULL;                    // 存储配置信息的字符串
        this->m_FocColor    = RGB(255,255,255);        // 获得焦点后的背景颜色
        this->m_EnbColor    = RGB(170,160,180);        // 控件不可用时的颜色
        this->m_ForColor    = RGB(39102203);    // 前台颜色
        this->m_BgColor        = RGB(170,160,180);        // 背景颜色
        this->m_strImgSrc    = "";        // 背景图路径
        this->m_nImgIndex    = 1;        // 背景图索引
        this->m_nImgCount    = 1;        // 背景图图数
        this->m_bIsShow        = true;        // 控件是否显示
        this->m_strText        = "";        // 控件文本
        this->m_bIsBold        = true;        // 字体是否粗体
        this->m_bIsEnable    = false;    // 控件是否不可用
        this->m_TextFormat    = DT_SINGLELINE|DT_VCENTER|DT_CENTER;    // 文本的现实位置

        
    // 创建控件时的默认样式
        CProperty::SetDefaultContrlStruct(this->m_WndStruct);

        
    // 控件文本的默认字体
        CProperty::SetDefaultFont(this->m_logfont);
    }

    CProperty::
    ~CProperty()
    {
    }

    // 根据配置信息设置控件属性值
    void CProperty::SetValue(char *chValue, char* key)
    {
        
    if (!chValue)
        {
            
    return;
        }

        
    // 将配置文本存储起来,用到时就不用再从配置文件中读取了。当前该属性没有用到
        
    //this->m_chValue = new char[strlen(chValue) + 1];
        
    //::strcpy(this->m_chValue, chValue);

        
    // 根据配置文件中的信息配置控件的属性
        m_strKey = CString(key);                                            // 控件主键    
        this->GetColor(chValue, m_BgColor, ItemKey::CONTROL_BGCOLOR);        // 背景颜色    
        this->GetColor(chValue, m_ForColor, ItemKey::CONTROL_FORCOLOR);        // 前台颜色    
        this->GetColor(chValue, m_FocColor, ItemKey::CONTROL_FOCCOLOR);        // 获得焦点后的背景颜色
        this->GetCString(chValue, m_strImgSrc, ItemKey::CONTROL_IMGSRC);    // 获得背景图片的路径
        this->GetImgInfo(chValue, m_nImgCount, m_nImgIndex);                // 背景图片的路径,个数及索引号    
        this->GetCString(chValue, m_strType, ItemKey::CONTROL_TYPE);        // 控件类型
        this->GetCString(chValue, m_strText, ItemKey::CONTROL_TEXT);        // 控件文本
        this->GetRect(chValue, m_Rect, ItemKey::CONTROL_RECT);                // 控件位置信息
        
        
    int nIsShow;                                                        // 是否显示
        this->GetInt(chValue, nIsShow, ItemKey::CONTROL_ISSHOW);
        
    if (nIsShow == 0)
        {
            m_bIsShow 
    = false;
        }

        
    int nIsBold;                                                    // 字体是否粗体
        this->GetInt(chValue, nIsBold, ItemKey::CONTROL_BOLD);
        
    if (nIsBold == 0)
        {
            m_bIsBold 
    = false;
        }

        
    this->m_WndStruct.lpszClass = TEXT(this->m_strType);

        
    // 按钮
        if (this->m_strType == "button")
        {
            
    this->m_WndStruct.nWndType    = 1;
            
    this->m_WndStruct.dwstyle    =  WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_OWNERDRAW;
        }
        
    // 标签
        else if (this->m_strType == "static")
        {
            
    this->m_WndStruct.nWndType    =  3;
            
    this->m_WndStruct.dwstyle    =  WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_BORDER;
        }
        
    // 文本框
        else if (this->m_strType == "edit")
        {
            
    this->m_WndStruct.nWndType    =    2;
            
    this->m_WndStruct.dwstyle    =  WS_CHILD|WS_VISIBLE|ES_LEFT;
        }
        
    // 其他
        else
        {
            
    this->m_WndStruct.nWndType    = 1;
            
    this->m_WndStruct.dwstyle    =  WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_BORDER;
        }
    }

    // 取得字符串中某特定字符串第一次出现时的指针
    void CProperty::GetSpeCharPtr(char *chValue, char* &chSpe, char* key)
    {
        
    int len = strlen(key);
        chSpe 
    = ::strstr(chValue, key);
        
    if (chSpe == NULL)
        {
            
    return;
        }
        chSpe 
    = chSpe + len;
    }

    // 从配置文件中读取控件的颜色
    void CProperty::GetColor(char *chValue, COLORREF &color, char* key)
    {
        
    char* chSpeValue = NULL;
        
    this->GetSpeCharPtr(chValue, chSpeValue, key);
        
    if (chSpeValue != NULL)
        {        
            
    int r = 0;
            
    int g = 0;
            
    int b = 0;
            sscanf(chSpeValue, (
    "=%d,%d,%d;%*"), &r, &g, &b);
            
    if (r >= 0 && g >= 0 && b >= 0)
            {
                color 
    = RGB(r, g, b);
            }
        }
    }

    // 从配置文件中读取一个字符串
    void CProperty::GetCString(char *chValue, CString &strSrc, char* key)
    {
        
    char* chSpeValue = NULL;
        
    this->GetSpeCharPtr(chValue, chSpeValue, key); 
        
    if (chSpeValue == NULL)
        {
            
    return;
        }

        
    char chBmpPath[512];
        ::memset(chBmpPath, 
    0512);
        ::sscanf(chSpeValue, (
    "=%[^;]s;%*"), chBmpPath);
        ToolKit::TrimForCharArray(chBmpPath);
        strSrc.Format(
    "%s", chBmpPath);
    }

    // 从配置文件中读取背景图片的信息
    void CProperty::GetImgInfo(char *chValue, int &nCount, int &nIndex)
    {
        nCount 
    = 1;
        nIndex 
    = 1;

        
    char* chSpeValue = NULL;
        
    this->GetSpeCharPtr(chValue, chSpeValue, ItemKey::CONTROL_IMG_NUM);

        
    if (chSpeValue == NULL)
        {
            
    return;
        }
        ::sscanf(chSpeValue, (
    "=%d,%d;%*"), &nCount, &nIndex);
    }

    // 从配置文件中读取一个整数
    void CProperty::GetInt(char *chValue, int &nInt, char* key)
    {
        
    char* chSpeValue = NULL;
        
    this->GetSpeCharPtr(chValue, chSpeValue, key);

        
    if (chSpeValue == NULL)
        {
            
    return;
        }
        ::sscanf(chSpeValue, (
    "=%d;%*"), &nInt);
    }

    // 从配置文件中读取控件的位置信息
    void CProperty::GetRect(char *chValue, RECT &rect, char* key)
    {
        
    char* chSpeValue = NULL;
        
    this->GetSpeCharPtr(chValue, chSpeValue, key);
        
    if (chSpeValue == NULL)
        {
            
    return;
        }
        ::sscanf(chSpeValue, 
    "=%d,%d,%d,%d;%*"&rect.left, &rect.right, &rect.top, &rect.bottom);
    }

    2.控件基类:
    控件基类实现创建控件,画出控件样式,设置控件位置等,并提供一些基础操作。
    头文件代码:

    /*******************************************************************************
    * Copyright (C) 1980-2008 Xumingxsh
    * All rights reserved.
    *
    * 文件名称:    BaseControl.h
    * 创建日期:    2008-04-28
    * 创 建 人:    徐敏荣
    * 说    明:    类BaseControl头文件。
    *-------------------------------------------------------------------------------
    * 版本        日    期        修改人    修改说明
    *-------------------------------------------------------------------------------
    * 1.0.0        2008-04-28    徐敏荣        完成初始版本
    ******************************************************************************
    */


    #if !defined(AFX_BASECONTROL_H__23E10B1F_1547_40AF_BDD1_26F500A1F985__INCLUDED_)
    #define AFX_BASECONTROL_H__23E10B1F_1547_40AF_BDD1_26F500A1F985__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    #include 
    "ToolKit.h"
    #include 
    "Property.h"

    /**=============================================================================
    * 名    称:    <BaseControl>
    * 说    明:     <控件的基类,从配置文件中读取控件属性,并提供控件的基本方法>
    * 接    口:    
    * 调    用:    
    * 编    程:    <徐敏荣>,<2008-04-28>
    *=============================================================================
    */
    class BaseControl  
    {
    private:    
        
    static HWND CreateControl(CreateWndStruct &controlStruct);
    public:
        BaseControl();
        
    virtual ~BaseControl();
    public:
        
    /**------------------------------------------------------------------------------------
        * 名    称:    <SetImg>
        * 说    明:     <设置控件背景图片的索引值>
        * 参    数:        <int nIndex>:<索引值>    
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */
        
    void SetImg(int nIndex);

        
    /**------------------------------------------------------------------------------------
        * 名    称:    <SetText>
        * 说    明:     <设置控件的文本>
        * 参    数:        <CString strText>:<控件文本>    
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */    
        
    void SetText(CString strText);
        
    void SetText(char* chText);

        
    /**------------------------------------------------------------------------------------
        * 名    称:    <ShowWnd>
        * 说    明:     <显示或隐藏控件>
        * 参    数:        <bool isSHOW>:<控件是否显示>    
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */    
        
    void ShowWnd(bool isSHOW);

        
    /**------------------------------------------------------------------------------------
        * 名    称:    <DrawItem>
        * 说    明:     <根据属性画出控件>
        * 参    数:        <WPARAM &wParam>
        *                <LPARAM &lParam>
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */    
        
    void DrawItem(WPARAM &wParam, LPARAM &lParam);

        
    /**------------------------------------------------------------------------------------
        * 名    称:    <CreateControl>
        * 说    明:     <创建控件>
        * 参    数:        <char *chValue>:<存储配置信息的字符串>    
        *                <char* key>:<主键>
        *                <HWND hWnd>:<控件句柄>    
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */    
        
    void CreateControl(char* chValue, char* key, HWND hWnd);

        
    /**------------------------------------------------------------------------------------
        * 名    称:    <OnMove>
        * 说    明:     <设置控件位置>
        * 参    数:        
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */    
        
    void OnMove();
    private:    
        
    /**------------------------------------------------------------------------------------
        * 名    称:    <SetTitleText>
        * 说    明:     <设置控件的文本>
        * 参    数:        <HWND hWnd>:<控件句柄>    
        *                <TCHAR* chLabel>:<文本内容>
        *                <HFONT hFont>:<字体样式>    
        *                <UINT textFormat>:<文本样式>
        *                <COLORREF rgb>:<字体颜色>    
        * 返    回:    
        *---------------------------------------------------------------------------------------
    */
        
    void SetTitleText(HWND hWnd, TCHAR* chLabel, 
                                         HFONT hFont, UINT textFormat, COLORREF rgb);
        
        
    /**------------------------------------------------------------------------------------
        * 名    称:    <DrawBitBmp>
        * 说    明:     <添加控件的背景图片>
        * 参    数:     <HWND hwnd>:<控件句柄>
        *               <CString strFilePath>:<图片路径>
        *                <int nCount>:<图片索引总数>
        *               <int nIndex>:<当前索引>
        * 返    回:
        *---------------------------------------------------------------------------------------
    */
        
    void DrawBitBmp(HWND hwnd, CString strFilePath, int nCount, int nIndex);    

    public:
        
    bool IsCurContrl(int nID);
        
    void SetEnable(bool isAble);
        CString GetText();
        
    int                m_nID;                // 控件ID
        HWND            m_hWnd;                // 控件句柄
        HWND            m_parentWnd;        // 父控件的句柄
        CProperty        m_CProperty;
    };

    #endif // !defined(AFX_BASECONTROL_H__23E10B1F_1547_40AF_BDD1_26F500A1F985__INCLUDED_)

    实现文件代码:

    /*******************************************************************************
    * Copyright (C) 1980-2008 Xumingxsh
    * All rights reserved.
    *
    * 文件名称:    BaseControl.cpp
    * 创建日期:    2008-04-28
    * 创 建 人:    徐敏荣
    * 说    明:    类BaseControl的实现文件
    *-------------------------------------------------------------------------------
    * 版本        日    期        修改人    修改说明
    *-------------------------------------------------------------------------------
    * 1.0.0        2008-04-28    徐敏荣        完成初始版本
    ******************************************************************************
    */

    #include 
    "stdafx.h"
    #include 
    "BaseControl.h"

    #include 
    "LogFile.h"
    #include 
    "ItemKey.h"

    #include 
    "ImgLib.h"

    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    HWND BaseControl::CreateControl(CreateWndStruct 
    &controlStruct)
    {
        
    return ::CreateWindowEx(
                controlStruct.dwExStyle,
                controlStruct.lpszClass,
                controlStruct.lpszName,
                controlStruct.style,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                controlStruct.hwndParent,
                controlStruct.hMenu,
                controlStruct.hInstance,
                controlStruct.lpCreateParams);
    }

    // 构造函数
    BaseControl::BaseControl()
    {
    }

    // 析构函数
    BaseControl::~BaseControl()
    {
    }

    // 设置控件的位置
    void BaseControl::OnMove()
    {
        ::MoveWindow(
    this->m_hWnd, this->m_CProperty.m_Rect.left, this->m_CProperty.m_Rect.top, 
            
    this->m_CProperty.m_Rect.right - this->m_CProperty.m_Rect.left,    
            
    this->m_CProperty.m_Rect.bottom - this->m_CProperty.m_Rect.top, true);
    }

    // 创建控件的位置
    void BaseControl::CreateControl(char* chValue, char* key, HWND hParentWnd)
    {
        
    this->m_CProperty.SetValue(chValue, key);

        
    this->m_parentWnd = hParentWnd;

        
    this->m_CProperty.m_WndStruct.hwndParent = this->m_parentWnd;
        
    this->m_CProperty.m_WndStruct.hMenu = (HMENU)this->m_nID;

        
    this->m_hWnd = BaseControl::CreateControl(this->m_CProperty.m_WndStruct);

        
    // 如果不是文本框,且文本不为空,则设置控件文本
        if (this->m_CProperty.m_strText != "")
        {
            ::SetWindowText(
    this->m_hWnd, TEXT(this->m_CProperty.m_strText));
        }
    }

    // 画出控件
    void BaseControl::DrawItem(WPARAM &wParam, LPARAM &lParam)
    {
        LPDRAWITEMSTRUCT        pdis;
        pdis 
    = (LPDRAWITEMSTRUCT) lParam;

        TCHAR szTemp[MAX_PATH];

        
    // 背景图片
        if (this->m_CProperty.m_strImgSrc != "")
        {
            
    if (this->m_CProperty.m_nImgCount == 0)
            {
                
    this->m_CProperty.m_nImgCount = 1;
            }

            
    if (this->m_CProperty.m_nImgIndex == 0)
            {
                
    this->m_CProperty.m_nImgIndex = 1;
            }

            
    // 将取得的图片添加到控件上
            this->DrawBitBmp(pdis->hwndItem, this->m_CProperty.m_strImgSrc,
                
    this->m_CProperty.m_nImgCount, this->m_CProperty.m_nImgIndex);
        }
        
    else
        {
            
    // 设置背景颜色
            FillRect(pdis->hDC, &pdis->rcItem, CreateSolidBrush(this->m_CProperty.m_BgColor));

            
    // 如果控件无背景图片,则用户选择了控件后,背景颜色改变
            switch(pdis->itemState)
            {
            
    case ODS_FOCUS:
                {
                    ::FillRect(pdis
    ->hDC, &pdis->rcItem, CreateSolidBrush(this->m_CProperty.m_FocColor));
                }
                
    break;
            
    case ODS_DISABLED:
                {
                    ::FillRect(pdis
    ->hDC, &pdis->rcItem,  CreateSolidBrush(this->m_CProperty.m_EnbColor));
                }
                
    break;
            }
        }

        
    // 设置控件的字体
        if (GetWindowText(pdis->hwndItem, szTemp, MAX_PATH) > 0)
        {
            
    this->SetTitleText(pdis->hwndItem, szTemp, NULL, 
                
    this->m_CProperty.m_TextFormat, this->m_CProperty.m_ForColor);
        }

        
    if (!this->m_CProperty.m_bIsShow)
        {
            ::ShowWindow(pdis
    ->hwndItem, SW_HIDE);
        }
        
    else
        {
            ::ShowWindow(pdis
    ->hwndItem, SW_SHOW);
        }
    }

    // 给控件设置图片
    void BaseControl::DrawBitBmp(HWND hwnd, CString strFilePath, int nCount, int nIndex)
    {
        
    if (nIndex < 1)
        {
            nIndex 
    = 1;
        }

        HBITMAP hBmp;
        CBMPLib::LoadMap(strFilePath, hBmp);

        
    if (hBmp)
        {
            BITMAP bmp;
            ::GetObject(hBmp, 
    sizeof(BITMAP), &bmp);

            
    long nWidth;
            
    if (nCount < 1)
            {
                nCount 
    = 1;
            }
            nWidth 
    = bmp.bmWidth/nCount;

            RECT rect;    
            rect.left 
    = (nIndex - 1)*nWidth;
            rect.top 
    = 0;
            rect.right
    = nWidth;
            rect.bottom 
    = bmp.bmHeight;

            RECT rc;
            rc.left 
    = 0;
            rc.right 
    = 0;
            rc.top 
    = 0;
            rc.bottom 
    = 0;
            ::GetClientRect(hwnd, 
    &rc);    
            HDC hDC 
    = ::GetDC(hwnd);

            HDC hMemoryDC 
    = ::CreateCompatibleDC(hDC);

            HBITMAP hOldBitMap 
    = (HBITMAP)::SelectObject(hMemoryDC, hBmp);
            
            ::StretchBlt(hDC,    
    00, rc.right,    rc.bottom, hMemoryDC,
                rect.left, rect.top, rect.right, rect.bottom, SRCCOPY);

            
    //开辟了内存,用完一定要记得释放
            ::SelectObject(hMemoryDC, hOldBitMap);        
            ::ReleaseDC(hwnd, hDC);
        }
    }

    // 设置控件字体
    void BaseControl::SetTitleText(HWND hWnd, TCHAR* chLabel, 
                                   HFONT hFont, UINT textFormat, COLORREF rgb)
    {
        
    if (!*chLabel || strcmp(chLabel, ""== 0)
        {
            
    return;
        }
        
        
    //显示文字的区域
        RECT        rt,rect;
        ::GetClientRect(hWnd, 
    &rt);
        ::SetRect(
    &rect, rt.left, rt.top, rt.right, rt.bottom);
        
        
    // 设置字体
        HFONT        hOldFont;
        
    if (this->m_CProperty.m_bIsBold)
        {
            
    this->m_CProperty.m_logfont.lfWeight = 700;
        }
        
    else
        {
            
    this->m_CProperty.m_logfont.lfWeight = 0;
        }
        hFont 
    = ::CreateFontIndirect(&this->m_CProperty.m_logfont);
        HDC            dc 
    = ::GetDC(hWnd);

        
    if(hFont != NULL)//使用配置的字体,否则使用默认的字体
        {
            hOldFont 
    = (HFONT)::SelectObject(dc, hFont);
        }

        
    //背景透明效果
        ::SetBkMode(dc,TRANSPARENT);

        
    //文字的颜色
        COLORREF    hOldColor; 
        hOldColor 
    = ::SetTextColor(dc, rgb);
        ::DrawText( dc, chLabel, 
    -1&rect, textFormat);

        
    //释放对象
        ::SetTextColor(dc, hOldColor);
        ::SelectObject(dc, hOldFont);    
        ::ReleaseDC(hWnd,dc);
    }

    // 显示或隐藏控件
    void BaseControl::ShowWnd(bool isSHOW)
    {
       
    if (isSHOW && !this->m_CProperty.m_bIsShow)
       {
           
    this->m_CProperty.m_bIsShow = true;
           ::ShowWindow(m_hWnd, SW_SHOW);
       }

       
    if (!isSHOW && this->m_CProperty.m_bIsShow)
       {
           
    this->m_CProperty.m_bIsShow = false;
           ::ShowWindow(m_hWnd, SW_HIDE);
       }
    }

    // 设置控件文本
    void BaseControl::SetText(CString strText)
    {
        
    this->m_CProperty.m_strText = strText;
        ::SetWindowText(
    this->m_hWnd, TEXT(this->m_CProperty.m_strText));
    }

    void BaseControl::SetText(char* chText)
    {
        CString strText(chText);
        
    this->SetText(strText);
    }

    // 设置控件背景图片
    void BaseControl::SetImg(int nIndex)
    {
        
    if (nIndex <= this->m_CProperty.m_nImgCount)
        {
            
    this->m_CProperty.m_nImgIndex = nIndex;

            
    // 将取得的图片添加到控件上
            this->DrawBitBmp(this->m_hWnd, this->m_CProperty.m_strImgSrc, 
                
    this->m_CProperty.m_nImgCount, this->m_CProperty.m_nImgIndex);
        }
    }

    CString BaseControl::GetText()
    {
        TCHAR chInput[MAX_PATH] 
    = {0};
        ::GetWindowText(
    this->m_hWnd, chInput, MAX_PATH);
        
    return CString(chInput);
    }

    void BaseControl::SetEnable(bool isAble)
    {
        
    this->m_CProperty.m_bIsEnable = !isAble;
        ::EnableWindow(
    this->m_hWnd, isAble);
    }

    bool BaseControl::IsCurContrl(int nID)
    {
        
    if (nID == this->m_nID)
        {
            
    return true;
        }
        
    return false;
    }



     

     

  • 相关阅读:
    ASP.NET中常用的附件上传下载
    C#中导出Excel的常用方式
    ASP.NET中AjaxPro.dll的简单应用
    在ASP.NET中使用FusionCharts图表
    ASP.NET中使用MagicAjax.dll
    FusionCharts图表导出
    C#中经常注入的一些Javascript代码
    CodeSmith3.2(.net2.0)教程
    您未必知道的Css技巧
    Web Service简介
  • 原文地址:https://www.cnblogs.com/admin11/p/1204891.html
Copyright © 2011-2022 走看看