程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> WIN32API 自定義顏色下拉列表控件,win32api控件

WIN32API 自定義顏色下拉列表控件,win32api控件

編輯:C++入門知識

WIN32API 自定義顏色下拉列表控件,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;

原創-轉載請著名來源

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved