Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Windows.Forms;
- using SharpDX;
- using SharpDX.Direct3D9;
- using SharpDX.Mathematics;
- using SharpDX.Mathematics.Interop;
- using Color = SharpDX.Color;
- using Font = SharpDX.Direct3D9.Font;
- namespace SoratosExternalWH
- {
- public partial class frm_drawing : Form
- {
- private Margins marg;
- //this is used to specify the boundaries of the transparent area
- internal struct Margins
- {
- public int Left, Right, Top, Bottom;
- }
- [DllImport("user32.dll", SetLastError = true)]
- private static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
- [DllImport("user32.dll")]
- static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
- [DllImport("user32.dll")]
- static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
- public const int GWL_EXSTYLE = -20;
- public const int WS_EX_LAYERED = 0x80000;
- public const int WS_EX_TRANSPARENT = 0x20;
- public const int LWA_ALPHA = 0x2;
- public const int LWA_COLORKEY = 0x1;
- [DllImport("dwmapi.dll")]
- static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMargins);
- [StructLayout(LayoutKind.Sequential)]
- public struct RECT
- {
- public int Left; // x position of upper-left corner
- public int Top; // y position of upper-left corner
- public int Right; // x position of lower-right corner
- public int Bottom; // y position of lower-right corner
- }
- struct FIXED {
- public short fract;
- public short value;
- }
- struct POINTFX {
- public int x;
- public int y;
- }
- [DllImport("user32.dll", SetLastError=true)]
- static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
- [DllImport("user32.dll")]
- static extern bool ClientToScreen(IntPtr hWnd, ref POINTFX lpPoint);
- [DllImport("user32.dll", SetLastError = true)]
- static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("user32.dll")]
- static extern bool AdjustWindowRectEx(ref RECT lpRect, uint dwStyle,
- bool bMenu, uint dwExStyle);
- enum DWMWINDOWATTRIBUTE : uint
- {
- NCRenderingEnabled = 1,
- NCRenderingPolicy,
- TransitionsForceDisabled,
- AllowNCPaint,
- CaptionButtonBounds,
- NonClientRtlLayout,
- ForceIconicRepresentation,
- Flip3DPolicy,
- ExtendedFrameBounds,
- HasIconicBitmap,
- DisallowPeek,
- ExcludedFromPeek,
- Cloak,
- Cloaked,
- FreezeRepresentation
- }
- [DllImport("dwmapi.dll")]
- static extern int DwmGetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, out RECT pvAttribute, int cbAttribute);
- private Device _device;
- private Font _font;
- private RECT windowBounds;
- public frm_drawing()
- {
- InitializeComponent();
- InitDevice();
- IntPtr csHandle = FindWindow((string)null, "Unbenannt - Editor");
- // if window found
- if (csHandle != IntPtr.Zero)
- {
- Thread renderThread = new Thread(Render) {IsBackground = true};
- renderThread.Start(csHandle);
- }
- }
- private void InitDevice()
- {
- //Make the window's border completely transparant
- SetWindowLong(this.Handle, GWL_EXSTYLE,
- (IntPtr)(GetWindowLong(this.Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED ^ WS_EX_TRANSPARENT));
- //Set the Alpha on the Whole Window to 255 (solid)
- SetLayeredWindowAttributes(this.Handle, 0, 255, LWA_ALPHA);
- PresentParameters pp = new PresentParameters
- {
- Windowed = true,
- SwapEffect = SwapEffect.Discard,
- BackBufferFormat = Format.A8R8G8B8
- };
- _device = new Device(new Direct3D(), 0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, pp);
- var fontDescription = new FontDescription
- {
- Height = 16,
- FaceName = "Consolas",
- PitchAndFamily = FontPitchAndFamily.Mono,
- Quality = FontQuality.Draft
- };
- _font = new Font(_device, fontDescription);
- }
- private RECT GetWindowClientArea(IntPtr handle)
- {
- //int size = Marshal.SizeOf(typeof(RECT));
- //DwmGetWindowAttribute(handle, DWMWINDOWATTRIBUTE.ExtendedFrameBounds, out var rect, size);
- /*
- GetWindowRect(handle, out var rect);
- WindowStyles style = WindowStyles.WS_CAPTION | WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE |
- WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_SYSMENU | WindowStyles.WS_MINIMIZEBOX;
- WindowStylesEx stylesEx = WindowStylesEx.WS_EX_LEFT | WindowStylesEx.WS_EX_LTRREADING |
- WindowStylesEx.WS_EX_RIGHTSCROLLBAR | WindowStylesEx.WS_EX_WINDOWEDGE;
- AdjustWindowRectEx(ref rect, (uint)style, false, (uint)stylesEx);*/
- /* POINTFX pt;
- pt.x = 0;
- pt.y = 0;
- ClientToScreen(handle, ref pt);*/
- /*
- rect.Left = pt.x;
- rect.Right += pt.x;
- rect.Top += pt.y;
- rect.Bottom += pt.y;*/
- GetWindowRect(handle, out var rect);
- return rect;
- }
- private float GetCenterOfScreenX()
- {
- //return (windowBounds.Right / 2) + (windowBounds.Left / 2);
- return windowBounds.Left;
- }
- private float GetCenterOfScreenY()
- {
- //return (windowBounds.Bottom / 2) + (windowBounds.Top / 2);
- return windowBounds.Top;
- }
- private void Render(object windowHandle)
- {
- while (true)
- {
- windowBounds = GetWindowClientArea((IntPtr)windowHandle);
- _device.Clear(ClearFlags.Target, new RawColorBGRA(0,0,0,0), 1.0f, 1);
- /*
- _device.SetRenderState(RenderState.ZEnable, false);
- _device.SetRenderState(RenderState.Lighting, false);
- _device.SetRenderState(RenderState.CullMode, Cull.None);
- _device.SetTransform(TransformState.Projection, Matrix.OrthoOffCenterLH(windowBounds.Left, windowBounds.Right, windowBounds.Bottom, windowBounds.Top, 0, 1));*/
- _device.BeginScene();
- /*
- string formatted = $"Left: {windowBounds.Left} Top: {windowBounds.Top} Right: {windowBounds.Right} Bottom: {windowBounds.Bottom} ";
- _font.DrawText(null, formatted, 100, 100, Color.Red);
- _font.DrawText(null, $"Center X: {GetCenterOfScreenX()} Center Y: {GetCenterOfScreenY()}", 100, 120, Color.Red);
- */
- Line crossLine = new Line(_device);
- Vector2[] vecCross1 = {
- new Vector2(windowBounds.Left, windowBounds.Top),
- new Vector2(windowBounds.Right, windowBounds.Top)
- };
- Vector2[] vecCross2 = {
- new Vector2(windowBounds.Left, windowBounds.Top),
- new Vector2(windowBounds.Left, windowBounds.Bottom)
- };
- /*
- Vector2[] vecCross1 = {
- new Vector2(GetCenterOfScreenX() - 10, GetCenterOfScreenY()),
- new Vector2(GetCenterOfScreenX() + 10, GetCenterOfScreenY())
- };
- Vector2[] vecCross2 = {
- new Vector2(GetCenterOfScreenX(), GetCenterOfScreenY()-10),
- new Vector2(GetCenterOfScreenX(), GetCenterOfScreenY()+10)
- };*/
- crossLine.Draw(vecCross1, Color.Red);
- crossLine.Draw(vecCross2, Color.Red);
- //crossLine.Draw(vecCross2, Color.Red);
- /*
- Line line = new Line(_device);
- line.GLLines = true;
- line.Antialias = false;
- line.Width = 1;
- Vector2[] vec = {new Vector2(0+windowBounds.Left, 0+windowBounds.Top), new Vector2(100+windowBounds.Left, 100+windowBounds.Top)};
- */
- _device.EndScene();
- _device.Present();
- }
- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- marg.Left = 0;
- marg.Top = 0;
- marg.Right = this.Width;
- marg.Bottom = this.Height;
- DwmExtendFrameIntoClientArea(this.Handle, ref marg);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement