WndProc 覆盖不一致地捕获消息 C#
WndProc override is not consistently catching messages C#
所以我遇到了一个非常奇怪的 bug/timing 问题,所以我简化了下面的代码以准确显示发生了什么。它在单步调试中完美运行,但在没有断点的情况下做了一些奇怪的事情 运行ning。
在我的 MainForm 中,我捕捉到任务栏点击
const int WM_ACTIVATEAPP = 0x1C;
int count = 0;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_ACTIVATEAPP)
{
if (m.LParam.ToInt32() == 2504)
{
using (StreamWriter writer = new StreamWriter("C:\log.txt", true))
{
count++;
writer.WriteLine(count);
}
}
}
}
m.LParam.ToInt32() == 2504表示程序在任务栏中被点击
如果我在 "using (StreamWriter..."、运行 调试上放置断点并单击 10 次(每次之后必须在编译器中按 f5),我将在日志中显示 1-10。
如果我取消断点并每隔几秒单击一次,总共 10 次(与上面相同),我最终得到 1-2、1-4,但再也没有了。
没有断点,快速双击 10 次,每次都会记录 1-20。
m.LParam.ToInt32() == 2504
means the program was clicked in the task bar.
我不知道你为什么这么想,也不知道你从哪里得到神奇的数字 2504。
If the wParam
parameter is TRUE
, lParam
is the identifier of the thread that owns the window being deactivated. If wParam
is FALSE
, lParam
is the identifier of the thread that owns the window being activated.
每次您 运行 程序时,线程标识符都会发生变化,因此您看不到这个数字并不奇怪。
如果您问消息怎么会比点击次数多,那是因为 WM_ACTIVATEAPP
是来来去去发送的。您忽略了 wParam
,这让您可以区分。
如果您想知道为什么断点会改变行为,那是因为 lParam
依赖于 "the other window"。断点导致调试器 window 被激活,因此 windows 接收焦点的顺序发生变化。
所以我遇到了一个非常奇怪的 bug/timing 问题,所以我简化了下面的代码以准确显示发生了什么。它在单步调试中完美运行,但在没有断点的情况下做了一些奇怪的事情 运行ning。
在我的 MainForm 中,我捕捉到任务栏点击
const int WM_ACTIVATEAPP = 0x1C;
int count = 0;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_ACTIVATEAPP)
{
if (m.LParam.ToInt32() == 2504)
{
using (StreamWriter writer = new StreamWriter("C:\log.txt", true))
{
count++;
writer.WriteLine(count);
}
}
}
}
m.LParam.ToInt32() == 2504表示程序在任务栏中被点击
如果我在 "using (StreamWriter..."、运行 调试上放置断点并单击 10 次(每次之后必须在编译器中按 f5),我将在日志中显示 1-10。
如果我取消断点并每隔几秒单击一次,总共 10 次(与上面相同),我最终得到 1-2、1-4,但再也没有了。
没有断点,快速双击 10 次,每次都会记录 1-20。
m.LParam.ToInt32() == 2504
means the program was clicked in the task bar.
我不知道你为什么这么想,也不知道你从哪里得到神奇的数字 2504。
If the
wParam
parameter isTRUE
,lParam
is the identifier of the thread that owns the window being deactivated. IfwParam
isFALSE
,lParam
is the identifier of the thread that owns the window being activated.
每次您 运行 程序时,线程标识符都会发生变化,因此您看不到这个数字并不奇怪。
如果您问消息怎么会比点击次数多,那是因为 WM_ACTIVATEAPP
是来来去去发送的。您忽略了 wParam
,这让您可以区分。
如果您想知道为什么断点会改变行为,那是因为 lParam
依赖于 "the other window"。断点导致调试器 window 被激活,因此 windows 接收焦点的顺序发生变化。