Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using SharpDX;
  10. using SharpDX.Direct3D9;
  11. using SharpDX.Mathematics;
  12. using SharpDX.Mathematics.Interop;
  13. using Color = SharpDX.Color;
  14. using Font = SharpDX.Direct3D9.Font;
  15.  
  16. namespace SoratosExternalWH
  17. {
  18.     public partial class frm_drawing : Form
  19.     {
  20.         private Margins marg;
  21.  
  22.         //this is used to specify the boundaries of the transparent area
  23.         internal struct Margins
  24.         {
  25.             public int Left, Right, Top, Bottom;
  26.         }
  27.  
  28.         [DllImport("user32.dll", SetLastError = true)]
  29.  
  30.         private static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
  31.  
  32.         [DllImport("user32.dll")]
  33.  
  34.         static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
  35.  
  36.         [DllImport("user32.dll")]
  37.  
  38.         static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
  39.  
  40.         public const int GWL_EXSTYLE = -20;
  41.  
  42.         public const int WS_EX_LAYERED = 0x80000;
  43.  
  44.         public const int WS_EX_TRANSPARENT = 0x20;
  45.  
  46.         public const int LWA_ALPHA = 0x2;
  47.  
  48.         public const int LWA_COLORKEY = 0x1;
  49.  
  50.         [DllImport("dwmapi.dll")]
  51.         static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMargins);
  52.        
  53.         [StructLayout(LayoutKind.Sequential)]
  54.         public struct RECT
  55.         {
  56.             public int Left;        // x position of upper-left corner
  57.             public int Top;         // y position of upper-left corner
  58.             public int Right;       // x position of lower-right corner
  59.             public int Bottom;      // y position of lower-right corner
  60.         }
  61.        
  62.         struct FIXED {
  63.             public short fract;
  64.             public short value;
  65.         }
  66.        
  67.         struct POINTFX {
  68.             public int x;
  69.             public int y;
  70.         }
  71.        
  72.         [DllImport("user32.dll", SetLastError=true)]
  73.         static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
  74.        
  75.         [DllImport("user32.dll")]
  76.         static extern bool ClientToScreen(IntPtr hWnd, ref POINTFX lpPoint);
  77.        
  78.         [DllImport("user32.dll", SetLastError = true)]
  79.         static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  80.        
  81.         [DllImport("user32.dll")]
  82.         static extern bool AdjustWindowRectEx(ref RECT lpRect, uint dwStyle,
  83.             bool bMenu, uint dwExStyle);
  84.        
  85.         enum DWMWINDOWATTRIBUTE : uint
  86.         {
  87.             NCRenderingEnabled = 1,
  88.             NCRenderingPolicy,
  89.             TransitionsForceDisabled,
  90.             AllowNCPaint,
  91.             CaptionButtonBounds,
  92.             NonClientRtlLayout,
  93.             ForceIconicRepresentation,
  94.             Flip3DPolicy,
  95.             ExtendedFrameBounds,
  96.             HasIconicBitmap,
  97.             DisallowPeek,
  98.             ExcludedFromPeek,
  99.             Cloak,
  100.             Cloaked,
  101.             FreezeRepresentation
  102.         }
  103.        
  104.         [DllImport("dwmapi.dll")]
  105.         static extern int DwmGetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, out RECT pvAttribute, int cbAttribute);
  106.  
  107.         private Device _device;
  108.         private Font _font;
  109.        
  110.         private RECT windowBounds;
  111.         public frm_drawing()
  112.         {
  113.             InitializeComponent();
  114.             InitDevice();
  115.  
  116.             IntPtr csHandle = FindWindow((string)null, "Unbenannt - Editor");
  117.  
  118.             // if window found
  119.             if (csHandle != IntPtr.Zero)
  120.             {
  121.                 Thread renderThread = new Thread(Render) {IsBackground = true};
  122.                 renderThread.Start(csHandle);
  123.             }
  124.         }
  125.  
  126.         private void InitDevice()
  127.         {
  128.             //Make the window's border completely transparant
  129.             SetWindowLong(this.Handle, GWL_EXSTYLE,
  130.                 (IntPtr)(GetWindowLong(this.Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED ^ WS_EX_TRANSPARENT));
  131.  
  132.             //Set the Alpha on the Whole Window to 255 (solid)
  133.             SetLayeredWindowAttributes(this.Handle, 0, 255, LWA_ALPHA);
  134.            
  135.             PresentParameters pp = new PresentParameters
  136.             {
  137.                 Windowed = true,
  138.                 SwapEffect = SwapEffect.Discard,
  139.                 BackBufferFormat = Format.A8R8G8B8
  140.             };
  141.            
  142.             _device = new Device(new Direct3D(), 0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, pp);
  143.             var fontDescription = new FontDescription
  144.             {
  145.                 Height         = 16,
  146.                 FaceName       = "Consolas",
  147.                 PitchAndFamily = FontPitchAndFamily.Mono,
  148.                 Quality        = FontQuality.Draft
  149.             };
  150.             _font = new Font(_device, fontDescription);
  151.            
  152.         }
  153.  
  154.         private RECT GetWindowClientArea(IntPtr handle)
  155.         {
  156.             //int size = Marshal.SizeOf(typeof(RECT));
  157.             //DwmGetWindowAttribute(handle, DWMWINDOWATTRIBUTE.ExtendedFrameBounds, out var rect, size);
  158.            
  159.             /*
  160.             GetWindowRect(handle, out var rect);
  161.             WindowStyles style = WindowStyles.WS_CAPTION | WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE |
  162.                                  WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_SYSMENU | WindowStyles.WS_MINIMIZEBOX;
  163.  
  164.             WindowStylesEx stylesEx = WindowStylesEx.WS_EX_LEFT | WindowStylesEx.WS_EX_LTRREADING |
  165.                                       WindowStylesEx.WS_EX_RIGHTSCROLLBAR | WindowStylesEx.WS_EX_WINDOWEDGE;
  166.            
  167.             AdjustWindowRectEx(ref rect, (uint)style, false, (uint)stylesEx);*/
  168.            
  169.            /* POINTFX pt;
  170.             pt.x = 0;
  171.             pt.y = 0;
  172.            
  173.             ClientToScreen(handle, ref pt);*/
  174.  
  175.             /*
  176.             rect.Left = pt.x;
  177.             rect.Right += pt.x;
  178.             rect.Top += pt.y;
  179.             rect.Bottom += pt.y;*/
  180.             GetWindowRect(handle, out var rect);
  181.             return rect;
  182.         }
  183.  
  184.         private float GetCenterOfScreenX()
  185.         {
  186.             //return (windowBounds.Right / 2) + (windowBounds.Left / 2);
  187.             return windowBounds.Left;
  188.         }
  189.        
  190.         private float GetCenterOfScreenY()
  191.         {
  192.             //return (windowBounds.Bottom / 2) + (windowBounds.Top / 2);
  193.             return windowBounds.Top;
  194.         }
  195.        
  196.  
  197.         private void Render(object windowHandle)
  198.         {
  199.             while (true)
  200.             {
  201.                 windowBounds = GetWindowClientArea((IntPtr)windowHandle);
  202.                 _device.Clear(ClearFlags.Target, new RawColorBGRA(0,0,0,0), 1.0f, 1);
  203.                
  204.                 /*
  205.                 _device.SetRenderState(RenderState.ZEnable, false);
  206.                 _device.SetRenderState(RenderState.Lighting, false);
  207.                 _device.SetRenderState(RenderState.CullMode, Cull.None);            
  208.                 _device.SetTransform(TransformState.Projection, Matrix.OrthoOffCenterLH(windowBounds.Left, windowBounds.Right, windowBounds.Bottom, windowBounds.Top, 0, 1));*/
  209.  
  210.                 _device.BeginScene();
  211. /*
  212.                 string formatted = $"Left: {windowBounds.Left} Top: {windowBounds.Top} Right: {windowBounds.Right} Bottom: {windowBounds.Bottom} ";
  213.                 _font.DrawText(null, formatted, 100, 100, Color.Red);
  214.                
  215.                 _font.DrawText(null, $"Center X: {GetCenterOfScreenX()} Center Y: {GetCenterOfScreenY()}", 100, 120, Color.Red);
  216. */
  217.                 Line crossLine = new Line(_device);
  218.                 Vector2[] vecCross1 = {
  219.                     new Vector2(windowBounds.Left, windowBounds.Top),
  220.                     new Vector2(windowBounds.Right, windowBounds.Top)
  221.                 };
  222.                 Vector2[] vecCross2 = {
  223.                     new Vector2(windowBounds.Left, windowBounds.Top),
  224.                     new Vector2(windowBounds.Left, windowBounds.Bottom)
  225.                 };
  226.                 /*
  227.                 Vector2[] vecCross1 = {
  228.                     new Vector2(GetCenterOfScreenX() - 10, GetCenterOfScreenY()),
  229.                     new Vector2(GetCenterOfScreenX() + 10, GetCenterOfScreenY())
  230.                 };
  231.                 Vector2[] vecCross2 = {
  232.                     new Vector2(GetCenterOfScreenX(), GetCenterOfScreenY()-10),
  233.                     new Vector2(GetCenterOfScreenX(), GetCenterOfScreenY()+10)
  234.                 };*/
  235.  
  236.                 crossLine.Draw(vecCross1, Color.Red);
  237.                 crossLine.Draw(vecCross2, Color.Red);
  238.                 //crossLine.Draw(vecCross2, Color.Red);
  239.                
  240.                 /*
  241.                 Line line = new Line(_device);
  242.                 line.GLLines = true;
  243.                 line.Antialias = false;
  244.                 line.Width = 1;
  245.                 Vector2[] vec = {new Vector2(0+windowBounds.Left, 0+windowBounds.Top), new Vector2(100+windowBounds.Left, 100+windowBounds.Top)};
  246. */
  247.        
  248.                 _device.EndScene();
  249.                 _device.Present();
  250.             }
  251.         }
  252.  
  253.         private void Form1_Paint(object sender, PaintEventArgs e)
  254.         {
  255.             marg.Left = 0;
  256.             marg.Top = 0;
  257.             marg.Right = this.Width;
  258.             marg.Bottom = this.Height;
  259.            
  260.             DwmExtendFrameIntoClientArea(this.Handle, ref marg);
  261.         }
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement