简体   繁体   中英

Reverse Form Click-Through?

So you can make a form Click-Through-Able...

Imports:

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

Code:

int initialStyle = GetWindowLong(this.Handle, -20);
SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);

Now how would I go about reversing the effect after running the code once?

I tried this:

int initialStyle = GetWindowLong(this.Handle, -20);
SetWindowLong(this.Handle, -20, initialStyle | 0x10000 | 0x10);

But that did not work.

Thanks in advance!

As another option, you can remove those styles this way:

var style = GetWindowLong(this.Handle, -20);
SetWindowLong(this.Handle, -20, style & ~(0x80000 | 0x20));

Note

The code would be more understandable using these constants:

const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x80000;
const int WS_EX_TRANSPARENT = 0x20;

In order to restore the style back to its initial state, you need to set the style to the value of initialStyle from the first snippet.

You can't just simply keep appending more flags onto the style and expecting it to return to normal.


public class Example
{
    private int _initialStyle = 0;

    public void ApplyStyle()
    {
        _initialStyle = GetWindowLong(...);
        SetWindowLong(..., _initialStyle | /* styles */);
    }

    public void RestoreStyle()
    {
        SetWindowLong(..., _initialStyle);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM