Test.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #include <AsynchronCall.h>
  2. #include <Bildschirm.h>
  3. #include <Console.h>
  4. #include <DateiSystem.h>
  5. #include <Fenster.h>
  6. #include <Globals.h>
  7. #include <iostream>
  8. #include <psapi.h>
  9. #include "TextAnalyser.h"
  10. using namespace Framework;
  11. ConsoleHandler* console;
  12. Datei result;
  13. int imgCount;
  14. Text resultFolder;
  15. void makePicture(HWND hwnd, WFenster* zf)
  16. {
  17. HDC hdcScreen = GetDC(NULL);
  18. HDC hdcWindow = GetDC(hwnd);
  19. HDC hdcMemDC = CreateCompatibleDC(hdcWindow);
  20. HDC hdcMemDC2 = CreateCompatibleDC(hdcWindow);
  21. if (!hdcMemDC)
  22. {
  23. MessageBoxA(hwnd, "CreateCompatibleDC has failed", "Failed", MB_OK);
  24. return;
  25. }
  26. RECT rcClient;
  27. GetWindowRect(hwnd, &rcClient);
  28. HBITMAP hbmScreen = CreateCompatibleBitmap(hdcWindow,
  29. rcClient.right - rcClient.left,
  30. rcClient.bottom - rcClient.top);
  31. if (!hbmScreen)
  32. {
  33. MessageBox(hwnd, "CreateCompatibleBitmap Failed", "Failed", MB_OK);
  34. return;
  35. }
  36. SelectObject(hdcMemDC, hbmScreen);
  37. if (!BitBlt(hdcMemDC,
  38. 0,
  39. 0,
  40. rcClient.right - rcClient.left,
  41. rcClient.bottom - rcClient.top,
  42. hdcScreen,
  43. rcClient.left,
  44. rcClient.top,
  45. SRCCOPY))
  46. {
  47. MessageBox(hwnd, "BitBlt has failed", "Failed", MB_OK);
  48. return;
  49. }
  50. BITMAP bmpScreen;
  51. GetObject(hbmScreen, sizeof(BITMAP), &bmpScreen);
  52. BITMAPINFOHEADER bi;
  53. bi.biSize = sizeof(BITMAPINFOHEADER);
  54. bi.biWidth = bmpScreen.bmWidth;
  55. bi.biHeight = bmpScreen.bmHeight;
  56. bi.biPlanes = 1;
  57. bi.biBitCount = 32;
  58. bi.biCompression = BI_RGB;
  59. bi.biSizeImage = 0;
  60. bi.biXPelsPerMeter = 0;
  61. bi.biYPelsPerMeter = 0;
  62. bi.biClrUsed = 0;
  63. bi.biClrImportant = 0;
  64. DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4
  65. * bmpScreen.bmHeight;
  66. // Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented
  67. // as wrapper functions that call HeapAlloc using a handle to the process's
  68. // default heap. Therefore, GlobalAlloc and LocalAlloc have greater overhead
  69. // than HeapAlloc.
  70. HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
  71. char* lpbitmap = (char*)GlobalLock(hDIB);
  72. // Gets the "bits" from the bitmap, and copies them into a buffer
  73. // that's pointed to by lpbitmap.
  74. GetDIBits(hdcWindow,
  75. hbmScreen,
  76. 0,
  77. (UINT)bmpScreen.bmHeight,
  78. lpbitmap,
  79. (BITMAPINFO*)&bi,
  80. DIB_RGB_COLORS);
  81. TextAnalyser ta(zf);
  82. RECT cutRect = ta.findRect(bmpScreen.bmWidth, bmpScreen.bmHeight, lpbitmap);
  83. if (ta.hasImage())
  84. {
  85. HBITMAP hbmScreen2 = CreateCompatibleBitmap(hdcWindow,
  86. cutRect.right - cutRect.left,
  87. cutRect.bottom - cutRect.top);
  88. SelectObject(hdcMemDC2, hbmScreen2);
  89. if (!BitBlt(hdcMemDC2,
  90. 0,
  91. 0,
  92. cutRect.right - cutRect.left,
  93. cutRect.bottom - cutRect.top,
  94. hdcScreen,
  95. rcClient.left + cutRect.left,
  96. rcClient.top + cutRect.top,
  97. SRCCOPY))
  98. {
  99. MessageBox(hwnd, "BitBlt has failed", "Failed", MB_OK);
  100. return;
  101. }
  102. BITMAP bmpScreen2;
  103. GetObject(hbmScreen2, sizeof(BITMAP), &bmpScreen2);
  104. BITMAPFILEHEADER bmfHeader;
  105. BITMAPINFOHEADER bi2;
  106. bi2.biSize = sizeof(BITMAPINFOHEADER);
  107. bi2.biWidth = bmpScreen2.bmWidth;
  108. bi2.biHeight = bmpScreen2.bmHeight;
  109. bi2.biPlanes = 1;
  110. bi2.biBitCount = 32;
  111. bi2.biCompression = BI_RGB;
  112. bi2.biSizeImage = 0;
  113. bi2.biXPelsPerMeter = 0;
  114. bi2.biYPelsPerMeter = 0;
  115. bi2.biClrUsed = 0;
  116. bi2.biClrImportant = 0;
  117. DWORD dwBmpSize2 = ((bmpScreen2.bmWidth * bi2.biBitCount + 31) / 32) * 4
  118. * bmpScreen2.bmHeight;
  119. // Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are
  120. // implemented as wrapper functions that call HeapAlloc using a handle
  121. // to the process's default heap. Therefore, GlobalAlloc and LocalAlloc
  122. // have greater overhead than HeapAlloc.
  123. HANDLE hDIB2 = GlobalAlloc(GHND, dwBmpSize2);
  124. char* lpbitmap2 = (char*)GlobalLock(hDIB2);
  125. // Gets the "bits" from the bitmap, and copies them into a buffer
  126. // that's pointed to by lpbitmap.
  127. GetDIBits(hdcWindow,
  128. hbmScreen2,
  129. 0,
  130. (UINT)bmpScreen2.bmHeight,
  131. lpbitmap2,
  132. (BITMAPINFO*)&bi2,
  133. DIB_RGB_COLORS);
  134. // A file is created, this is where we will save the screen capture.
  135. Text imgName(imgCount++);
  136. imgName += ".bmp";
  137. HANDLE hFile = CreateFile(Text("output/") + imgName.getText(),
  138. GENERIC_WRITE,
  139. 0,
  140. NULL,
  141. CREATE_ALWAYS,
  142. FILE_ATTRIBUTE_NORMAL,
  143. NULL);
  144. DWORD dwSizeofDIB
  145. = dwBmpSize2 + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  146. // Offset to where the actual bitmap bits start.
  147. bmfHeader.bfOffBits
  148. = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
  149. // Size of the file.
  150. bmfHeader.bfSize = dwSizeofDIB;
  151. // bfType must always be BM for Bitmaps.
  152. bmfHeader.bfType = 0x4D42; // BM.
  153. DWORD dwBytesWritten = 0;
  154. WriteFile(hFile,
  155. (LPSTR)&bmfHeader,
  156. sizeof(BITMAPFILEHEADER),
  157. &dwBytesWritten,
  158. NULL);
  159. WriteFile(hFile,
  160. (LPSTR)&bi2,
  161. sizeof(BITMAPINFOHEADER),
  162. &dwBytesWritten,
  163. NULL);
  164. WriteFile(hFile, (LPSTR)lpbitmap2, dwBmpSize2, &dwBytesWritten, NULL);
  165. // Unlock and Free the DIB from the heap.
  166. GlobalUnlock(hDIB2);
  167. GlobalFree(hDIB2);
  168. // Close the handle for the file that was created.
  169. CloseHandle(hFile);
  170. DeleteObject(hbmScreen2);
  171. Text imgTag;
  172. imgTag.append() << "<img src=\"" << imgName.getText() << "\"></img>\n";
  173. result.schreibe(imgTag.getText(), imgTag.getLength());
  174. }
  175. else
  176. {
  177. ta.writeToFile(result);
  178. }
  179. GlobalUnlock(hDIB);
  180. GlobalFree(hDIB);
  181. DeleteObject(hbmScreen);
  182. DeleteObject(hdcMemDC);
  183. DeleteObject(hdcMemDC2);
  184. ReleaseDC(NULL, hdcScreen);
  185. ReleaseDC(hwnd, hdcWindow);
  186. }
  187. void nextPage(HWND hwnd, unsigned int scanCode)
  188. {
  189. SetForegroundWindow(hwnd);
  190. SetFocus(hwnd);
  191. SendMessageA(hwnd, WM_KEYDOWN, VK_RIGHT, 1 | ((scanCode & 0xFF) << 16));
  192. SendMessageA(hwnd, WM_KEYUP, VK_RIGHT, 1 | ((scanCode & 0xFF) << 16));
  193. Sleep(10000);
  194. }
  195. void doWork(HWND hwnd, WFenster* zf)
  196. {
  197. result.setDatei("output/index.html");
  198. if (!result.existiert())
  199. {
  200. result.erstellen();
  201. }
  202. result.open(
  203. Datei::Style::lesen | Datei::Style::schreiben | Datei::Style::ende);
  204. resultFolder = "output";
  205. Datei outF;
  206. outF.setDatei(resultFolder);
  207. imgCount = 5;
  208. RCArray<Text>* subFiles = outF.getDateiListe();
  209. for (Text* t : *subFiles)
  210. {
  211. if (t->hatAt(t->getLength() - 4, ".bmp"))
  212. {
  213. imgCount++;
  214. }
  215. }
  216. subFiles->release();
  217. RCArray<Framework::Text> commands;
  218. Critical cs;
  219. new AsynchronCall([&commands, &cs]() {
  220. while (true)
  221. {
  222. std::string line;
  223. getline(std::cin, line);
  224. cs.lock();
  225. commands.add(new Text(line.c_str()));
  226. cs.unlock();
  227. }
  228. });
  229. DWORD dwPID = 0;
  230. DWORD hThread = GetWindowThreadProcessId(hwnd, &dwPID);
  231. HKL hkl = GetKeyboardLayout(hThread);
  232. unsigned int scanCode = MapVirtualKeyExW(VK_RIGHT, MAPVK_VK_TO_VSC, hkl);
  233. bool ok = 1;
  234. while (ok)
  235. {
  236. makePicture(hwnd, zf);
  237. std::cout << "Page completed" << std::endl;
  238. cs.lock();
  239. while (commands.getEintragAnzahl() > 0)
  240. {
  241. Text* command = commands.z(0);
  242. commands.remove(0);
  243. if (command->istGleich("exit"))
  244. {
  245. ok = 0;
  246. }
  247. command->release();
  248. }
  249. cs.unlock();
  250. result.close();
  251. result.open(
  252. Datei::Style::lesen | Datei::Style::schreiben | Datei::Style::ende);
  253. nextPage(hwnd, scanCode);
  254. }
  255. result.close();
  256. }
  257. BOOL CALLBACK speichereFenster(HWND hwnd, LPARAM lParam)
  258. {
  259. const DWORD TITLE_SIZE = 1024;
  260. char processName[TITLE_SIZE];
  261. unsigned long pid = 0;
  262. GetWindowThreadProcessId(hwnd, &pid);
  263. HANDLE process
  264. = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
  265. if (process == nullptr)
  266. {
  267. return TRUE; // Skip this window if we can't open the process
  268. }
  269. // get process name
  270. int result = GetModuleBaseNameA(process, NULL, processName, TITLE_SIZE);
  271. Text txt(processName);
  272. if (txt.hatAt(0, "msedge"))
  273. {
  274. RECT rect;
  275. GetWindowRect(hwnd, &rect);
  276. GetWindowTextA(hwnd, processName, TITLE_SIZE);
  277. Text txt2(processName);
  278. if (txt2.hatAt(0, "Kindle"))
  279. {
  280. std::cout << "Found Kindle window!" << std::endl;
  281. doWork(hwnd, (WFenster*)lParam);
  282. }
  283. }
  284. return TRUE;
  285. }
  286. void updateChars(const char* result, WFenster* zf)
  287. {
  288. std::cout << "list all known chars '" << result << "'" << std::endl;
  289. Bild* b = new Bild();
  290. int factor = 5;
  291. for (int i = 0; i < chars->getEintragAnzahl(); i++)
  292. {
  293. CharMask* charMask = chars->get(i);
  294. if (charMask->getResult().istGleich(result))
  295. {
  296. b->neuBild(charMask->getWidth() * factor,
  297. charMask->getHeight() * factor,
  298. 0xFF000000);
  299. for (int x = 0; x < charMask->getWidth() * factor; x++)
  300. {
  301. for (int y = 0; y < charMask->getHeight() * factor; y++)
  302. {
  303. if (charMask->getMask()[(y / factor) * charMask->getWidth()
  304. + x / factor])
  305. {
  306. b->setPixelDP(x, y, 0xFFFFFFFF);
  307. }
  308. }
  309. }
  310. BildZ* z = new Framework::BildZ();
  311. z->setBildZ(dynamic_cast<Bild*>(b->getThis()));
  312. z->setSize(
  313. charMask->getWidth() * factor, charMask->getHeight() * factor);
  314. z->setPosition(5, 5);
  315. z->setStyle(Framework::BildZ::Style::Sichtbar
  316. | Framework::BildZ::Style::Erlaubt);
  317. zf->zBildschirm()->addMember(z);
  318. zf->zBildschirm()->setBackBufferSize(
  319. charMask->getWidth() * factor + 10,
  320. charMask->getHeight() * factor + 10);
  321. zf->setSize(charMask->getWidth() * factor + 10,
  322. charMask->getHeight() * factor + 10);
  323. zf->zBildschirm()->update();
  324. zf->zBildschirm()->render();
  325. zf->zBildschirm()->render();
  326. std::string command;
  327. std::getline(std::cin, command);
  328. if (command.starts_with("del"))
  329. {
  330. std::cout << "deleted." << std::endl;
  331. chars->remove(i);
  332. i--;
  333. saveChars();
  334. }
  335. if (command.starts_with("exit"))
  336. {
  337. i = chars->getEintragAnzahl();
  338. }
  339. zf->zBildschirm()->removeMember(z);
  340. }
  341. }
  342. b->release();
  343. std::cout << "finished editing masks for '" << result << "'" << std::endl;
  344. }
  345. int main()
  346. {
  347. initFramework();
  348. loadChars();
  349. LTDSDatei font;
  350. font.setPfad(new Framework::Text("normal.ltds"));
  351. font.leseDaten();
  352. schrift = font.ladeSchrift();
  353. WNDCLASS wc = F_Normal(GetModuleHandle(NULL));
  354. wc.lpszClassName = "Kindle Grabber";
  355. WFenster* f = new WFenster();
  356. f->erstellen(WS_POPUP, wc);
  357. f->setMausAktion(_ret1ME);
  358. f->setTastaturAktion(_ret1TE);
  359. f->setAnzeigeModus(SW_SHOWNORMAL);
  360. f->setSize(100, 100);
  361. f->setPosition(Punkt(100, 100));
  362. Bildschirm* screen = new Bildschirm3D(
  363. dynamic_cast<WFenster*>(f->getThis()), GraphicApiType::DIRECTX11);
  364. screen->setRenderZeichnungen(1);
  365. screen->setdeckFarbe(0xFF000000);
  366. screen->setFill(1);
  367. screen->setTestRend(0);
  368. f->setBildschirm(screen);
  369. std::string response;
  370. std::getline(std::cin, response);
  371. while (response.length() > 0)
  372. {
  373. updateChars(response.c_str(), f);
  374. std::getline(std::cin, response);
  375. }
  376. new AsynchronCall(
  377. "Kindle Grabber",
  378. [f]() { EnumWindows(speichereFenster, reinterpret_cast<LPARAM>(f)); },
  379. 0);
  380. Framework::StartNachrichtenSchleife();
  381. return 0;
  382. }