Advertisement
alien_fx_fiend

Dictionary GUI-Based Win32 (Now /w ListBox Double-Click Definitions!!!!) **FINISHED PROJECT** BESTPR

Jul 30th, 2024 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.66 KB | None | 0 0
  1. realtime-search.cpp file::
  2. #include <Windows.h>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. #include <algorithm>
  7. #include <vector>
  8. #include "resource2.h"
  9.  
  10. using namespace std;
  11.  
  12. // Structure to represent a dictionary entry
  13. struct DictionaryEntry {
  14.     wstring word;
  15.     wstring definition;
  16. };
  17.  
  18. // Structure to represent a node in the linked list
  19. struct Node {
  20.     DictionaryEntry data;
  21.     Node* next;
  22. };
  23.  
  24. Node* dictionary = nullptr;
  25. vector<wstring> words;
  26.  
  27. // Function prototypes
  28. void insertNode(Node*& head, const DictionaryEntry& entry);
  29. //void addWord(Node*& head, const wstring& word, const wstring& definition);
  30. bool addWord(Node*& head, const wstring& word, const wstring& definition);
  31. void deleteWord(Node*& head, const wstring& keyword);
  32. wstring InputBox(HWND hwnd, const wstring& prompt, const wstring& title);
  33. void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition);
  34. void editWord(Node* head, const wstring& oldWord, const wstring& newWord, const wstring& newDefinition);
  35. void listWords(const Node* head, vector<wstring>& words);
  36. void serializeDictionary(const Node* head, const wstring& filename);
  37. void deserializeDictionary(Node*& head, const wstring& filename);
  38. void searchWordRealtime(const Node* head, const wstring& keyword, vector<wstring>& suggestions);
  39. int pruneDictionary(Node*& head);
  40. void updateDictionaryAfterPrune(Node*& head, vector<wstring>& words, HWND hwnd);
  41. void updateListBox(HWND hwnd, const vector<wstring>& words);
  42.  
  43. // Function implementations
  44. void insertNode(Node*& head, const DictionaryEntry& entry) {
  45.     Node* newNode = new Node{ entry, nullptr };
  46.     if (!head) {
  47.         head = newNode;
  48.     }
  49.     else {
  50.         Node* current = head;
  51.         while (current->next) {
  52.             current = current->next;
  53.         }
  54.         current->next = newNode;
  55.     }
  56. }
  57.  
  58. bool addWord(Node*& head, const wstring& word, const wstring& definition) {
  59.     // Check if the word already exists
  60.     Node* current = head;
  61.     while (current) {
  62.         if (current->data.word == word) {
  63.             return false; // Word already exists, don't add
  64.         }
  65.         current = current->next;
  66.     }
  67.  
  68.     // Word doesn't exist, add it
  69.     insertNode(head, { word, definition });
  70.     return true;
  71. }
  72. // w/o duplicates
  73. /* // Modify the addWord function to check for duplicates
  74. bool addWord(Node*& head, const wstring& word, const wstring& definition) {
  75.     // Check if the word already exists
  76.     Node* current = head;
  77.     while (current) {
  78.         if (current->data.word == word) {
  79.             return false; // Word already exists, don't add
  80.         }
  81.         current = current->next;
  82.     }
  83.  
  84.     // Word doesn't exist, add it
  85.     insertNode(head, { word, definition });
  86.     return true;
  87. }*/
  88. // /w duplicates
  89. /*void addWord(Node*& head, const wstring& word, const wstring& definition) {
  90.     insertNode(head, { word, definition });
  91. }*/
  92.  
  93. void deleteWord(Node*& head, const wstring& keyword) {
  94.     if (!head) return;
  95.  
  96.     if (head->data.word == keyword) {
  97.         Node* temp = head;
  98.         head = head->next;
  99.         delete temp;
  100.         return;
  101.     }
  102.  
  103.     Node* current = head;
  104.     while (current->next && current->next->data.word != keyword) {
  105.         current = current->next;
  106.     }
  107.  
  108.     if (current->next) {
  109.         Node* temp = current->next;
  110.         current->next = current->next->next;
  111.         delete temp;
  112.     }
  113. }
  114.  
  115. // Modify the InputBox function to pre-populate the input
  116. wstring InputBox(HWND hwnd, const wstring& prompt, const wstring& title, const wstring& defaultValue = L"") {
  117.     wstring input = defaultValue;
  118.     DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_INPUT), hwnd,
  119.         [](HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -> INT_PTR {
  120.             static wstring* pInput;
  121.             switch (message) {
  122.             case WM_INITDIALOG:
  123.                 pInput = (wstring*)lParam;
  124.                 SetDlgItemText(hDlg, IDC_INPUT, pInput->c_str());
  125.                 SendDlgItemMessage(hDlg, IDC_INPUT, EM_SETSEL, 0, -1);  // Select all text
  126.                 return (INT_PTR)TRUE;
  127.             case WM_COMMAND:
  128.                 if (LOWORD(wParam) == IDOK) {
  129.                     HWND hEdit = GetDlgItem(hDlg, IDC_INPUT);
  130.                     int len = GetWindowTextLength(hEdit) + 1;
  131.                     wchar_t* buffer = new wchar_t[len];
  132.                     GetWindowText(hEdit, buffer, len);
  133.                     *pInput = buffer;
  134.                     delete[] buffer;
  135.                     EndDialog(hDlg, IDOK);
  136.                     return (INT_PTR)TRUE;
  137.                 }
  138.                 else if (LOWORD(wParam) == IDCANCEL) {
  139.                     EndDialog(hDlg, IDCANCEL);
  140.                     return (INT_PTR)TRUE;
  141.                 }
  142.                 break;
  143.             }
  144.             return (INT_PTR)FALSE;
  145.         }, (LPARAM)&input);
  146.     return input;
  147. }
  148. /*wstring InputBox(HWND hwnd, const wstring& prompt, const wstring& title) {
  149.     wchar_t input[156] = L"";
  150.     if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_INPUT), hwnd,
  151.         [](HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -> INT_PTR {
  152.             static wchar_t input[156];
  153.             switch (message) {
  154.             case WM_INITDIALOG: {
  155.                 HWND hEdit = GetDlgItem(hDlg, IDC_INPUT);
  156.                 SetWindowText(hEdit, (LPCWSTR)lParam);
  157.                 SendMessage(hEdit, EM_SETSEL, 0, -1); // Select all text
  158.                 break;
  159.             }
  160.             case WM_COMMAND:
  161.                 if (LOWORD(wParam) == IDOK) {
  162.                     GetDlgItemText(hDlg, IDC_INPUT, input, 156);
  163.                     EndDialog(hDlg, IDOK);
  164.                     return (INT_PTR)TRUE;
  165.                 }
  166.                 else if (LOWORD(wParam) == IDCANCEL) {
  167.                     EndDialog(hDlg, IDCANCEL);
  168.                     return (INT_PTR)TRUE;
  169.                 }
  170.                 break;
  171.             }
  172.             return (INT_PTR)FALSE;
  173.         }, (LPARAM)prompt.c_str()) == IDOK) {
  174.         return (wstring)input;
  175.     }
  176.     return L"";
  177. }*/
  178.  
  179. void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition) {
  180.     wstring message = L"Definition of '" + word + L"':\n\n" + definition;
  181.     HWND hDlg = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DEFINITION), hwnd, [](HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -> INT_PTR {
  182.         switch (message) {
  183.         case WM_INITDIALOG:
  184.         {
  185.             wstring* definition = (wstring*)lParam;
  186.             SetDlgItemText(hDlg, IDC_DEFINITION_EDIT, definition->c_str());
  187.             delete definition;
  188.         }
  189.         return (INT_PTR)TRUE;
  190.         case WM_COMMAND:
  191.             if (LOWORD(wParam) == IDCANCEL) {
  192.                 EndDialog(hDlg, LOWORD(wParam));
  193.                 return (INT_PTR)TRUE;
  194.             }
  195.             break;
  196.         case WM_CLOSE:
  197.             EndDialog(hDlg, 0);
  198.             return (INT_PTR)TRUE;
  199.         }
  200.         return (INT_PTR)FALSE;
  201.         }, (LPARAM)new wstring(message));
  202.     ShowWindow(hDlg, SW_SHOW);
  203. }
  204. /*void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition) {
  205.     wstring message = L"Definition of '" + word + L"':\n\n" + definition;
  206.     MessageBox(hwnd, message.c_str(), L"Word Definition", MB_OK | MB_ICONINFORMATION);
  207. }*/
  208.  
  209. void editWord(Node* head, const wstring& oldWord, const wstring& newWord, const wstring& newDefinition) {
  210.     Node* current = head;
  211.     while (current) {
  212.         if (current->data.word == oldWord) {
  213.             current->data.word = newWord;
  214.             current->data.definition = newDefinition;
  215.             return;
  216.         }
  217.         current = current->next;
  218.     }
  219. }
  220.  
  221. void listWords(const Node* head, vector<wstring>& words) {
  222.     words.clear();
  223.     const Node* current = head;
  224.     while (current) {
  225.         words.push_back(current->data.word);
  226.         current = current->next;
  227.     }
  228. }
  229.  
  230. void serializeDictionary(const Node* head, const wstring& filename) {
  231.     wofstream file(filename);
  232.     const Node* current = head;
  233.     while (current) {
  234.         file << current->data.word << L";" << current->data.definition << endl;
  235.         current = current->next;
  236.     }
  237.     file.close();
  238. }
  239.  
  240. void deserializeDictionary(Node*& head, const wstring& filename) {
  241.     wifstream file(filename);
  242.     wstring line;
  243.     while (getline(file, line)) {
  244.         size_t pos = line.find(L';');
  245.         if (pos != wstring::npos) {
  246.             wstring word = line.substr(0, pos);
  247.             wstring definition = line.substr(pos + 1);
  248.             addWord(head, word, definition);
  249.         }
  250.     }
  251.     file.close();
  252. }
  253.  
  254. // Add this function to convert a string to lowercase
  255. wstring toLower(const wstring& str) {
  256.     wstring lower = str;
  257.     transform(lower.begin(), lower.end(), lower.begin(), ::towlower);
  258.     return lower;
  259. }
  260.  
  261. // Modify the searchWordRealtime function to be case-insensitive
  262. void searchWordRealtime(const Node* head, const wstring& keyword, vector<wstring>& suggestions) {
  263.     suggestions.clear();
  264.     const Node* current = head;
  265.     wstring lowercaseKeyword = toLower(keyword);
  266.     while (current) {
  267.         wstring lowercaseWord = toLower(current->data.word);
  268.         if (lowercaseWord.find(lowercaseKeyword) != wstring::npos) {
  269.             suggestions.push_back(current->data.word);
  270.         }
  271.         current = current->next;
  272.     }
  273. }
  274.  
  275. // Prune Functions
  276. int pruneDictionary(Node*& head) {
  277.     if (!head) return 0;
  278.  
  279.     int duplicatesRemoved = 0;
  280.     Node* current = head;
  281.  
  282.     while (current && current->next) {
  283.         Node* runner = current;
  284.         while (runner->next) {
  285.             if (current->data.word == runner->next->data.word) {
  286.                 Node* duplicate = runner->next;
  287.                 runner->next = runner->next->next;
  288.                 delete duplicate;
  289.                 duplicatesRemoved++;
  290.             }
  291.             else {
  292.                 runner = runner->next;
  293.             }
  294.         }
  295.         current = current->next;
  296.     }
  297.  
  298.     return duplicatesRemoved;
  299. }
  300.  
  301. // Prune Functions
  302. void updateDictionaryAfterPrune(Node*& head, vector<wstring>& words, HWND hwnd) {
  303.     listWords(head, words);
  304.     updateListBox(hwnd, words);
  305.     serializeDictionary(head, L"myDictionary99x.txt");
  306. }
  307.  
  308. void updateListBox(HWND hwnd, const vector<wstring>& words) {
  309.     HWND hListBox = GetDlgItem(hwnd, IDC_LISTBOX);
  310.     SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
  311.     for (const auto& word : words) {
  312.         SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)word.c_str());
  313.     }
  314. }
  315.  
  316. // Dialog procedure
  317. // Modify the DialogProc function
  318. INT_PTR CALLBACK DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  319.     switch (uMsg) {
  320.     case WM_INITDIALOG:
  321.         deserializeDictionary(dictionary, L"myDictionary99x.txt");
  322.         listWords(dictionary, words);
  323.         updateListBox(hwnd, words);
  324.         return TRUE;
  325.  
  326.     case WM_COMMAND:
  327.         // Start Double-Click View ListBox Definitions function block
  328.         switch (HIWORD(wParam)) {
  329.         case LBN_DBLCLK:
  330.             if (LOWORD(wParam) == IDC_LISTBOX) {
  331.                 int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  332.                 if (index != LB_ERR) {
  333.                     wchar_t word[256];
  334.                     SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
  335.  
  336.                     // Find the definition for the selected word
  337.                     Node* current = dictionary;
  338.                     while (current) {
  339.                         if (current->data.word == word) {
  340.                             DisplayDefinition(hwnd, word, current->data.definition);
  341.                             break;
  342.                         }
  343.                         current = current->next;
  344.                     }
  345.                 }
  346.                 return TRUE;
  347.             }
  348.             break;
  349.         }
  350.         // End Double-Click View ListBox Definitions function block
  351.         // Handle other controls
  352.         switch (LOWORD(wParam)) {
  353.         case IDC_ADD_WORD: {
  354.             wchar_t word[256], definition[1024];
  355.             GetDlgItemText(hwnd, IDC_WORD_INPUT, word, 256);
  356.             GetDlgItemText(hwnd, IDC_DEFINITION_INPUT, definition, 1024);
  357.             if (wcslen(word) > 0) {
  358.                 if (!addWord(dictionary, word, definition)) {
  359.                     // Word already exists, show confirmation box
  360.                     wstring message = L"An entry with the name '";
  361.                     message += word;
  362.                     message += L"' already exists. Do you want to add a duplicate entry?";
  363.                     int result = MessageBox(hwnd, message.c_str(), L"Confirm Duplicate Entry", MB_YESNO | MB_ICONQUESTION);
  364.  
  365.                     if (result == IDYES) {
  366.                         // User confirmed, add the duplicate entry
  367.                         insertNode(dictionary, { word, definition });
  368.                         listWords(dictionary, words);
  369.                         updateListBox(hwnd, words);
  370.                         serializeDictionary(dictionary, L"myDictionary99x.txt");
  371.                         SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  372.                         SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  373.                     }
  374.                     // If result is IDNO, do nothing (silently ignore)
  375.                 }
  376.                 else {
  377.                     // Word added successfully
  378.                     listWords(dictionary, words);
  379.                     updateListBox(hwnd, words);
  380.                     serializeDictionary(dictionary, L"myDictionary99x.txt");
  381.                     SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  382.                     SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  383.                 }
  384.             }
  385.             return TRUE;
  386.         }
  387.         // w/o duplicates
  388.         /*case IDC_ADD_WORD: {
  389.             wchar_t word[256], definition[1024];
  390.             GetDlgItemText(hwnd, IDC_WORD_INPUT, word, 256);
  391.             GetDlgItemText(hwnd, IDC_DEFINITION_INPUT, definition, 1024);
  392.             if (wcslen(word) > 0) {
  393.                 if (addWord(dictionary, word, definition)) {
  394.                     listWords(dictionary, words);
  395.                     updateListBox(hwnd, words);
  396.                     serializeDictionary(dictionary, L"myDictionary99x.txt");
  397.                     SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  398.                     SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  399.                 }
  400.                 // If addWord returns false, it means the word already exists.
  401.                 // We silently ignore it as per the requirement.
  402.             }
  403.             return TRUE;
  404.         }*/
  405.         // /w duplicates        
  406.         /*case IDC_ADD_WORD: {
  407.             wchar_t word[256], definition[1024];
  408.             GetDlgItemText(hwnd, IDC_WORD_INPUT, word, 256);
  409.             GetDlgItemText(hwnd, IDC_DEFINITION_INPUT, definition, 1024);
  410.             if (wcslen(word) > 0) {  // Only check if word is not empty
  411.                 addWord(dictionary, word, definition);
  412.                 listWords(dictionary, words);
  413.                 updateListBox(hwnd, words);
  414.                 serializeDictionary(dictionary, L"myDictionary99x.txt");
  415.                 SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  416.                 SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  417.             }
  418.             return TRUE;
  419.         }*/
  420.  
  421.         case IDC_DELETE_WORD: {
  422.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  423.             if (index != LB_ERR) {
  424.                 wchar_t word[256];
  425.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
  426.  
  427.                 // Create the confirmation message
  428.                 wstring message = L"Are you sure you want to delete the word '";
  429.                 message += word;
  430.                 message += L"'?";
  431.  
  432.                 // Show the confirmation box
  433.                 int result = MessageBox(hwnd, message.c_str(), L"Confirm Deletion", MB_YESNO | MB_ICONQUESTION);
  434.  
  435.                 // If the user clicks "Yes", proceed with deletion
  436.                 if (result == IDYES) {
  437.                     deleteWord(dictionary, word);
  438.                     listWords(dictionary, words);
  439.                     updateListBox(hwnd, words);
  440.                     serializeDictionary(dictionary, L"myDictionary99x.txt");
  441.                 }
  442.             }
  443.             return TRUE;
  444.         }
  445.  
  446.         case IDC_EDIT_WORD: {
  447.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  448.             if (index != LB_ERR) {
  449.                 wchar_t wordToEdit[256];
  450.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)wordToEdit);
  451.                 Node* current = dictionary;
  452.                 while (current) {
  453.                     if (current->data.word == wordToEdit) {
  454.                         wstring newWord = InputBox(hwnd, L"Edit Word", L"Edit Word", current->data.word);
  455.                         if (!newWord.empty()) {
  456.                             wstring newDefinition = InputBox(hwnd, L"Edit Definition", L"Edit Definition", current->data.definition);
  457.                             // newDefinition can be empty now
  458.                             SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_DELETESTRING, index, 0); // Delete the old word
  459.                             editWord(dictionary, wordToEdit, newWord, newDefinition);
  460.                             listWords(dictionary, words);
  461.                             updateListBox(hwnd, words);
  462.                             // fix for C++ best practices "<" Signed/ Unsigned Mismatch
  463.                             for (size_t i = 0; i < words.size(); i++) {
  464.                             //for (int i = 0; i < words.size(); i++) {
  465.                                 if (words[i] == newWord) {
  466.                                     SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_SETCURSEL, static_cast<WPARAM>(i), 0); // Select the edited word
  467.                                     // fix for C++ best practices "<" Signed Unsigned Mismatch
  468.                                     //SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_SETCURSEL, i, 0); // Select the edited word
  469.                                     break;
  470.                                 }
  471.                             }
  472.                             serializeDictionary(dictionary, L"myDictionary99x.txt");
  473.                         }
  474.                         break;
  475.                     }
  476.                     current = current->next;
  477.                 }
  478.                 if (!current) {
  479.                     MessageBox(hwnd, L"Word not found in the dictionary.", L"Error", MB_OK | MB_ICONERROR);
  480.                 }
  481.             }
  482.             return TRUE;
  483.         }
  484.         // /w duplicates (does not affect duplicates tho only no check for blank definitions
  485.         /*case IDC_EDIT_WORD: {
  486.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  487.             if (index != LB_ERR) {
  488.                 wchar_t wordToEdit[256];
  489.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)wordToEdit);
  490.                 Node* current = dictionary;
  491.                 while (current) {
  492.                     if (current->data.word == wordToEdit) {
  493.                         wstring newWord = InputBox(hwnd, L"Edit Word", L"Edit Word", current->data.word);
  494.                         if (!newWord.empty()) {
  495.                             wstring newDefinition = InputBox(hwnd, L"Edit Definition", L"Edit Definition", current->data.definition);
  496.                             SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_DELETESTRING, index, 0); // Delete the old word
  497.                             editWord(dictionary, wordToEdit, newWord, newDefinition);
  498.                             listWords(dictionary, words);
  499.                             updateListBox(hwnd, words);
  500.                             for (int i = 0; i < words.size(); i++) {
  501.                                 if (words[i] == newWord) {
  502.                                     SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_SETCURSEL, i, 0); // Select the edited word
  503.                                     break;
  504.                                 }
  505.                             }
  506.                             serializeDictionary(dictionary, L"myDictionary99x.txt");
  507.                         }
  508.                         break;
  509.                     }
  510.                     current = current->next;
  511.                 }
  512.                 if (!current) {
  513.                     MessageBox(hwnd, L"Word not found in the dictionary.", L"Error", MB_OK | MB_ICONERROR);
  514.                 }
  515.             }
  516.             return TRUE;
  517.         }*/
  518.  
  519.         case IDC_VIEW_DEFINITION: {
  520.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  521.             if (index != LB_ERR) {
  522.                 wchar_t word[256];
  523.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
  524.                 Node* current = dictionary;
  525.                 while (current) {
  526.                     if (current->data.word == word) {
  527.                         DisplayDefinition(hwnd, current->data.word, current->data.definition);
  528.                         break;
  529.                     }
  530.                     current = current->next;
  531.                 }
  532.             }
  533.             return TRUE;
  534.         }
  535.  
  536.         case IDC_SEARCH_INPUT: {
  537.             if (HIWORD(wParam) == EN_CHANGE) {
  538.                 wchar_t query[256];
  539.                 GetDlgItemText(hwnd, IDC_SEARCH_INPUT, query, 256);
  540.                 if (wcslen(query) == 0) {
  541.                     updateListBox(hwnd, words);
  542.                 }
  543.                 else {
  544.                     vector<wstring> suggestions;
  545.                     searchWordRealtime(dictionary, query, suggestions);
  546.                     updateListBox(hwnd, suggestions);
  547.                 }
  548.             }
  549.             return TRUE;
  550.         }
  551.  
  552.         case IDC_CLEAR_SEARCH: {
  553.             SetDlgItemText(hwnd, IDC_SEARCH_INPUT, L"");
  554.             updateListBox(hwnd, words);
  555.             return TRUE;
  556.         }
  557.  
  558.         //Prune Functions
  559.         case IDC_PRUNE: {
  560.             int result = MessageBox(hwnd, L"Are you sure you want to prune the list and remove duplicates?", L"Confirm Prune", MB_YESNO | MB_ICONQUESTION);
  561.             if (result == IDYES) {
  562.                 int duplicatesRemoved = pruneDictionary(dictionary);
  563.                 updateDictionaryAfterPrune(dictionary, words, hwnd);
  564.  
  565.                 wstring message = L"Pruning complete. ";
  566.                 message += to_wstring(duplicatesRemoved);
  567.                 message += L" duplicate entries were removed.";
  568.                 MessageBox(hwnd, message.c_str(), L"Prune Results", MB_OK | MB_ICONINFORMATION);
  569.             }
  570.             return TRUE;
  571.         }
  572.  
  573.         case IDCANCEL:
  574.             DestroyWindow(hwnd);
  575.             PostQuitMessage(0);
  576.             return TRUE;
  577.         }      
  578.         break;
  579.  
  580.     case WM_CLOSE:
  581.         DestroyWindow(hwnd);
  582.         PostQuitMessage(0);
  583.         return TRUE;
  584.     }
  585.     return FALSE;
  586. }
  587.  
  588. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
  589.     HWND hDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), 0, DialogProc, 0);
  590.     if (!hDlg) {
  591.         MessageBox(NULL, L"Failed to create dialog", L"Error", MB_ICONERROR | MB_OK);
  592.         return 1;
  593.     }
  594.  
  595.     ShowWindow(hDlg, nCmdShow);
  596.  
  597.     MSG msg;
  598.     BOOL bRet;
  599.     while ((bRet = GetMessage(&msg, nullptr, 0, 0)) != 0) {
  600.         if (bRet == -1) {
  601.             // Handle the error and possibly exit
  602.             break;
  603.         }
  604.         else if (!IsWindow(hDlg) || !IsDialogMessage(hDlg, &msg)) {
  605.             TranslateMessage(&msg);
  606.             DispatchMessage(&msg);
  607.         }
  608.     }
  609.  
  610.     return (int)msg.wParam;
  611. }
  612.  
  613. Resource2.rc file::
  614. #include <windows.h>
  615. #include "resource2.h"
  616.  
  617. IDD_MAINDIALOG DIALOGEX 0, 0, 320, 240
  618. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  619. CAPTION "Dictionary Application"
  620. FONT 8, "MS Shell Dlg", 400, 0, 0x1
  621. BEGIN
  622. LTEXT           "Word:", -1, 10, 10, 30, 8
  623. EDITTEXT        IDC_WORD_INPUT, 50, 8, 100, 14, ES_AUTOHSCROLL
  624. LTEXT           "Definition:", -1, 10, 30, 40, 8
  625. EDITTEXT        IDC_DEFINITION_INPUT, 50, 28, 200, 14, ES_AUTOHSCROLL
  626. PUSHBUTTON      "Add Word", IDC_ADD_WORD, 260, 8, 50, 14
  627. PUSHBUTTON      "Delete Word", IDC_DELETE_WORD, 260, 28, 50, 14
  628. PUSHBUTTON      "Edit Word", IDC_EDIT_WORD, 260, 48, 50, 14
  629. PUSHBUTTON      "View Definition", IDC_VIEW_DEFINITION, 205, 8, 55, 14
  630. PUSHBUTTON      "Prune", IDC_PRUNE, 160, 8, 25, 14
  631. PUSHBUTTON      "Clear", IDC_CLEAR_SEARCH, 185, 8, 20, 14
  632. LTEXT           "Search:", -1, 10, 50, 30, 8
  633. EDITTEXT        IDC_SEARCH_INPUT, 50, 48, 200, 14, ES_AUTOHSCROLL
  634. LISTBOX         IDC_LISTBOX, 10, 70, 300, 160, LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
  635. END
  636.  
  637. IDD_INPUT DIALOGEX 0, 0, 250, 120
  638. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  639. CAPTION "Input"
  640. FONT 8, "MS Shell Dlg", 400, 0, 0x1
  641. BEGIN
  642. LTEXT           "Input:", -1, 7, 14, 50, 8
  643. EDITTEXT        IDC_INPUT, 7, 30, 230, 20, ES_AUTOHSCROLL
  644. DEFPUSHBUTTON   "OK", IDOK, 70, 60, 50, 14
  645. PUSHBUTTON      "Cancel", IDCANCEL, 130, 60, 50, 14
  646. END
  647.  
  648. IDD_DEFINITION DIALOGEX 0, 0, 300, 200
  649. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  650. CAPTION "Definition"
  651. FONT 8, "MS Shell Dlg"
  652. BEGIN
  653. EDITTEXT        IDC_DEFINITION_EDIT, 7, 7, 286, 186, ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_VSCROLL
  654. END
  655.  
  656. /*IDD_INPUT DIALOGEX 0, 0, 200, 100
  657. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  658. CAPTION "Input"
  659. FONT 8, "MS Shell Dlg"
  660. BEGIN
  661. LTEXT           "Input:", -1, 7, 14, 50, 8
  662. EDITTEXT        IDC_INPUT, 7, 24, 186, 12, ES_AUTOHSCROLL
  663. DEFPUSHBUTTON   "OK", IDOK, 139, 7, 50, 14
  664. PUSHBUTTON      "Cancel", IDCANCEL, 139, 24, 50, 14
  665. END*/
  666.  
  667. //EDITTEXT        IDC_WORD_INPUT, 50, 8, 100, 14, ES_AUTOHSCROLL
  668. //PUSHBUTTON      "Add Word", IDC_ADD_WORD, 260, 8, 50, 14
  669. //PUSHBUTTON      "View Definition", IDC_VIEW_DEFINITION, 260, 68, 50, 14
  670.  
  671. resource2.h file::
  672. #ifndef RESOURCE2_H
  673. #define RESOURCE2_H
  674.  
  675. #define IDD_MAINDIALOG                  101
  676. #define IDC_WORD_INPUT                  1001
  677. #define IDC_DEFINITION_INPUT            1002
  678. #define IDC_ADD_WORD                    1003
  679. #define IDC_DELETE_WORD                 1004
  680. #define IDC_SEARCH_INPUT                1005
  681. #define IDC_LISTBOX                     1006
  682. #define IDC_INPUT                       1007  // Add this line if you're using IDC_INPUT
  683. #define IDC_EDIT_WORD                   1008
  684. #define IDC_VIEW_DEFINITION             1009
  685. #define IDD_INPUT                       1010
  686. #define IDD_DEFINITION                  1011
  687. #define IDC_DEFINITION_EDIT             1012
  688. #define IDC_CLEAR_SEARCH                1013
  689. #define IDC_PRUNE                       1014
  690.  
  691. #endif // RESOURCE2_H
  692.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement