我无法让 PeekMessage 工作。实际上,我希望它会向我发送消息,但它的返回值为 0。
我使用 WinForm,启动一个正在查看消息的后台线程并使用鼠标使用该窗口。该窗口像往常一样可用,但无法查看任何消息。我究竟做错了什么 ?最后一个错误始终为 0。
[StructLayout(LayoutKind.Sequential)]
public struct NativeMessage
{
public IntPtr handle;
public uint msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public Point p;
public override string ToString()
{
return handle + ", " + msg + ", " + wParam + ", " + lParam + ", " + time + ", " + p;
}
}
[DllImport("user32.dll")]
public static extern int PeekMessage(out NativeMessage lpMsg, IntPtr window, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg);
public Form1()
{
ThreadPool.QueueUserWorkItem(o => run());
}
private void run()
{
for (int i = 0; i < 1000000; )
{
NativeMessage a = new NativeMessage();
int c = PeekMessage(out a, IntPtr.Zero, (uint) 0, (uint) 0, (uint) 0);
if (c != 0)
trace(" -> " + c); // prints strings
}
}
解决了:
- 我在主线程中调用
Show()
以显示我的表单 - 并重定向主线程以记录消息
- (不是 XY 问题,我需要 PeekMessage 才能工作或至少了解如何使用它)
(感谢您向我展示我犯的错误)