Code:
//Basic Header Files
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <dwmapi.h>
//DirectX Header Files
#include <d3d9.h>
#include <d3dx9.h>
//Loads the DirectX libs
#pragma comment(lib, "dwmapi.lib")
#pragma comment(lib,"d3d9.lib")
#ifdef _DEBUG
#pragma comment(lib,"d3dx9d.lib")
#else
#pragma comment(lib,"d3dx9.lib")
#endif
//---------------Defines--------------------------//
#define WS_EX_LAYERED 0x00080000 //
#define LWA_ALPHA 0x00000002 //
#define ARGB_TRANS 0x00000000 // 100% transparent
//---------------Globals Declaration---------------------------------//
LPDIRECT3D9EX d3d; // The D3D Object
LPDIRECT3DDEVICE9EX d3ddev; //The D3DDevice
int w_Width = 800; //Initial Window Width
int w_Height = 600; //Initial Window Height
ID3DXFont *font = NULL; //Font to write on the window
wchar_t status[512] = L"waiting"; //status message to display
HRESULT hr = D3D_OK;
MARGINS g_mgDWMMargins= {-1,-1,-1,-1}; //Margins for thw Window
WCHAR *AppName = L"DirectX Test"; //Application Name
HWND Mw2_hWnd = NULL; //MW2 Window
DWORD Mw2_pid = 0; //MW2 Process Pid
HANDLE Mw2_process = NULL; //MW2 Process
bool init_ok = false; //Is Everything initialized?
COORD w_pos = {50,50}; //Initial Window Position
COORD w_res = {800, 600}; //Initial Window Resolution
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void DrawStringAt(LPCWSTR string, int x, int y,COLORREF color, ID3DXFont *Font);
HRESULT D3DStartup(HWND hWnd)
{
BOOL bCompOk = FALSE; // Is composition enabled?
D3DPRESENT_PARAMETERS pp; // Presentation prefs
DWORD msqAAQuality = 0; // Non-maskable quality
HRESULT hr;
// Make sure that DWM composition is enabled
DwmIsCompositionEnabled(&bCompOk);
if(!bCompOk) return E_FAIL;
// Create a Direct3D object
hr = Direct3DCreate9Ex(D3D_SDK_VERSION, &d3d);
if (FAILED(hr)) {
swprintf_s(status, L"Error Direct3DCreate9Ex:%u", hr);
MessageBox(0,status,L"Error",MB_ICONERROR);
return E_FAIL;
}
// Setup presentation parameters
ZeroMemory(&pp, sizeof(pp));
pp.Windowed = TRUE;
pp.SwapEffect = D3DSWAPEFFECT_DISCARD; // Required for multi sampling
pp.BackBufferFormat = D3DFMT_A8R8G8B8; // Back buffer format with alpha channel
// Set highest quality non-maskable AA available or none if not
if (SUCCEEDED(d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
D3DFMT_A8R8G8B8,
TRUE,
D3DMULTISAMPLE_NONMASKABLE,
&msqAAQuality
)))
{
// Set AA quality
pp.MultiSampleType = D3DMULTISAMPLE_NONMASKABLE;
pp.MultiSampleQuality = msqAAQuality - 1;
}
else
{
// No AA
pp.MultiSampleType = D3DMULTISAMPLE_NONE;
}
// Create a Direct3D device object
hr = d3d->CreateDeviceEx(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&pp,
NULL,
&d3ddev
);
if (FAILED(hr)) {
swprintf_s(status, L"Error CreateDeviceEx:%u", hr);
MessageBox(0,status,L"Error",MB_ICONERROR);
return E_FAIL;
}
}
//---------------------------------------------------------------------------
// Render()
//
// Render the scene to the back buffer
VOID Render(VOID)
{
COLORREF color = 0xFFFF0000;
// Sanity check
if(d3ddev == NULL) return;
// Clear the back buffer and z buffer to transparent
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, ARGB_TRANS, 1.0f, 0);
// Render scene
d3ddev->BeginScene();
DrawStringAt(L"MW2 FourDeltaOne ESP", 5,20,color, font);
d3ddev->EndScene();
// Update display
d3ddev->PresentEx(NULL, NULL, NULL, NULL, NULL);
}
void D3DShutdown(void)
{
if(d3ddev != NULL)
d3ddev->Release();
if(d3d != NULL)
d3d->Release();
}
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
//----------Enabling the Console Window----------//
AllocConsole();
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
FILE* hf_out = _fdopen(hCrt, "w");
setvbuf(hf_out, NULL, _IONBF, 1);
*stdout = *hf_out;
HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
FILE* hf_in = _fdopen(hCrt, "r");
setvbuf(hf_in, NULL, _IONBF, 128);
*stdin = *hf_in;
//------------------------------------------------//
HWND hWnd = NULL;
MSG uMsg;
HRESULT hr;
long Loops = 0;
WNDCLASSEX wc = {sizeof(WNDCLASSEX), // cbSize
NULL, // style
WindowProc, // lpfnWndProc
NULL, // cbClsExtra
NULL, // cbWndExtra
hInstance, // hInstance
LoadIcon(NULL, IDI_APPLICATION), // hIcon
LoadCursor(NULL, IDC_ARROW), // hCursor
NULL, // hbrBackground
NULL, // lpszMenuName
AppName, // lpszClassName
LoadIcon(NULL, IDI_APPLICATION)};// hIconSm
RegisterClassEx(&wc);
hWnd = CreateWindowEx(WS_EX_TOPMOST | WS_EX_COMPOSITED | WS_EX_TRANSPARENT | WS_EX_LAYERED, // dwExStyle
// WS_EX_COMPOSITED: for best performance in transparent mode
// WS_EX_TOPMOST: always on top window
// WS_EX_TRANSPARENT | WS_EX_LAYERED: click-through window
AppName, // lpClassName
AppName, // lpWindowName
WS_POPUP, // dwStyle
// dropped the WS_SIZEBOX, otherwise we have a "glow" background
//WS_POPUP | WS_SIZEBOX, // dwStyle
w_pos.X, w_pos.Y, // x, y
w_res.X, w_res.Y, // nWidth, nHeight
NULL, // hWndParent
NULL, // hMenu
hInstance, // hInstance
NULL); // lpParam
// Extend glass to cover whole window
hr = DwmExtendFrameIntoClientArea(hWnd, &g_mgDWMMargins);
// Initialize Direct3D
if (SUCCEEDED(D3DStartup(hWnd)))
{
D3DXCreateFont(d3ddev, 20, 10, FW_NORMAL, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), &font );
// Show the window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
// Enter main loop
while(TRUE)
{
// Check for a message
if(PeekMessage(&uMsg, NULL, 0, 0, PM_REMOVE))
{
// Check if the message is WM_QUIT
if(uMsg.message == WM_QUIT)
{
// Break out of main loop
break;
}
// Pump the message
TranslateMessage(&uMsg);
DispatchMessage(&uMsg);
}
if (Mw2_hWnd == NULL) {
Mw2_hWnd = FindWindow(NULL,L"Modern Warfare 2");
if (Mw2_hWnd != NULL)
swprintf_s(status, L"app found\n");
}
if ((Mw2_hWnd != NULL) && (Mw2_pid == 0)) {
GetWindowThreadProcessId(Mw2_hWnd,&Mw2_pid);
}
if ((Mw2_process == NULL) && (Mw2_pid != 0)) {
Mw2_process = OpenProcess(PROCESS_VM_READ,false,Mw2_pid);
}
if (Mw2_process != NULL) {
if ((!init_ok) || ((Loops % 20) == 0)) {
RECT client_rect;
GetClientRect(Mw2_hWnd, &client_rect); //Get The Resolution
w_res.X = client_rect.right;
w_res.Y = client_rect.bottom;
RECT bounding_rect;
GetWindowRect(Mw2_hWnd,&bounding_rect); //Get the Position
if (init_ok) {
if ((w_pos.X != bounding_rect.left) || (w_pos.Y != bounding_rect.top)) {
MoveWindow(hWnd, bounding_rect.left,bounding_rect.top, client_rect.right,client_rect.bottom,false);
w_pos.X = bounding_rect.left;
w_pos.Y = bounding_rect.top;
}
} else {
if ((bounding_rect.left == 0) && (bounding_rect.top == 0)) {
// we force a 1 pixel move, otherwise the window is not resized (bug?)
MoveWindow(hWnd, bounding_rect.left-1, bounding_rect.top-1, client_rect.right,client_rect.bottom,false);
}
MoveWindow(hWnd, bounding_rect.left,bounding_rect.top, client_rect.right,client_rect.bottom,false);
//D3DShutdown(); // we resized then window so we need to recreate all D3D objects
//D3DStartup(hWnd);
}
init_ok = true; // off we go!
}
}
if(Loops % 10 == 0){ //Check if MW2 is still Open, if not, closes everything
if (FindWindow(NULL,L"Modern Warfare 2") == NULL) {
SendMessage(hWnd, WM_CLOSE, NULL, NULL); // quit if game is closed
}
}
Loops++;
if(Loops >100) Loops = 0;
Render();
}
}
// Shutdown Direct3D
D3DShutdown();
// Exit application
return 0;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
void DrawStringAt(LPCWSTR string, int x, int y,COLORREF color, ID3DXFont *Font){
RECT font_rct;
SetRect(&font_rct, x, y, w_res.X, w_res.Y);
Font->DrawText(NULL, string, -1, &font_rct, DT_LEFT | DT_NOCLIP | DT_SINGLELINE, color);
} Change the Lines FindWindow(NULL,L"Modern Warfare 2") to the name of your application window and it should work fine.