I want to keep track of the pen's position from anywhere. I want WndProc to be called even if it's on the button. But, If there is a button in the form, wndProc does not occur. What should I do?
Some details:
Certain pen mouse message comes in wndProc's message. (pen mouse message's Msg is 0x0711)
If I move the pen inside the form, the value continues to come up with wndProc. But, If there is a button in the form, wndProc does not occur on a button.
public const int PEN = 0x0711;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (PEN == m.Msg)
{
// TODO: function
}
}
This was not tested as I do not have a Pen, but in principle it the concept should work.
Use an IMessageFilter Interface implementation to detect the PEN message being sent to the form or one of its child controls and execute the desired function.
class PenFilter : IMessageFilter
{
private const int PEN = 0x0711;
private readonly Form parent;
public PenFilter(Form parent)
{
this.parent = parent;
}
bool IMessageFilter.PreFilterMessage(ref Message m)
{
Control targetControl = Control.FromChildHandle(m.HWnd);
if (targetControl != null && (targetControl == parent || parent == targetControl.FindForm()))
{
// execute your function
}
return false;
}
}
Install/remove the filter based on form activation/deactivation.
public partial class Form1 : Form
{
private PenFilter penFilter;
public Form1()
{
InitializeComponent();
penFilter = new PenFilter(this);
}
protected override void OnActivated(EventArgs e)
{
Application.AddMessageFilter(penFilter);
base.OnActivated(e);
}
protected override void OnDeactivate(EventArgs e)
{
Application.RemoveMessageFilter(penFilter);
base.OnDeactivate(e);
}
}