CasualGamer

Untitled

May 26th, 2020
3,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.68 KB | None | 0 0
  1. //overlay.cpp
  2. // ExternalOverlay.cpp : Defines the entry point for the application.
  3. //
  4. #include <Dwmapi.h>
  5.  
  6. #include "framework.h"
  7. #include "ExternalOverlay.h"
  8.  
  9. // Global Variables:
  10. HINSTANCE hInst;                                // current instance
  11. WCHAR overlayWindowName[100] = L"Overlay";  // main window class name & The title bar text
  12. LPCSTR targetWindowName = "Halo";  // main window class name & The title bar text
  13. HWND targetHWND, overlayHWND;
  14. int width, height;
  15. Paint paint;
  16.  
  17. // Forward declarations of functions included in this code module:
  18. ATOM                registerClass(HINSTANCE hInstance);
  19. BOOL                InitInstance(HINSTANCE, int);
  20. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  21.  
  22. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,_In_opt_ HINSTANCE hPrevInstance,_In_ LPWSTR    lpCmdLine, _In_ int       nCmdShow)
  23. {
  24.     UNREFERENCED_PARAMETER(hPrevInstance);
  25.     UNREFERENCED_PARAMETER(lpCmdLine);
  26.  
  27.     registerClass(hInstance);
  28.  
  29.    
  30.     targetHWND = FindWindowA(0, targetWindowName);
  31.     if (targetHWND) {
  32.         RECT rect;
  33.         GetWindowRect(targetHWND, &rect);
  34.         width = rect.right - rect.left;
  35.         height = rect.bottom - rect.top;
  36.     }
  37.     else
  38.         return FALSE;
  39.  
  40.     // Perform application initialization:
  41.     if (!InitInstance(hInstance, SW_SHOW)){
  42.         return FALSE;
  43.     }
  44.     paint = Paint(overlayHWND,targetHWND,width,height);
  45.     MSG msg;
  46.  
  47.     // Main message loop:
  48.     while (GetMessage(&msg, nullptr, 0, 0)){
  49.         TranslateMessage(&msg);
  50.         DispatchMessage(&msg);
  51.  
  52.         RECT rect;
  53.         GetWindowRect(targetHWND, &rect);
  54.         width = rect.right - rect.left;
  55.         height = rect.bottom - rect.top;
  56.  
  57.         MoveWindow(overlayHWND, rect.left, rect.top, width, height, true);
  58.  
  59.     }
  60.  
  61.     return (int) msg.wParam;
  62. }
  63.  
  64.  
  65.  
  66. //
  67. //  FUNCTION: MyRegisterClass()
  68. //
  69. //  PURPOSE: Registers the window class.
  70. //
  71. ATOM registerClass(HINSTANCE hInstance)
  72. {
  73.     WNDCLASSEXW wcex;
  74.  
  75.     wcex.cbSize = sizeof(WNDCLASSEX);
  76.  
  77.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  78.     wcex.lpfnWndProc    = WndProc;
  79.     wcex.cbClsExtra     = 0;
  80.     wcex.cbWndExtra     = 0;
  81.     wcex.hInstance      = hInstance;
  82.     wcex.hIcon          = 0;
  83.     wcex.hCursor        = LoadCursor(nullptr, IDC_CROSS);
  84.     wcex.hbrBackground  = CreateSolidBrush(RGB(0, 0, 0));
  85.     wcex.lpszMenuName   = overlayWindowName;
  86.     wcex.lpszClassName  = overlayWindowName;
  87.     wcex.hIconSm        = 0;
  88.  
  89.     return RegisterClassExW(&wcex);
  90. }
  91.  
  92. //
  93. //   FUNCTION: InitInstance(HINSTANCE, int)
  94. //
  95. //   PURPOSE: Saves instance handle and creates main window
  96. //
  97. //   COMMENTS:
  98. //
  99. //        In this function, we save the instance handle in a global variable and
  100. //        create and display the main program window.
  101. //
  102. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  103. {
  104.    hInst = hInstance; // Store instance handle in our global variable
  105.  
  106.    overlayHWND = CreateWindowExW(WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED, overlayWindowName, overlayWindowName, WS_POPUP,
  107.       1, 1, width, height, nullptr, nullptr, hInstance, nullptr);
  108.  
  109.    if (!overlayHWND){
  110.       return FALSE;
  111.    }
  112.    SetLayeredWindowAttributes(overlayHWND, RGB(0, 0, 0), 0, LWA_COLORKEY);
  113.  
  114.    ShowWindow(overlayHWND, nCmdShow);
  115.  
  116.    return TRUE;
  117. }
  118.  
  119. //
  120. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  121. //
  122. //  PURPOSE: Processes messages for the main window.
  123. //
  124. //  WM_COMMAND  - process the application menu
  125. //  WM_PAINT    - Paint the main window
  126. //  WM_DESTROY  - post a quit message and return
  127. //
  128. //
  129. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
  130.     switch (message){
  131.     case WM_PAINT:
  132.         paint.render();
  133.         break;
  134.     case WM_DESTROY:
  135.         PostQuitMessage(0);
  136.         break;
  137.     default:
  138.         return DefWindowProc(hWnd, message, wParam, lParam);
  139.     }
  140.     return 0;
  141. }
  142.  
  143.  
  144. //Paint.h
  145. #pragma once
  146.  
  147. #include <string> //save error
  148. #include <Windows.h>
  149.  
  150. #include <d3d9.h>
  151. #include <d3dx9.h>
  152. #pragma comment(lib, "d3d9.lib")
  153. #pragma comment(lib, "d3dx9.lib")
  154.  
  155. #include <DxErr.h> //get error from error code
  156. #pragma comment(lib, "dxerr.lib")
  157. #pragma comment(lib, "legacy_stdio_definitions.lib")
  158.  
  159. class Paint{
  160. private:
  161.     IDirect3D9Ex* d3dObject = NULL; //used to create device
  162.     IDirect3DDevice9Ex* d3dDevice = NULL; //contains functions like begin and end scene
  163.     D3DPRESENT_PARAMETERS d3dparams; //parameters for creating device
  164.     ID3DXFont* d3dFont = 0; // font used when displaying text
  165.     HWND targetWnd;
  166.     int width;
  167.     int height;
  168.  
  169.     int d3D9Init(HWND hWnd);
  170.     void drawText(char* String, int x, int y, int a, int r, int g, int b);
  171. public:
  172.     Paint();
  173.     Paint(HWND hWnd, HWND targetWnd, int width, int height);
  174.     int render();
  175. };
  176.  
  177.  
  178. //Paint.cpp
  179. #include "Paint.h"
  180.  
  181.  
  182. int Paint::d3D9Init(HWND hWnd){
  183.  
  184.     if (FAILED(Direct3DCreate9Ex(D3D_SDK_VERSION, &d3dObject))){
  185.         exit(1);
  186.     }
  187.  
  188.     ZeroMemory(&d3dparams, sizeof(d3dparams));
  189.  
  190.     d3dparams.BackBufferWidth = width;
  191.     d3dparams.BackBufferHeight = height;
  192.     d3dparams.Windowed = TRUE;
  193.     d3dparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
  194.     d3dparams.hDeviceWindow = hWnd;
  195.     d3dparams.MultiSampleQuality = D3DMULTISAMPLE_NONE;
  196.     d3dparams.BackBufferFormat = D3DFMT_A8R8G8B8;
  197.     d3dparams.EnableAutoDepthStencil = TRUE;
  198.     d3dparams.AutoDepthStencilFormat = D3DFMT_D16;
  199.  
  200.     HRESULT res = d3dObject->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dparams, 0, &d3dDevice);
  201.  
  202.     if (FAILED(res)){
  203.         std::wstring ws(DXGetErrorString(res));
  204.         std::string str(ws.begin(), ws.end());
  205.         std::wstring ws2(DXGetErrorDescription(res));
  206.         std::string str2(ws2.begin(), ws2.end());
  207.         std::string error = "Error: " + str + " error description: " + str2;
  208.         exit(1);
  209.     }
  210.  
  211.     D3DXCreateFont(d3dDevice, 50, 0, FW_BOLD, 1, false, DEFAULT_CHARSET, OUT_DEVICE_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH, L"Comic Sans", &d3dFont);
  212.  
  213.     return 0;
  214.  
  215. }
  216.  
  217. Paint::Paint() {};
  218.  
  219. Paint::Paint(HWND hWnd, HWND targetWnd, int width, int height){
  220.     this->width = width;
  221.     this->height = height;
  222.     this->targetWnd = targetWnd;
  223.     d3D9Init(hWnd);
  224. }
  225.  
  226.  
  227. int Paint::render()
  228. {
  229.     if (d3dDevice == nullptr)
  230.         return 1;
  231.     d3dDevice->Clear(0, 0, D3DCLEAR_TARGET, 0, 1.0f, 0);
  232.     d3dDevice->BeginScene();
  233.  
  234.     if (targetWnd == GetForegroundWindow())
  235.     {
  236.         drawText((char*)"U got hacked broo", width / 10, height / 10, 255, 171, 0, 182);
  237.     }
  238.  
  239.     d3dDevice->EndScene();
  240.     d3dDevice->PresentEx(0, 0, 0, 0, 0);
  241.  
  242.     return 0;
  243. }
  244.  
  245.  
  246. void Paint::drawText(char* String, int x, int y, int a, int r, int g, int b)
  247. {
  248.     RECT FontPos;
  249.     FontPos.left = x;
  250.     FontPos.top = y;
  251.     d3dFont->DrawTextA(0, String, strlen(String), &FontPos, DT_NOCLIP, D3DCOLOR_ARGB(a, r, g, b));
  252. }
Add Comment
Please, Sign In to add comment