Win32API a custom color drop-down list control

Source: Internet
Author: User

The effect is as follows:

Original-Reproduced please famous source

1, new Color attribute class "Cncolor":

class cncolor{public:    colorref    m_crcolor;         // color RGB Values    WCHAR        m_ccolor[];        // Color name    Cncolor (COLORREF cr, WCHAR * crstr);     ~* crstr) {this->m_crcolor =+);} Cncolor::~Cncolor () {}

2, New color List control class "Cncolorlist":

classcncolorlist{ Public: HWND M_mhwnd; List<CNColor*>m_mcolors; Color ListPrivate:
Initialize the color listvoidInitalcolorarray (); Public: Cncolorlist (); ~cncolorlist (); //Creating Controls intCreat (HWND hwnd,//parent window handleHInstance Hinst,//Program Global handleUINT uid,//Control NumberRECT Rec//control outer frame size ); //Control Message Handling StaticLRESULT CALLBACK Ncolorlistproc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM); //Draw the child voidDrawItem (HDC hdc,intItmid, RECT rec); //Get Stringcncolor* Getcolorbyid (intnid);};

3, new class pointer class "Cncolorlistptr"

//Save class pointersclasscncolorlistptr{ Public: UINT muid; //ID NumberCncolorlist *pcolorlist = NULL;//Color list PointerWNDPROC 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 listvoidCncolorlist::initalcolorarray () {M_mcolors.push_back (NewCncolor (RGB (0xF0,0xF8,0xFF), L"AliceBlue")); M_mcolors.push_back (NewCncolor (RGB (0xFA,0xEB,0xD7), L"Antiquewhite")); M_mcolors.push_back (NewCncolor (RGB (0x00,0xFF,0xFF), L"Aqua")); M_mcolors.push_back (NewCncolor (RGB (0x7F,0xFF,0xd4), L"Aquamarine")); M_mcolors.push_back (NewCncolor (RGB (0xF0,0xFF,0xFF), L"Azure")); M_mcolors.push_back (NewCncolor (RGB (0xf5,0xf5,0xDC), L"Beige")); M_mcolors.push_back (NewCncolor (RGB (0xFF,0xE4,0xc4), L"Bisque")); M_mcolors.push_back (NewCncolor (RGB (0x00,0x00,0x00), L"Black"));}

5, creating the control window

//Create ColorListintCncolorlist::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);//bound control event handlersCncolorlistptr *mptr =NewCncolorlistptr (UID, This, Nproc);        SetWindowLong (hwnd, Gwl_userdata, (LONG) mptr); //saves the current window pointerList<cncolor*>:: iterator ite;  for(ite = M_mcolors.begin (); ITE! = M_mcolors.end (); ite++)    {        //Add Stringcombobox_addstring (M_mhwnd, (LPARAM) (Cncolor*) (*ite))M_ccolor); }    return 0;}

6, define Message processing:

//Message ProcessingLRESULT CALLBACK Cncolorlist::ncolorlistproc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) {cncolorlistptr* ptr = (cncolorlistptr*) GetWindowLong (hwnd, gwl_userdata);//Get window pointerWNDPROC Nproc = ptr->Mproc; intidx; WCHAR str[ +]; Switch(msg) { CaseWm_drawitem: {if(WParam = = ptr->muid) {ptr->pcolorlist->DrawItem ((lpdrawitemstruct) lParam)-HDC, ((lpdrawitemstruct) lParam)-ItemID, ((lpdrawitemstruct) lParam)-RcItem); }         Break; }    default:         Break; }    returnCallWindowProc (Nproc, hwnd, MSG, WParam, LParam);//passing messages to the main window}

7. Draw the drop-down list item

//Draw the childvoidCncolorlist::D Rawitem (HDC hdc,intItmid, RECT rec) {    if(Itmid = =-1)return; Cncolor*MC =Getcolorbyid (Itmid); //Create a penHpen 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, invoking the control in the main program

8.1 Defining the control ID

#define Idb_crlist 8000

8.2 New Control Object

Cncolorlist nCr = Cncolorlist ();

8.3 Creating a control in message wm_creat

 Case wm_create:        ncr.creat (            hwnd,             (HINSTANCE) GetWindowLong (hwnd, gwl_hinstance),            idb_crlist,             (a         );          break;

Original-Reproduced please famous source

Win32API a custom color drop-down list control

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.