WIN32API custom color drop-down list control, win32api Control

Source: Internet
Author: User

WIN32API custom color drop-down list control, win32api Control

The effect is as follows:

Original-Reprinted from famous sources

1. Create a Color Attribute Class "CNColor ":

Class CNColor {public: COLORREF m_crColor; // color RGB value WCHAR m_cColor [32]; // color name CNColor (COLORREF cr, WCHAR * crStr );~ CNColor () ;}; CNColor: CNColor (COLORREF cr, WCHAR * crStr) {this-> m_crColor = cr; lstrcpyn (m_cColor, crStr, 32);} CNColor ::~ CNColor (){}

2. Create a color list control class "CNColorList ":

Class CNColorList {public: HWND m_mHwnd; list <CNColor *> m_mColors; // color list private:
// Initialize the color list void InitalColorArray (); public: CNColorList ();~ CNColorList (); // create the control int Creat (HWND hwnd, // HINSTANCE hinst, // global UINT uid, // control number RECT rec // control box size); // Control Message Processing static lresult callback NColorListProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ); // draw the subitem void DrawItem (HDC hdc, int itmId, RECT rec); // obtain the string CNColor * GetColorById (int nid );};

 

3. Create a class pointer class "CNColorListPtr"

// Save class pointer class CNColorListPtr {public: UINT mUid; // ID CNColorList * pColorList = NULL; // color list pointer WNDPROC mProc; // application handle public: CNColorListPtr (UINT uid, CNColorList * ptr, WNDPROC proc );~ CNColorListPtr () ;}; CNColorListPtr: CNColorListPtr (UINT uid, CNColorList * ptr, WNDPROC proc) {mUid = uid; pColorList = ptr; mProc = proc ;}

 

4. initialize the color list.

// Initialize the color list 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. Create a control window

// Create ColorListint 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 (hwn D, GWL_WNDPROC, (LONG) NColorListProc); // bind the control event handler CNColorListPtr * mptr = new CNColorListPtr (uid, this, nProc); SetWindowLong (hwnd, GWL_USERDATA, (LONG) mptr); // Save the current window pointer 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. Define message processing:

// Message Processing lresult callback CNColorList: NColorListProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {CNColorListPtr * ptr = (CNColorListPtr *) GetWindowLong (hwnd, GWL_USERDATA ); // obtain the window pointer 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 ); // send the message to the main window}

 

7. Draw the drop-down table ITEM

// Draw the subitem void CNColorList: DrawItem (HDC hdc, int itmId, RECT rec) {if (itmId =-1) return; CNColor * mc = GetColorById (itmId ); // create pen 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. Call the control in the main program

8.1 define the Control ID

  #define IDB_CRLIST 8000

8.2 create a control object

CNColorList nCr = CNColorList();

8.3 create a control in message WM_CREAT

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

Original-Reprinted from famous sources

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.