In my project, I use the WS_EX_TRANSPARENT flag to define dynamically whether the translucent form should recieve the users mouse events.
To make it more intuitive, I have added code to disable all the visible controls when WS_EX_TRANSPARENT is enabled, however, when this code is called, it appears to ‘lock out’ my OnMouseWheel override.
Below is my code. I should point out that this code works fine if I comment out the ‘EnableGUIControls’ method – in fact, if I comment out any line inside the ‘EnableGUIControls’ method it works perfectly – so its something to do with disabling all of the controls.
Removing focus from the form and reactivating it resolves the issue, but a manual call to Form.Activte() does not.
I am thinking along the lines of disabling all the visible controls disables the parent in some way? Does anyone have any idea what is happening?
private void SetTransparentToMouse(bool should_be_transparent)
{
IntPtr flags = GetWindowLong(this.Handle, GWL_EXSTYLE);
if (((flags.ToInt64() & WS_EX_TRANSPARENT.ToInt64()) > 0) == should_be_transparent)
{
return;
}
else
{
SwapTransparent();
EnableGUIControls(!should_be_transparent);
}
}
private void SwapTransparent()
{
IntPtr flags = new IntPtr(GetWindowLong(this.Handle, GWL_EXSTYLE).ToInt64() ^ WS_EX_TRANSPARENT.ToInt64());
SetWindowLong(this.Handle, GWL_EXSTYLE, flags);
}
protected override void OnKeyUp(KeyEventArgs e)
{
if (!e.Alt)
{
SetTransparentToMouse(default_mouse_transparency);
}
base.OnKeyUp(e);
}
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
if (e.Alt)
{
SetTransparentToMouse(!default_mouse_transparency);
}
base.OnKeyDown(e);
}
//Commenting out the call to this, or any line within this resolves the problem!:
void EnableGUIControls(bool enabled)
{
this.Button_Opacity.Enabled = enabled;
this.Button_Close.Enabled = enabled;
this.Button_Minimize.Enabled = enabled;
this.Button_Open.Enabled = enabled;
this.Button_Pan.Enabled = enabled;
this.Button_Sizemode.Enabled = enabled;
this.Button_Zoom.Enabled = enabled;
}
When all controls are disabled, there is no focussed control left that catches and redirects the mouse messages to the Form. Try Form.Focus() instead of Form.Activate().