#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <mmsystem.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"dwmapi.lib")
#include <dwmapi.h>
#include <iostream>
#include <string>
#include <d3d9types.h>
using namespace std;
INT SCREEN_WIDTH = 800;
INT SCREEN_HEIGHT = 600;
HWND OVERLAY_WINDOW;
const MARGINS margin = { 0,0, SCREEN_WIDTH,SCREEN_HEIGHT };
LPDIRECT3DDEVICE9 D3D_DEVICE;
LPD3DXFONT TEXT_FONT;
void initD3D(HWND hwnd)
{
LPDIRECT3D9 d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.BackBufferFormat = D3DFMT_A8B8G8R8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &D3D_DEVICE);
D3DXCreateFont(D3D_DEVICE, 16, 0, FW_NORMAL, 1, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial", &TEXT_FONT);
}
void DrawFilledRectangle(LONG x, LONG y, LONG h, LONG w, DWORD color)
{
D3DRECT rect = { x,y,w,h };
D3D_DEVICE->Clear(1, &rect, D3DCLEAR_TARGET, color, 0, 0);
}
void DrawBorderBox(int x,int y,int w,int h,int thinkness,DWORD color)
{
DrawFilledRectangle(x, y, w, y + thinkness, color);
DrawFilledRectangle(x, y, h, x + thinkness, color);
DrawFilledRectangle(x, y, w, h + thinkness, color);
DrawFilledRectangle(x, y, w + thinkness, h + thinkness, color);
}
void DrawString(int x, int y, DWORD color, LPD3DXFONT g_pFont, string str)
{
RECT fontPos = { x,y,x + 120,y + 16 };
g_pFont->DrawText(nullptr, str.c_str(), -1, &fontPos, DT_NOCLIP, color);
}
void Line(float x1, float x2, float y1, float y2, float width, bool antialias, DWORD color)
{
ID3DXLine* line;
D3DXCreateLine(D3D_DEVICE, &line);
D3DXVECTOR2 linepos[] = { D3DXVECTOR2(x1,y1),D3DXVECTOR2(x2,y2) };
line->SetWidth(width);
if (antialias)
line->SetAntialias(antialias);
line->Begin();
line->Draw(linepos, 2, color);
line->End();
line->Release();
}
void render()
{
D3D_DEVICE->Clear(0, nullptr, D3DCLEAR_TARGET, D3DCOLOR_ARGB(0, 0, 0, 0), 1,0);
D3D_DEVICE->BeginScene();
POINT cursor;
GetCursorPos(&cursor);
Line(0, 0, cursor.x, cursor.y, 3, true, D3DCOLOR_ARGB(255, 255, 0, 255));
D3D_DEVICE->EndScene();
D3D_DEVICE->Present(nullptr, nullptr, nullptr, nullptr);
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message)
{
case WM_CREATE:
DwmExtendFrameIntoClientArea(hwnd, &margin);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, message, wparam, lparam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevhinstanse, LPSTR lpcmdline, INT ncmdshow)
{
RECT rc;
const HWND desktop = GetDesktopWindow();
if (desktop != nullptr)
{
GetWindowRect(desktop, &rc);
SCREEN_WIDTH = rc.right = rc.left;
SCREEN_HEIGHT = rc.bottom - rc.top;
}
else {
ExitProcess(0);
}
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WinProc;
wc.hInstance = hinstance;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = HBRUSH(RGB(0, 0, 0));
wc.lpszClassName = " ";
RegisterClassEx(&wc);
OVERLAY_WINDOW = CreateWindowEx(0, " ", " ", WS_EX_TOPMOST | WS_POPUP, rc.left, rc.top, SCREEN_WIDTH, SCREEN_HEIGHT, nullptr, nullptr, hinstance, nullptr);
SetWindowLong(OVERLAY_WINDOW, GWL_EXSTYLE, GetWindowLong(OVERLAY_WINDOW, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT);
SetLayeredWindowAttributes(OVERLAY_WINDOW, RGB(0, 0, 0), 0, ULW_COLORKEY);
SetLayeredWindowAttributes(OVERLAY_WINDOW, 0, 255, LWA_ALPHA);
ShowWindow(OVERLAY_WINDOW, ncmdshow);
initD3D(OVERLAY_WINDOW);
MSG msg;
while (TRUE) {
SetWindowPos(OVERLAY_WINDOW, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
render();
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}