1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>
#pragma comment(lib, "comctl32.lib")
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#define NDEBUG
#define ID_LIST 1
#define ID_BUTTON 2
struct ClipboardItem {
std::string text;
};
std::vector<ClipboardItem> clipboardItems;
HINSTANCE hinst;
HWND hwndListView, hwndButton;
std::string currentClipboardText;
int selectedIndex = -1;
bool simulateCtrlV = false; // État de la simulation
HHOOK hMouseHook = NULL;
// Ajoute un élément si ce n'est pas un doublon
void AddClipboardItem(const std::string& text) {
if (!clipboardItems.empty() && clipboardItems.back().text == text)
return;
if (selectedIndex != -1 && clipboardItems[selectedIndex].text == text)
return;
ClipboardItem item{text};
clipboardItems.push_back(item);
LVITEM lvItem = {};
lvItem.mask = LVIF_TEXT;
lvItem.iItem = clipboardItems.size() - 1;
lvItem.iSubItem = 0;
lvItem.pszText = const_cast<char*>(item.text.c_str());
SendMessage(hwndListView, LVM_INSERTITEM, 0, (LPARAM)&lvItem);
InvalidateRect(hwndListView, NULL, TRUE); // Rafraîchir la ListView
}
// Surveille le presse-papiers et met à jour la couleur
void MonitorClipboard(HWND hwnd) {
if (OpenClipboard(hwnd)) {
HANDLE hData = GetClipboardData(CF_TEXT);
if (hData) {
char* pszText = static_cast<char*>(GlobalLock(hData));
if (pszText) {
std::string newClipboardText = pszText;
GlobalUnlock(hData);
if (newClipboardText != currentClipboardText) {
currentClipboardText = newClipboardText;
AddClipboardItem(newClipboardText);
InvalidateRect(hwndListView, NULL, TRUE); // Met à jour la ListView
}
}
}
CloseClipboard();
}
}
// Copie un texte dans le presse-papiers
void CopyToClipboard(HWND hwnd, const std::string& text) {
if (OpenClipboard(hwnd)) {
EmptyClipboard();
HGLOBAL hClipboardData = GlobalAlloc(GMEM_MOVEABLE, text.size() + 1);
if (hClipboardData) {
char* pchData = static_cast<char*>(GlobalLock(hClipboardData));
if (pchData) {
strcpy(pchData, text.c_str());
GlobalUnlock(hClipboardData);
SetClipboardData(CF_TEXT, hClipboardData);
}
}
CloseClipboard();
}
}
// Simule Ctrl+V dans la fenêtre cible
void SimulateCtrlV(HWND hwndTarget) {
INPUT CTRLVInputs[4] = {0};
CTRLVInputs[0].type = INPUT_KEYBOARD;
CTRLVInputs[0].ki.wVk = VK_CONTROL; // Press Ctrl
CTRLVInputs[0].ki.dwFlags = 0; // Pas de flag, press normal
CTRLVInputs[1].type = INPUT_KEYBOARD;
CTRLVInputs[1].ki.wVk = 'V'; // Press V
CTRLVInputs[1].ki.dwFlags = 0; // Pas de flag, press normal
CTRLVInputs[2].type = INPUT_KEYBOARD;
CTRLVInputs[2].ki.wVk = 'V'; // Release V
CTRLVInputs[2].ki.dwFlags = KEYEVENTF_KEYUP;
CTRLVInputs[3].type = INPUT_KEYBOARD;
CTRLVInputs[3].ki.wVk = VK_CONTROL; // Release Ctrl
CTRLVInputs[3].ki.dwFlags = KEYEVENTF_KEYUP; // Release normal
// Attacher les threads d'entrée pour garantir que les entrées sont envoyées à la fenêtre active
DWORD foregroundThreadId = GetWindowThreadProcessId(hwndTarget, NULL);
DWORD currentThreadId = GetCurrentThreadId();
AttachThreadInput(currentThreadId, foregroundThreadId, TRUE);
#if defined(NDEBUG)
std::cout << "Envoie de l'évenement Ctrl+v\n";
#endif
// Envoyer l'entrée clavier à la fenêtre active
SendInput(4, CTRLVInputs, sizeof(INPUT));
// Détacher les threads d'entrée
AttachThreadInput(currentThreadId, foregroundThreadId, FALSE);
}
// Simule Return dans la fenêtre cible
void SimulateReturn(HWND hwndTarget) {
INPUT enterInput[2] = {0};
enterInput[0].type = INPUT_KEYBOARD;
enterInput[0].ki.wVk = VK_RETURN;
enterInput[1].ki.dwFlags = 0;
enterInput[1].type = INPUT_KEYBOARD;
enterInput[1].ki.wVk = VK_RETURN;
enterInput[1].ki.dwFlags = KEYEVENTF_KEYUP;
// Attacher les threads d'entrée pour garantir que les entrées sont envoyées à la fenêtre active
DWORD foregroundThreadId = GetWindowThreadProcessId(hwndTarget, NULL);
DWORD currentThreadId = GetCurrentThreadId();
AttachThreadInput(currentThreadId, foregroundThreadId, TRUE);
#if defined(NDEBUG)
std::cout << "Envoie de l'évenement entrée\n";
#endif
// Envoyer l'entrée clavier à la fenêtre active
SendInput(2, enterInput, sizeof(INPUT));
// Détacher les threads d'entrée
AttachThreadInput(currentThreadId, foregroundThreadId, FALSE);
}
// Fonction de callback pour le hook de la souris
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION && simulateCtrlV) {
MSLLHOOKSTRUCT* pMouseStruct = (MSLLHOOKSTRUCT*)lParam;
if (wParam == WM_LBUTTONDOWN) {
HWND hwndActive = GetForegroundWindow();
if (hwndActive && IsWindow(hwndActive)) {
char windowTitle[256];
GetWindowTextA(hwndActive, windowTitle, sizeof(windowTitle));
#if defined(NDEBUG)
std::cout << "Fenêtre active: " << windowTitle << std::endl;
#endif
SetForegroundWindow(hwndActive);
SimulateCtrlV(hwndActive);
SimulateReturn(hwndActive);
simulateCtrlV = false; // Désactiver après un seul clic
SetWindowText(hwndButton, "Activer Simulation Ctrl+V");
}
}
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
// Gestion du dessin personnalisé de la ListView
LRESULT HandleCustomDraw(LPARAM lParam) {
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
switch (lplvcd->nmcd.dwDrawStage) {
case CDDS_PREPAINT:
return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT: {
int itemIndex = static_cast<int>(lplvcd->nmcd.dwItemSpec);
if (clipboardItems[itemIndex].text == currentClipboardText) {
lplvcd->clrText = RGB(255, 0, 0); // Rouge
}
return CDRF_NEWFONT;
}
}
return CDRF_DODEFAULT;
}
// Procédure de fenêtre principale
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
INITCOMMONCONTROLSEX icex = { sizeof(INITCOMMONCONTROLSEX), ICC_LISTVIEW_CLASSES };
InitCommonControlsEx(&icex);
hwndListView = CreateWindowEx(0, WC_LISTVIEW, NULL,
WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL,
10, 10, 460, 200, hwnd, (HMENU) ID_LIST, hinst, NULL);
hwndButton = CreateWindow("BUTTON", "Activer Simulation Ctrl+V",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
140, 220, 200, 30, hwnd, (HMENU) ID_BUTTON, hinst, NULL);
LVCOLUMN lvc = {};
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
lvc.fmt = LVCFMT_LEFT;
lvc.cx = 450;
lvc.pszText = const_cast<char*>("Clipboard Items");
SendMessage(hwndListView, LVM_INSERTCOLUMN, 0, (LPARAM)&lvc);
SetTimer(hwnd, 1, 1000, NULL);
// Installer le hook global pour la souris
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hinst, 0);
break;
}
case WM_TIMER:
MonitorClipboard(hwnd);
break;
case WM_COMMAND:
if (LOWORD(wParam) == ID_BUTTON) { // Bouton cliqué
simulateCtrlV = !simulateCtrlV;
SetWindowText(hwndButton, simulateCtrlV ? "Desactiver Simulation Ctrl+V" : "Activer Simulation Ctrl+V");
}
break;
case WM_NOTIFY: {
if (((LPNMHDR)lParam)->hwndFrom == hwndListView) {
if (((LPNMHDR)lParam)->code == LVN_ITEMCHANGED) {
LPNMLISTVIEW pnm = (LPNMLISTVIEW)lParam;
if (pnm->uNewState & LVIS_SELECTED) {
selectedIndex = pnm->iItem;
CopyToClipboard(hwnd, clipboardItems[selectedIndex].text);
}
} else if (((LPNMHDR)lParam)->code == NM_CUSTOMDRAW) {
return HandleCustomDraw(lParam);
}
}
break;
}
case WM_DESTROY:
// Retirer le hook avant de quitter
if (hMouseHook) {
UnhookWindowsHookEx(hMouseHook);
}
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
#if defined(NDEBUG)
// Créer une fenêtre de console si nécessaire
AllocConsole();
freopen("CONOUT$", "w", stdout); // Rediriger stdout vers la console
#endif
HWND hwnd;
MSG msg;
WNDCLASS wc = {};
hinst = hinstance;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MainWndProc;
wc.hInstance = hinstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.lpszClassName = "MaWinClass";
if (!RegisterClass(&wc))
return FALSE;
hwnd = CreateWindow("MaWinClass", "Gestionnaire de Presse-papiers", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hinstance, NULL);
if (!hwnd)
return FALSE;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#if defined(NDEBUG)
FreeConsole();
#endif
return (int)msg.wParam;
} |
Partager