Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8125345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:47:05+00:00 2026-06-06T06:47:05+00:00

This is some code that I picked up which I tried to implement. Its

  • 0

This is some code that I picked up which I tried to implement. Its purpose is to create a form layer which is transparent, full screen, borderless, clickthrough, and always on top of other windows. It then lets you draw using directx over the top of it remaining otherwise transparent.

The parts that don’t work are the click-through part, and the directx render. When I run it I basically have an invisible force field in front of all other windows and have to alt-tab around to visual studio to quickly press ALT F5 and end the debug (so at least the always on top and transparency works). I have been trying to figure out why those parts don’t work, but my newbie c# skills fail me. hopefully someone can spot why and provide a modification.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Globalization;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Threading;


namespace MinimapSpy
{
public partial class Form1 : 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);

    private Device device = null;



    public Form1()
    {

        //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);

        //Init DirectX
        //This initializes the DirectX device. It needs to be done once.
        //The alpha channel in the backbuffer is critical.
        PresentParameters presentParameters = new PresentParameters();
        presentParameters.Windowed = true;
        presentParameters.SwapEffect = SwapEffect.Discard;
        presentParameters.BackBufferFormat = Format.A8R8G8B8;

        this.device = new Device(0, DeviceType.Hardware, this.Handle,
        CreateFlags.HardwareVertexProcessing, presentParameters);


        Thread dx = new Thread(new ThreadStart(this.dxThread));
        dx.IsBackground = true;
        dx.Start();  
        InitializeComponent();

    }

   protected override void OnPaint(PaintEventArgs e)
   {
        //Create a margin (the whole form)
      marg.Left = 0;
     marg.Top = 0;
      marg.Right = this.Width;
      marg.Bottom = this.Height;

        //Expand the Aero Glass Effect Border to the WHOLE form.
        // since we have already had the border invisible we now
        // have a completely invisible window - apart from the DirectX
        // renders NOT in black.
     DwmExtendFrameIntoClientArea(this.Handle, ref marg);  

  }
    private void Form1_Load(object sender, EventArgs e)
    {

    }
    private void dxThread()
    {
        while (true)
        {
            //Place your update logic here
            device.Clear(ClearFlags.Target, Color.FromArgb(0, 0, 0, 0), 1.0f, 0);
            device.RenderState.ZBufferEnable = false;
            device.RenderState.Lighting = false;
            device.RenderState.CullMode = Cull.None;
            device.Transform.Projection = Matrix.OrthoOffCenterLH(0, this.Width, this.Height, 0, 0, 1);
            device.BeginScene();

            //Place your rendering logic here

            device.EndScene();
            //device.Present();
        }

        this.device.Dispose();
        Application.Exit();
    }  

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:47:08+00:00Added an answer on June 6, 2026 at 6:47 am

    Here’s a refined full sample code for making a window topmost – click through – transparent (= alpha blended). The sample makes a rotating color wheel which is rendered with DirectX, or actually with XNA 4.0, because I believe Microsoft has discontinued developing the managed directx and favours XNA today.

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using Microsoft.Xna.Framework.Graphics;
    
    namespace ClickThroughXNA
    {
        public partial class Form1 : Form
        {
            // Directx graphics device
            GraphicsDevice dev = null;        
            BasicEffect effect = null;     
    
            // Wheel vertexes
            VertexPositionColor[] v = new VertexPositionColor[100];
    
            // Wheel rotation
            float rot = 0;
    
            public Form1()
            {
                InitializeComponent();
    
                StartPosition = FormStartPosition.CenterScreen;   
                Size = new System.Drawing.Size(500, 500);
                FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;  // no borders
    
                TopMost = true;        // make the form always on top                     
                Visible = true;        // Important! if this isn't set, then the form is not shown at all
    
                // Set the form click-through
                int initialStyle = GetWindowLong(this.Handle, -20);
                SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
    
                // Create device presentation parameters
                PresentationParameters p = new PresentationParameters();
                p.IsFullScreen = false;
                p.DeviceWindowHandle = this.Handle;
                p.BackBufferFormat = SurfaceFormat.Vector4;
                p.PresentationInterval = PresentInterval.One;
    
                // Create XNA graphics device
                dev = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.Reach, p);
    
                // Init basic effect
                effect = new BasicEffect(dev);
    
                // Extend aero glass style on form init
                OnResize(null);
            }
    
    
            protected override void OnResize(EventArgs e)
            {
                int[] margins = new int[] { 0, 0, Width, Height };
    
                // Extend aero glass style to whole form
                DwmExtendFrameIntoClientArea(this.Handle, ref margins);  
            }
    
    
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                // do nothing here to stop window normal background painting
            }
    
    
            protected override void OnPaint(PaintEventArgs e)
            {                
                // Clear device with fully transparent black
                dev.Clear(new Microsoft.Xna.Framework.Color(0, 0, 0, 0.0f));
    
                // Rotate wheel a bit
                rot+=0.1f;
    
                // Make the wheel vertexes and colors for vertexes
                for (int i = 0; i < v.Length; i++)
                {                    
                    if (i % 3 == 1)
                        v[i].Position = new Microsoft.Xna.Framework.Vector3((float)Math.Sin((i + rot) * (Math.PI * 2f / (float)v.Length)), (float)Math.Cos((i + rot) * (Math.PI * 2f / (float)v.Length)), 0);
                    else if (i % 3 == 2)
                        v[i].Position = new Microsoft.Xna.Framework.Vector3((float)Math.Sin((i + 2 + rot) * (Math.PI * 2f / (float)v.Length)), (float)Math.Cos((i + 2 + rot) * (Math.PI * 2f / (float)v.Length)), 0);
    
                    v[i].Color = new Microsoft.Xna.Framework.Color(1 - (i / (float)v.Length), i / (float)v.Length, 0, i / (float)v.Length);
                }
    
                // Enable position colored vertex rendering
                effect.VertexColorEnabled = true;
                foreach (EffectPass pass in effect.CurrentTechnique.Passes) pass.Apply();
    
                // Draw the primitives (the wheel)
                dev.DrawUserPrimitives(PrimitiveType.TriangleList, v, 0, v.Length / 3, VertexPositionColor.VertexDeclaration);
    
                // Present the device contents into form
                dev.Present();
    
                // Redraw immediatily
                Invalidate();            
            }
    
    
            [DllImport("user32.dll", SetLastError = true)]
            static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    
            [DllImport("user32.dll")]
            static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    
            [DllImport("dwmapi.dll")]
            static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref int[] pMargins);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
This is some code that is behaving peculiarly. This is a simplified version of
this is some code that SDL requires in visual studios 2005 in order for
This is some simple code that draws to the screen. GLuint vbo; glGenBuffers(1, &vbo);
I have a java code that looks like this: //UI thread //Some code Job
I have some code that looks like this: foreach(var obj in collection) { try
I have some code that looks like this: var MyObject = function () {
I have some code that looks like this. There is also an autoincrement field
This is a minimal test case of some code that I actually have. It
So I picked up a job that requires me to fix some broken code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.