Advertisement
Combreal

dropDownList.cpp

Apr 2nd, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. #include <windows.h>
  2. #include <string>
  3. #include "commctrl.h"
  4. #include <fstream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <tchar.h>
  8.  
  9.  
  10. #define ID_BUTTON 1
  11. #define ID_DROPBOX 2
  12.  
  13. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  14. HANDLE hwndButton, hwndDropbox;
  15. bool isInTheVectorc(std::string passedString, std::vector<std::string>& passed_vector);
  16. void CenterWindow(HWND);
  17. std::string lpwstrToString(LPWSTR var);
  18. LPWSTR stringToLPWSTR(const std::string& instr);
  19. std::ifstream groups("groups_ldap_primary");
  20. std::string shareGroup;
  21.  
  22. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  23. {
  24.         MSG msg;    
  25.         WNDCLASSW wc = {0};
  26.         wc.lpszClassName = L"test";
  27.         wc.hInstance = hInstance;
  28.         wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  29.         wc.lpfnWndProc = WndProc;
  30.         wc.hCursor = LoadCursor(0, IDC_ARROW);
  31.         RegisterClassW(&wc);
  32.         CreateWindowW(wc.lpszClassName, L"Test106", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 260, 118, 231, 235, 0, 0, hInstance, 0);  
  33.         while(GetMessage(&msg, NULL, 0, 0))
  34.         {
  35.             TranslateMessage(&msg);
  36.             DispatchMessage(&msg);
  37.         }
  38.         return (int) msg.wParam;
  39. }
  40.  
  41. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  42. {
  43.     //shareGroup = "ABS";
  44.     int counter = 0;
  45.     std::vector<std::string> groupList;
  46.     std::string passedGroup;
  47.     TCHAR ListItem[256];
  48.     switch(msg)
  49.     {
  50.     case WM_KEYDOWN:
  51.         if (wParam == VK_ESCAPE)
  52.         {
  53.             int ret = MessageBoxW(hwnd, L"Woot?", L"Message", MB_OKCANCEL);                            
  54.             if (ret == IDOK)
  55.             {
  56.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  57.             }
  58.         }    
  59.         break;
  60.     case WM_CREATE:
  61.         CenterWindow(hwnd);
  62.         hwndButton=CreateWindowW(L"Button", L"Click", WS_VISIBLE|WS_CHILD, 50, 50, 61, 21, hwnd, (HMENU)ID_BUTTON, NULL, NULL);
  63.         hwndDropbox = CreateWindow(WC_COMBOBOX, TEXT(""), CBS_DROPDOWN | CBS_HASSTRINGS | WS_VSCROLL | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE, 10, 10, 200, 200, hwnd, (HMENU)ID_DROPBOX, NULL, NULL);
  64.         while(getline(groups, passedGroup, '\n'))
  65.         {
  66.             std::transform(passedGroup.begin(), passedGroup.end(),passedGroup.begin(), ::toupper);
  67.             groupList.push_back(passedGroup);
  68.             SendMessageW(GetDlgItem(hwnd, ID_DROPBOX), (UINT)CB_ADDSTRING, (WPARAM)counter, (LPARAM)stringToLPWSTR(passedGroup));
  69.             counter++;
  70.         }
  71.         groups.close();
  72.         SendMessage(GetDlgItem(hwnd, ID_DROPBOX), (UINT)CB_SETCURSEL, (WPARAM)0, (LPARAM)0);
  73.         break;  
  74.     case WM_COMMAND:
  75.         if(LOWORD(wParam)==ID_BUTTON)
  76.         {
  77.             if(shareGroup == "")
  78.             {
  79.                 shareGroup = "ABS";
  80.             }
  81.             MessageBox(hwnd, stringToLPWSTR(shareGroup), TEXT("équipe"), MB_OK);
  82.         }
  83.         if(HIWORD(wParam) == CBN_SELCHANGE)
  84.         {
  85.             shareGroup.clear();
  86.             int ItemIndex = SendMessage(GetDlgItem(hwnd, ID_DROPBOX), (UINT)CB_GETCURSEL, (WPARAM)0, (LPARAM)0);
  87.             (TCHAR) SendMessage(GetDlgItem(hwnd, ID_DROPBOX), (UINT) CB_GETLBTEXT, (WPARAM) ItemIndex, (LPARAM)ListItem);
  88.             OutputDebugString(ListItem);
  89.             shareGroup = lpwstrToString(ListItem);
  90.         }
  91.         break;
  92.     case WM_DESTROY:  
  93.         PostQuitMessage(0);
  94.         break;
  95.     }
  96.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  97. }
  98.  
  99. void CenterWindow(HWND hwnd)
  100. {
  101.     RECT rc = {0};
  102.     GetWindowRect(hwnd, &rc);
  103.     int win_w = rc.right - rc.left;
  104.     int win_h = rc.bottom - rc.top;
  105.     int screen_w = GetSystemMetrics(SM_CXSCREEN);
  106.     int screen_h = GetSystemMetrics(SM_CYSCREEN);
  107.     SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w)/2, (screen_h - win_h)/2, 0, 0, SWP_NOSIZE);
  108. }
  109.  
  110. std::string lpwstrToString(LPWSTR input)
  111. {
  112.     int cSize = WideCharToMultiByte (CP_ACP, 0, input, wcslen(input), NULL, 0, NULL, NULL);
  113.     std::string output(static_cast<size_t>(cSize), '\0');
  114.     WideCharToMultiByte (CP_ACP, 0, input, wcslen(input), reinterpret_cast<char*>(&output[0]), cSize, NULL, NULL);
  115.     return output;
  116. }
  117.  
  118. LPWSTR stringToLPWSTR(const std::string& instr)
  119. {
  120.     int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
  121.     LPWSTR widestr = new WCHAR[bufferlen + 1];
  122.     ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
  123.     widestr[bufferlen] = 0;
  124.     return widestr;
  125. }
  126.  
  127. bool isInTheVectorc(std::string passedString, std::vector<std::string>& passedVector)
  128. {
  129.     bool isInTheVector=false;
  130.     for(size_t i=0, size=passedVector.size(); i<size; ++i)
  131.     {
  132.         std::transform(passedString.begin(), passedString.end(), passedString.begin(), ::tolower);
  133.         if(passedVector.at(i)==passedString)
  134.         {
  135.             isInTheVector = true;
  136.             break;
  137.         }
  138.     }
  139.     return isInTheVector;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement