Question

I am making an overlay. I have this code here

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;


    namespace HyperBox
    {

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();

        this.TopMost = true; // make the form always on top
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; // hidden border
        this.WindowState = FormWindowState.Maximized; // maximized
        this.MinimizeBox = this.MaximizeBox = false; // not allowed to be minimized
        this.MinimumSize = this.MaximumSize = this.Size; // not allowed to be resized
        this.TransparencyKey = this.BackColor = Color.Red; // the color key to transparent, choose a color that you don't use

        // Set the form click-through
        int initialStyle = GetWindowLong(this.Handle, -20);
        SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
    }

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha,         uint dwFlags);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern int SetParent(int hWndChild, int hWndNewParent);


    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        // draw what you want
        e.Graphics.FillEllipse(Brushes.Blue, 30, 30, 100, 100);

    }
    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {

    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }



}

    }

It draws an ellipse onto a form, which is transparent and always ontop. The problem is that it doesn't work over fullscreen.

I have tried using this

    SetParent(this.handle, FindWindow(null, "<parent window title here>"));

except I get errors. Could someone please help?

Was it helpful?

Solution

I believe your error is right here

[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SetParent(int hWndChild, int hWndNewParent);

It's expecting two arguments of type IntPtr not int and it returns an IntPtr not an int.

MSDN provides more information. See the user contributions towards the bottom for some good C# examples.

Keep in mind that an extern, when used with DllImport, is a reference to unmanaged code. The method called SetParent() in the user32.dll doesn't have a definition that accepts two ints as parameters.

So that block should read:

[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top