我无法使PeekMessage正常工作。其实我希望它用信息泛滥我,但它的返回值是0.WinApi - PeekMessage始终返回False
我使用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工作或至少了解如何使用它)
(感谢给我看我做了错误)
根据您的代码,您不会在该线程上创建窗口。你为什么期望线程队列中有消息? – Noseratio
@Noseratio不需要线程拥有任何窗口来接收消息。毕竟有['PostThreadMessage'](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644946.aspx)。 – IInspectable
@IInspectable,我没有看到在上面的代码中的任何地方。这就是为什么我问OP为什么期待消息泛滥的原因。 – Noseratio