#include <d3d9.h> #include <d3dx9.h> #pragma comment(lib, "d3d9.lib") #pragma comment(lib, "d3dx9.lib") #include <dwmapi.h> #pragma comment(lib, "dwmapi.lib")
#include <iostream> #include<Windows.h>
static MARGINS Margin; static LPDIRECT3D9 g_pD3D = NULL; static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; static D3DPRESENT_PARAMETERS g_d3dpp = {}; static ID3DXLine* pLine = 0; static ID3DXFont* Font;
static HWND 辅助窗口句柄, GameHwnd; static RECT 窗口矩形; static int 窗口宽, 窗口高;
static WNDCLASSEX wClass;
typedef void(*Draw)(); static Draw Render;
LRESULT WinProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);
bool 初始化D3D();
void 创建透明窗口(HWND 游戏窗口句柄, Draw 绘制函数);
void 窗口消息循环();
void 画线(D3DCOLOR Color, float X1, float Y1, float X2, float Y2, float Width);
void 绘制文字(float X, float Y, const char* Str, D3DCOLOR Color);
void 画框(float X, float Y, float W, float H, float Width, D3DCOLOR Color);
void 绘制开始();
void 绘制结束();
bool 初始化D3D() {
if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL) return false;
ZeroMemory(&g_d3dpp, sizeof(g_d3dpp)); g_d3dpp.Windowed = TRUE; g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; g_d3dpp.EnableAutoDepthStencil = TRUE; g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 辅助窗口句柄, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0) return false;
if (pLine == NULL) D3DXCreateLine(g_pd3dDevice, &pLine);
D3DXCreateFontW(g_pd3dDevice, 16, 0, FW_DONTCARE, D3DX_DEFAULT, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, L"Vernada", &Font);
return true; }
void 创建透明窗口(HWND 游戏窗口句柄, Draw 绘制函数) { if (绘制函数 == NULL || 游戏窗口句柄 == 0) return;
GameHwnd = 游戏窗口句柄; Render = 绘制函数;
wClass.cbClsExtra = NULL; wClass.cbSize = sizeof(WNDCLASSEX); wClass.cbWndExtra = NULL; wClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 0)); wClass.hCursor = LoadCursor(0, IDC_ARROW); wClass.hIcon = LoadIcon(0, IDI_APPLICATION); wClass.hIconSm = LoadIcon(0, IDI_APPLICATION); wClass.hInstance = GetModuleHandle(NULL); wClass.lpfnWndProc = (WNDPROC)WinProc; wClass.lpszClassName = L" "; wClass.lpszMenuName = L" "; wClass.style = CS_VREDRAW | CS_HREDRAW;
if (RegisterClassEx(&wClass) == 0) { MessageBox(NULL, L"创建窗口出错!", L"提示!", 0); exit(1); }
GetWindowRect(GameHwnd, &窗口矩形); 窗口宽 = 窗口矩形.right - 窗口矩形.left; 窗口高 = 窗口矩形.bottom - 窗口矩形.top; 辅助窗口句柄 = CreateWindowEx(WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED, L" ", L" ", WS_POPUP, 1, 1, 窗口宽, 窗口高, 0, 0, 0, 0);
SetLayeredWindowAttributes(辅助窗口句柄, 0, RGB(0, 0, 0), LWA_COLORKEY); ShowWindow(辅助窗口句柄, SW_SHOW);
初始化D3D(); }
void 窗口消息循环() { while (1) { if (GameHwnd) { GetWindowRect(GameHwnd, &窗口矩形); 窗口宽 = 窗口矩形.right - 窗口矩形.left; 窗口高 = 窗口矩形.bottom - 窗口矩形.top; DWORD dwStyle = GetWindowLong(GameHwnd, GWL_STYLE); if (dwStyle & WS_BORDER) { 窗口矩形.top += 23; 窗口高 -= 23; } MoveWindow(辅助窗口句柄, 窗口矩形.left, 窗口矩形.top, 窗口宽, 窗口高, true); }
MSG Message; ZeroMemory(&Message, sizeof(Message)); if (PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) { DispatchMessage(&Message); TranslateMessage(&Message); }
Sleep(1); }
if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; } if (g_pD3D) { g_pD3D->Release(); g_pD3D = NULL; } CloseWindow(辅助窗口句柄);
::UnregisterClass(wClass.lpszClassName, wClass.hInstance); }
LRESULT WinProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch (Message) { case WM_PAINT: if (g_pd3dDevice)Render(); break;
case WM_CREATE: DwmExtendFrameIntoClientArea(hWnd, &Margin); break;
case WM_DESTROY: { g_pD3D->Release(); g_pd3dDevice->Release(); exit(1); return 0; } default: return DefWindowProc(hWnd, Message, wParam, lParam); break; } return 0; }
void line(float X1, float Y1, float X2, float Y2) { D3DXVECTOR2 Vertex[2] = { { X1, Y1 }, { X2, Y2 } }; pLine->SetWidth(1); pLine->Draw(Vertex, 2, D3DCOLOR_ARGB(12, 255, 255, 25)); }
void 绘制文字(float X, float Y, const char* Str, D3DCOLOR Color) { RECT Rect = { (LONG)X, (LONG)Y }; Font->DrawTextA(NULL, Str, -1, &Rect, DT_CALCRECT, Color); Font->DrawTextA(NULL, Str, -1, &Rect, DT_LEFT, Color); }
void 画框(float X, float Y, float W, float H, float Width, D3DCOLOR Color) { D3DXVECTOR2 Vertex[5] = { { X, Y }, { X + W, Y }, { X + W, Y + H }, { X, Y + H }, { X, Y } }; pLine->SetWidth(Width); pLine->Draw(Vertex, 5, Color); }
void DrawBox(LPDIRECT3DDEVICE9 pDevice, int x, int y, int w, int h, D3DCOLOR Color) { struct Vertex { float x, y, z, ht; DWORD Color; };
Vertex V[8] = { 0 };
V[0].Color = V[1].Color = V[2].Color = V[3].Color = Color; V[0].z = V[1].z = V[2].z = V[3].z = 0.0f; V[0].ht = V[1].ht = V[2].ht = V[3].ht = 0.0f; V[0].x = V[1].x = (float)x; V[0].y = V[2].y = (float)(y + h); V[1].y = V[3].y = (float)y; V[2].x = V[3].x = (float)(x + w);
pDevice->SetTexture(0, NULL);
pDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
pDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, V, sizeof(Vertex)); }
void draw_line(int x, int y, int w, int h, int color) { D3DRECT rect{ x + 0, y + 0, x + w, y + h }; g_pd3dDevice->Clear(2, &rect, D3DCLEAR_TARGET, color, 0.0f, 0); }
void fk(int zb, int db, int kd, int gd, int xc) { draw_line(zb + 1, db + gd - xc, kd - 2, xc, D3DCOLOR_ARGB(255, 255, 255, 255)); draw_line(zb, db + 1, xc, gd - 2, D3DCOLOR_ARGB(255, 255, 255, 255)); draw_line(zb + 1, db, kd - 2, xc, D3DCOLOR_ARGB(255, 255, 255, 255)); draw_line(zb + kd - xc + 1, db + 1, xc, gd - 2, D3DCOLOR_ARGB(255, 255, 255, 255)); }
void box(float X, float Y, float 宽度, float 高度, float Width, D3DCOLOR Color) { D3DXVECTOR2 Vertex[5] = { { X, Y }, { X + 宽度, Y }, { X + 宽度, Y + 高度 }, { X, Y + 高度 }, { X, Y } }; pLine->SetWidth(Width); pLine->Draw(Vertex, 5, Color); }
void 绘制开始() { g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET, 0, 1.0f, 0); g_pd3dDevice->BeginScene(); }
void 绘制结束() { g_pd3dDevice->EndScene(); g_pd3dDevice->Present(0, 0, 0, 0); }
int 线粗 = 2; D3DCOLOR 红色 = D3DCOLOR_ARGB(255, 255, 255, 255);
void 绘制() { 绘制开始();
draw_line(110, 300, 1, (200 / 2) - 30, D3DCOLOR_ARGB(56, 0, 52, 0)); draw_line(110, 300 + (200 / 2 + 30), 1, (200 / 2) - 30, D3DCOLOR_ARGB(56, 0, 100, 0));
draw_line(200, 300, 1, (200 / 2) - 30, D3DCOLOR_ARGB(56, 0, 52, 0)); draw_line(200, 300 + (200 / 2 + 30), 1, (200 / 2) - 30, D3DCOLOR_ARGB(56, 0, 100, 0));
绘制结束(); }
HWND 游戏窗口 = (HWND)0x002F02D4;
void 开始() { 创建透明窗口(游戏窗口, 绘制); 窗口消息循环(); }
int main() { CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)开始, NULL, 0, NULL); while (1) { std::cout << "输入233关闭:" << std::endl; int a = 0; std::cin >> a; if (a == 233) { return 0; } }
return 0; }
|