我無法使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