// This is the initializer WindowProc for the class. It is only called until the NCCREATE message // is arrived, at which point the WindowInstanceInitializerProc is called, which again is only // executed once. It performs nifty trick to replace the WindowProc of the instance, by chaining // WndProc and swapping out base classes to be able to do it without any extra overhead of the // traditional Win32 WndProc methods. private unsafe IntPtr ClassInitializerProc(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam) { try { Debug.WriteLine("[ClassInitializerProc]: " + hwnd); WindowProc winInstanceInitializerProc = null; if (msg == (int)WM.NCCREATE) { var wParamForNcCreate = Marshal.GetFunctionPointerForDelegate(this.m_windowProc); var createStruct = *(CreateStruct *)lParam.ToPointer(); var instancePtr = createStruct.CreateParams; if (instancePtr != IntPtr.Zero) { var winInstance = (WindowCore)GCHandle.FromIntPtr(instancePtr).Target; if (winInstance != null) { winInstanceInitializerProc = winInstance.WindowInstanceInitializerProc; wParam = wParamForNcCreate; } } } return(winInstanceInitializerProc?.Invoke(hwnd, msg, wParam, lParam) ?? this.m_windowProc(hwnd, msg, wParam, lParam)); } catch (Exception ex) { if (!WindowCore.HandleException(ex, null)) { throw; } return(IntPtr.Zero); } }
public static bool HandleException(Exception ex, WindowCore window) { var windowException = new WindowException(ex, window); window?.Exception?.Invoke(windowException); if (!windowException.IsHandled) { UnhandledException?.Invoke(windowException); } return(windowException.IsHandled); }
public int Run(WindowCore mainWindow = null) { Action destroyHandler = () => { MessageHelpers.PostQuitMessage(); }; if (mainWindow != null) { mainWindow.Destroyed += destroyHandler; } var res = this.RunCore(); // Technically, this can be avoided by setting the handler to auto disconnect. // However, this helps keep the mainWindow alive, and use this instead of // GC.KeepAlive pattern. if (mainWindow != null) { mainWindow.Destroyed -= destroyHandler; } return(res); }
public WindowException(Exception ex, WindowCore window, string message) : base(message, ex) { this.Window = window; this.IsHandled = false; }
public WindowException(Exception ex, WindowCore window) : this(ex, window, null) { }