Advertisement
alien_fx_fiend

AdvTitleChanger *FINAL RELEASE !*

Oct 22nd, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.65 KB | Source Code | 0 0
  1. ==++"APISpy.cpp" File 1/2 SourceCode++==::
  2. #include <Windows.h>
  3. #include <CommCtrl.h>
  4. #include <string>
  5. #include <TlHelp32.h>
  6. #include <tchar.h>
  7. #include <Psapi.h>
  8. #include <iostream>
  9. #include <vector>
  10. #include <sstream>
  11. #include <thread>
  12. #include <WinUser.h>
  13. #include <richedit.h>
  14. #include <shellapi.h>
  15. #include "resource.h"
  16.  
  17. #pragma comment(lib, "Psapi.lib")
  18. #pragma comment(lib, "Comctl32.lib")
  19. #pragma comment(lib, "Shell32.lib")
  20.  
  21. #define WS_EX_NOMAXIMIZEBOX (WS_EX_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX)
  22.  
  23. bool isCopying = false;
  24. bool isPasting = false;
  25. bool running = true;
  26.  
  27. INT_PTR CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  28.  
  29. struct ProcessInfo {
  30.     std::wstring processName;
  31.     std::wstring windowTitle;
  32.     HWND hwnd;
  33. };
  34.  
  35. std::vector<ProcessInfo> allProcesses;
  36. std::vector<ProcessInfo> targetProcesses;
  37. std::vector<std::wstring> processNames;
  38.  
  39. HHOOK keyboardHook;
  40. HWND g_hListDialog = NULL; // Global handle for the list dialog
  41. HWND g_mainHwnd; // Global handle for the main window
  42.  
  43. // Function prototypes
  44. void DeleteWordToLeft(HWND hEdit);
  45. INT_PTR CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  46. void AppendTextToRichEdit(HWND hRichText, const std::wstring& text);
  47. BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege);
  48. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
  49. bool ModifyWindowTitles(const std::wstring& newTitle);
  50. DWORD WINAPI TitleModifierThread(LPVOID lpParam);
  51. std::vector<std::wstring> Split(const std::wstring& s, wchar_t delimiter);
  52. void ProcessInput(HWND hwnd);
  53. void OpenListDialog(HWND parentHwnd);
  54.  
  55. HWND hRichTextBox, hTextBox, hButton, hCloseButton, hGroupBox, hLabel;
  56. HWND hListView = NULL;
  57.  
  58. // Low-level keyboard hook procedure
  59. LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
  60.     if (nCode == HC_ACTION && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) {
  61.         KBDLLHOOKSTRUCT* kbdStruct = (KBDLLHOOKSTRUCT*)lParam;
  62.         HWND foregroundWindow = GetForegroundWindow();
  63.  
  64.         if (kbdStruct->vkCode == VK_ESCAPE) {
  65.             if (g_hListDialog && IsWindow(g_hListDialog) &&
  66.                 (foregroundWindow == g_hListDialog || IsChild(g_hListDialog, foregroundWindow))) {
  67.                 PostMessage(g_hListDialog, WM_KEYDOWN, VK_ESCAPE, 0);
  68.             }
  69.             else if (foregroundWindow == g_mainHwnd || IsChild(g_mainHwnd, foregroundWindow)) {
  70.                 PostMessage(g_mainHwnd, WM_USER + 1, VK_ESCAPE, 0);
  71.             }
  72.             return 1;
  73.         }
  74.         else if (kbdStruct->vkCode == VK_F1) {
  75.             if (foregroundWindow == g_mainHwnd || IsChild(g_mainHwnd, foregroundWindow)) {
  76.                 PostMessage(g_mainHwnd, WM_USER + 1, VK_F1, 0);
  77.                 return 1;
  78.             }
  79.         }
  80.     }
  81.     return CallNextHookEx(NULL, nCode, wParam, lParam);
  82. }
  83.  
  84. void UninstallHook() {
  85.     if (keyboardHook) {
  86.         UnhookWindowsHookEx(keyboardHook);
  87.         keyboardHook = NULL;
  88.     }
  89. }
  90.  
  91. void InstallHook(HWND hWnd) {
  92.     keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0);
  93.     if (!keyboardHook) {
  94.         // Handle error - could not set hook
  95.         DWORD errorCode = GetLastError();
  96.         // Log or display the error
  97.     }
  98. }
  99.  
  100. void ResetFocusAndHookState() {
  101.     if (g_mainHwnd) {
  102.         SetFocus(g_mainHwnd); // Ensure the main window gets focus back
  103.                 // Optionally, bring the window to the foreground
  104.         SetForegroundWindow(g_mainHwnd);
  105.     }
  106.  
  107.     // Refresh the hook by uninstalling and then reinstalling it
  108.     if (keyboardHook) {
  109.         UninstallHook(); // Assuming this function is defined to uninstall the hook
  110.         InstallHook(g_mainHwnd); // Assuming this function is defined to install the hook
  111.     }
  112. }
  113.  
  114. void OpenListDialog(HWND parentHwnd) {
  115.     g_hListDialog = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), parentHwnd, DialogProc);
  116.     if (g_hListDialog != NULL) {
  117.         ShowWindow(g_hListDialog, SW_SHOW);
  118.     }
  119. }
  120.  
  121. void AppendTextToRichEdit(HWND hRichText, const std::wstring& text) {
  122.     int len = GetWindowTextLength(hRichText);
  123.     SendMessage(hRichText, EM_SETSEL, (WPARAM)len, (LPARAM)len);
  124.     SendMessage(hRichText, EM_REPLACESEL, 0, (LPARAM)text.c_str());
  125. }
  126.  
  127. BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege) {
  128.     TOKEN_PRIVILEGES tp;
  129.     LUID luid;
  130.  
  131.     if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid)) {
  132.         AppendTextToRichEdit(hRichTextBox, L"LookupPrivilegeValue error: " + std::to_wstring(GetLastError()) + L"\n");
  133.         return FALSE;
  134.     }
  135.  
  136.     tp.PrivilegeCount = 1;
  137.     tp.Privileges[0].Luid = luid;
  138.     tp.Privileges[0].Attributes = (bEnablePrivilege) ? SE_PRIVILEGE_ENABLED : 0;
  139.  
  140.     if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
  141.         AppendTextToRichEdit(hRichTextBox, L"AdjustTokenPrivileges error: " + std::to_wstring(GetLastError()) + L"\n");
  142.         return FALSE;
  143.     }
  144.  
  145.     if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
  146.         AppendTextToRichEdit(hRichTextBox, L"The token does not have the specified privilege.\n");
  147.         return FALSE;
  148.     }
  149.  
  150.     return TRUE;
  151. }
  152.  
  153. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
  154.     DWORD processId;
  155.     GetWindowThreadProcessId(hwnd, &processId);
  156.  
  157.     HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
  158.     if (hProcess != NULL) {
  159.         TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
  160.         if (GetModuleFileNameEx(hProcess, NULL, szProcessName, MAX_PATH)) {
  161.             std::wstring processName(szProcessName);
  162.             size_t pos = processName.find_last_of(L"\\");
  163.             if (pos != std::wstring::npos) {
  164.                 processName = processName.substr(pos + 1);
  165.             }
  166.  
  167.             TCHAR windowTitle[MAX_PATH];
  168.             GetWindowText(hwnd, windowTitle, MAX_PATH);
  169.  
  170.             if (IsWindowVisible(hwnd) && wcslen(windowTitle) > 0) {
  171.                 allProcesses.push_back({ processName, windowTitle, hwnd });
  172.             }
  173.  
  174.             for (const auto& targetName : *(std::vector<std::wstring>*)lParam) {
  175.                 if (processName == targetName) {
  176.                     targetProcesses.push_back({ processName, windowTitle, hwnd });
  177.                 }
  178.             }
  179.         }
  180.         CloseHandle(hProcess);
  181.     }
  182.     return TRUE;
  183. }
  184.  
  185. bool ModifyWindowTitles(const std::wstring& newTitle) {
  186.     bool anyModified = false;
  187.     for (const auto& process : targetProcesses) {
  188.         if (SetWindowText(process.hwnd, newTitle.c_str())) {
  189.             anyModified = true;
  190.             AppendTextToRichEdit(hRichTextBox, L"\nModified window title of " + process.processName + L"\n");
  191.         }
  192.         else {
  193.             AppendTextToRichEdit(hRichTextBox, L"\nFailed to modify window title of " + process.processName + L"\n");
  194.         }
  195.     }
  196.     return anyModified;
  197. }
  198.  
  199. DWORD WINAPI TitleModifierThread(LPVOID lpParam) {
  200.     std::wstring newTitle = *(std::wstring*)lpParam;
  201.     while (running) {
  202.         ModifyWindowTitles(newTitle);
  203.         Sleep(1000);
  204.     }
  205.     return 0;
  206. }
  207.  
  208. std::vector<std::wstring> Split(const std::wstring& s, wchar_t delimiter) {
  209.     std::vector<std::wstring> tokens;
  210.     std::wstring token;
  211.     std::wistringstream tokenStream(s);
  212.     while (std::getline(tokenStream, token, delimiter)) {
  213.         tokens.push_back(token);
  214.     }
  215.     return tokens;
  216. }
  217.  
  218. void ProcessInput(HWND hwnd) {
  219.     static int step = 0;
  220.     static std::wstring newTitle;
  221.     static std::wstring persistence;
  222.  
  223.     wchar_t buffer[256];
  224.     GetWindowText(hTextBox, buffer, 256);
  225.     std::wstring input(buffer);
  226.  
  227.     AppendTextToRichEdit(hRichTextBox, L"> " + input + L"\n");
  228.  
  229.     switch (step) {
  230.     case 0:
  231.         processNames = Split(input, L';');
  232.         AppendTextToRichEdit(hRichTextBox, L"\nEnter the new window title text: \n");
  233.         step++;
  234.         break;
  235.  
  236.     case 1:
  237.         newTitle = input;
  238.         AppendTextToRichEdit(hRichTextBox, L"\n\nDo you want persistence (Y/N)? \n");
  239.         step++;
  240.         break;
  241.  
  242.     case 2:
  243.         persistence = input;
  244.         HANDLE hToken;
  245.         if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
  246.             AppendTextToRichEdit(hRichTextBox, L"OpenProcessToken error: " + std::to_wstring(GetLastError()) + L"\n");
  247.             return;
  248.         }
  249.  
  250.         if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
  251.             AppendTextToRichEdit(hRichTextBox, L"Failed to enable debug privilege.\n");
  252.             CloseHandle(hToken);
  253.             return;
  254.         }
  255.  
  256.         EnumWindows(EnumWindowsProc, (LPARAM)&processNames);
  257.  
  258.         if (targetProcesses.empty()) {
  259.             AppendTextToRichEdit(hRichTextBox, L"No windows found for the specified processes.\n");
  260.         }
  261.         else {
  262.             if (persistence == L"Y" || persistence == L"y") {
  263.                 std::thread titleModifierThread(TitleModifierThread, &newTitle);
  264.                 titleModifierThread.detach();
  265.  
  266.                 AppendTextToRichEdit(hRichTextBox, L"Window titles are being modified persistently. Press Enter to stop and exit...\n");
  267.             }
  268.             else {
  269.                 ModifyWindowTitles(newTitle);
  270.                 AppendTextToRichEdit(hRichTextBox, L"Window titles modified once. Press Enter to exit...\n");
  271.             }
  272.         }
  273.  
  274.         SetPrivilege(hToken, SE_DEBUG_NAME, FALSE);
  275.         CloseHandle(hToken);
  276.        
  277.         // Reset to the beginning
  278.         step = 0;
  279.         AppendTextToRichEdit(hRichTextBox, L"\nEnter the process names separated by semicolons (e.g., notepad.exe;calc.exe): \n");
  280.         break;
  281.     }
  282.  
  283.     SetWindowText(hTextBox, L"");
  284. }
  285.  
  286.  
  287. LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  288.     switch (uMsg) {
  289.     case WM_CREATE:
  290.         g_mainHwnd = hwnd; // Set the global main window handle
  291.         InstallHook(hwnd);
  292.         hRichTextBox = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL,
  293.             WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_VSCROLL,
  294.             10, 10, 372, 200, hwnd, (HMENU)1, NULL, NULL);
  295.         hTextBox = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_MULTILINE,
  296.             10, 220, 280, 20, hwnd, (HMENU)2, NULL, NULL);
  297.         hButton = CreateWindowEx(0, L"BUTTON", L"Send", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  298.             300, 220, 80, 20, hwnd, (HMENU)3, NULL, NULL);
  299.         CreateWindowEx(0, L"BUTTON", L"List", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  300.             300, 250, 80, 20, hwnd, (HMENU)5, NULL, NULL);
  301.         hCloseButton = CreateWindowEx(0, L"BUTTON", L"Close", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  302.             10, 250, 290, 20, hwnd, (HMENU)4, NULL, NULL);
  303.         hLabel = CreateWindowEx(0, L"STATIC", L"2024 (c) Entisoft Software. All Rights Reserved. Evans Thorpemorton.", WS_CHILD | WS_VISIBLE | SS_CENTER,
  304.             10, 280, 372, 20, hwnd, (HMENU)6, NULL, NULL);
  305.  
  306.         AppendTextToRichEdit(hRichTextBox, L"Enter the process names separated by semicolons (e.g., notepad.exe;calc.exe): \n");
  307.         SetTimer(hwnd, 1, 10, NULL);
  308.  
  309.         SCROLLINFO si;
  310.         si.cbSize = sizeof(si);
  311.         si.fMask = SIF_ALL;
  312.         si.nMin = 0;
  313.         si.nMax = 100;
  314.         si.nPage = 10;
  315.         si.nPos = 0;
  316.  
  317.         SetScrollInfo(hRichTextBox, SB_VERT, &si, TRUE);
  318.  
  319.         // Set focus on the input field
  320.         SetFocus(hTextBox);
  321.         //InstallHook(hwnd);
  322.         break;
  323.  
  324.     case WM_USER + 1:
  325.         switch (wParam) {
  326.         case VK_ESCAPE:
  327.             //if (g_hListDialog && IsWindow(g_hListDialog)) {
  328.             //    EndDialog(g_hListDialog, 0);
  329.             //}
  330.             //else {                
  331.                 DestroyWindow(hwnd);
  332.             //}
  333.             return 0;
  334.         case VK_F1:
  335.             OpenListDialog(hwnd);
  336.             return 0;
  337.         }
  338.         break;
  339.  
  340.     case WM_CLOSE:
  341.         if (g_hListDialog && IsWindow(g_hListDialog)) {
  342.             EndDialog(g_hListDialog, 0);
  343.             g_hListDialog = NULL;
  344.         DestroyWindow(hwnd);
  345.             return 0; // Prevent the main window from closing if we just closed the dialog
  346.         }
  347.         // Allow the window to close normally if there's no dialog open
  348.         break;
  349.  
  350.     case WM_COMMAND:
  351.         if (LOWORD(wParam) == 3) {
  352.             ProcessInput(hwnd);
  353.         }
  354.         else if (LOWORD(wParam) == 4) {
  355.             PostQuitMessage(0);
  356.         }
  357.         /*else if (LOWORD(wParam) == 5) {
  358.             DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DialogProc);
  359.         }*/
  360.         if (LOWORD(wParam) == 5) { // ID for opening the list dialog
  361.             OpenListDialog(hwnd);
  362.             return 0;
  363.         }
  364.         break;
  365.  
  366.     case WM_TIMER:
  367.         if (wParam == 1) {
  368.             if (GetFocus() == hTextBox) {
  369.                 if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
  370.                     static DWORD lastEnterTime = 0;
  371.                     DWORD currentTime = GetTickCount();
  372.                     if (currentTime - lastEnterTime > 500) {
  373.                         ProcessInput(hwnd);
  374.                         lastEnterTime = currentTime;
  375.                     }
  376.                     return 0;
  377.                 }
  378.                 if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState('A') & 0x8000)) {
  379.                     SendMessage(hTextBox, EM_SETSEL, 0, -1);
  380.                     return 0;
  381.                 }
  382.                 if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState(VK_BACK) & 0x8000)) {
  383.                     static DWORD lastCtrlBackspaceTime = 0;
  384.                     DWORD currentTime = GetTickCount();
  385.                     if (currentTime - lastCtrlBackspaceTime > 100) {
  386.                         DeleteWordToLeft(hTextBox);
  387.                         lastCtrlBackspaceTime = currentTime;
  388.                     }
  389.                     return 0;
  390.                 }
  391.                 if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState('C') & 0x8000)) {
  392.                     static DWORD lastCtrlCTime = 0;
  393.                     DWORD currentTime = GetTickCount();
  394.                     if (currentTime - lastCtrlCTime > 500) {
  395.                         SendMessage(hTextBox, WM_COPY, 0, 0);
  396.                         lastCtrlCTime = currentTime;
  397.                     }
  398.                     return 0;
  399.                 }
  400.             }
  401.         }
  402.         if (wParam == 2) {
  403.             KillTimer(hwnd, 2);
  404.             isPasting = false;
  405.         }
  406.         else if (wParam == 3) {
  407.             KillTimer(hwnd, 3);
  408.             isCopying = false;
  409.         }
  410.         break;
  411.  
  412.     case WM_KEYDOWN:
  413.         switch (wParam) {
  414.         case VK_ESCAPE:
  415.             PostQuitMessage(0); // Exit the application
  416.             return 0;
  417.         case VK_F1:
  418.             DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DialogProc); // Open List window
  419.             return 0;
  420.         }
  421.         break;
  422.         //break;
  423.         if (GetFocus() == hTextBox) {
  424.             if (wParam == 'V' && (GetKeyState(VK_CONTROL) & 0x8000)) {
  425.                 if (!isPasting) {
  426.                     isPasting = true;
  427.  
  428.                     // Store current text
  429.                     int textLength = GetWindowTextLength(hTextBox);
  430.                     std::wstring currentText(textLength + 1, L'\0');
  431.                     GetWindowText(hTextBox, &currentText[0], textLength + 1);
  432.  
  433.                     // Perform paste
  434.                     SendMessage(hTextBox, WM_PASTE, 0, 0);
  435.  
  436.                     // Get new text
  437.                     int newTextLength = GetWindowTextLength(hTextBox);
  438.                     std::wstring newText(newTextLength + 1, L'\0');
  439.                     GetWindowText(hTextBox, &newText[0], newTextLength + 1);
  440.  
  441.                     // If text didn't change, revert
  442.                     if (newText == currentText) {
  443.                         SetWindowText(hTextBox, currentText.c_str());
  444.                     }
  445.  
  446.                     // Set a timer to reset isPasting
  447.                     SetTimer(hwnd, 2, 100, NULL);
  448.                 }
  449.                 return 0;
  450.             }
  451.             else if (wParam == 'C' && (GetKeyState(VK_CONTROL) & 0x8000)) {
  452.                 if (!isCopying) {
  453.                     isCopying = true;
  454.                     SendMessage(hTextBox, WM_COPY, 0, 0);
  455.                     SetTimer(hwnd, 3, 100, NULL);
  456.                 }
  457.                 return 0;
  458.             }
  459.             break;
  460.         }
  461.  
  462.     case WM_KEYUP:
  463.         if (wParam == 'V' || wParam == 'C' || wParam == VK_CONTROL) {
  464.             isPasting = false;
  465.             isCopying = false;
  466.         }
  467.         break;
  468.  
  469.     case WM_GETDLGCODE:
  470.         if (wParam == VK_ESCAPE) {
  471.             return DLGC_WANTALLKEYS;
  472.         }
  473.         if (wParam == VK_F1) {
  474.             return DLGC_WANTALLKEYS | DLGC_WANTCHARS;
  475.         }
  476.         break;
  477.  
  478.     case WM_GETMINMAXINFO:
  479.     {
  480.         MINMAXINFO* mmi = (MINMAXINFO*)lParam;
  481.         mmi->ptMinTrackSize.x = 400; // Set your desired minimum width
  482.         mmi->ptMinTrackSize.y = 350; // Set your desired minimum height
  483.         mmi->ptMaxTrackSize.x = 400; // Set your desired maximum width
  484.         mmi->ptMaxTrackSize.y = 350; // Set your desired maximum height
  485.         return 0;
  486.     }
  487.  
  488.     case WM_DESTROY:
  489.         UninstallHook();
  490.         if (g_hListDialog) {
  491.             EndDialog(g_hListDialog, 0); // Ensure the dialog is closed on exit
  492.         }
  493.         running = false;
  494.         KillTimer(hwnd, 1);
  495.         PostQuitMessage(0);
  496.         break;
  497.  
  498.     case WM_SIZE:
  499.         MoveWindow(hLabel, 10, HIWORD(lParam) - 20, 372, 20, TRUE);
  500.         break;
  501.  
  502.     default:
  503.         return DefWindowProc(hwnd, uMsg, wParam, lParam);
  504.     }
  505.  
  506.     return 0;
  507. }
  508.  
  509. /*
  510. void OpenListDialog(HWND parentHwnd) {
  511.     g_hListDialog = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), parentHwnd, DialogProc);
  512.     if (g_hListDialog != NULL) {
  513.         ShowWindow(g_hListDialog, SW_SHOW);
  514.     }
  515. }*/
  516.  
  517. INT_PTR CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {    
  518.     switch (message) {
  519.     case WM_INITDIALOG:
  520.     {
  521.         // Center the dialog on the screen
  522.         RECT rc, rcDlg, rcOwner;
  523.         GetWindowRect(GetDesktopWindow(), &rc);
  524.         GetWindowRect(hDlg, &rcDlg);
  525.         int x = (rc.right - rc.left - (rcDlg.right - rcDlg.left)) / 2;
  526.         int y = (rc.bottom - rc.top - (rcDlg.bottom - rcDlg.top)) / 2;
  527.         SetWindowPos(hDlg, HWND_TOP, x, y, 0, 0, SWP_NOSIZE);
  528.  
  529.         hListView = GetDlgItem(hDlg, IDC_LIST1);
  530.         ListView_SetExtendedListViewStyle(hListView, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
  531.  
  532.         LVCOLUMN lvc = { 0 };
  533.         lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
  534.  
  535.         lvc.iSubItem = 0;
  536.         lvc.pszText = (LPWSTR)L"Icon";
  537.         lvc.cx = 50;
  538.         ListView_InsertColumn(hListView, 0, &lvc);
  539.  
  540.         lvc.iSubItem = 1;
  541.         lvc.pszText = (LPWSTR)L"Window Title";
  542.         lvc.cx = 200;
  543.         ListView_InsertColumn(hListView, 1, &lvc);
  544.  
  545.         lvc.iSubItem = 2;
  546.         lvc.pszText = (LPWSTR)L"Process Name";
  547.         lvc.cx = 150;
  548.         ListView_InsertColumn(hListView, 2, &lvc);
  549.  
  550.         allProcesses.clear();
  551.         EnumWindows(EnumWindowsProc, (LPARAM)&processNames);
  552.  
  553.         for (const auto& process : allProcesses) {
  554.             LVITEM lvi = { 0 };
  555.             lvi.mask = LVIF_TEXT | LVIF_IMAGE;
  556.             lvi.iItem = ListView_GetItemCount(hListView);
  557.  
  558.             // Get the icon for the process
  559.             HICON hIcon = NULL;
  560.             SHFILEINFO sfi = { 0 };
  561.             if (SHGetFileInfo(process.processName.c_str(), 0, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SMALLICON)) {
  562.                 hIcon = sfi.hIcon;
  563.             }
  564.  
  565.             // Create an image list if it doesn't exist
  566.             HIMAGELIST hImageList = ListView_GetImageList(hListView, LVSIL_SMALL);
  567.             if (!hImageList) {
  568.                 hImageList = ImageList_Create(16, 16, ILC_COLOR32 | ILC_MASK, 1, 1);
  569.                 ListView_SetImageList(hListView, hImageList, LVSIL_SMALL);
  570.             }
  571.  
  572.             // Add the icon to the image list
  573.             int imageIndex = -1;
  574.             if (hIcon) {
  575.                 imageIndex = ImageList_AddIcon(hImageList, hIcon);
  576.                 DestroyIcon(hIcon);
  577.             }
  578.  
  579.             lvi.iImage = imageIndex;
  580.             ListView_InsertItem(hListView, &lvi);
  581.  
  582.             ListView_SetItemText(hListView, lvi.iItem, 1, (LPWSTR)process.windowTitle.c_str());
  583.             ListView_SetItemText(hListView, lvi.iItem, 2, (LPWSTR)process.processName.c_str());
  584.         }
  585.  
  586.         return (INT_PTR)TRUE;
  587.     }
  588.  
  589.     case WM_COMMAND:
  590.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
  591.             EndDialog(hDlg, LOWORD(wParam));
  592.             g_hListDialog = NULL; // Reset when dialog closes
  593.             return (INT_PTR)TRUE;
  594.         }
  595.         break;
  596.  
  597.     case WM_CLOSE:
  598.         EndDialog(hDlg, 0);
  599.         g_hListDialog = NULL; // Reset when dialog closes
  600.         ResetFocusAndHookState(); // Reset focus or hook state here
  601.         return (INT_PTR)TRUE;
  602.     //}
  603.     //return (INT_PTR)FALSE;
  604. //}
  605.  
  606.     case WM_KEYDOWN:
  607.         if (wParam == VK_ESCAPE) {
  608.             EndDialog(hDlg, 0); // Close the dialog when Escape is pressed
  609.             return (INT_PTR)TRUE;
  610.         }
  611.         break;
  612.  
  613.     case WM_GETDLGCODE:
  614.         if (wParam == VK_ESCAPE) {
  615.             return DLGC_WANTALLKEYS;
  616.         }
  617.         break;
  618.     }
  619.     return (INT_PTR)FALSE;
  620. }
  621.  
  622. void DeleteWordToLeft(HWND hWnd) {
  623.     DWORD start, end;
  624.     SendMessage(hWnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
  625.     if (start == 0) return;
  626.  
  627.     std::wstring text(GetWindowTextLength(hWnd), L'\0');
  628.     GetWindowText(hWnd, &text[0], text.size() + 1);
  629.     text.resize(text.size() - 1);
  630.  
  631.     size_t pos = start - 1;
  632.     while (pos > 0 && iswspace(text[pos])) {
  633.         --pos;
  634.     }
  635.     while (pos > 0 && !iswspace(text[pos])) {
  636.         --pos;
  637.     }
  638.  
  639.     text.erase(pos, start - pos);
  640.     SetWindowText(hWnd, text.c_str());
  641.     SendMessage(hWnd, EM_SETSEL, pos, pos);
  642. }
  643.  
  644. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  645.     WNDCLASSEX wcex;
  646.     wcex.cbSize = sizeof(WNDCLASSEX);
  647.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  648.     wcex.lpfnWndProc = WndProc;
  649.     wcex.cbClsExtra = 0;
  650.     wcex.cbWndExtra = 0;
  651.     wcex.hInstance = hInstance;
  652.     wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  653.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  654.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  655.     wcex.lpszMenuName = NULL;
  656.     wcex.lpszClassName = L"WindowTitleModifierClass";
  657.     wcex.hIconSm = (HICON)LoadImage(wcex.hInstance,
  658.         MAKEINTRESOURCE(IDI_ICON1),
  659.         IMAGE_ICON,
  660.         16,    // Small icon width
  661.         16,    // Small icon height
  662.         LR_DEFAULTCOLOR);
  663.     //wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
  664.  
  665.     if (!RegisterClassEx(&wcex)) {
  666.         MessageBox(NULL, L"Window Registration Failed!", L"Error", MB_ICONEXCLAMATION | MB_OK);
  667.         return 0;
  668.     }
  669.  
  670.     // Get the screen dimensions
  671.     int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  672.     int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  673.  
  674.     // Set window dimensions
  675.     int windowWidth = 400;
  676.     int windowHeight = 350;
  677.  
  678.     // Calculate the position for the centered window
  679.     int posX = (screenWidth - windowWidth) / 2;
  680.     int posY = (screenHeight - windowHeight) / 2;
  681.  
  682.     HWND hWnd = CreateWindowEx(
  683.         //WS_EX_CLIENTEDGE,
  684.         WS_EX_NOMAXIMIZEBOX,
  685.         L"WindowTitleModifierClass",
  686.         L"Window Title Modifier (F1=ProcessList)",
  687.         //WS_OVERLAPPEDWINDOW,
  688.         //WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,
  689.         WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
  690.         posX, posY, windowWidth, windowHeight,
  691.         NULL, NULL, hInstance, NULL);
  692.  
  693.     if (hWnd == NULL) {
  694.         MessageBox(NULL, L"Window Creation Failed!", L"Error", MB_ICONEXCLAMATION | MB_OK);
  695.         return 0;
  696.     }
  697.  
  698.     ShowWindow(hWnd, nCmdShow);
  699.     UpdateWindow(hWnd);
  700.  
  701.     MSG msg;
  702.     while (GetMessage(&msg, NULL, 0, 0)) {
  703.         TranslateMessage(&msg);
  704.         DispatchMessage(&msg);
  705.     }
  706.  
  707.     return (int)msg.wParam;
  708. }
  709.  
  710. ==++"AdvTitleChanger" File 2/2 SourceCode::++==
  711. IDD_DIALOG1 DIALOGEX 0, 0, 400, 300
  712. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  713. CAPTION "Window Titles and Processes"
  714. FONT 8, "MS Shell Dlg", 400, 0, 0x1
  715. BEGIN
  716. CONTROL         "", IDC_LIST1, "SysListView32", LVS_REPORT | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP, 7, 7, 386, 266
  717. DEFPUSHBUTTON   "OK", IDOK, 339, 279, 50, 14
  718. END
  719.  
Tags: #multifile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement