Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.05 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <Windows.h>
  3. #include "DXMenu.h"
  4.  
  5. BOOL APIENTRY DllMain(HINSTANCE hInst     /* Library instance handle. */,
  6.     DWORD reason        /* Reason this function is being called. */,
  7.     LPVOID reserved     /* Not used. */)
  8. {
  9.     DllModuleHandle = hInst;
  10.     inj_hModule = hInst;
  11.     inj_hModule2 = hInst;
  12.     switch (reason)
  13.     {
  14.     case DLL_PROCESS_ATTACH:
  15.         AllocConsole();
  16.         freopen("CONOUT$", "wt", stdout);
  17.         CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)DXmain, 0, 0, 0);
  18.         break;
  19.  
  20.     case DLL_PROCESS_DETACH:
  21.         break;
  22.  
  23.     case DLL_THREAD_ATTACH:
  24.         break;
  25.  
  26.     case DLL_THREAD_DETACH:
  27.         break;
  28.     }
  29.     return TRUE;
  30. }
  31. ///  "DXMenu.h"
  32. #pragma once
  33. // ImGui - standalone example application for DirectX 11
  34. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  35.  
  36. #include "imgui.h"
  37. #include "imgui_internal.h"
  38. #include "ImGuiDX11.h" 
  39. #include <d3d11.h>
  40. #define DIRECTINPUT_VERSION 0x0800
  41. #include <dinput.h>
  42. #include <tchar.h>
  43.  
  44. #include <dwmapi.h>
  45. #pragma comment (lib, "dwmapi.lib")
  46. // Data
  47. HINSTANCE  inj_hModule2;          //Injected Modules Handle
  48. static ID3D11Device*            g_pd3dDevice = NULL;
  49. static ID3D11DeviceContext*     g_pd3dDeviceContext = NULL;
  50. static IDXGISwapChain*          g_pSwapChain = NULL;
  51. static ID3D11RenderTargetView*  g_mainRenderTargetView = NULL;
  52.  
  53. void CreateRenderTarget()
  54. {
  55.     DXGI_SWAP_CHAIN_DESC sd;
  56.     g_pSwapChain->GetDesc(&sd);
  57.  
  58.     // Create the render target
  59.     ID3D11Texture2D* pBackBuffer;
  60.     D3D11_RENDER_TARGET_VIEW_DESC render_target_view_desc;
  61.     ZeroMemory(&render_target_view_desc, sizeof(render_target_view_desc));
  62.     render_target_view_desc.Format = sd.BufferDesc.Format;
  63.     render_target_view_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  64.     g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
  65.     g_pd3dDevice->CreateRenderTargetView(pBackBuffer, &render_target_view_desc, &g_mainRenderTargetView);
  66.     g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
  67.     pBackBuffer->Release();
  68. }
  69.  
  70. void CleanupRenderTarget()
  71. {
  72.     if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; }
  73. }
  74.  
  75. HRESULT CreateDeviceD3D(HWND hWnd)
  76. {
  77.     // Setup swap chain
  78.     DXGI_SWAP_CHAIN_DESC sd;
  79.     {
  80.         ZeroMemory(&sd, sizeof(sd));
  81.         sd.BufferCount = 2;
  82.         sd.BufferDesc.Width = 0;
  83.         sd.BufferDesc.Height = 0;
  84.         sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  85.         sd.BufferDesc.RefreshRate.Numerator = 60;
  86.         sd.BufferDesc.RefreshRate.Denominator = 1;
  87.         sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  88.         sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  89.         sd.OutputWindow = hWnd;
  90.         sd.SampleDesc.Count = 1;
  91.         sd.SampleDesc.Quality = 0;
  92.         sd.Windowed = TRUE;
  93.         sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  94.     }
  95.  
  96.     UINT createDeviceFlags = 0;
  97.     //createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
  98.     D3D_FEATURE_LEVEL featureLevel;
  99.     const D3D_FEATURE_LEVEL featureLevelArray[1] = { D3D_FEATURE_LEVEL_11_0, };
  100.     if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 1, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
  101.         return E_FAIL;
  102.  
  103.     CreateRenderTarget();
  104.  
  105.     return S_OK;
  106. }
  107.  
  108. void CleanupDeviceD3D()
  109. {
  110.     CleanupRenderTarget();
  111.     if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; }
  112.     if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = NULL; }
  113.     if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
  114. }
  115.  
  116. extern LRESULT ImGui_ImplDX11_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  117. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  118. {
  119.     if (ImGui_ImplDX11_WndProcHandler(hWnd, msg, wParam, lParam))
  120.         return true;
  121.  
  122.     switch (msg)
  123.     {
  124.     case WM_SIZE:
  125.         if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
  126.         {
  127.             ImGui_ImplDX11_InvalidateDeviceObjects();
  128.             CleanupRenderTarget();
  129.             g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
  130.             CreateRenderTarget();
  131.             ImGui_ImplDX11_CreateDeviceObjects();
  132.         }
  133.         return 0;
  134.     case WM_SYSCOMMAND:
  135.         if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  136.             return 0;
  137.         break;
  138.     case WM_DESTROY:
  139.         PostQuitMessage(0);
  140.         return 0;
  141.     }
  142.     return DefWindowProc(hWnd, msg, wParam, lParam);
  143. }
  144. wchar_t *cW(const char* charArray)
  145. {
  146.     wchar_t* wString = new wchar_t[4096];
  147.     MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
  148.     return wString;
  149. }
  150. static int DXmain()
  151. {
  152.     // Create application window
  153.     WNDCLASSEX wc;
  154.     wc.hInstance = inj_hModule2;
  155.     wc.lpszClassName = "InjectedDLLWindowMenu";
  156.     wc.lpfnWndProc = WndProc;
  157.     wc.style = ACS_TRANSPARENT;
  158.     wc.cbSize = sizeof(WNDCLASSEX);
  159.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  160.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  161.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  162.     wc.lpszMenuName = NULL;
  163.     wc.cbClsExtra = 0;
  164.     wc.cbWndExtra = 0;
  165.     wc.hbrBackground = (HBRUSH)0;
  166.     RegisterClassEx(&wc);
  167.  
  168.     //HWND hwnd = CreateWindow(, _T("ImGui DirectX11 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
  169.     HWND hwnd = CreateWindowExW(WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE, cW("InjectedDLLWindowMenu"),
  170.         cW("Menu"), WS_POPUP, 100, 100, 1280, 800, 0, 0, wc.hInstance, 0);;
  171.  
  172.     std::cout << "CreateWindowExW okay" << std::endl;
  173.     SetLayeredWindowAttributes(hwnd, 0, 1.0f, LWA_ALPHA);
  174.     SetLayeredWindowAttributes(hwnd, 0, RGB(0, 0, 0), LWA_COLORKEY);
  175.     MARGINS margins = { -1 };
  176.     DwmExtendFrameIntoClientArea(hwnd, &margins);
  177.     // Initialize Direct3D
  178.     if (CreateDeviceD3D(hwnd) < 0)
  179.     {
  180.         CleanupDeviceD3D();
  181.         UnregisterClass(_T("ImGui Example"), wc.hInstance);
  182.         std::cout << "CreateDeviceD3D failed" << std::endl;
  183.         return 0  ;
  184.     }
  185.     std::cout << "CreateDeviceD3D okay" << std::endl;
  186.  
  187.     // Show the window
  188.  
  189.     ShowWindow(hwnd, SW_SHOWDEFAULT);
  190.     UpdateWindow(hwnd);
  191.     /*long style = GetWindowLong(hwnd, GWL_EXSTYLE);
  192.     style |= WS_EX_LAYERED;
  193.     SetWindowLong(hwnd, GWL_EXSTYLE, style);
  194.     //windowstate = 0;*/
  195.  
  196.     // Setup ImGui binding
  197.     InitImGUI(hwnd, g_pd3dDevice, g_pd3dDeviceContext);
  198.     std::cout << "ImGui_ImplDX11_Init okay" << std::endl;
  199.  
  200.     // Load Fonts
  201.     // (there is a default font, this is only if you want to change it. see extra_fonts/README.txt for more details)
  202.     //ImGuiIO& io = ImGui::GetIO();
  203.     //io.Fonts->AddFontDefault();
  204.     //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f);
  205.     //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f);
  206.     //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f);
  207.     //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f);
  208.     //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  209.  
  210.     bool show_test_window = true;
  211.     bool show_another_window = false;
  212.     ImVec4 clear_col = ImColor(114, 144, 154);
  213.  
  214.     // Main loop
  215.     MSG msg;
  216.     ZeroMemory(&msg, sizeof(msg));
  217.     while (msg.message != WM_QUIT)
  218.     {
  219.         if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  220.         {
  221.             TranslateMessage(&msg);
  222.             DispatchMessage(&msg);
  223.             continue;
  224.         }
  225.         ImGui_ImplDX11_NewFrame();
  226.  
  227.         // 1. Show a simple window
  228.         // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
  229.         {
  230.             static float f = 0.0f;
  231.             ImGui::Text("Hello, world!");
  232.             ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
  233.             ImGui::ColorEdit3("clear color", (float*)&clear_col);
  234.             if (ImGui::Button("Test Window")) show_test_window ^= 1;
  235.             if (ImGui::Button("Another Window")) show_another_window ^= 1;
  236.             ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  237.         }
  238.  
  239.         // 2. Show another simple window, this time using an explicit Begin/End pair
  240.         if (show_another_window)
  241.         {
  242.             ImGui::Begin("Another Window", &show_another_window);
  243.             ImGui::Text("Hello from another window!");
  244.             ImGui::End();
  245.         }
  246.  
  247.         // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
  248.         if (show_test_window)
  249.         {
  250.             ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver);     // Normally user code doesn't need/want to call it because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
  251.             ImGui::ShowTestWindow(&show_test_window);
  252.         }
  253.  
  254.         /*long style = GetWindowLong(hwnd, GWL_EXSTYLE);
  255.         if (ImGui::IsItemHovered())
  256.         {
  257.         style &= ~WS_EX_LAYERED;
  258.         SetWindowLong(hwnd, GWL_EXSTYLE, style);
  259.         SetForegroundWindow(hwnd);
  260.         }
  261.         else
  262.         {
  263.         style |= WS_EX_LAYERED;
  264.         SetWindowLong(hwnd, GWL_EXSTYLE, style);
  265.         }*/
  266.  
  267.         // Rendering
  268.         float clearColor[4] = { 0.0f,0.0f,0.0f,0.0f };
  269.         g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clearColor);
  270.         ImGui::Render();
  271.  
  272.         g_pSwapChain->Present(1, 0); // Present with vsync
  273.                                      //g_pSwapChain->Present(0, 0); // Present without vsync
  274.     }
  275.  
  276.     ImGui_ImplDX11_Shutdown();
  277.     CleanupDeviceD3D();
  278.     UnregisterClass(_T("ImGui Example"), wc.hInstance);
  279.  
  280.     return 0;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement