110,502
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// This class is derived from ToolStrip. It is used to fix the problem that
/// buttons in toolstrip can't get Click event if user click them when the form
/// is not focused.
/// </summary>
class ToolStripEx
: ToolStrip
{
private const uint WM_MOUSEACTIVATE = 0x21;
private const uint MA_ACTIVATE = 1;
private const uint MA_ACTIVATEANDEAT = 2;
private const uint MA_NOACTIVATE = 3;
private const uint MA_NOACTIVATEANDEAT = 4;
/// <summary>
/// This field is used to control whether buttons in ToolStrip get Click event
/// when the form is not focused.
///
/// Type : System.Boolean
///
/// false is the same behavior provided by the base ToolStrip class
/// true is that buttons can get Click event even the form is not focused
/// Default value is false
/// </summary>
private bool _clickThrough = false;
/// <summary>
/// This property is used to control whether buttons in ToolStrip get Click event
/// when the form is not focused.
///
/// Type : System.Boolean
///
/// false is the same behavior provided by the base ToolStrip class
/// true is that buttons can get Click event even the form is not focused
/// Default value is false
/// </summary>
public bool ClickThrough
{
get { return _clickThrough; }
set { _clickThrough = value; }
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (_clickThrough &&
m.Msg == WM_MOUSEACTIVATE &&
m.Result == (IntPtr)MA_ACTIVATEANDEAT)
{
m.Result = (IntPtr)MA_ACTIVATE;
}
}
}