Advertisement
alien_fx_fiend

Dictionary Win32 *FINAL RELEASE !*

Oct 22nd, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 47.14 KB | Source Code | 0 0
  1. ==++"realtime.cpp" File SourceCode++==::
  2. #include <Windows.h>
  3. #include <CommCtrl.h>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <string>
  7. #include <algorithm>
  8. #include <vector>
  9. #include "resource2.h"
  10.  
  11. #pragma comment(lib, "comctl32.lib")
  12.  
  13. using namespace std;
  14.  
  15. // Structure to represent a dictionary entry
  16. struct DictionaryEntry {
  17.     wstring word;
  18.     wstring definition;
  19. };
  20.  
  21. // Structure to represent a node in the linked list
  22. struct Node {
  23.     DictionaryEntry data;
  24.     Node* next;
  25. };
  26.  
  27. Node* dictionary = nullptr;
  28. vector<wstring> words;
  29. WNDPROC oldWordEditProc;
  30. WNDPROC oldDefinitionEditProc;
  31. HHOOK g_hHook = NULL;
  32. HWND g_hDlg = NULL;
  33. HWND g_hDefinitionDlg = NULL; // Add this near where you declare g_hDlg
  34.  
  35. // Function prototypes
  36. //LRESULT CALLBACK EditSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  37. LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
  38. INT_PTR CALLBACK ViewDefinitionDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  39. void AddWordToList(HWND hwnd);
  40. void insertNode(Node*& head, const DictionaryEntry& entry);
  41. //void addWord(Node*& head, const wstring& word, const wstring& definition);
  42. bool addWord(Node*& head, const wstring& word, const wstring& definition);
  43. void deleteWord(Node*& head, const wstring& keyword);
  44. wstring InputBox(HWND hwnd, const wstring& prompt, const wstring& title);
  45. void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition);
  46. void editWord(Node* head, const wstring& oldWord, const wstring& newWord, const wstring& newDefinition);
  47. void listWords(const Node* head, vector<wstring>& words);
  48. void serializeDictionary(const Node* head, const wstring& filename);
  49. void deserializeDictionary(Node*& head, const wstring& filename);
  50. void searchWordRealtime(const Node* head, const wstring& keyword, vector<wstring>& suggestions);
  51. int pruneDictionary(Node*& head);
  52. void updateDictionaryAfterPrune(Node*& head, vector<wstring>& words, HWND hwnd);
  53. void updateListBox(HWND hwnd, const vector<wstring>& words);
  54.  
  55. LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
  56.     if (nCode == HC_ACTION) {
  57.         KBDLLHOOKSTRUCT* pKbdStruct = (KBDLLHOOKSTRUCT*)lParam;
  58.         if (wParam == WM_KEYDOWN) {
  59.             switch (pKbdStruct->vkCode) {
  60.             case VK_RETURN:
  61.                 if (GetFocus() == GetDlgItem(g_hDlg, IDC_WORD_INPUT) ||
  62.                     GetFocus() == GetDlgItem(g_hDlg, IDC_DEFINITION_INPUT)) {
  63.                     AddWordToList(g_hDlg);
  64.                     return 1;
  65.                 }
  66.                 break;
  67.             case VK_ESCAPE:
  68.                 if (GetDlgItem(g_hDlg, IDCANCEL)) {
  69.                     SendMessage(g_hDlg, WM_COMMAND, MAKEWPARAM(IDCANCEL, 0), 0);
  70.                     return TRUE;
  71.                 }
  72.                 else if (GetDlgItem(g_hDlg, IDNO)) {
  73.                     SendMessage(g_hDlg, WM_COMMAND, MAKEWPARAM(IDNO, 0), 0);
  74.                     return TRUE;
  75.                 }
  76.                 else {
  77.                     SendMessage(g_hDlg, WM_CLOSE, 0, 0);
  78.                     return TRUE;
  79.                 }
  80.                 break;
  81.             case VK_F1:
  82.             {
  83.                 if (g_hDefinitionDlg && IsWindowVisible(g_hDefinitionDlg)) {
  84.                     // If the definition window is visible, hide it
  85.                     ShowWindow(g_hDefinitionDlg, SW_HIDE);
  86.                 }
  87.                 else {
  88.                     // If the definition window is not visible or doesn't exist, show/create it
  89.                     HWND hListBox = GetDlgItem(g_hDlg, IDC_LISTBOX);
  90.                     int selectedIndex = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
  91.                     if (selectedIndex != LB_ERR) {
  92.                         wchar_t selectedWord[256];
  93.                         SendMessage(hListBox, LB_GETTEXT, selectedIndex, (LPARAM)selectedWord);
  94.                         Node* current = dictionary;
  95.                         while (current) {
  96.                             if (current->data.word == selectedWord) {
  97.                                 wstring message = L"Definition of '" + current->data.word + L"': \n\n" + current->data.definition;
  98.                                 if (g_hDefinitionDlg) {
  99.                                     // Update existing dialog
  100.                                     SetDlgItemText(g_hDefinitionDlg, IDC_DEFINITION_EDIT, message.c_str());
  101.                                     ShowWindow(g_hDefinitionDlg, SW_SHOW);
  102.                                 }
  103.                                 else {
  104.                                     // Create new dialog
  105.                                     g_hDefinitionDlg = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DEFINITION), g_hDlg, ViewDefinitionDlgProc, (LPARAM)new wstring(message));
  106.                                     ShowWindow(g_hDefinitionDlg, SW_SHOW);
  107.                                 }
  108.                                 break;
  109.                             }
  110.                             current = current->next;
  111.                         }
  112.                     }
  113.                 }
  114.                 return 1;
  115.             }
  116.             /* {
  117.                 if (g_hDefinitionDlg) {
  118.                     if (IsWindowVisible(g_hDefinitionDlg)) {
  119.                         ShowWindow(g_hDefinitionDlg, SW_HIDE);
  120.                     }
  121.                     else {
  122.                         ShowWindow(g_hDefinitionDlg, SW_SHOW);
  123.                     }
  124.                 }
  125.                 else {
  126.                     HWND hListBox = GetDlgItem(g_hDlg, IDC_LISTBOX);
  127.                     int selectedIndex = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
  128.                     if (selectedIndex != LB_ERR) {
  129.                         wchar_t selectedWord[256];
  130.                         SendMessage(hListBox, LB_GETTEXT, selectedIndex, (LPARAM)selectedWord);
  131.                         Node* current = dictionary;
  132.                         while (current) {
  133.                             if (current->data.word == selectedWord) {
  134.                                 g_hDefinitionDlg = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DEFINITION), g_hDlg, ViewDefinitionDlgProc, (LPARAM)new wstring(current->data.definition));
  135.                                 ShowWindow(g_hDefinitionDlg, SW_SHOW);
  136.                                 break;
  137.                             }
  138.                             current = current->next;
  139.                         }
  140.                     }
  141.                 }
  142.                 return 1;
  143.             }*/
  144.             }
  145.         }
  146.     }
  147.     return CallNextHookEx(NULL, nCode, wParam, lParam);
  148. }
  149.  
  150. /*INT_PTR CALLBACK ViewDefinitionDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
  151.     switch (message) {
  152.     case WM_INITDIALOG:
  153.     {
  154.         wstring* definition = (wstring*)lParam;
  155.         SetDlgItemText(hDlg, IDC_DEFINITION_EDIT, definition->c_str());
  156.         delete definition;
  157.         return (INT_PTR)TRUE;
  158.     }
  159.     case WM_COMMAND:
  160.         if (LOWORD(wParam) == IDCANCEL) {
  161.             EndDialog(hDlg, LOWORD(wParam));
  162.             return (INT_PTR)TRUE;
  163.         }
  164.         break;
  165.     case WM_CLOSE:
  166.         EndDialog(hDlg, 0);
  167.         return (INT_PTR)TRUE;
  168.     }
  169.     return (INT_PTR)FALSE;
  170. }*/
  171. INT_PTR CALLBACK ViewDefinitionDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
  172.     switch (message) {
  173.     case WM_INITDIALOG:
  174.     {
  175.         wstring* fullMessage = (wstring*)lParam;
  176.         SetDlgItemText(hDlg, IDC_DEFINITION_EDIT, fullMessage->c_str());
  177.         delete fullMessage;
  178.         return (INT_PTR)TRUE;
  179.     }
  180.     case WM_COMMAND:
  181.         if (LOWORD(wParam) == IDCANCEL) {
  182.             ShowWindow(hDlg, SW_HIDE);
  183.             return (INT_PTR)TRUE;
  184.         }
  185.         break;
  186.     case WM_CLOSE:
  187.         ShowWindow(hDlg, SW_HIDE);
  188.         return (INT_PTR)TRUE;
  189.     }
  190.     return (INT_PTR)FALSE;
  191. }
  192.  
  193. // Function implementations
  194. void insertNode(Node*& head, const DictionaryEntry& entry) {
  195.     Node* newNode = new Node{ entry, nullptr };
  196.     if (!head) {
  197.         head = newNode;
  198.     }
  199.     else {
  200.         Node* current = head;
  201.         while (current->next) {
  202.             current = current->next;
  203.         }
  204.         current->next = newNode;
  205.     }
  206. }
  207.  
  208. bool addWord(Node*& head, const wstring& word, const wstring& definition) {
  209.     // Check if the word already exists
  210.     Node* current = head;
  211.     while (current) {
  212.         if (current->data.word == word) {
  213.             return false; // Word already exists, don't add
  214.         }
  215.         current = current->next;
  216.     }
  217.  
  218.     // Word doesn't exist, add it
  219.     insertNode(head, { word, definition });
  220.     return true;
  221. }
  222. // w/o duplicates
  223. /* // Modify the addWord function to check for duplicates
  224. bool addWord(Node*& head, const wstring& word, const wstring& definition) {
  225.     // Check if the word already exists
  226.     Node* current = head;
  227.     while (current) {
  228.         if (current->data.word == word) {
  229.             return false; // Word already exists, don't add
  230.         }
  231.         current = current->next;
  232.     }
  233.  
  234.     // Word doesn't exist, add it
  235.     insertNode(head, { word, definition });
  236.     return true;
  237. }*/
  238. // /w duplicates
  239. /*void addWord(Node*& head, const wstring& word, const wstring& definition) {
  240.     insertNode(head, { word, definition });
  241. }*/
  242.  
  243. void deleteWord(Node*& head, const wstring& keyword) {
  244.     if (!head) return;
  245.  
  246.     if (head->data.word == keyword) {
  247.         Node* temp = head;
  248.         head = head->next;
  249.         delete temp;
  250.         return;
  251.     }
  252.  
  253.     Node* current = head;
  254.     while (current->next && current->next->data.word != keyword) {
  255.         current = current->next;
  256.     }
  257.  
  258.     if (current->next) {
  259.         Node* temp = current->next;
  260.         current->next = current->next->next;
  261.         delete temp;
  262.     }
  263. }
  264.  
  265. // Modify the InputBox function to pre-populate the input
  266. wstring InputBox(HWND hwnd, const wstring& prompt, const wstring& title, const wstring& defaultValue = L"") {
  267.     wstring input = defaultValue;
  268.     DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_INPUT), hwnd,
  269.         [](HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -> INT_PTR {
  270.             static wstring* pInput;
  271.             switch (message) {
  272.             case WM_INITDIALOG:
  273.                 pInput = (wstring*)lParam;
  274.                 SetDlgItemText(hDlg, IDC_INPUT, pInput->c_str());
  275.                 SendDlgItemMessage(hDlg, IDC_INPUT, EM_SETSEL, 0, -1);  // Select all text
  276.                 return (INT_PTR)TRUE;
  277.             case WM_COMMAND:
  278.                 if (LOWORD(wParam) == IDOK) {
  279.                     HWND hEdit = GetDlgItem(hDlg, IDC_INPUT);
  280.                     int len = GetWindowTextLength(hEdit) + 1;
  281.                     wchar_t* buffer = new wchar_t[len];
  282.                     GetWindowText(hEdit, buffer, len);
  283.                     *pInput = buffer;
  284.                     delete[] buffer;
  285.                     EndDialog(hDlg, IDOK);
  286.                     return (INT_PTR)TRUE;
  287.                 }
  288.                 else if (LOWORD(wParam) == IDCANCEL) {
  289.                     EndDialog(hDlg, IDCANCEL);
  290.                     return (INT_PTR)TRUE;
  291.                 }
  292.                 break;
  293.             }
  294.             return (INT_PTR)FALSE;
  295.         }, (LPARAM)&input);
  296.     return input;
  297. }
  298. /*wstring InputBox(HWND hwnd, const wstring& prompt, const wstring& title) {
  299.     wchar_t input[156] = L"";
  300.     if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_INPUT), hwnd,
  301.         [](HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -> INT_PTR {
  302.             static wchar_t input[156];
  303.             switch (message) {
  304.             case WM_INITDIALOG: {
  305.                 HWND hEdit = GetDlgItem(hDlg, IDC_INPUT);
  306.                 SetWindowText(hEdit, (LPCWSTR)lParam);
  307.                 SendMessage(hEdit, EM_SETSEL, 0, -1); // Select all text
  308.                 break;
  309.             }
  310.             case WM_COMMAND:
  311.                 if (LOWORD(wParam) == IDOK) {
  312.                     GetDlgItemText(hDlg, IDC_INPUT, input, 156);
  313.                     EndDialog(hDlg, IDOK);
  314.                     return (INT_PTR)TRUE;
  315.                 }
  316.                 else if (LOWORD(wParam) == IDCANCEL) {
  317.                     EndDialog(hDlg, IDCANCEL);
  318.                     return (INT_PTR)TRUE;
  319.                 }
  320.                 break;
  321.             }
  322.             return (INT_PTR)FALSE;
  323.         }, (LPARAM)prompt.c_str()) == IDOK) {
  324.         return (wstring)input;
  325.     }
  326.     return L"";
  327. }*/
  328.  
  329. void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition) {
  330.     wstring message = L"Definition of '" + word + L"': \n\n" + definition;
  331.     HWND hDlg = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DEFINITION), hwnd, [](HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -> INT_PTR {
  332.         switch (message) {
  333.         case WM_INITDIALOG:
  334.         {
  335.             wstring* definition = (wstring*)lParam;
  336.             SetDlgItemText(hDlg, IDC_DEFINITION_EDIT, definition->c_str());
  337.             delete definition;
  338.         }
  339.         return (INT_PTR)TRUE;
  340.         case WM_COMMAND:
  341.             if (LOWORD(wParam) == IDCANCEL) {
  342.                 EndDialog(hDlg, LOWORD(wParam));
  343.                 return (INT_PTR)TRUE;
  344.             }
  345.             break;
  346.         case WM_CLOSE:
  347.             EndDialog(hDlg, 0);
  348.             return (INT_PTR)TRUE;
  349.         }
  350.         return (INT_PTR)FALSE;
  351.         }, (LPARAM)new wstring(message));
  352.     ShowWindow(hDlg, SW_SHOW);
  353. }
  354. /*void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition) {
  355.     wstring message = L"Definition of '" + word + L"':\n\n" + definition;
  356.     MessageBox(hwnd, message.c_str(), L"Word Definition", MB_OK | MB_ICONINFORMATION);
  357. }*/
  358.  
  359. void editWord(Node* head, const wstring& oldWord, const wstring& newWord, const wstring& newDefinition) {
  360.     Node* current = head;
  361.     while (current) {
  362.         if (current->data.word == oldWord) {
  363.             current->data.word = newWord;
  364.             current->data.definition = newDefinition;
  365.             return;
  366.         }
  367.         current = current->next;
  368.     }
  369. }
  370.  
  371. void listWords(const Node* head, vector<wstring>& words) {
  372.     words.clear();
  373.     const Node* current = head;
  374.     while (current) {
  375.         words.push_back(current->data.word);
  376.         current = current->next;
  377.     }
  378. }
  379.  
  380. // Delimiter Old Semicolon
  381. void serializeDictionary(const Node* head, const wstring& filename) {
  382.     wofstream file(filename, ios::out | ios::trunc);
  383.     if (!file.is_open()) {
  384.         MessageBox(NULL, L"Failed to open file for writing", L"Error", MB_OK | MB_ICONERROR);
  385.         return;
  386.     }
  387.     const Node* current = head;
  388.     while (current) {
  389.         file << current->data.word << L";" << current->data.definition << endl;
  390.         current = current->next;
  391.     }
  392.     file.close();
  393. }
  394.  
  395. // Delimiter Old Semicolon
  396. void deserializeDictionary(Node*& head, const wstring& filename) {
  397.     wifstream file(filename);
  398.     if (!file.is_open()) {
  399.         MessageBox(NULL, L"Failed to open dictionary file", L"Error", MB_OK | MB_ICONERROR);
  400.         return;
  401.     }
  402.     wstring line;
  403.     while (getline(file, line)) {
  404.         size_t pos = line.find(L';');
  405.         if (pos != wstring::npos) {
  406.             wstring word = line.substr(0, pos);
  407.             wstring definition = line.substr(pos + 1);
  408.             addWord(head, word, definition);
  409.         }
  410.     }
  411.     file.close();
  412. }
  413.  
  414. // Add this function to convert a string to lowercase
  415. wstring toLower(const wstring& str) {
  416.     wstring lower = str;
  417.     transform(lower.begin(), lower.end(), lower.begin(), ::towlower);
  418.     return lower;
  419. }
  420.  
  421. // Modify the searchWordRealtime function to be case-insensitive
  422. void searchWordRealtime(const Node* head, const wstring& keyword, vector<wstring>& suggestions) {
  423.     suggestions.clear();
  424.     const Node* current = head;
  425.     wstring lowercaseKeyword = toLower(keyword);
  426.     while (current) {
  427.         wstring lowercaseWord = toLower(current->data.word);
  428.         if (lowercaseWord.find(lowercaseKeyword) != wstring::npos) {
  429.             suggestions.push_back(current->data.word);
  430.         }
  431.         current = current->next;
  432.     }
  433. }
  434.  
  435. // Prune Functions
  436. int pruneDictionary(Node*& head) {
  437.     if (!head) return 0;
  438.  
  439.     int duplicatesRemoved = 0;
  440.     Node* current = head;
  441.  
  442.     while (current && current->next) {
  443.         Node* runner = current;
  444.         while (runner->next) {
  445.             if (current->data.word == runner->next->data.word) {
  446.                 Node* duplicate = runner->next;
  447.                 runner->next = runner->next->next;
  448.                 delete duplicate;
  449.                 duplicatesRemoved++;
  450.             }
  451.             else {
  452.                 runner = runner->next;
  453.             }
  454.         }
  455.         current = current->next;
  456.     }
  457.  
  458.     return duplicatesRemoved;
  459. }
  460.  
  461. // Prune Functions
  462. void updateDictionaryAfterPrune(Node*& head, vector<wstring>& words, HWND hwnd) {
  463.     listWords(head, words);
  464.     updateListBox(hwnd, words);
  465.     serializeDictionary(head, L"myDictionary99x.txt");
  466. }
  467.  
  468. void updateListBox(HWND hwnd, const vector<wstring>& words) {
  469.     HWND hListBox = GetDlgItem(hwnd, IDC_LISTBOX);
  470.     SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
  471.     for (const auto& word : words) {
  472.         SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)word.c_str());
  473.     }
  474. }
  475.  
  476. void AddWordToList(HWND hwnd) {
  477.     wchar_t word[256], definition[1024];
  478.     GetDlgItemText(hwnd, IDC_WORD_INPUT, word, 256);
  479.     GetDlgItemText(hwnd, IDC_DEFINITION_INPUT, definition, 1024);
  480.     if (wcslen(word) > 0) {
  481.         if (!addWord(dictionary, word, definition)) {
  482.             wstring message = L"An entry with the name '";
  483.             message += word;
  484.             message += L"' already exists. Do you want to add a duplicate entry?";
  485.             int result = MessageBox(hwnd, message.c_str(), L"Confirm Duplicate Entry", MB_YESNO | MB_ICONQUESTION);
  486.  
  487.             if (result == IDYES) {
  488.                 insertNode(dictionary, { word, definition });
  489.             }
  490.         }
  491.         listWords(dictionary, words);
  492.         updateListBox(hwnd, words);
  493.         serializeDictionary(dictionary, L"myDictionary99x.txt");
  494.         SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  495.         SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  496.     }
  497. }
  498.  
  499. /*LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
  500.     if (nCode == HC_ACTION) {
  501.         KBDLLHOOKSTRUCT* pKbdStruct = (KBDLLHOOKSTRUCT*)lParam;
  502.         if (wParam == WM_KEYDOWN && pKbdStruct->vkCode == VK_RETURN) {
  503.             HWND hFocus = GetFocus();
  504.             if (hFocus == GetDlgItem(g_hDlg, IDC_WORD_INPUT) ||
  505.                 hFocus == GetDlgItem(g_hDlg, IDC_DEFINITION_INPUT)) {
  506.                 AddWordToList(g_hDlg);
  507.                 return 1;  // Prevent further processing of this key
  508.             }
  509.         }
  510.     }
  511.     return CallNextHookEx(g_hHook, nCode, wParam, lParam);
  512. }*/
  513.  
  514.  
  515. //Return this function
  516. /*LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
  517.     if (nCode == HC_ACTION) {
  518.         KBDLLHOOKSTRUCT* pKbdStruct = (KBDLLHOOKSTRUCT*)lParam;
  519.         if (wParam == WM_KEYDOWN) {
  520.             switch (pKbdStruct->vkCode) {
  521.             case VK_RETURN:
  522.                 if (GetFocus() == GetDlgItem(g_hDlg, IDC_WORD_INPUT) ||
  523.                     GetFocus() == GetDlgItem(g_hDlg, IDC_DEFINITION_INPUT)) {
  524.                     AddWordToList(g_hDlg);
  525.                     return 1;
  526.                 }
  527.                 break;
  528.             case VK_ESCAPE:
  529.                 /*if (IsWindowVisible(g_hDlg)) { // Assuming g_hDlg is your dialog handle
  530.                     PostMessage(g_hDlg, WM_COMMAND, IDCANCEL, 0);
  531.                     return 1; // Signal that we've handled this key
  532.                 }
  533.                 // Check for Cancel button                
  534.                 if (GetDlgItem(g_hDlg, IDCANCEL)) {
  535.                     SendMessage(g_hDlg, WM_COMMAND, MAKEWPARAM(IDCANCEL, 0), 0);
  536.                     return TRUE;
  537.                 }
  538.                 // Check for No button
  539.                 else if (GetDlgItem(g_hDlg, IDNO)) {
  540.                     SendMessage(g_hDlg, WM_COMMAND, MAKEWPARAM(IDNO, 0), 0);
  541.                     return TRUE;
  542.                 }
  543.                 // For dialogs with only a close button (like View Definition)
  544.                 else {
  545.                     SendMessage(g_hDlg, WM_CLOSE, 0, 0);
  546.                     return TRUE;
  547.                 }
  548.                 break;
  549.             case VK_F1:
  550.             {
  551.     if (g_hDefinitionDlg && IsWindowVisible(g_hDefinitionDlg)) {
  552.         // If the dialog is visible, hide it
  553.         ShowWindow(g_hDefinitionDlg, SW_HIDE);
  554.         g_hDefinitionDlg = NULL; // Reset since we're hiding it
  555.     } else {
  556.         // If not visible or doesn't exist, show it or create it
  557.         HWND hListBox = GetDlgItem(g_hDlg, IDC_LISTBOX);
  558.         int selectedIndex = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
  559.         if (selectedIndex != LB_ERR) {
  560.             wchar_t selectedWord[256];
  561.             SendMessage(hListBox, LB_GETTEXT, selectedIndex, (LPARAM)selectedWord);
  562.             Node* current = dictionary;
  563.             while (current) {
  564.                 if (current->data.word == selectedWord) {
  565.                     // Here, instead of calling DisplayDefinition directly, we'll manage the dialog
  566.                     if (!g_hDefinitionDlg) {
  567.                         g_hDefinitionDlg = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DEFINITION), g_hDlg, ViewDefinitionDlgProc, (LPARAM)new wstring(current->data.definition));
  568.                         ShowWindow(g_hDefinitionDlg, SW_SHOW);
  569.                     }
  570.                     break;
  571.                 }
  572.                 current = current->next;
  573.             }
  574.         }
  575.     }
  576.     return 1;
  577. }*/
  578.  
  579. /*LRESULT CALLBACK EditSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  580. {
  581.     if (uMsg == WM_KEYDOWN && wParam == VK_RETURN)
  582.     {
  583.         HWND hwndParent = (HWND)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  584.         if (hwndParent)
  585.         {
  586.             HWND hwndAddButton = GetDlgItem(hwndParent, IDC_ADD_WORD);
  587.             if (hwndAddButton)
  588.             {
  589.                 SendMessage(hwndParent, WM_COMMAND, MAKEWPARAM(IDC_ADD_WORD, BN_CLICKED), (LPARAM)hwndAddButton);
  590.                 return 0;
  591.             }
  592.         }
  593.     }
  594.  
  595.     return CallWindowProc((WNDPROC)GetWindowLongPtr(hWnd, GWLP_USERDATA), hWnd, uMsg, wParam, lParam);
  596. }*/
  597.  
  598. /*LRESULT CALLBACK EditSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam,
  599.     LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
  600. {
  601.     if (uMsg == WM_KEYDOWN && wParam == VK_RETURN) {
  602.         HWND hwndParent = (HWND)dwRefData;
  603.         SendMessage(hwndParent, WM_COMMAND, MAKEWPARAM(IDC_ADD_WORD, BN_CLICKED),
  604.             (LPARAM)GetDlgItem(hwndParent, IDC_ADD_WORD));
  605.         return 0;
  606.     }
  607.     return DefSubclassProc(hWnd, uMsg, wParam, lParam);
  608. }*/
  609. /*LRESULT CALLBACK WordEditSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  610.     if (uMsg == WM_KEYDOWN && wParam == VK_RETURN) {
  611.         SendMessage(GetParent(hWnd), WM_COMMAND, MAKEWPARAM(IDC_ADD_WORD, BN_CLICKED), (LPARAM)GetDlgItem(GetParent(hWnd), IDC_ADD_WORD));
  612.         return 0;
  613.     }
  614.     return CallWindowProc(oldWordEditProc, hWnd, uMsg, wParam, lParam);
  615. }
  616.  
  617. LRESULT CALLBACK DefinitionEditSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  618.     if (uMsg == WM_KEYDOWN && wParam == VK_RETURN) {
  619.         SendMessage(GetParent(hWnd), WM_COMMAND, MAKEWPARAM(IDC_ADD_WORD, BN_CLICKED), (LPARAM)GetDlgItem(GetParent(hWnd), IDC_ADD_WORD));
  620.         return 0;
  621.     }
  622.     return CallWindowProc(oldDefinitionEditProc, hWnd, uMsg, wParam, lParam);
  623. }*/            
  624.  
  625. // Dialog procedure
  626. // Modify the DialogProc function
  627. INT_PTR CALLBACK DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  628.     static HWND hFocusedEdit = nullptr; // Define hFocusedEdit here
  629.     static HWND hWordEdit = nullptr;
  630.     static HWND hDefinitionEdit = nullptr;
  631.     switch (uMsg) {
  632.     case WM_INITDIALOG:
  633.     {
  634.         // Load dictionary
  635.         /*wchar_t path[MAX_PATH];
  636.         GetModuleFileName(NULL, path, MAX_PATH);
  637.         wstring exePath(path);
  638.         wstring dirPath = exePath.substr(0, exePath.find_last_of(L"\\/"));
  639.         wstring filePath = dirPath + L"\\myDictionary99x.txt";*/
  640.  
  641.         deserializeDictionary(dictionary, L"myDictionary99x.txt");
  642.         listWords(dictionary, words);
  643.         updateListBox(hwnd, words);
  644.         g_hDlg = hwnd;  // Store the dialog handle globally
  645.         g_hHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0);
  646.         //hWordEdit = GetDlgItem(hwnd, IDC_WORD_INPUT);
  647.         //hDefinitionEdit = GetDlgItem(hwnd, IDC_DEFINITION_INPUT);
  648.  
  649.         // Set up subclassing
  650.         /*SetWindowLongPtr(hWordEdit, GWLP_USERDATA, (LONG_PTR)hwnd);
  651.         SetWindowLongPtr(hDefinitionEdit, GWLP_USERDATA, (LONG_PTR)hwnd);
  652.         SetWindowLongPtr(hWordEdit, GWLP_WNDPROC, (LONG_PTR)EditSubclassProc);
  653.         SetWindowLongPtr(hDefinitionEdit, GWLP_WNDPROC, (LONG_PTR)EditSubclassProc);*/
  654.         SetFocus(GetDlgItem(hwnd, IDC_SEARCH_INPUT));  // Set focus to search field
  655.         return TRUE;
  656.     }
  657.  
  658.     case WM_COMMAND:
  659.         // Start Double-Click View ListBox Definitions function block
  660.         switch (HIWORD(wParam)) {
  661.         case LBN_DBLCLK:
  662.             if (LOWORD(wParam) == IDC_LISTBOX) {
  663.                 int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  664.                 if (index != LB_ERR) {
  665.                     wchar_t word[256];
  666.                     SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
  667.  
  668.                     // Find the definition for the selected word
  669.                     Node* current = dictionary;
  670.                     while (current) {
  671.                         if (current->data.word == word) {
  672.                             DisplayDefinition(hwnd, word, current->data.definition);
  673.                             break;
  674.                         }
  675.                         current = current->next;
  676.                     }
  677.                 }
  678.                 return TRUE;
  679.             }
  680.             //break;
  681.         //}
  682.  
  683.         case EN_SETFOCUS:
  684.             if (LOWORD(wParam) == IDC_WORD_INPUT || LOWORD(wParam) == IDC_DEFINITION_INPUT) {
  685.                 hFocusedEdit = (HWND)lParam;
  686.             }
  687.             break;
  688.         }
  689.  
  690.         // End Double-Click View ListBox Definitions function block
  691.         // Handle other controls
  692.         switch (LOWORD(wParam)) {
  693.         case IDC_ADD_WORD: {
  694.             if (LOWORD(wParam) == IDC_ADD_WORD) {
  695.                 AddWordToList(hwnd);
  696.                 return TRUE;
  697.             }
  698.             else if ((LOWORD(wParam) == IDC_WORD_INPUT || LOWORD(wParam) == IDC_DEFINITION_INPUT) &&
  699.                 HIWORD(wParam) == EN_SETFOCUS) {
  700.                 SetTimer(hwnd, 1, 10, NULL);
  701.                 return TRUE;
  702.             }
  703.             else if ((LOWORD(wParam) == IDC_WORD_INPUT || LOWORD(wParam) == IDC_DEFINITION_INPUT) &&
  704.                 HIWORD(wParam) == EN_KILLFOCUS) {
  705.                 KillTimer(hwnd, 1);
  706.                 return TRUE;
  707.             }
  708.             break;
  709.         }
  710.                          /*wchar_t word[256], definition[1024];
  711.                          GetDlgItemText(hwnd, IDC_WORD_INPUT, word, 256);
  712.                          GetDlgItemText(hwnd, IDC_DEFINITION_INPUT, definition, 1024);
  713.                          if (wcslen(word) > 0) {
  714.                              if (!addWord(dictionary, word, definition)) {
  715.                                  // Word already exists, show confirmation box
  716.                                  wstring message = L"An entry with the name '";
  717.                                  message += word;
  718.                                  message += L"' already exists. Do you want to add a duplicate entry?";
  719.                                  int result = MessageBox(hwnd, message.c_str(), L"Confirm Duplicate Entry", MB_YESNO | MB_ICONQUESTION);
  720.  
  721.                                  if (result == IDYES) {
  722.                                      // User confirmed, add the duplicate entry
  723.                                      insertNode(dictionary, { word, definition });
  724.                                      listWords(dictionary, words);
  725.                                      updateListBox(hwnd, words);
  726.                                      serializeDictionary(dictionary, L"myDictionary99x.txt");
  727.                                      SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  728.                                      SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  729.                                  }
  730.                                  // If result is IDNO, do nothing (silently ignore)
  731.                              }
  732.                              else {
  733.                                  // Word added successfully
  734.                                  listWords(dictionary, words);
  735.                                  updateListBox(hwnd, words);
  736.                                  serializeDictionary(dictionary, L"myDictionary99x.txt");
  737.                                  SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  738.                                  SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  739.                              }
  740.                          }
  741.                          return TRUE;
  742.                      }
  743.                  }*/
  744.                  // w/o duplicates
  745.                  /*case IDC_ADD_WORD: {
  746.                      wchar_t word[256], definition[1024];
  747.                      GetDlgItemText(hwnd, IDC_WORD_INPUT, word, 256);
  748.                      GetDlgItemText(hwnd, IDC_DEFINITION_INPUT, definition, 1024);
  749.                      if (wcslen(word) > 0) {
  750.                          if (addWord(dictionary, word, definition)) {
  751.                              listWords(dictionary, words);
  752.                              updateListBox(hwnd, words);
  753.                              serializeDictionary(dictionary, L"myDictionary99x.txt");
  754.                              SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  755.                              SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  756.                          }
  757.                          // If addWord returns false, it means the word already exists.
  758.                          // We silently ignore it as per the requirement.
  759.                      }
  760.                      return TRUE;
  761.                  }*/
  762.                  // /w duplicates        
  763.                  /*case IDC_ADD_WORD: {
  764.                      wchar_t word[256], definition[1024];
  765.                      GetDlgItemText(hwnd, IDC_WORD_INPUT, word, 256);
  766.                      GetDlgItemText(hwnd, IDC_DEFINITION_INPUT, definition, 1024);
  767.                      if (wcslen(word) > 0) {  // Only check if word is not empty
  768.                          addWord(dictionary, word, definition);
  769.                          listWords(dictionary, words);
  770.                          updateListBox(hwnd, words);
  771.                          serializeDictionary(dictionary, L"myDictionary99x.txt");
  772.                          SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
  773.                          SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
  774.                      }
  775.                      return TRUE;
  776.                  }*/
  777.  
  778.         case IDC_DELETE_WORD: {
  779.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  780.             if (index != LB_ERR) {
  781.                 wchar_t word[256];
  782.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
  783.  
  784.                 // Create the confirmation message
  785.                 wstring message = L"Are you sure you want to delete the word '";
  786.                 message += word;
  787.                 message += L"'?";
  788.  
  789.                 // Show the confirmation box
  790.                 int result = MessageBox(hwnd, message.c_str(), L"Confirm Deletion", MB_YESNO | MB_ICONQUESTION);
  791.  
  792.                 // If the user clicks "Yes", proceed with deletion
  793.                 if (result == IDYES) {
  794.                     deleteWord(dictionary, word);
  795.                     listWords(dictionary, words);
  796.                     updateListBox(hwnd, words);
  797.                     serializeDictionary(dictionary, L"myDictionary99x.txt");
  798.                 }
  799.             }
  800.             return TRUE;
  801.         }
  802.  
  803.         case IDC_EDIT_WORD: {
  804.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  805.             if (index != LB_ERR) {
  806.                 wchar_t wordToEdit[256];
  807.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)wordToEdit);
  808.                 Node* current = dictionary;
  809.                 while (current) {
  810.                     if (current->data.word == wordToEdit) {
  811.                         wstring newWord = InputBox(hwnd, L"Edit Word", L"Edit Word", current->data.word);
  812.                         if (!newWord.empty()) {
  813.                             wstring newDefinition = InputBox(hwnd, L"Edit Definition", L"Edit Definition", current->data.definition);
  814.                             // newDefinition can be empty now
  815.                             SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_DELETESTRING, index, 0); // Delete the old word
  816.                             editWord(dictionary, wordToEdit, newWord, newDefinition);
  817.                             listWords(dictionary, words);
  818.                             updateListBox(hwnd, words);
  819.                             // fix for C++ best practices "<" Signed/ Unsigned Mismatch
  820.                             for (size_t i = 0; i < words.size(); i++) {
  821.                                 //for (int i = 0; i < words.size(); i++) {
  822.                                 if (words[i] == newWord) {
  823.                                     SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_SETCURSEL, static_cast<WPARAM>(i), 0); // Select the edited word
  824.                                     // fix for C++ best practices "<" Signed Unsigned Mismatch
  825.                                     //SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_SETCURSEL, i, 0); // Select the edited word
  826.                                     break;
  827.                                 }
  828.                             }
  829.                             serializeDictionary(dictionary, L"myDictionary99x.txt");
  830.                         }
  831.                         break;
  832.                     }
  833.                     current = current->next;
  834.                 }
  835.                 if (!current) {
  836.                     MessageBox(hwnd, L"Word not found in the dictionary.", L"Error", MB_OK | MB_ICONERROR);
  837.                 }
  838.             }
  839.             return TRUE;
  840.         }
  841.                           // /w duplicates (does not affect duplicates tho only no check for blank definitions
  842.                           /*case IDC_EDIT_WORD: {
  843.                               int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  844.                               if (index != LB_ERR) {
  845.                                   wchar_t wordToEdit[256];
  846.                                   SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)wordToEdit);
  847.                                   Node* current = dictionary;
  848.                                   while (current) {
  849.                                       if (current->data.word == wordToEdit) {
  850.                                           wstring newWord = InputBox(hwnd, L"Edit Word", L"Edit Word", current->data.word);
  851.                                           if (!newWord.empty()) {
  852.                                               wstring newDefinition = InputBox(hwnd, L"Edit Definition", L"Edit Definition", current->data.definition);
  853.                                               SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_DELETESTRING, index, 0); // Delete the old word
  854.                                               editWord(dictionary, wordToEdit, newWord, newDefinition);
  855.                                               listWords(dictionary, words);
  856.                                               updateListBox(hwnd, words);
  857.                                               for (int i = 0; i < words.size(); i++) {
  858.                                                   if (words[i] == newWord) {
  859.                                                       SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_SETCURSEL, i, 0); // Select the edited word
  860.                                                       break;
  861.                                                   }
  862.                                               }
  863.                                               serializeDictionary(dictionary, L"myDictionary99x.txt");
  864.                                           }
  865.                                           break;
  866.                                       }
  867.                                       current = current->next;
  868.                                   }
  869.                                   if (!current) {
  870.                                       MessageBox(hwnd, L"Word not found in the dictionary.", L"Error", MB_OK | MB_ICONERROR);
  871.                                   }
  872.                               }
  873.                               return TRUE;
  874.                           }*/
  875.  
  876.         case IDC_VIEW_DEFINITION: {
  877.             int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
  878.             if (index != LB_ERR) {
  879.                 wchar_t word[256];
  880.                 SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
  881.                 Node* current = dictionary;
  882.                 while (current) {
  883.                     if (current->data.word == word) {
  884.                         DisplayDefinition(hwnd, current->data.word, current->data.definition);
  885.                         break;
  886.                     }
  887.                     current = current->next;
  888.                 }
  889.             }
  890.             return TRUE;
  891.         }
  892.  
  893.         case IDC_SEARCH_INPUT: {
  894.             if (HIWORD(wParam) == EN_CHANGE) {
  895.                 wchar_t query[256];
  896.                 GetDlgItemText(hwnd, IDC_SEARCH_INPUT, query, 256);
  897.                 if (wcslen(query) == 0) {
  898.                     updateListBox(hwnd, words);
  899.                 }
  900.                 else {
  901.                     vector<wstring> suggestions;
  902.                     searchWordRealtime(dictionary, query, suggestions);
  903.                     updateListBox(hwnd, suggestions);
  904.                 }
  905.             }
  906.             return TRUE;
  907.         }
  908.  
  909.         case IDC_CLEAR_SEARCH: {
  910.             SetDlgItemText(hwnd, IDC_SEARCH_INPUT, L"");
  911.             updateListBox(hwnd, words);
  912.             return TRUE;
  913.         }
  914.  
  915.                              //Prune Functions
  916.         case IDC_PRUNE: {
  917.             int result = MessageBox(hwnd, L"Are you sure you want to prune the list and remove duplicates?", L"Confirm Prune", MB_YESNO | MB_ICONQUESTION);
  918.             if (result == IDYES) {
  919.                 int duplicatesRemoved = pruneDictionary(dictionary);
  920.                 updateDictionaryAfterPrune(dictionary, words, hwnd);
  921.  
  922.                 wstring message = L"Pruning complete. ";
  923.                 message += to_wstring(duplicatesRemoved);
  924.                 message += L" duplicate entries were removed.";
  925.                 MessageBox(hwnd, message.c_str(), L"Prune Results", MB_OK | MB_ICONINFORMATION);
  926.             }
  927.             return TRUE;
  928.         }
  929.  
  930.         case WM_TIMER:
  931.             if (wParam == 1) {
  932.                 if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
  933.                     AddWordToList(hwnd);
  934.                     KillTimer(hwnd, 1);
  935.                     SetTimer(hwnd, 1, 10, NULL);  // Restart the timer
  936.                 }
  937.             }
  938.             break;
  939.             /*case IDC_WORD_INPUT:
  940.             case IDC_DEFINITION_INPUT:
  941.                 if (HIWORD(wParam) == EN_SETFOCUS) {
  942.                     hFocusedEdit = (HWND)lParam;
  943.                 }
  944.                 break;
  945.                 //}
  946.                 //break;
  947.  
  948.                     case WM_KEYDOWN:
  949.                 if (wParam == VK_RETURN) {
  950.                     HWND focusedControl = GetFocus();
  951.                     if (focusedControl == hWordEdit || focusedControl == hDefinitionEdit) {
  952.                         SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDC_ADD_WORD, BN_CLICKED), 0);
  953.                         return TRUE;
  954.                     }
  955.                 }
  956.                 break;*/
  957.  
  958.         case IDCANCEL:
  959.             DestroyWindow(hwnd);
  960.             PostQuitMessage(0);
  961.             return TRUE;
  962.         }
  963.         break;
  964.  
  965.     case WM_KEYDOWN:
  966.         if (wParam == VK_ESCAPE) {
  967.             // Check for Cancel button
  968.             if (GetDlgItem(hwnd, IDCANCEL)) {
  969.                 SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, 0), 0);
  970.                 return TRUE;
  971.             }
  972.             // Check for No button
  973.             else if (GetDlgItem(hwnd, IDNO)) {
  974.                 SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDNO, 0), 0);
  975.                 return TRUE;
  976.             }
  977.             // For dialogs with only a close button (like View Definition)
  978.             else {
  979.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  980.                 return TRUE;
  981.             }
  982.         }
  983.         //break;
  984.         /*if (wParam == VK_ESCAPE) {
  985.             if (GetDlgItem(hwnd, IDCANCEL)) {
  986.             SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, 0), 0);
  987.             return TRUE;
  988.         }
  989.     }
  990.         break;*/
  991.         if (wParam == VK_F1) {
  992.             HWND hListBox = GetDlgItem(hwnd, IDC_LISTBOX);
  993.             int selectedIndex = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
  994.             if (selectedIndex != LB_ERR) {
  995.                 wchar_t selectedWord[256];
  996.                 SendMessage(hListBox, LB_GETTEXT, selectedIndex, (LPARAM)selectedWord);
  997.                 Node* current = dictionary;
  998.                 while (current) {
  999.                     if (current->data.word == selectedWord) {
  1000.                         DisplayDefinition(hwnd, current->data.word, current->data.definition);
  1001.                         break;
  1002.                     }
  1003.                     current = current->next;
  1004.                 }
  1005.             }
  1006.             return TRUE;
  1007.         }
  1008.         break;
  1009.  
  1010.     case WM_GETDLGCODE:
  1011.         if (wParam == VK_ESCAPE) {
  1012.             return DLGC_WANTALLKEYS;
  1013.         }
  1014.         if (wParam == VK_F1) {
  1015.             return DLGC_WANTALLKEYS | DLGC_WANTCHARS;
  1016.         }
  1017.         break;
  1018.  
  1019.     case WM_SHOWWINDOW:
  1020.         if (wParam == TRUE) {  // Only set focus when the window is shown
  1021.             SetFocus(GetDlgItem(hwnd, IDC_SEARCH_INPUT));
  1022.         }
  1023.         return 0;
  1024.  
  1025.     /*case WM_DESTROY:
  1026.         // In your cleanup or WM_DESTROY handling
  1027.         UnhookWindowsHookEx(g_hHook);
  1028.         DestroyWindow(hwnd);
  1029.         PostQuitMessage(0);*/
  1030.  
  1031.     case WM_CLOSE:
  1032.         if (g_hHook) {
  1033.             UnhookWindowsHookEx(g_hHook);
  1034.         }
  1035.         DestroyWindow(hwnd);
  1036.         PostQuitMessage(0);
  1037.         return TRUE;
  1038.     }
  1039.    
  1040.     return FALSE;
  1041. }
  1042.  
  1043. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
  1044.     HWND hDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), 0, DialogProc, 0);
  1045.     if (!hDlg) {
  1046.         MessageBox(NULL, L"Failed to create dialog", L"Error", MB_ICONERROR | MB_OK);
  1047.         return 1;
  1048.     }
  1049.  
  1050.     // Retrieve screen dimensions
  1051.     RECT desktop;
  1052.     const HWND hDesktop = GetDesktopWindow();
  1053.     GetWindowRect(hDesktop, &desktop);
  1054.  
  1055.     // Retrieve window dimensions
  1056.     RECT windowRect;
  1057.     GetWindowRect(hDlg, &windowRect);
  1058.     int windowWidth = windowRect.right - windowRect.left;
  1059.     int windowHeight = windowRect.bottom - windowRect.top;
  1060.  
  1061.     // Calculate position to center the window
  1062.     int posX = (desktop.right / 2) - (windowWidth / 2);
  1063.     int posY = (desktop.bottom / 2) - (windowHeight / 2);
  1064.  
  1065.     // Set window position
  1066.     SetWindowPos(hDlg, NULL, posX, posY, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
  1067.  
  1068.     ShowWindow(hDlg, nCmdShow);
  1069.  
  1070.     MSG msg;
  1071.     BOOL bRet;
  1072.     while ((bRet = GetMessage(&msg, nullptr, 0, 0)) != 0) {
  1073.         if (bRet == -1) {
  1074.             // Handle the error and possibly exit
  1075.             break;
  1076.         }
  1077.         else if (!IsWindow(hDlg) || !IsDialogMessage(hDlg, &msg)) {
  1078.             TranslateMessage(&msg);
  1079.             DispatchMessage(&msg);
  1080.         }
  1081.     }
  1082.  
  1083.     return (int)msg.wParam;
  1084. }
  1085.  
  1086. ==++"Resource2.rc" File SourceCode++==::
  1087. #include <windows.h>
  1088. #include "resource2.h"
  1089.  
  1090. IDD_MAINDIALOG DIALOGEX 0, 0, 320, 240
  1091. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  1092. CAPTION "Dictionary Application (F1=QuickViewToggle)"
  1093. FONT 8, "MS Shell Dlg", 400, 0, 0x1
  1094. BEGIN
  1095. LTEXT           "Word:", -1, 10, 10, 30, 8
  1096. EDITTEXT        IDC_WORD_INPUT, 50, 8, 100, 14, ES_AUTOHSCROLL | WS_TABSTOP
  1097. LTEXT           "Definition:", -1, 10, 30, 40, 8
  1098. EDITTEXT        IDC_DEFINITION_INPUT, 50, 28, 200, 14, ES_AUTOHSCROLL | WS_TABSTOP
  1099. PUSHBUTTON      "Add Word", IDC_ADD_WORD, 260, 8, 50, 14
  1100. PUSHBUTTON      "Delete Word", IDC_DELETE_WORD, 260, 28, 50, 14
  1101. PUSHBUTTON      "Edit Word", IDC_EDIT_WORD, 260, 48, 50, 14
  1102. PUSHBUTTON      "View Definition", IDC_VIEW_DEFINITION, 205, 8, 55, 14
  1103. PUSHBUTTON      "Prune", IDC_PRUNE, 160, 8, 25, 14
  1104. PUSHBUTTON      "Clear", IDC_CLEAR_SEARCH, 185, 8, 20, 14
  1105. LTEXT           "Search:", -1, 10, 50, 30, 8
  1106. EDITTEXT        IDC_SEARCH_INPUT, 50, 48, 200, 14, ES_AUTOHSCROLL
  1107. LISTBOX         IDC_LISTBOX, 10, 70, 300, 160, LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL
  1108. END
  1109.  
  1110. IDD_INPUT DIALOGEX 0, 0, 250, 120
  1111. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  1112. CAPTION "Input"
  1113. FONT 8, "MS Shell Dlg", 400, 0, 0x1
  1114. BEGIN
  1115. LTEXT           "Input:", -1, 7, 14, 50, 8
  1116. EDITTEXT        IDC_INPUT, 7, 30, 230, 65, ES_MULTILINE | ES_AUTOVSCROLL | WS_VSCROLL
  1117. DEFPUSHBUTTON   "OK", IDOK, 70, 100, 50, 14
  1118. PUSHBUTTON      "Cancel", IDCANCEL, 130, 100, 50, 14
  1119. END
  1120. //20->65 (EditText last value), 60->100 (DefPushButton second value), 60->100 (PushButton second value) Only ES_AUTOHSCROLL (in EditText)
  1121.  
  1122. IDD_DEFINITION DIALOGEX 0, 0, 300, 200
  1123. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  1124. CAPTION "Definition"
  1125. FONT 8, "MS Shell Dlg"
  1126. BEGIN
  1127. EDITTEXT        IDC_DEFINITION_EDIT, 7, 7, 286, 186, ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_VSCROLL
  1128. END
  1129.  
  1130. /*IDD_INPUT DIALOGEX 0, 0, 200, 100
  1131. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  1132. CAPTION "Input"
  1133. FONT 8, "MS Shell Dlg"
  1134. BEGIN
  1135. LTEXT           "Input:", -1, 7, 14, 50, 8
  1136. EDITTEXT        IDC_INPUT, 7, 24, 186, 12, ES_AUTOHSCROLL
  1137. DEFPUSHBUTTON   "OK", IDOK, 139, 7, 50, 14
  1138. PUSHBUTTON      "Cancel", IDCANCEL, 139, 24, 50, 14
  1139. END*/
  1140.  
  1141. //EDITTEXT        IDC_WORD_INPUT, 50, 8, 100, 14, ES_AUTOHSCROLL
  1142. //PUSHBUTTON      "Add Word", IDC_ADD_WORD, 260, 8, 50, 14
  1143. //PUSHBUTTON      "View Definition", IDC_VIEW_DEFINITION, 260, 68, 50, 14
  1144.  
  1145. ==++"resource2.h"++== File SourceCode::
  1146. #ifndef RESOURCE2_H
  1147. #define RESOURCE2_H
  1148.  
  1149. #define IDD_MAINDIALOG                  101
  1150. #define IDC_WORD_INPUT                  1001
  1151. #define IDC_DEFINITION_INPUT            1002
  1152. #define IDC_ADD_WORD                    1003
  1153. #define IDC_DELETE_WORD                 1004
  1154. #define IDC_SEARCH_INPUT                1005
  1155. #define IDC_LISTBOX                     1006
  1156. #define IDC_INPUT                       1007  // Add this line if you're using IDC_INPUT
  1157. #define IDC_EDIT_WORD                   1008
  1158. #define IDC_VIEW_DEFINITION             1009
  1159. #define IDD_INPUT                       1010
  1160. #define IDD_DEFINITION                  1011
  1161. #define IDC_DEFINITION_EDIT             1012
  1162. #define IDC_CLEAR_SEARCH                1013
  1163. #define IDC_PRUNE                       1014
  1164.  
  1165. #endif // RESOURCE2_H
  1166.  
Tags: #multifile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement