016 cmobobox

Post date: 2015/1/27 上午 08:42:30

#include <windows.h> #include <CommCtrl.h> #include <ctime> #include <cstdlib> using namespace std; #pragma comment(lib, "comctl32.lib") class static_control; class combobox_control { private: HINSTANCE instance_handle; wchar_t * window_class_name; HWND parent_handle; HWND combobox_handle; static_control * text; public: combobox_control(HINSTANCE instance, HWND parent, static_control * text_area) : instance_handle(instance), parent_handle(parent), text(text_area) { combobox_handle = CreateWindowEx(WS_EX_CLIENTEDGE, L"Combobox", NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_VSCROLL | CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED | CBS_HASSTRINGS | CBS_SORT, 20, 20, 200, 200, parent_handle, NULL, instance_handle, NULL); SetWindowLongPtr(combobox_handle, GWL_USERDATA, (LONG)this); } static LRESULT combobox_window_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { combobox_control * This = (combobox_control *)GetWindowLongPtr(hwnd, GWL_USERDATA); return DefWindowProc(hwnd, message, wparam, lparam); } void add_item(wchar_t * string) { int index = (int)SendMessage(combobox_handle, CB_INSERTSTRING, -1, (LPARAM)string); if ( index == 0 ) { SendMessage(combobox_handle, CB_SETCURSEL, index, 0); } } void measure_item(LPMEASUREITEMSTRUCT item) { item->itemHeight = 20; item->itemWidth = 200; } void draw_item(LPDRAWITEMSTRUCT item) { wchar_t string[120]; if ( item->itemID == -1 ) return; SendMessage(combobox_handle, CB_GETLBTEXT, item->itemID, (LPARAM)string); int length = 0; wchar_t * s = string; while ( *s++ != '\t' ) length++; COLORREF back_colour; if ( item->itemState & ODS_SELECTED ) { back_colour = RGB(0, 0, 255); HBRUSH back_brush = CreateSolidBrush(back_colour); FillRect(item->hDC, &item->rcItem, back_brush); DeleteObject(back_brush); SetBkColor(item->hDC, back_colour); SetTextColor(item->hDC, RGB(255, 255, 255)); } else { back_colour = RGB(255, 255, 255); HBRUSH back_brush = CreateSolidBrush(back_colour); FillRect(item->hDC, &item->rcItem, back_brush); DeleteObject(back_brush); SetBkColor(item->hDC, back_colour); SetTextColor(item->hDC, RGB(0, 0, 0)); } DrawText(item->hDC, string, length, &item->rcItem, DT_LEFT|DT_SINGLELINE); } void new_selection(); }; class static_control { private: HINSTANCE instance_handle; wchar_t * window_class_name; HWND parent_handle; HWND static_handle; public: static_control(HINSTANCE instance, HWND parent) : instance_handle(instance), parent_handle(parent) { static_handle = CreateWindowEx(0, L"Static", NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN, 230, 24, 200, 22, parent_handle, NULL, instance_handle, NULL); SetWindowLongPtr(static_handle, GWL_USERDATA, (LONG)this); } static LRESULT combobox_window_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { static_control * This = (static_control *)GetWindowLongPtr(hwnd, GWL_USERDATA); return DefWindowProc(hwnd, message, wparam, lparam); } void set_text(wchar_t * string) { SetWindowText(static_handle, string); } }; class frame_window { private: LPWSTR window_class_name; HINSTANCE instance_handle; HCURSOR cursor_arrow; HWND window_handle; HWND header_handle; RECT client_rectangle; combobox_control * combobox; static_control * text; public: frame_window(LPWSTR window_class_identity) : window_class_name(window_class_identity) { int screen_width = GetSystemMetrics(SM_CXFULLSCREEN); int screen_height = GetSystemMetrics(SM_CYFULLSCREEN); instance_handle = GetModuleHandle(NULL); WNDCLASS window_class = { CS_OWNDC, main_window_proc, 0, 0, instance_handle, NULL, NULL, NULL, NULL, window_class_name }; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Create a standard frame window //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ RegisterClass(&window_class); window_handle = CreateWindowEx(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE, window_class_name, L"Combobox Control Example", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, (screen_width-900)/2, (screen_height-700)/2, 900, 700, NULL, NULL, instance_handle, NULL); SetWindowLongPtr(window_handle, GWL_USERDATA, (LONG)this); GetClientRect(window_handle, &client_rectangle); int width = client_rectangle.right - client_rectangle.left; int height = client_rectangle.bottom - client_rectangle.top; text = new static_control(instance_handle, window_handle); combobox = new combobox_control(instance_handle, window_handle, text); SetCursor(LoadCursor(NULL, IDC_ARROW)); ShowWindow(window_handle, SW_SHOW); UpdateWindow(window_handle); } ~frame_window() { UnregisterClass(window_class_name, instance_handle); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Windows message processing function //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ static LRESULT WINAPI main_window_proc(HWND window_handle, UINT message, WPARAM wparam, LPARAM lparam) { frame_window *This = (frame_window *)GetWindowLongPtr(window_handle, GWL_USERDATA); switch ( message ) { case WM_PAINT: { PAINTSTRUCT paint_structure; RECT client_rect; HDC paint_device_context, paint_dc; HBITMAP bitmap; paint_device_context = BeginPaint(window_handle, &paint_structure); paint_dc = CreateCompatibleDC(paint_device_context); GetClientRect(window_handle, &client_rect); int window_width = client_rect.right - client_rect.left; int window_height = client_rect.bottom - client_rect.top; bitmap = CreateBitmap(window_width, window_height, 1, 32, NULL); HGDIOBJ old_bitmap = SelectObject(paint_dc, bitmap); // Fill the client aread with the user selected face colour HBRUSH light_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE)); FillRect(paint_dc, &paint_structure.rcPaint, light_brush); DeleteObject(light_brush); BitBlt(paint_device_context, 0, 0, client_rect.right-client_rect.left, client_rect.bottom-client_rect.top, paint_dc, 0, 0, SRCCOPY); SelectObject(paint_dc, old_bitmap); DeleteObject(bitmap); DeleteDC(paint_dc); EndPaint(window_handle, &paint_structure); return 0; } case WM_ERASEBKGND: { return TRUE; } case WM_SIZE: { InvalidateRect(window_handle, NULL, TRUE); return 0; } case WM_MEASUREITEM: { This->combobox->measure_item((LPMEASUREITEMSTRUCT)lparam); return TRUE; } case WM_DRAWITEM: { This->combobox->draw_item((LPDRAWITEMSTRUCT)lparam); return TRUE; } case WM_COMMAND: { if ( HIWORD(wparam) == CBN_SELCHANGE ) { This->combobox->new_selection(); } return 0; } case WM_CLOSE: { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // The user wants to close the window //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ PostQuitMessage(0); return 0; } } return DefWindowProc(window_handle, message, wparam, lparam); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Windows message loop //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void run() { MSG window_message; while ( GetMessage(&window_message, NULL, 0, 0) ) { TranslateMessage(&window_message); DispatchMessage(&window_message); } } void add_item(wchar_t * string) { combobox->add_item(string); } }; void combobox_control::new_selection() { wchar_t string[120]; int selection = (int)SendMessage(combobox_handle, CB_GETCURSEL, 0, 0); if ( selection != CB_ERR ) { SendMessage(combobox_handle, CB_GETLBTEXT, selection, (LPARAM)string); int length = 0; wchar_t * s = string; while ( *s++ != '\t' ) length++; text->set_text(s); } } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Windows main entry point //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ int WINAPI wWinMain(HINSTANCE instance_handle, HINSTANCE, LPWSTR, INT) { frame_window main_window(L"my base window"); main_window.add_item(L"mark.bottomley\tExpert"); main_window.add_item(L"megamu\tExpert"); main_window.add_item(L"Bench\tExpert"); main_window.add_item(L"Cerolobo\tExpert"); main_window.add_item(L"CyanBlue\tExpert"); main_window.add_item(L"DilutedImage\tExpert"); main_window.add_item(L"g00se\tExpert"); main_window.add_item(L"gregoryH\tExpert"); main_window.add_item(L"ajwsurfer\tMentor"); main_window.add_item(L"baavgai\tMentor"); main_window.add_item(L"CTphpnwb\tMentor"); main_window.add_item(L"Dogstopper\tMentor"); main_window.add_item(L"eclipsed4utoo\tMentor"); main_window.add_item(L"Gloin\tMentor"); main_window.add_item(L"gregwhitworth\tExpert"); main_window.add_item(L"GWatt\tExpert"); main_window.add_item(L"KevinADC\tExpert"); main_window.add_item(L"mostyfriedman\tExpert"); main_window.add_item(L"n8wxs\tExpert"); main_window.add_item(L"perfectly.insane\tExpert"); main_window.add_item(L"letthecolorsrumble\tExpert"); main_window.add_item(L"lockdown\tExpert"); main_window.add_item(L"Martyn.Rae\tMentor"); main_window.add_item(L"Martyr2\tMentor"); main_window.add_item(L"pbl\tMentor"); main_window.add_item(L"PennyBoki\tMentor"); main_window.add_item(L"Programmist\tMentor"); main_window.add_item(L"macosxnerd101\tExpert"); main_window.add_item(L"mocker\tExpert"); main_window.add_item(L"MorphiusFaydal\tExpert"); main_window.add_item(L"thava\tExpert"); main_window.add_item(L"Ancient Dragon\tExpert"); main_window.add_item(L"EdwinNameless\tExpert"); main_window.add_item(L"erik.price\tExpert"); main_window.add_item(L"wildgoose\tExpert"); main_window.add_item(L"FlashM\tExpert"); main_window.add_item(L"Fuzzyness\tExpert"); main_window.add_item(L"Arbitrator\tExpert"); main_window.add_item(L"malkiri\tExpert"); main_window.add_item(L"red01\tExpert"); main_window.add_item(L"MentalFloss\tExpert"); main_window.add_item(L"japanir\tExpert"); main_window.add_item(L"joeyadms\tExpert"); main_window.add_item(L"mikeblas\tExpert"); main_window.add_item(L"reyco1\tExpert"); main_window.add_item(L"RodgerB\tExpert"); main_window.add_item(L"runtime error\tExpert"); main_window.add_item(L"ScottAdams\tExpert"); main_window.add_item(L"ShaneK\tExpert"); main_window.add_item(L"SwiftStriker00\tExpert"); main_window.add_item(L"horace\tExpert"); main_window.add_item(L"janotte\tExpert"); main_window.add_item(L"akozlik\tExpert"); main_window.add_item(L"Voodoo Doll\tExpert"); main_window.add_item(L"WolfCoder\tExpert"); main_window.run(); return 0; }