Advertisement
Guest User

Functions.h

a guest
Jul 11th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <Windows.h>
  5. #include <windef.h>
  6. #include <fstream>
  7. #include <time.h>
  8. #include <ole2.h>
  9. #include <olectl.h>
  10. using namespace std;
  11.  
  12. HWND FindLolClient(CONST LPCSTR handlename, CONST LPCSTR childname, HWND &RCLIENT)
  13. {
  14.     HWND Leaguehwnd, Clienthwnd;
  15.     int MessageBox_Value;
  16.     CHAR TargetNameClass[50];
  17.  
  18.     do
  19.     {
  20.  
  21.         Leaguehwnd = FindWindow(handlename, NULL);
  22.         Clienthwnd = FindWindowEx(Leaguehwnd, NULL, childname, NULL);
  23.  
  24.         if (!Clienthwnd)
  25.         {
  26.             cout << "Finestra non trovata...\n";
  27.             MessageBox_Value = MessageBox(NULL, "Impossibile trovare l'handle di League Of Legends...", "Errore", MB_RETRYCANCEL);
  28.  
  29.             if (MessageBox_Value == IDCANCEL) return NULL;
  30.            
  31.         }
  32.         else
  33.         {
  34.             Clienthwnd = GetWindow(Clienthwnd, GW_CHILD);
  35.             Clienthwnd = GetWindow(Clienthwnd, GW_CHILD);
  36.             GetClassNameA(Clienthwnd, TargetNameClass, 49);
  37.             cout << "Nome della classe trovata: " << TargetNameClass << endl;
  38.             RCLIENT = Leaguehwnd;
  39.             return Clienthwnd;
  40.         }
  41.  
  42.     } while (true);
  43.  
  44. }
  45.  
  46. BOOL CenterWindow(HWND hwnd, int &xPos, int &yPos, int &width, int &height, RECT &rc)
  47. {
  48.     GetWindowRect(hwnd, &rc);
  49.  
  50.     width  = GetSystemMetrics(SM_CXSCREEN);
  51.     height = GetSystemMetrics(SM_CYSCREEN);
  52.  
  53.     xPos = (width / 2) -  ((rc.right-rc.left) / 2);
  54.     yPos = (height / 2) - ((rc.bottom - rc.top) / 2);
  55.  
  56.     SetWindowPos(hwnd, 0, xPos, yPos, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
  57.  
  58.     cout <<"Finestra spostata alle coordinate:\nx = " << xPos << "\ny = " << yPos << endl;
  59.  
  60.     return TRUE;
  61.  
  62. }
  63.  
  64. BOOL IsWindowCentred(HWND hwnd, int x, int y, int width, int height, RECT rc)
  65. {
  66.  
  67.     int xPos = (width / 2) - ((rc.right - rc.left) / 2);
  68.     int yPos = (height / 2) - ((rc.bottom - rc.top) / 2);
  69.  
  70.     if (xPos == x && yPos == y) return TRUE;
  71.     else return FALSE;
  72.  
  73. }
  74.  
  75. void CreateTextFile(char Text[])
  76. {
  77.     char myPath[_MAX_PATH + 1];
  78.     GetModuleFileName(NULL, myPath, _MAX_PATH);
  79.  
  80.     char filename[] = "EXIT_FAILURE.txt";
  81.     ofstream myfile;
  82.     myfile.open("EXIT_FAILURE.txt", ofstream::out | ofstream::app);
  83.  
  84.     time_t now = time(0);
  85.     tm* localtm = localtime(&now);
  86.  
  87.     myfile << "-" << asctime(localtm) << Text;
  88.  
  89.     myfile.close();
  90.     myfile.clear();
  91.  
  92.     cout << "Creato file di debug all'indirizzo dell'exe...\n"<<myPath << endl;
  93.  
  94.     return;
  95. }
  96.  
  97. bool saveBitmap(LPCSTR filename, HBITMAP bmp, HPALETTE pal)
  98. {
  99.     bool result = false;
  100.     PICTDESC pd;
  101.  
  102.     pd.cbSizeofstruct = sizeof(PICTDESC);
  103.     pd.picType = PICTYPE_BITMAP;
  104.     pd.bmp.hbitmap = bmp;
  105.     pd.bmp.hpal = pal;
  106.  
  107.     LPPICTURE picture;
  108.     HRESULT res = OleCreatePictureIndirect(&pd, IID_IPicture, false,
  109.         reinterpret_cast<void**>(&picture));
  110.  
  111.     if (!SUCCEEDED(res))
  112.         return false;
  113.  
  114.     LPSTREAM stream;
  115.     res = CreateStreamOnHGlobal(0, true, &stream);
  116.  
  117.     if (!SUCCEEDED(res))
  118.     {
  119.         picture->Release();
  120.         return false;
  121.     }
  122.  
  123.     LONG bytes_streamed;
  124.     res = picture->SaveAsFile(stream, true, &bytes_streamed);
  125.  
  126.     HANDLE file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, 0,
  127.         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  128.  
  129.     if (!SUCCEEDED(res) || !file)
  130.     {
  131.         stream->Release();
  132.         picture->Release();
  133.         return false;
  134.     }
  135.  
  136.     HGLOBAL mem = 0;
  137.     GetHGlobalFromStream(stream, &mem);
  138.     LPVOID data = GlobalLock(mem);
  139.  
  140.     DWORD bytes_written;
  141.  
  142.     result = !!WriteFile(file, data, bytes_streamed, &bytes_written, 0);
  143.     result &= (bytes_written == static_cast<DWORD>(bytes_streamed));
  144.  
  145.     GlobalUnlock(mem);
  146.     CloseHandle(file);
  147.  
  148.     stream->Release();
  149.     picture->Release();
  150.  
  151.     return result;
  152. }
  153.  
  154. BOOL GetPicture(int x, int y, int w, int h, LPCSTR fname)
  155. {
  156.    
  157.     HDC hdcSource = GetDC(NULL);
  158.     HDC hdcMemory = CreateCompatibleDC(hdcSource);
  159.  
  160.     int capX = GetDeviceCaps(hdcSource, HORZRES);
  161.     int capY = GetDeviceCaps(hdcSource, VERTRES);
  162.  
  163.     HBITMAP hBitmap = CreateCompatibleBitmap(hdcSource, w, h);
  164.     HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hBitmap);
  165.  
  166.     BitBlt(hdcMemory, 0, 0, w, h, hdcSource, x, y, SRCCOPY);
  167.     hBitmap = (HBITMAP)SelectObject(hdcMemory, hBitmapOld);
  168.  
  169.     DeleteDC(hdcSource);
  170.     DeleteDC(hdcMemory);
  171.  
  172.     HPALETTE hpal = NULL;
  173.     if (saveBitmap(fname, hBitmap, hpal)) return true;
  174.     return false;
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement