1

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!

Nathan
  • 131
  • 1
  • 12

2 Answers2

5

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;
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Please see David Hefferman's comment to Xiaoy312's answer. The OP needs to understand what their code is doing in the first place before they can understand yours. – andlabs Aug 17 '16 at 22:05
  • @andlabs I read the comment and posted the link to original answer containing the code which the OP is using. Hope it help the OP to learn what they are doing. – Reza Aghaei Aug 17 '16 at 22:09
  • @Nathan Let me know if you have any question about the answer :) – Reza Aghaei Aug 18 '16 at 21:15
3

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);
    }
}
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • Could you please show me an example of what you mean? – Nathan Aug 17 '16 at 21:59
  • You seem to have no understanding of your code. Did you copy it from somewhere. Setting the style to initialSyle could not be simpler. If you can't understand it then copying yet more code won't help you. You must seek understanding. – David Heffernan Aug 17 '16 at 22:03
  • 3
    @DavidHeffernan It seems the code has taken from [here](http://stackoverflow.com/a/2798294/3110834) – Reza Aghaei Aug 17 '16 at 22:04
  • @Xiaoy312 ;) My first option was the answer which you posted. I also wanted to share another solution for the OP which shows him how to remove styles :)++ – Reza Aghaei Aug 17 '16 at 22:15
  • 1
    Storing redundant information (*_initialStyle*) is a bug waiting to happen. The implementation for `RestoreStyle` unilaterally sets a window's styles to an arbitrary value it had at a random point in time. Reza Aghaei's [answer](http://stackoverflow.com/a/39006954/1889329) is superior in many ways: It doesn't store redundant information, and it's explicit in what it does, by manipulating the desired bits only. – IInspectable Aug 18 '16 at 02:35