Advertisement
Guest User

D2D1 Overlay

a guest
Oct 4th, 2015
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <windows.h>
  2. #include <d2d1.h>
  3. #include <dwmapi.h>
  4.  
  5. LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  6. {
  7.     switch (uMsg)
  8.     {
  9.         case WM_DESTROY:
  10.         {
  11.             PostQuitMessage(0);
  12.             return 0;
  13.         }
  14.     }
  15.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  16. }
  17.  
  18. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  19. {
  20.     WNDCLASSEX wndClass;
  21.     ZeroMemory(&wndClass, sizeof(WNDCLASSEX));
  22.    
  23.     wndClass.cbSize = sizeof(WNDCLASSEX);
  24.     wndClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 0));
  25.     wndClass.hInstance = hInstance;
  26.     wndClass.lpfnWndProc = WindowProc;
  27.     wndClass.lpszClassName = "Overlay";
  28.     wndClass.style = CS_HREDRAW | CS_VREDRAW;
  29.    
  30.     RegisterClassEx(&wndClass);
  31.    
  32.     RECT rc = {0, 0, 1440, 900};
  33.     AdjustWindowRectEx(&rc, WS_OVERLAPPEDWINDOW, false, WS_EX_OVERLAPPEDWINDOW);
  34.    
  35.     HWND hWnd = CreateWindowEx(WS_EX_LAYERED, "Overlay", "OverlayWindow", WS_EX_TOPMOST | WS_POPUP,
  36.         0, 0, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL);
  37.        
  38.     ID2D1Factory *factory = NULL;
  39.     ID2D1HwndRenderTarget *renderTarget = NULL;
  40.     ID2D1SolidColorBrush *brush = NULL;
  41.    
  42.     HRESULT hResult = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &factory);
  43.    
  44.     RECT rc2;
  45.     GetClientRect(hWnd, &rc2);
  46.    
  47.     factory->CreateHwndRenderTarget(
  48.         D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED)),
  49.         D2D1::HwndRenderTargetProperties(hWnd, D2D1::SizeU(rc2.right, rc2.bottom), D2D1_PRESENT_OPTIONS_IMMEDIATELY),
  50.         &renderTarget);
  51.        
  52.     renderTarget->CreateSolidColorBrush(D2D1::ColorF(0, 0, 0, 0), &brush);
  53.    
  54.     const MARGINS margin = {-1};
  55.     SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 255, ULW_COLORKEY | LWA_ALPHA);
  56.     DwmExtendFrameIntoClientArea(hWnd, &margin);
  57.    
  58.     ShowWindow(hWnd, nShowCmd);
  59.     MSG messages = {0};
  60.    
  61.     while (messages.message != WM_QUIT)
  62.     {
  63.         if (PeekMessage(&messages, NULL, 0, 0, PM_REMOVE))
  64.         {
  65.             TranslateMessage(&messages);
  66.             DispatchMessage(&messages);
  67.         }
  68.         else
  69.         {
  70.             renderTarget->BeginDraw();
  71.             renderTarget->Clear(D2D1::ColorF(0.0f, 0.0f, 0.0f, 0.0f));
  72.             brush->SetColor(D2D1::ColorF(1.0f, 0.0f, 0.0f, 1.0f));
  73.             renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(100, 100), 50, 50), brush, 3.0f);
  74.             renderTarget->EndDraw();
  75.         }
  76.     }
  77.    
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement