zoukankan      html  css  js  c++  java
  • WIN32API 自定义颜色下拉列表控件

    效果如下:

    原创-转载请著名来源

    1,新建颜色属性类“CNColor”:

    class CNColor
    {
    public:
        COLORREF    m_crColor;        //颜色RGB值
        WCHAR        m_cColor[32];        //颜色名
        CNColor(COLORREF cr, WCHAR * crStr);
        ~CNColor();
    };
    
    CNColor::CNColor(COLORREF cr, WCHAR * crStr)
    {
    this->m_crColor = cr;
    lstrcpyn(m_cColor, crStr, 32);
    }
    
    CNColor::~CNColor()
    {
    }

    2,新建颜色列表控件类“CNColorList”:

    class CNColorList
    {
    public:
        HWND m_mHwnd;
        list<CNColor*> m_mColors;  //颜色列表
    private:
      //初始化颜色列表
    void InitalColorArray(); public: CNColorList(); ~CNColorList(); //创建控件 int Creat( HWND hwnd, //父窗口句柄 HINSTANCE hinst, //程序全局句柄 UINT uid, //控件号 RECT rec //控件外框大小 ); //控件消息处理 static LRESULT CALLBACK NColorListProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); //绘制子项 void DrawItem(HDC hdc, int itmId, RECT rec); //获取字符串 CNColor* GetColorById(int nid); };

    3,新建类指针类“CNColorListPtr”

    // 保存类指针
    class CNColorListPtr
    {
    public:
        UINT mUid;                    //ID号
        CNColorList *pColorList = NULL;    //颜色列表指针
        WNDPROC mProc;                    //应用程序句柄
    public:
        CNColorListPtr(UINT uid,CNColorList* ptr, WNDPROC proc);
        ~CNColorListPtr();
    };
    
    CNColorListPtr::CNColorListPtr(UINT uid, CNColorList* ptr, WNDPROC proc)
    {
        mUid = uid;
        pColorList = ptr;
        mProc = proc;
    }

    4,初始化颜色列表

    //初始化颜色列表
    void CNColorList::InitalColorArray()
    {
        m_mColors.push_back(new CNColor(RGB(0xF0, 0xF8, 0xFF), L"AliceBlue"));
        m_mColors.push_back(new CNColor(RGB(0xFA, 0xEB, 0xD7), L"AntiqueWhite"));
        m_mColors.push_back(new CNColor(RGB(0x00, 0xFF, 0xFF), L"Aqua"));
        m_mColors.push_back(new CNColor(RGB(0x7F, 0xFF, 0xD4), L"Aquamarine"));
        m_mColors.push_back(new CNColor(RGB(0xF0, 0xFF, 0xFF), L"Azure"));
        m_mColors.push_back(new CNColor(RGB(0xF5, 0xF5, 0xDC), L"Beige"));
        m_mColors.push_back(new CNColor(RGB(0xFF, 0xE4, 0xC4), L"Bisque"));
        m_mColors.push_back(new CNColor(RGB(0x00, 0x00, 0x00), L"Black"));
    }

    5,创建控件窗口

    // 创建ColorList
    int CNColorList::Creat(HWND hwnd, HINSTANCE hinst, UINT uid, RECT rec)
    {
        if (m_mColors.size() == 0)
            InitalColorArray();
        m_mHwnd = CreateWindow(L"combobox", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST | CBS_HASSTRINGS | CBS_OWNERDRAWFIXED,
            rec.left, rec.top, rec.right-rec.left, rec.bottom-rec.top, hwnd, (HMENU)uid, hinst, NULL);
        WNDPROC nProc = (WNDPROC)SetWindowLong(hwnd, GWL_WNDPROC, (LONG)NColorListProc);    // 绑定控件事件处理程序
        CNColorListPtr *mptr = new CNColorListPtr(uid, this, nProc);
        SetWindowLong(hwnd, GWL_USERDATA, (LONG)mptr);        // 保存当前窗口指针
        list<CNColor*>::iterator ite;
        for (ite = m_mColors.begin(); ite != m_mColors.end(); ite++)
        {
            //add string
            ComboBox_AddString(
                m_mHwnd,
                (LPARAM)((CNColor*)(*ite))->m_cColor
            );
        }
        return 0;
    }

    6,定义消息处理:

    // 消息处理
    LRESULT CALLBACK CNColorList::NColorListProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        CNColorListPtr* ptr = (CNColorListPtr*)GetWindowLong(hwnd, GWL_USERDATA);    //获取窗口指针
        WNDPROC nProc = ptr->mProc;
        int idx;
        WCHAR str[32];
        switch (msg)
        {
        case WM_DRAWITEM:
        {
            if (wParam == ptr->mUid)
            {
                ptr->pColorList->DrawItem(
                    ((LPDRAWITEMSTRUCT)lParam)->hDC,
                    ((LPDRAWITEMSTRUCT)lParam)->itemID,
                    ((LPDRAWITEMSTRUCT)lParam)->rcItem
                );
            }
            break;
        }
        default:
            break;
        }
        return CallWindowProc(nProc, hwnd, msg, wParam, lParam);    //传递消息给主窗口
    }

    7,绘制下拉表ITEM

        

    // 绘制子项
    void CNColorList::DrawItem(HDC hdc, int itmId, RECT rec)
    {
        if (itmId == -1) return;
        CNColor *mc = GetColorById(itmId);
        // 创建钢笔      
        HPEN pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
        HBRUSH brush = CreateSolidBrush(mc->m_crColor);
        SelectObject(hdc, pen);
        SelectObject(hdc, brush);
        RECT nrec = {
            rec.left + 1,
            rec.top + 1,
            (rec.right + rec.left)*0.3,
            rec.bottom - 1
        };
        FillRect(hdc, &nrec, brush);
        Rectangle(hdc, nrec.left, nrec.top, nrec.right, nrec.bottom);
        RECT trec = {
            (rec.right + rec.left)*0.3 + 4,
            rec.top,
            rec.right - 2,
            rec.bottom
        };
        SetTextColor(hdc, RGB(0, 0, 0));
        DrawText(hdc, mc->m_cColor, -1, &trec, DT_LEFT);
        DeleteObject(pen);
        DeleteObject(brush);
    }

    8,在主程序中调用控件

      8.1 定义控件ID   

      #define IDB_CRLIST 8000

      8.2 新建控件对象  

    CNColorList nCr = CNColorList();

      8.3 在消息WM_CREAT中创建控件

    case WM_CREATE:
            nCr.Creat(
                hwnd, 
                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                IDB_CRLIST,
                { 20,20,160,180 }
            );
            break;

    原创-转载请著名来源

  • 相关阅读:
    开放GIS标准OGC之路(4)之 解密Filter(摘抄)
    在WCF中使用async/await 关键字的简化的异步编程模型(译)
    WinCE仿真器设置
    oracle PL/SQL编程详解
    oracle 日期格式
    钢笔工具使用教程
    使用Signature Tool自动生成P/Invoke调用Windows API的C#函数声明
    利用bitmap将图片部分颜色透明
    PS圆角图片并保留透明背景
    Script Debugger的使用
  • 原文地址:https://www.cnblogs.com/visio/p/5726444.html
Copyright © 2011-2022 走看看