.net core NET内核中的窗口消息循环(WndProc | While(GetMEssage))线程

.net core NET内核中的窗口消息循环(WndProc | While(GetMEssage))线程,.net-core,pinvoke,wndproc,window-messages,message-loop,.net Core,Pinvoke,Wndproc,Window Messages,Message Loop,我正在尝试使用.Net Core订阅窗口消息 我能够接收初始消息以创建窗口(通过pinvoke)并销毁消息。但在此之后,我创建的窗口被阻止,不会收到任何其他消息 public class CustomWindow : IDisposable { delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); [StructLayout(LayoutKind.Sequential,Cha

我正在尝试使用.Net Core订阅窗口消息

我能够接收初始消息以创建窗口(通过pinvoke)并销毁消息。但在此之后,我创建的窗口被阻止,不会收到任何其他消息

 public class CustomWindow : IDisposable
{
    delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
    struct WNDCLASS
    {
        public uint style;
        public IntPtr lpfnWndProc;
        public int cbClsExtra;
        public int cbWndExtra;
        public IntPtr hInstance;
        public IntPtr hIcon;
        public IntPtr hCursor;
        public IntPtr hbrBackground;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string lpszMenuName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string lpszClassName;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MSG
    {
        public IntPtr hwnd;
        public uint message;
        public IntPtr wParam;
        public IntPtr lParam;
        public uint time;
    }

    [DllImport("user32.dll", SetLastError = true)]
    static extern UInt16 RegisterClassW([In] ref WNDCLASS lpWndClass);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr CreateWindowExW(
       UInt32 dwExStyle,
       [MarshalAs(UnmanagedType.LPWStr)]string lpClassName,
       [MarshalAs(UnmanagedType.LPWStr)]string lpWindowName,
       UInt32 dwStyle,
       Int32 x,
       Int32 y,
       Int32 nWidth,
       Int32 nHeight,
       IntPtr hWndParent,
       IntPtr hMenu,
       IntPtr hInstance,
       IntPtr lpParam
    );

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr DefWindowProcW(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool DestroyWindow(IntPtr hWnd);

    private const int ERROR_CLASS_ALREADY_EXISTS = 1410;

    private bool _mDisposed;
    public IntPtr Hwnd;

    public List<uint> Messages { get; set; }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (!_mDisposed)
        {
            if (disposing)
            {
                // Dispose managed resources
            }

            // Dispose unmanaged resources
            if (Hwnd != IntPtr.Zero)
            {
                DestroyWindow(Hwnd);
                Hwnd = IntPtr.Zero;
            }
        }
    }

    public CustomWindow()
    {
        Messages = new List<uint>();
        var className = "InvisibleWindow";

        _mWndProcDelegate = CustomWndProc;

        // Create WNDCLASS
        WNDCLASS windClass = new WNDCLASS
        {
            lpszClassName = className,
            lpfnWndProc = Marshal.GetFunctionPointerForDelegate(_mWndProcDelegate)
        };

        UInt16 classAtom = RegisterClassW(ref windClass);

        int lastError = Marshal.GetLastWin32Error();

        if (classAtom == 0 && lastError != ERROR_CLASS_ALREADY_EXISTS)
        {
            throw new Exception("Could not register window class");
        }

        const UInt32 WS_OVERLAPPEDWINDOW = 0xcf0000;
        const UInt32 WS_VISIBLE = 0x10000000;


        // Create window
        Hwnd = CreateWindowExW(
            0,
            className,
            "My WIndow",
            WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 400,
            IntPtr.Zero,
            IntPtr.Zero,
            IntPtr.Zero,
            IntPtr.Zero
        );

        Importer.ShowWindow(Hwnd, 1);
        Importer.UpdateWindow(Hwnd);
    }

    private  IntPtr CustomWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
    {
        Messages.Add(msg);
        return DefWindowProcW(hWnd, msg, wParam, lParam);
    }

    private WndProc _mWndProcDelegate;
}

实现了GetMessage循环,我的窗口现在接收所有消息,并且不会被阻止,但它现在阻止了主线程。我将看看是否可以在单独的线程上创建窗口并在该线程上运行消息循环。

最终找到了一个有效的解决方案

<> P>在C++中创建的窗口在自己的线程中自动工作,无需任何输入,并且能够处理自己的消息队列。另外,因为它主要是GUI,所以无论如何输入都是这样接收的

在.Net中,不能将对象分配给特定线程。因此,即使在新线程或任务中启动GetMessage循环,窗口本身也会被阻塞,因为它在主线程中。
要克服这个问题,在主线程中运行C++窗口和GETMeScript循环,在第二个线程中运行控制台/Gui/API。< /P>你需要对消息循环进行编码(GETMeal/Debug CurrimeMe:):你已经这样做了吗?@ SimonMourier,这正是我要问的,我如何去做,你必须调用/调用这些Windows API方法,您必须模拟Windows应用程序,就像您对CreateWindow等所做的一样。@SimonMourier我是否在主线程中使用Missic getMessage?这在C++ GUI应用程序中起作用,因为这就是GUI动作如何执行。但在.Net内核中,它会锁定自己接口的执行。我如何确保getMessage循环始终在侦听,而我的.Net核心api/UI也是如此?您是否有一个完整的复制项目,以便我们可以对其进行测试?
 private void GetMessage()
    {
       IntPtr hwnd = _customWindow.Hwnd;
        int bRet;
        while ((bRet = Importer.GetMessage(out var msg, _customWindow.GetHandle, 0, 0)) !=0 )
        {
            if (bRet == -1)
            {
                Console.WriteLine("Error");
            }
            else
            {
                Importer.TranslateMessage(ref msg);
                Importer.DispatchMessage(ref msg);
                Console.WriteLine(_customWindow.Messages.LastOrDefault());
            }
        }
    }